Add REST (un)assign ds tag tests (#1899)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-10-17 15:15:45 +03:00
committed by GitHub
parent 4d0cba7c3b
commit b48a50ee0f
5 changed files with 115 additions and 48 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
/** /**
@@ -416,9 +417,9 @@ public interface MgmtDistributionSetTagRestApi {
+ MgmtRestConstants.DISTRIBUTIONSET_TAG_DISTRIBUTIONSETS_REQUEST_MAPPING, consumes = { + MgmtRestConstants.DISTRIBUTIONSET_TAG_DISTRIBUTIONSETS_REQUEST_MAPPING, consumes = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE }, produces = { MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE }, produces = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE }) MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<List<MgmtDistributionSet>> assignDistributionSets( ResponseEntity<Void> assignDistributionSets(
@PathVariable("distributionsetTagId") Long distributionsetTagId, @PathVariable("distributionsetTagId") Long distributionsetTagId,
List<Long> distributionsetIds); @RequestBody List<Long> distributionsetIds);
/** /**
* Handles the DELETE request to unassign one distribution set from the given tag id. * 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, @PathVariable("distributionsetTagId") Long distributionsetTagId,
List<Long> distributionsetId); List<Long> distributionsetId);
/** /**
* Handles the POST request to toggle the assignment of distribution sets by * Handles the POST request to toggle the assignment of distribution sets by
* the given tag id. * the given tag id.
* *

View File

@@ -172,14 +172,14 @@ public class MgmtDistributionSetTagResource implements MgmtDistributionSetTagRes
} }
@Override @Override
public ResponseEntity<List<MgmtDistributionSet>> assignDistributionSets( public ResponseEntity<Void> assignDistributionSets(
final Long distributionsetTagId, final Long distributionsetTagId,
final List<Long> distributionsetIds) { final List<Long> distributionsetIds) {
log.debug("Assign DistributionSet {} for ds tag {}", distributionsetIds.size(), distributionsetTagId); log.debug("Assign DistributionSet {} for ds tag {}", distributionsetIds.size(), distributionsetTagId);
final List<DistributionSet> assignedDs = this.distributionSetManagement final List<DistributionSet> assignedDs = this.distributionSetManagement
.assignTag(distributionsetIds, distributionsetTagId); .assignTag(distributionsetIds, distributionsetTagId);
log.debug("Assigned DistributionSet {}", assignedDs.size()); log.debug("Assigned DistributionSet {}", assignedDs.size());
return ResponseEntity.ok(MgmtDistributionSetMapper.toResponseDistributionSets(assignedDs)); return ResponseEntity.ok().build();
} }
@Override @Override

View File

@@ -34,6 +34,7 @@ import org.eclipse.hawkbit.repository.model.BaseEntity;
import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetTag; import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.Tag; 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.Expect;
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents; import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
import org.eclipse.hawkbit.rest.util.JsonBuilder; import org.eclipse.hawkbit.rest.util.JsonBuilder;
@@ -338,11 +339,105 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt
} }
@Test @Test
@Description("Verfies that tag assignments done through tag API command are correctly stored in the repository.") @Description("Verifies that tag assignments done through tag API command are correctly stored in the repository.")
@ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), @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<DistributionSet> 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 = DistributionSetCreatedEvent.class, count = 2),
@Expect(type = DistributionSetUpdatedEvent.class, count = 2) }) @Expect(type = DistributionSetUpdatedEvent.class, count = 2) })
public void assignDistributionSets() throws Exception { public void assignDistributionSets() throws Exception {
final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0);
final List<DistributionSet> 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<DistributionSet> 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<DistributionSet> 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<DistributionSet> 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<DistributionSet> 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<DistributionSet> 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 DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0);
final int setsAssigned = 2; final int setsAssigned = 2;
final List<DistributionSet> sets = testdataFactory.createDistributionSetsWithoutModules(setsAssigned); final List<DistributionSet> sets = testdataFactory.createDistributionSetsWithoutModules(setsAssigned);
@@ -364,27 +459,4 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt
result.andExpect(applyBaseEntityMatcherOnArrayResult(updated.get(0))) result.andExpect(applyBaseEntityMatcherOnArrayResult(updated.get(0)))
.andExpect(applyBaseEntityMatcherOnArrayResult(updated.get(1))); .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<DistributionSet> 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<DistributionSet> updated = distributionSetManagement.findByTag(PAGE, tag.getId()).getContent();
assertThat(updated.stream().map(DistributionSet::getId).collect(Collectors.toList()))
.containsOnly(assigned.getId());
}
} }

View File

@@ -268,9 +268,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
@Expect(type = TargetUpdatedEvent.class, count = 1) }) @Expect(type = TargetUpdatedEvent.class, count = 1) })
public void assignTarget() throws Exception { public void assignTarget() throws Exception {
final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0);
final int targetsAssigned = 1; final Target assigned = testdataFactory.createTargets(1).get(0);
final List<Target> targets = testdataFactory.createTargets(targetsAssigned);
final Target assigned = targets.get(0);
mvc.perform(post(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned/" + mvc.perform(post(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned/" +
assigned.getControllerId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); assigned.getControllerId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk());
@@ -288,13 +286,12 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
@Expect(type = TargetUpdatedEvent.class, count = 2) }) @Expect(type = TargetUpdatedEvent.class, count = 2) })
public void assignTargets() throws Exception { public void assignTargets() throws Exception {
final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0);
final int targetsAssigned = 2; final List<Target> targets = testdataFactory.createTargets(2);
final List<Target> targets = testdataFactory.createTargets(targetsAssigned);
final Target assigned0 = targets.get(0); final Target assigned0 = targets.get(0);
final Target assigned1 = targets.get(1);; final Target assigned1 = targets.get(1);;
mvc.perform(put(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") 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)) .contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk());
@@ -305,12 +302,13 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
@Test @Test
@Description("Verifies that tag unassignments done through tag API command are correctly stored in the repository.") @Description("Verifies that tag unassignments done through tag API command are correctly stored in the repository.")
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 1), @ExpectEvents({
@Expect(type = TargetCreatedEvent.class, count = 2), @Expect(type = TargetUpdatedEvent.class, count = 3) }) @Expect(type = TargetTagCreatedEvent.class, count = 1),
@Expect(type = TargetCreatedEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 3) })
public void unassignTarget() throws Exception { public void unassignTarget() throws Exception {
final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0);
final int targetsAssigned = 2; final List<Target> targets = testdataFactory.createTargets(2);
final List<Target> targets = testdataFactory.createTargets(targetsAssigned);
final Target assigned = targets.get(0); final Target assigned = targets.get(0);
final Target unassigned = targets.get(1); final Target unassigned = targets.get(1);
@@ -332,8 +330,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
@Expect(type = TargetUpdatedEvent.class, count = 5) }) @Expect(type = TargetUpdatedEvent.class, count = 5) })
public void unassignTargets() throws Exception { public void unassignTargets() throws Exception {
final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0); final TargetTag tag = testdataFactory.createTargetTags(1, "").get(0);
final int targetsAssigned = 3; final List<Target> targets = testdataFactory.createTargets(3);
final List<Target> targets = testdataFactory.createTargets(targetsAssigned);
final Target assigned = targets.get(0); final Target assigned = targets.get(0);
final Target unassigned0 = targets.get(1); final Target unassigned0 = targets.get(1);
final Target unassigned1 = targets.get(2); 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()); targetManagement.assignTag(targets.stream().map(Target::getControllerId).collect(Collectors.toList()), tag.getId());
mvc.perform(delete(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") 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)) .contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()); .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk());

View File

@@ -34,9 +34,6 @@ import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Builder class for building certain json strings. * Builder class for building certain json strings.
*
*
*
*/ */
public abstract class JsonBuilder { public abstract class JsonBuilder {
@@ -49,9 +46,9 @@ public abstract class JsonBuilder {
return list.toString(); return list.toString();
} }
public static String controllerIds(final Collection<String> ids) throws JSONException { public static <T> String toArray(final Collection<T> ids) throws JSONException {
final JSONArray list = new JSONArray(); final JSONArray list = new JSONArray();
for (final String smID : ids) { for (final T smID : ids) {
list.put(smID); list.put(smID);
} }
return list.toString(); return list.toString();