From 1bc467d4705dcbfa57ffa83b5cad5f4ad4cae324 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Thu, 17 Oct 2024 17:18:36 +0300 Subject: [PATCH] Add notFound support (& test) for assign tag to ds REST (#1903) Signed-off-by: Marinov Avgustin --- .../DistributionSetTagManagementTest.java | 2 +- .../management/TargetTagManagementTest.java | 2 +- .../api/MgmtDistributionSetTagRestApi.java | 8 +-- .../mgmt/rest/api/MgmtTargetTagRestApi.java | 7 ++- .../MgmtDistributionSetTagResourceTest.java | 49 +++++++++++++++++++ .../resource/MgmtTargetTagResourceTest.java | 2 +- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DistributionSetTagManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DistributionSetTagManagementTest.java index dbff3fb4e..9b223fd76 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DistributionSetTagManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DistributionSetTagManagementTest.java @@ -194,7 +194,7 @@ public class DistributionSetTagManagementTest extends AbstractJpaIntegrationTest final List missing = new ArrayList<>(); for (int i = 0; i < 5; i++) { while (true) { - final Long id = RND.nextLong(); + final Long id = Math.abs(RND.nextLong()); if (!group.contains(id)) { missing.add(id); break; diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTagManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTagManagementTest.java index 39698de2f..c020d3ce8 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTagManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTagManagementTest.java @@ -190,7 +190,7 @@ class TargetTagManagementTest extends AbstractJpaIntegrationTest { final List missing = new ArrayList<>(); for (int i = 0; i < 5; i++) { while (true) { - final String id = String.valueOf(RND.nextLong()); + final String id = String.valueOf(Math.abs(RND.nextLong())); if (!group.contains(id)) { missing.add(id); break; 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 8135c4cdf..8b4153a6e 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 @@ -399,6 +399,8 @@ public interface MgmtDistributionSetTagRestApi { description = "Insufficient permissions, entity is not allowed to be changed (i.e. read-only) or " + "data volume restriction applies.", content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))), + @ApiResponse(responseCode = "404", description = "Not Found - e.g. a distribution set or tags are not found.", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = ExceptionInfo.class))), @ApiResponse(responseCode = "405", description = "The http request method is not allowed on the resource.", content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))), @ApiResponse(responseCode = "406", description = "In case accept header is specified and not application/json.", @@ -475,8 +477,8 @@ public interface MgmtDistributionSetTagRestApi { description = "Insufficient permissions, entity is not allowed to be changed (i.e. read-only) or " + "data volume restriction applies.", content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))), - @ApiResponse(responseCode = "404", description = "Distribution Set Tag not found.", - content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))), + @ApiResponse(responseCode = "404", description = "Not Found - e.g. a distribution set or tags are not found.", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = ExceptionInfo.class))), @ApiResponse(responseCode = "405", description = "The http request method is not allowed on the resource.", content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))), @ApiResponse(responseCode = "406", description = "In case accept header is specified and not application/json.", @@ -489,7 +491,7 @@ public interface MgmtDistributionSetTagRestApi { MgmtRestConstants.DISTRIBUTIONSET_TAG_DISTRIBUTIONSETS_REQUEST_MAPPING) ResponseEntity unassignDistributionSets( @PathVariable("distributionsetTagId") Long distributionsetTagId, - List distributionsetId); + @RequestBody List distributionsetId); /** * Handles the POST request to toggle the assignment of distribution sets by diff --git a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java index 5e03a530b..85fbc3160 100644 --- a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java +++ b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java @@ -335,8 +335,7 @@ public interface MgmtTargetTagRestApi { @ApiResponse(responseCode = "401", description = "The request requires user authentication."), @ApiResponse(responseCode = "403", description = "Insufficient permissions, entity is not allowed to be " + "changed (i.e. read-only) or data volume restriction applies."), - @ApiResponse(responseCode = "404", description = "Not Found - e.g. target tag not found. If targets not found - " + - "the body contains a list of ", + @ApiResponse(responseCode = "404", description = "Not Found - e.g. target tag not found. Contains info about not found.", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ExceptionInfo.class))), @ApiResponse(responseCode = "405", description = "The http request method is not allowed on the resource."), @ApiResponse(responseCode = "406", description = "In case accept header is specified and not application/json."), @@ -400,8 +399,8 @@ public interface MgmtTargetTagRestApi { @ApiResponse(responseCode = "401", description = "The request requires user authentication."), @ApiResponse(responseCode = "403", description = "Insufficient permissions, entity is not allowed to be " + "changed (i.e. read-only) or data volume restriction applies."), - @ApiResponse(responseCode = "404", description = "Target not found.", - content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))), + @ApiResponse(responseCode = "404", description = "Not Found - e.g. target tag not found. Contains info about not found.", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = ExceptionInfo.class))), @ApiResponse(responseCode = "405", description = "The http request method is not allowed on the resource."), @ApiResponse(responseCode = "406", description = "In case accept header is specified and not application/json."), @ApiResponse(responseCode = "429", description = "Too many requests. The server will refuse further attempts " + 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 dfef6d360..3bc45f2e1 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 @@ -20,8 +20,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; 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; +import java.util.Random; import java.util.stream.Collectors; import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants; @@ -30,13 +34,18 @@ import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreated import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagCreatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagUpdatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdatedEvent; +import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent; +import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent; +import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; 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.model.TargetTag; import org.eclipse.hawkbit.repository.test.matcher.Expect; import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents; +import org.eclipse.hawkbit.rest.json.model.ExceptionInfo; import org.eclipse.hawkbit.rest.util.JsonBuilder; import org.eclipse.hawkbit.rest.util.MockMvcResultPrinter; import org.json.JSONException; @@ -426,6 +435,46 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt .containsOnly(assigned.getId()); } + private static final Random RND = new Random(); + @Test + @Description("Verifies that tag assignments (multi targets) done through tag API command are correctly stored in the repository.") + @ExpectEvents({ + @Expect(type = DistributionSetTagCreatedEvent.class, count = 1), + @Expect(type = DistributionSetCreatedEvent.class, count = 2)}) + public void assignDistributionSetsNotFound() throws Exception { + final DistributionSetTag tag = testdataFactory.createDistributionSetTags(1).get(0); + final List sets = testdataFactory.createDistributionSetsWithoutModules(2).stream().map(DistributionSet::getId).toList(); + final List missing = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + while (true) { + final long id = Math.abs(RND.nextLong()); + if (!sets.contains(id)) { + missing.add(id); + break; + } + } + } + Collections.sort(missing); + final List withMissing = new ArrayList<>(sets); + withMissing.addAll(missing); + + mvc.perform( + put(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "/" + tag.getId() + "/assigned") + .content(JsonBuilder.toArray(withMissing)) + .contentType(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()) + .andExpect(status().isNotFound()) + .andExpect(handler -> { + final ExceptionInfo exceptionInfo = ResourceUtility.convertException(handler.getResponse().getContentAsString()); + final Map info = exceptionInfo.getInfo(); + assertThat(info).isNotNull(); + assertThat(info.get(EntityNotFoundException.TYPE)).isEqualTo(DistributionSet.class.getSimpleName()); + final List notFound = (List) info.get(EntityNotFoundException.ENTITY_ID); + Collections.sort(notFound); + assertThat(notFound).isEqualTo(missing); + }); + } + // DEPRECATED flows @Test 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 cb9cf405b..ed871f4ab 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 @@ -319,7 +319,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT final List missing = new ArrayList<>(); for (int i = 0; i < 5; i++) { while (true) { - final String id = String.valueOf(RND.nextLong()); + final String id = String.valueOf(Math.abs(RND.nextLong())); if (!targets.contains(id)) { missing.add(id); break;