From b5b71bd6d01d6f09c27e1cfaf6edf4c2ca6fd939 Mon Sep 17 00:00:00 2001 From: Stephan Girod Date: Thu, 25 Jan 2024 15:07:31 +0100 Subject: [PATCH] refactor: use NotFoundExceptionMapper --- .../ResourceDependencyResolverService.java | 5 +++-- .../releasing/boundary/ReleaseLocator.java | 14 ++++++++++++-- .../boundary/ResourceLocator.java | 5 +++-- .../mobiliar/rest/releases/ReleasesRest.java | 18 +++++------------- .../rest/resources/ResourceGroupsRest.java | 7 ++++--- .../rest/resources/ResourceGroupsRestTest.java | 6 +++--- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/generator/control/extracted/ResourceDependencyResolverService.java b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/generator/control/extracted/ResourceDependencyResolverService.java index 2cc340f9a..186135988 100644 --- a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/generator/control/extracted/ResourceDependencyResolverService.java +++ b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/generator/control/extracted/ResourceDependencyResolverService.java @@ -28,6 +28,7 @@ import ch.puzzle.itc.mobiliar.business.resourcegroup.entity.ResourceGroupEntity; import ch.puzzle.itc.mobiliar.business.resourcerelation.entity.ConsumedResourceRelationEntity; import ch.puzzle.itc.mobiliar.business.resourcerelation.entity.ProvidedResourceRelationEntity; +import ch.puzzle.itc.mobiliar.common.exception.NotFoundException; import ch.puzzle.itc.mobiliar.common.util.DefaultResourceTypeDefinition; import javax.ejb.Stateless; @@ -233,7 +234,7 @@ public ResourceEntity getResourceEntityForRelease(@NotNull ResourceGroupEntity r * @param releaseId * @return */ - public ResourceEntity getResourceEntityForRelease(@NotNull Integer resourceGroupId, @NotNull Integer releaseId) { + public ResourceEntity getResourceEntityForRelease(@NotNull Integer resourceGroupId, @NotNull Integer releaseId) throws NotFoundException { ResourceGroupEntity resourceGroup = resourceGroupLocator.getResourceGroupForCreateDeploy(resourceGroupId); return getResourceEntityForRelease(resourceGroup.getResources(), releaseLocator.getReleaseById(releaseId)); } @@ -250,7 +251,7 @@ public ResourceEntity getResourceEntityForRelease(@NotNull Collection 0) { - //If the release date of the current resource is later than the the best release we've found yet, it is better suited and is our new "best resource" + //If the release date of the current resource is later than the best release we've found yet, it is better suited and is our new "best resource" bestResource = resource; } } diff --git a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/releasing/boundary/ReleaseLocator.java b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/releasing/boundary/ReleaseLocator.java index d294fbf8b..f647258da 100644 --- a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/releasing/boundary/ReleaseLocator.java +++ b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/releasing/boundary/ReleaseLocator.java @@ -33,6 +33,8 @@ import ch.puzzle.itc.mobiliar.business.resourcegroup.entity.ResourceGroupEntity; import ch.puzzle.itc.mobiliar.business.security.entity.Permission; import ch.puzzle.itc.mobiliar.business.security.interceptor.HasPermission; +import ch.puzzle.itc.mobiliar.common.exception.NotFoundException; +import lombok.NonNull; import static ch.puzzle.itc.mobiliar.business.security.entity.Action.*; @@ -54,8 +56,16 @@ public ReleaseEntity getReleaseByName(String name) { } @HasPermission(permission = Permission.RELEASE, action = READ) - public ReleaseEntity getReleaseById(Integer id) { - return releaseRepository.find(id); + public ReleaseEntity getReleaseById(@NonNull Integer id) throws NotFoundException { + ReleaseEntity entity = releaseRepository.find(id); + this.requireNotNull(entity); + return entity; + } + + private void requireNotNull(ReleaseEntity entity) throws NotFoundException { + if (entity == null) { + throw new NotFoundException("Release not found."); + } } public List getReleasesForResourceGroup(ResourceGroupEntity resourceGroup) { diff --git a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/boundary/ResourceLocator.java b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/boundary/ResourceLocator.java index 1ba18746f..ebfbb1154 100644 --- a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/boundary/ResourceLocator.java +++ b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/boundary/ResourceLocator.java @@ -30,6 +30,7 @@ import ch.puzzle.itc.mobiliar.business.resourcegroup.entity.ResourceTypeEntity; import ch.puzzle.itc.mobiliar.business.resourcerelation.entity.ConsumedResourceRelationEntity; import ch.puzzle.itc.mobiliar.business.utils.ValidationHelper; +import ch.puzzle.itc.mobiliar.common.exception.NotFoundException; import ch.puzzle.itc.mobiliar.common.exception.ValidationException; import ch.puzzle.itc.mobiliar.common.util.ConfigKey; import ch.puzzle.itc.mobiliar.common.util.ConfigurationService; @@ -116,7 +117,7 @@ public ReleaseEntity getExactOrClosestPastReleaseByGroupNameAndRelease(String na * @param releaseId release id * @return ResourceEntity */ - public ResourceEntity getExactOrClosestPastReleaseByGroupIdAndReleaseId(@NotNull Integer groupId, @NotNull Integer releaseId) { + public ResourceEntity getExactOrClosestPastReleaseByGroupIdAndReleaseId(@NotNull Integer groupId, @NotNull Integer releaseId) throws NotFoundException { ReleaseEntity release = releaseLocator.getReleaseById(releaseId); ResourceGroupEntity resGroup = resourceGroupRepository.getResourceGroupById(groupId); @@ -135,7 +136,7 @@ public ResourceEntity getExactOrClosestPastReleaseByGroupIdAndReleaseId(@NotNull * @param releaseId release id * @return */ - public ResourceEntity getResourceByGroupIdAndRelease(@NotNull Integer groupId, @NotNull Integer releaseId) { + public ResourceEntity getResourceByGroupIdAndRelease(@NotNull Integer groupId, @NotNull Integer releaseId) throws NotFoundException { ReleaseEntity release = releaseLocator.getReleaseById(releaseId); try { diff --git a/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/releases/ReleasesRest.java b/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/releases/ReleasesRest.java index 2a69fe5a6..6dbadf27f 100644 --- a/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/releases/ReleasesRest.java +++ b/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/releases/ReleasesRest.java @@ -24,6 +24,7 @@ import ch.puzzle.itc.mobiliar.business.releasing.boundary.ReleaseLocator; import ch.puzzle.itc.mobiliar.business.releasing.control.ReleaseMgmtService; import ch.puzzle.itc.mobiliar.business.releasing.entity.ReleaseEntity; +import ch.puzzle.itc.mobiliar.common.exception.NotFoundException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -60,12 +61,8 @@ public List getReleases(@QueryParam("start") Integer start, @Quer @GET @Path("/{id}") @ApiOperation(value = "Get a release", notes = "Returns the specifed release") - public Response getRelease(@PathParam("id") int id) { + public Response getRelease(@PathParam("id") int id) throws NotFoundException { ReleaseEntity release = releaseLocator.getReleaseById(id); - if (release == null) { - return Response.status(Response.Status.NOT_FOUND).build(); - } - return Response.ok(release).build(); } @@ -103,10 +100,8 @@ public Response addRelease(@ApiParam() ReleaseEntity request) { // support digit only @Produces("application/json") @ApiOperation(value = "Update a release") - public Response updateRelease(@ApiParam("Release ID") @PathParam("id") Integer id, ReleaseEntity request) { - if (releaseLocator.getReleaseById(id) == null) { - return Response.status(NOT_FOUND).build(); - } + public Response updateRelease(@ApiParam("Release ID") @PathParam("id") Integer id, ReleaseEntity request) throws NotFoundException { + releaseLocator.getReleaseById(id); if (releaseLocator.update(request)) { return Response.status(OK).build(); } else { @@ -118,11 +113,8 @@ public Response updateRelease(@ApiParam("Release ID") @PathParam("id") Integer i @Path("/{id : \\d+}") // support digit only @ApiOperation(value = "Remove a release") - public Response deleteRelease(@ApiParam("Release ID") @PathParam("id") Integer id) { + public Response deleteRelease(@ApiParam("Release ID") @PathParam("id") Integer id) throws NotFoundException { ReleaseEntity release = releaseLocator.getReleaseById(id); - if (release == null) { - return Response.status(NOT_FOUND).build(); - } if (!releaseLocator.loadResourcesAndDeploymentsForRelease(id).keySet().isEmpty()) { return Response.status(CONFLICT).entity(new ExceptionDto("Constraint violation. Cascade-delete is not supported. ")).build(); } diff --git a/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRest.java b/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRest.java index a5c4d6356..4ba7c90e2 100644 --- a/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRest.java +++ b/AMW_rest/src/main/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRest.java @@ -42,6 +42,7 @@ import ch.puzzle.itc.mobiliar.business.security.boundary.PermissionBoundary; import ch.puzzle.itc.mobiliar.common.exception.AMWException; import ch.puzzle.itc.mobiliar.common.exception.ElementAlreadyExistsException; +import ch.puzzle.itc.mobiliar.common.exception.NotFoundException; import ch.puzzle.itc.mobiliar.common.exception.ResourceNotFoundException; import ch.puzzle.itc.mobiliar.common.exception.ValidationException; import io.swagger.annotations.Api; @@ -286,7 +287,7 @@ public List getAllResourceGroups(@QueryParam("includeAppServer @GET @ApiOperation(value = "Get resource in specific release - used by Angular") public ResourceDTO getResourceRelationListForRelease(@PathParam("resourceGroupId") Integer resourceGroupId, - @PathParam("releaseId") Integer releaseId) { + @PathParam("releaseId") Integer releaseId) throws NotFoundException { ResourceEntity resource = resourceDependencyResolverService.getResourceEntityForRelease(resourceGroupId, releaseId); if (resource == null) { @@ -303,7 +304,7 @@ public ResourceDTO getResourceRelationListForRelease(@PathParam("resourceGroupId @DELETE @ApiOperation(value = "Delete a specific resource release") public Response deleteResourceRelease(@PathParam("resourceGroupId") Integer resourceGroupId, - @PathParam("releaseId") Integer releaseId) throws ResourceNotFoundException, ElementAlreadyExistsException, ForeignableOwnerViolationException { + @PathParam("releaseId") Integer releaseId) throws NotFoundException, ElementAlreadyExistsException, ForeignableOwnerViolationException { ResourceEntity resource = resourceLocator.getResourceByGroupIdAndRelease(resourceGroupId, releaseId); if (resource == null) { return Response.status(NOT_FOUND).entity(new ExceptionDto("Resource not found")).build(); @@ -317,7 +318,7 @@ public Response deleteResourceRelease(@PathParam("resourceGroupId") Integer reso @ApiOperation(value = "Get application with version for a specific resourceGroup, release and context(s) - used by Angular") public Response getApplicationsWithVersionForRelease(@PathParam("resourceGroupId") Integer resourceGroupId, @PathParam("releaseId") Integer releaseId, - @QueryParam("context") List contextIds) { + @QueryParam("context") List contextIds) throws NotFoundException { ResourceEntity appServer = resourceLocator.getExactOrClosestPastReleaseByGroupIdAndReleaseId(resourceGroupId, releaseId); if (appServer == null) { diff --git a/AMW_rest/src/test/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRestTest.java b/AMW_rest/src/test/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRestTest.java index 852aa1e80..4f643c2cd 100644 --- a/AMW_rest/src/test/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRestTest.java +++ b/AMW_rest/src/test/java/ch/mobi/itc/mobiliar/rest/resources/ResourceGroupsRestTest.java @@ -334,7 +334,7 @@ public void shouldInvokeResourcesWithRightArgumentsOnGetClosestPastRelease() thr } @Test - public void shouldInvokeBoundaryWithRightArgumentsOnGetApplicationsWithVersionForRelease() { + public void shouldInvokeBoundaryWithRightArgumentsOnGetApplicationsWithVersionForRelease() throws NotFoundException { // given Integer resourceGroupId = 8; Integer releaseId = 9; @@ -412,7 +412,7 @@ public void shouldFilterOutAppServerContainer() { } @Test - public void shouldDeleteResource() throws ResourceNotFoundException, ElementAlreadyExistsException, ForeignableOwnerViolationException { + public void shouldDeleteResource() throws NotFoundException, ElementAlreadyExistsException, ForeignableOwnerViolationException { // given Integer resourceGroupId = 8; Integer resourceId = 9; @@ -429,7 +429,7 @@ public void shouldDeleteResource() throws ResourceNotFoundException, ElementAlre } @Test - public void shouldReturnNotFoundWhenDeleteResource() throws ResourceNotFoundException, ElementAlreadyExistsException, ForeignableOwnerViolationException { + public void shouldReturnNotFoundWhenDeleteResource() throws NotFoundException, ElementAlreadyExistsException, ForeignableOwnerViolationException { // given Integer resourceGroupId = 8; Integer releaseId = 10;