From b48a50ee0f5e242cc7f15e77f8a02940bfbe6798 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Thu, 17 Oct 2024 15:15:45 +0300 Subject: [PATCH] Add REST (un)assign ds tag tests (#1899) Signed-off-by: Marinov Avgustin --- .../api/MgmtDistributionSetTagRestApi.java | 7 +- .../MgmtDistributionSetTagResource.java | 4 +- .../MgmtDistributionSetTagResourceTest.java | 122 ++++++++++++++---- .../resource/MgmtTargetTagResourceTest.java | 23 ++-- .../hawkbit/rest/util/JsonBuilder.java | 7 +- 5 files changed, 115 insertions(+), 48 deletions(-) diff --git a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtDistributionSetTagRestApi.java b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtDistributionSetTagRestApi.java index 21f549345..8135c4cdf 100644 --- a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtDistributionSetTagRestApi.java +++ b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtDistributionSetTagRestApi.java @@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; /** @@ -416,9 +417,9 @@ public interface MgmtDistributionSetTagRestApi { + MgmtRestConstants.DISTRIBUTIONSET_TAG_DISTRIBUTIONSETS_REQUEST_MAPPING, consumes = { MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE }, produces = { MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE }) - ResponseEntity> assignDistributionSets( + ResponseEntity assignDistributionSets( @PathVariable("distributionsetTagId") Long distributionsetTagId, - List distributionsetIds); + @RequestBody List distributionsetIds); /** * Handles the DELETE request to unassign one distribution set from the given tag id. @@ -490,7 +491,7 @@ public interface MgmtDistributionSetTagRestApi { @PathVariable("distributionsetTagId") Long distributionsetTagId, List distributionsetId); - /** + /** * Handles the POST request to toggle the assignment of distribution sets by * the given tag id. * diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java index d83d27206..f3d0845e3 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java @@ -172,14 +172,14 @@ public class MgmtDistributionSetTagResource implements MgmtDistributionSetTagRes } @Override - public ResponseEntity> assignDistributionSets( + public ResponseEntity assignDistributionSets( final Long distributionsetTagId, final List distributionsetIds) { log.debug("Assign DistributionSet {} for ds tag {}", distributionsetIds.size(), distributionsetTagId); final List assignedDs = this.distributionSetManagement .assignTag(distributionsetIds, distributionsetTagId); log.debug("Assigned DistributionSet {}", assignedDs.size()); - return ResponseEntity.ok(MgmtDistributionSetMapper.toResponseDistributionSets(assignedDs)); + return ResponseEntity.ok().build(); } @Override diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java index 88d0263f3..94e2d280d 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java @@ -34,6 +34,7 @@ import org.eclipse.hawkbit.repository.model.BaseEntity; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetTag; import org.eclipse.hawkbit.repository.model.Tag; +import org.eclipse.hawkbit.repository.model.Target; import org.eclipse.hawkbit.repository.test.matcher.Expect; import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents; import org.eclipse.hawkbit.rest.util.JsonBuilder; @@ -338,11 +339,105 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt } @Test - @Description("Verfies that tag assignments done through tag API command are correctly stored in the repository.") - @ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), + @Description("Verifies that tag assignments done through tag API command are correctly stored in the repository.") + @ExpectEvents({ + @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), + @Expect(type = DistributionSetCreatedEvent.class, count = 1), + @Expect(type = DistributionSetUpdatedEvent.class, count = 1) }) + public void assignDistributionSet() throws Exception { + final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); + final DistributionSet set = testdataFactory.createDistributionSetsWithoutModules(1).get(0); + + mvc + .perform( + post(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned/" + + set.getId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); + + final List updated = distributionSetManagement.findByTag(PAGE, tag.getId()).getContent(); + assertThat(updated.stream().map(DistributionSet::getId).collect(Collectors.toList())) + .containsOnly(set.getId()); + } + + @Test + @Description("Verifies that tag assignments done through tag API command are correctly stored in the repository.") + @ExpectEvents({ + @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), @Expect(type = DistributionSetCreatedEvent.class, count = 2), @Expect(type = DistributionSetUpdatedEvent.class, count = 2) }) public void assignDistributionSets() throws Exception { + final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); + final List sets = testdataFactory.createDistributionSetsWithoutModules(2); + + mvc + .perform( + put(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") + .content(JsonBuilder.toArray(sets.stream().map(DistributionSet::getId).collect(Collectors.toList()))) + .contentType(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); + + final List updated = distributionSetManagement.findByTag(PAGE, tag.getId()).getContent(); + assertThat(updated.stream().map(DistributionSet::getId).collect(Collectors.toList())) + .containsAll(sets.stream().map(DistributionSet::getId).collect(Collectors.toList())); + } + + @Test + @Description("Verifies that tag unassignments done through tag API command are correctly stored in the repository.") + @ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), + @Expect(type = DistributionSetCreatedEvent.class, count = 2), + @Expect(type = DistributionSetUpdatedEvent.class, count = 3) }) + public void unassignDistributionSet() throws Exception { + final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); + final int setsAssigned = 2; + final List sets = testdataFactory.createDistributionSetsWithoutModules(setsAssigned); + final DistributionSet assigned = sets.get(0); + final DistributionSet unassigned = sets.get(1); + + distributionSetManagement.assignTag(sets.stream().map(BaseEntity::getId).collect(Collectors.toList()), tag.getId()); + + mvc.perform(delete(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned/" + + unassigned.getId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); + + final List updated = distributionSetManagement.findByTag(PAGE, tag.getId()).getContent(); + assertThat(updated.stream().map(DistributionSet::getId).collect(Collectors.toList())) + .containsOnly(assigned.getId()); + } + + @Test + @Description("Verifies that tag unassignments done through tag API command are correctly stored in the repository.") + @ExpectEvents({ + @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), + @Expect(type = DistributionSetCreatedEvent.class, count = 3), + @Expect(type = DistributionSetUpdatedEvent.class, count = 5) }) + public void unassignDistributionSets() throws Exception { + final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); + final List sets = testdataFactory.createDistributionSetsWithoutModules(3); + final DistributionSet assigned = sets.get(0); + final DistributionSet unassigned0 = sets.get(1); + final DistributionSet unassigned1 = sets.get(2); + + distributionSetManagement.assignTag(sets.stream().map(DistributionSet::getId).collect(Collectors.toList()), tag.getId()); + + mvc + .perform( + delete(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") + .content(JsonBuilder.toArray(List.of(unassigned0.getId(), unassigned1.getId()))) + .contentType(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); + + final List updated = distributionSetManagement.findByTag(PAGE, tag.getId()).getContent(); + assertThat(updated.stream().map(DistributionSet::getId).collect(Collectors.toList())) + .containsOnly(assigned.getId()); + } + + // DEPRECATED flows + + @Test + @Description("Verifies that tag assignments done through tag API command are correctly stored in the repository.") + @ExpectEvents({ + @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), + @Expect(type = DistributionSetCreatedEvent.class, count = 2), + @Expect(type = DistributionSetUpdatedEvent.class, count = 2) }) + public void assignDistributionSetsWithRequestBody() throws Exception { final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); final int setsAssigned = 2; final List sets = testdataFactory.createDistributionSetsWithoutModules(setsAssigned); @@ -364,27 +459,4 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt result.andExpect(applyBaseEntityMatcherOnArrayResult(updated.get(0))) .andExpect(applyBaseEntityMatcherOnArrayResult(updated.get(1))); } - - @Test - @Description("Verfies that tag unassignments done through tag API command are correctly stored in the repository.") - @ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), - @Expect(type = DistributionSetCreatedEvent.class, count = 2), - @Expect(type = DistributionSetUpdatedEvent.class, count = 3) }) - public void unassignDistributionSet() throws Exception { - final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); - final int setsAssigned = 2; - final List sets = testdataFactory.createDistributionSetsWithoutModules(setsAssigned); - final DistributionSet assigned = sets.get(0); - final DistributionSet unassigned = sets.get(1); - - distributionSetManagement.assignTag(sets.stream().map(BaseEntity::getId).collect(Collectors.toList()), tag.getId()); - - mvc.perform(delete(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned/" - + unassigned.getId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); - - final List updated = distributionSetManagement.findByTag(PAGE, tag.getId()).getContent(); - - assertThat(updated.stream().map(DistributionSet::getId).collect(Collectors.toList())) - .containsOnly(assigned.getId()); - } } diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java index 12be6b330..e2fa9d1ce 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java @@ -268,9 +268,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT @Expect(type = TargetUpdatedEvent.class, count = 1) }) public void assignTarget() throws Exception { final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); - final int targetsAssigned = 1; - final List targets = testdataFactory.createTargets(targetsAssigned); - final Target assigned = targets.get(0); + final Target assigned = testdataFactory.createTargets(1).get(0); mvc.perform(post(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned/" + assigned.getControllerId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); @@ -288,13 +286,12 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT @Expect(type = TargetUpdatedEvent.class, count = 2) }) public void assignTargets() throws Exception { final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); - final int targetsAssigned = 2; - final List targets = testdataFactory.createTargets(targetsAssigned); + final List targets = testdataFactory.createTargets(2); final Target assigned0 = targets.get(0); final Target assigned1 = targets.get(1);; mvc.perform(put(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") - .content(JsonBuilder.controllerIds(Arrays.asList(assigned0.getControllerId(), assigned1.getControllerId()))) + .content(JsonBuilder.toArray(Arrays.asList(assigned0.getControllerId(), assigned1.getControllerId()))) .contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); @@ -305,12 +302,13 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT @Test @Description("Verifies that tag unassignments done through tag API command are correctly stored in the repository.") - @ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 1), - @Expect(type = TargetCreatedEvent.class, count = 2), @Expect(type = TargetUpdatedEvent.class, count = 3) }) + @ExpectEvents({ + @Expect(type = TargetTagCreatedEvent.class, count = 1), + @Expect(type = TargetCreatedEvent.class, count = 2), + @Expect(type = TargetUpdatedEvent.class, count = 3) }) public void unassignTarget() throws Exception { final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); - final int targetsAssigned = 2; - final List targets = testdataFactory.createTargets(targetsAssigned); + final List targets = testdataFactory.createTargets(2); final Target assigned = targets.get(0); final Target unassigned = targets.get(1); @@ -332,8 +330,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT @Expect(type = TargetUpdatedEvent.class, count = 5) }) public void unassignTargets() throws Exception { final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); - final int targetsAssigned = 3; - final List targets = testdataFactory.createTargets(targetsAssigned); + final List targets = testdataFactory.createTargets(3); final Target assigned = targets.get(0); final Target unassigned0 = targets.get(1); final Target unassigned1 = targets.get(2); @@ -341,7 +338,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT targetManagement.assignTag(targets.stream().map(Target::getControllerId).collect(Collectors.toList()), tag.getId()); mvc.perform(delete(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") - .content(JsonBuilder.controllerIds(Arrays.asList(unassigned0.getControllerId(), unassigned1.getControllerId()))) + .content(JsonBuilder.toArray(Arrays.asList(unassigned0.getControllerId(), unassigned1.getControllerId()))) .contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); diff --git a/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/util/JsonBuilder.java b/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/util/JsonBuilder.java index 15d5dda9a..ddb2a32d5 100644 --- a/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/util/JsonBuilder.java +++ b/hawkbit-rest/hawkbit-rest-core/src/test/java/org/eclipse/hawkbit/rest/util/JsonBuilder.java @@ -34,9 +34,6 @@ import static org.junit.jupiter.api.Assertions.fail; /** * Builder class for building certain json strings. - * - * - * */ public abstract class JsonBuilder { @@ -49,9 +46,9 @@ public abstract class JsonBuilder { return list.toString(); } - public static String controllerIds(final Collection ids) throws JSONException { + public static String toArray(final Collection ids) throws JSONException { final JSONArray list = new JSONArray(); - for (final String smID : ids) { + for (final T smID : ids) { list.put(smID); } return list.toString();