Skip to content

Commit

Permalink
refactor: use NotFoundExceptionMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
StephGit committed Jan 26, 2024
1 parent cb64618 commit b5b71bd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand All @@ -250,7 +251,7 @@ public ResourceEntity getResourceEntityForRelease(@NotNull Collection<ResourceEn
//Otherwise, we're only interested in earlier releases than the requested one
else if (compareValue < 0) {
if (comparator.compare(resource.getRelease(), bestResource == null ? null : bestResource.getRelease()) > 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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<ReleaseEntity> getReleasesForResourceGroup(ResourceGroupEntity resourceGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,12 +61,8 @@ public List<ReleaseEntity> 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();
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -286,7 +287,7 @@ public List<ResourceGroupDTO> 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) {
Expand All @@ -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();
Expand All @@ -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<Integer> contextIds) {
@QueryParam("context") List<Integer> contextIds) throws NotFoundException {

ResourceEntity appServer = resourceLocator.getExactOrClosestPastReleaseByGroupIdAndReleaseId(resourceGroupId, releaseId);
if (appServer == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public void shouldInvokeResourcesWithRightArgumentsOnGetClosestPastRelease() thr
}

@Test
public void shouldInvokeBoundaryWithRightArgumentsOnGetApplicationsWithVersionForRelease() {
public void shouldInvokeBoundaryWithRightArgumentsOnGetApplicationsWithVersionForRelease() throws NotFoundException {
// given
Integer resourceGroupId = 8;
Integer releaseId = 9;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit b5b71bd

Please sign in to comment.