Implement target tag management using common RepositoryManagement (and abstract JPA impl) (#2580)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -38,6 +38,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -49,18 +50,15 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
public class MgmtTargetTagResource implements MgmtTargetTagRestApi {
|
||||
|
||||
private final TargetTagManagement tagManagement;
|
||||
private final TargetTagManagement<? extends TargetTag> tagManagement;
|
||||
private final TargetManagement targetManagement;
|
||||
private final EntityFactory entityFactory;
|
||||
private final TenantConfigHelper tenantConfigHelper;
|
||||
|
||||
MgmtTargetTagResource(
|
||||
final TargetTagManagement tagManagement, final TargetManagement targetManagement,
|
||||
final EntityFactory entityFactory,
|
||||
final TargetTagManagement<? extends TargetTag> tagManagement, final TargetManagement targetManagement,
|
||||
final SystemSecurityContext securityContext, final TenantConfigurationManagement configurationManagement) {
|
||||
this.tagManagement = tagManagement;
|
||||
this.targetManagement = targetManagement;
|
||||
this.entityFactory = entityFactory;
|
||||
tenantConfigHelper = TenantConfigHelper.usingContext(securityContext, configurationManagement);
|
||||
}
|
||||
|
||||
@@ -68,7 +66,7 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi {
|
||||
public ResponseEntity<PagedList<MgmtTag>> getTargetTags(
|
||||
final String rsqlParam, final int pagingOffsetParam, final int pagingLimitParam, final String sortParam) {
|
||||
final Pageable pageable = PagingUtility.toPageable(pagingOffsetParam, pagingLimitParam, sanitizeTagSortParam(sortParam));
|
||||
final Page<TargetTag> findTargetsAll;
|
||||
final Page<? extends TargetTag> findTargetsAll;
|
||||
if (rsqlParam == null) {
|
||||
findTargetsAll = this.tagManagement.findAll(pageable);
|
||||
} else {
|
||||
@@ -92,7 +90,7 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi {
|
||||
@Override
|
||||
public ResponseEntity<List<MgmtTag>> createTargetTags(final List<MgmtTagRequestBodyPut> tags) {
|
||||
log.debug("creating {} target tags", tags.size());
|
||||
final List<TargetTag> createdTargetTags = tagManagement.create(MgmtTagMapper.mapTagFromRequest(entityFactory, tags));
|
||||
final List<? extends TargetTag> createdTargetTags = tagManagement.create(MgmtTagMapper.mapTagFromRequest(tags));
|
||||
return new ResponseEntity<>(MgmtTagMapper.toResponse(createdTargetTags), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@@ -101,8 +99,8 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi {
|
||||
log.debug("update {} target tag", restTargetTagRest);
|
||||
|
||||
final TargetTag updateTargetTag = tagManagement
|
||||
.update(entityFactory.tag().update(targetTagId).name(restTargetTagRest.getName())
|
||||
.description(restTargetTagRest.getDescription()).colour(restTargetTagRest.getColour()));
|
||||
.update(TargetTagManagement.Update.builder().id(targetTagId).name(restTargetTagRest.getName())
|
||||
.description(restTargetTagRest.getDescription()).colour(restTargetTagRest.getColour()).build());
|
||||
|
||||
log.debug("target tag updated");
|
||||
|
||||
@@ -118,7 +116,7 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi {
|
||||
log.debug("Delete {} target tag", targetTagId);
|
||||
final TargetTag targetTag = findTargetTagById(targetTagId);
|
||||
|
||||
this.tagManagement.delete(targetTag.getName());
|
||||
this.tagManagement.delete(targetTag.getId());
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtDistributionSetTagRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtTargetTagRestApi;
|
||||
import org.eclipse.hawkbit.repository.EntityFactory;
|
||||
import org.eclipse.hawkbit.repository.builder.TagCreate;
|
||||
import org.eclipse.hawkbit.repository.TargetTagManagement;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
|
||||
import org.eclipse.hawkbit.repository.model.Tag;
|
||||
import org.eclipse.hawkbit.repository.model.TargetTag;
|
||||
@@ -36,7 +36,7 @@ import org.eclipse.hawkbit.rest.json.model.ResponseList;
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public final class MgmtTagMapper {
|
||||
|
||||
public static List<MgmtTag> toResponse(final List<TargetTag> targetTags) {
|
||||
public static List<MgmtTag> toResponse(final List<? extends TargetTag> targetTags) {
|
||||
final List<MgmtTag> tagsRest = new ArrayList<>();
|
||||
if (targetTags == null) {
|
||||
return tagsRest;
|
||||
@@ -106,10 +106,14 @@ public final class MgmtTagMapper {
|
||||
.withRel("assignedDistributionSets").expand());
|
||||
}
|
||||
|
||||
public static List<TagCreate<Tag>> mapTagFromRequest(final EntityFactory entityFactory, final Collection<MgmtTagRequestBodyPut> tags) {
|
||||
public static List<TargetTagManagement.Create> mapTagFromRequest(final Collection<MgmtTagRequestBodyPut> tags) {
|
||||
return tags.stream()
|
||||
.map(tagRest -> entityFactory.tag().create().name(tagRest.getName())
|
||||
.description(tagRest.getDescription()).colour(tagRest.getColour()))
|
||||
.map(tagRest -> TargetTagManagement.Create.builder()
|
||||
.name(tagRest.getName())
|
||||
.description(tagRest.getDescription())
|
||||
.colour(tagRest.getColour())
|
||||
.build())
|
||||
.map(TargetTagManagement.Create.class::cast)
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -29,6 +28,7 @@ import java.util.Random;
|
||||
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.util.ResourceUtility;
|
||||
import org.eclipse.hawkbit.repository.DistributionSetTagManagement;
|
||||
import org.eclipse.hawkbit.repository.event.remote.DistributionSetTagDeletedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagCreatedEvent;
|
||||
@@ -188,14 +188,16 @@ class MgmtDistributionSetTagResourceTest extends AbstractManagementApiIntegratio
|
||||
@Test
|
||||
@ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
|
||||
void createDistributionSetTags() throws Exception {
|
||||
final Tag tagOne = entityFactory.tag().create().colour("testcol1").description("its a test1").name("thetest1")
|
||||
final DistributionSetTagManagement.Create tagOne = DistributionSetTagManagement.Create.builder()
|
||||
.colour("testcol1").description("its a test1").name("thetest1")
|
||||
.build();
|
||||
final Tag tagTwo = entityFactory.tag().create().colour("testcol2").description("its a test2").name("thetest2")
|
||||
final DistributionSetTagManagement.Create tagTwo = DistributionSetTagManagement.Create.builder()
|
||||
.colour("testcol2").description("its a test2").name("thetest2")
|
||||
.build();
|
||||
|
||||
final ResultActions result = mvc
|
||||
.perform(post(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING)
|
||||
.content(JsonBuilder.tags(Arrays.asList(tagOne, tagTwo)))
|
||||
.content(JsonBuilder.dsTags(List.of(tagOne, tagTwo)))
|
||||
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
|
||||
.andDo(MockMvcResultPrinter.print())
|
||||
.andExpect(status().isCreated())
|
||||
@@ -210,8 +212,7 @@ class MgmtDistributionSetTagResourceTest extends AbstractManagementApiIntegratio
|
||||
assertThat(createdTwo.getDescription()).isEqualTo(tagTwo.getDescription());
|
||||
assertThat(createdTwo.getColour()).isEqualTo(tagTwo.getColour());
|
||||
|
||||
result.andExpect(applyTagMatcherOnArrayResult(createdOne))
|
||||
.andExpect(applyTagMatcherOnArrayResult(createdTwo));
|
||||
result.andExpect(applyTagMatcherOnArrayResult(createdOne)).andExpect(applyTagMatcherOnArrayResult(createdTwo));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,12 +226,13 @@ class MgmtDistributionSetTagResourceTest extends AbstractManagementApiIntegratio
|
||||
final List<DistributionSetTag> tags = testdataFactory.createDistributionSetTags(1);
|
||||
final DistributionSetTag original = tags.get(0);
|
||||
|
||||
final Tag update = entityFactory.tag().create().name("updatedName").colour("updatedCol")
|
||||
.description("updatedDesc").build();
|
||||
final DistributionSetTagManagement.Update update = DistributionSetTagManagement.Update.builder()
|
||||
.name("updatedName").colour("updatedCol").description("updatedDesc")
|
||||
.build();
|
||||
|
||||
final ResultActions result = mvc
|
||||
.perform(put(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + original.getId())
|
||||
.content(JsonBuilder.tag(update)).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(JsonBuilder.dsTag(update)).contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andDo(MockMvcResultPrinter.print())
|
||||
.andExpect(status().isOk())
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.Random;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtTargetTagRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.util.ResourceUtility;
|
||||
import org.eclipse.hawkbit.repository.TargetTagManagement;
|
||||
import org.eclipse.hawkbit.repository.event.remote.TargetTagDeletedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent;
|
||||
@@ -153,14 +154,16 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
|
||||
@Test
|
||||
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 2) })
|
||||
public void createTargetTags() throws Exception {
|
||||
final Tag tagOne = entityFactory.tag().create().colour("testcol1").description("its a test1").name("thetest1")
|
||||
final TargetTagManagement.Create tagOne = TargetTagManagement.Create.builder()
|
||||
.colour("testcol1").description("its a test1").name("thetest1")
|
||||
.build();
|
||||
final Tag tagTwo = entityFactory.tag().create().colour("testcol2").description("its a test2").name("thetest2")
|
||||
final TargetTagManagement.Create tagTwo = TargetTagManagement.Create.builder()
|
||||
.colour("testcol2").description("its a test2").name("thetest2")
|
||||
.build();
|
||||
|
||||
final ResultActions result = mvc
|
||||
.perform(post(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING)
|
||||
.content(JsonBuilder.tags(Arrays.asList(tagOne, tagTwo)))
|
||||
.content(JsonBuilder.targetTags(List.of(tagOne, tagTwo)))
|
||||
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
|
||||
.andDo(MockMvcResultPrinter.print())
|
||||
.andExpect(status().isCreated())
|
||||
@@ -190,12 +193,13 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
|
||||
final List<TargetTag> tags = testdataFactory.createTargetTags(1, "");
|
||||
final TargetTag original = tags.get(0);
|
||||
|
||||
final Tag update = entityFactory.tag().create().name("updatedName").colour("updatedCol")
|
||||
.description("updatedDesc").build();
|
||||
final TargetTagManagement.Update update = TargetTagManagement.Update.builder()
|
||||
.name("updatedName").colour("updatedCol").description("updatedDesc")
|
||||
.build();
|
||||
|
||||
final ResultActions result = mvc
|
||||
.perform(put(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + original.getId())
|
||||
.content(JsonBuilder.tag(update)).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(JsonBuilder.targetTag(update)).contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andDo(MockMvcResultPrinter.print())
|
||||
.andExpect(status().isOk())
|
||||
|
||||
Reference in New Issue
Block a user