Skip to content

Commit

Permalink
feat: add modificationCount tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Feb 12, 2024
1 parent 580b5de commit 98647db
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
Expand Down Expand Up @@ -62,7 +61,6 @@ public Response searchAssignments(AssignmentSearchCriteriaDTO assignmentSearchCr
}

@Override
@Transactional
public Response createAssignment(CreateAssignmentRequestDTO createAssignmentRequestDTO) {
var role = roleDAO.findById(createAssignmentRequestDTO.getRoleId());
if (role == null) {
Expand All @@ -82,7 +80,6 @@ public Response createAssignment(CreateAssignmentRequestDTO createAssignmentRequ
}

@Override
@Transactional
public Response deleteAssignment(String id) {
dao.deleteQueryById(id);
return Response.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.persistence.OptimisticLockException;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
Expand Down Expand Up @@ -39,7 +39,6 @@ public class RoleRestController implements RoleInternalApi {
UriInfo uriInfo;

@Override
@Transactional
public Response createRole(CreateRoleRequestDTO createRoleRequestDTO) {
var role = mapper.create(createRoleRequestDTO);
role = dao.create(role);
Expand All @@ -50,7 +49,6 @@ public Response createRole(CreateRoleRequestDTO createRoleRequestDTO) {
}

@Override
@Transactional
public Response deleteRole(String id) {
dao.deleteQueryById(id);
return Response.noContent().build();
Expand All @@ -73,7 +71,6 @@ public Response searchRoles(RoleSearchCriteriaDTO roleSearchCriteriaDTO) {
}

@Override
@Transactional
public Response updateRole(String id, UpdateRoleRequestDTO updateRoleRequestDTO) {
var role = dao.findById(id);
if (role == null) {
Expand All @@ -94,4 +91,9 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
public RestResponse<ProblemDetailResponseDTO> exception(ConstraintException ex) {
return exceptionMapper.exception(ex);
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTO> exception(OptimisticLockException ex) {
return exceptionMapper.optimisticLock(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.Set;

import jakarta.persistence.OptimisticLockException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Path;
Expand All @@ -13,22 +14,23 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
import org.tkit.quarkus.log.cdi.LogService;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailInvalidParamDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailParamDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailResponseDTO;

@Mapper(uses = { OffsetDateTimeMapper.class })
public abstract class ExceptionMapper {
public interface ExceptionMapper {

public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
default RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
var dto = exception(ErrorKeys.CONSTRAINT_VIOLATIONS.name(), ex.getMessage());
dto.setInvalidParams(createErrorValidationResponse(ex.getConstraintViolations()));
return RestResponse.status(Response.Status.BAD_REQUEST, dto);
}

public RestResponse<ProblemDetailResponseDTO> exception(ConstraintException ex) {
default RestResponse<ProblemDetailResponseDTO> exception(ConstraintException ex) {
var dto = exception(ex.getMessageKey().name(), ex.getConstraints());
dto.setParams(map(ex.namedParameters));
return RestResponse.status(Response.Status.BAD_REQUEST, dto);
Expand All @@ -38,9 +40,9 @@ public RestResponse<ProblemDetailResponseDTO> exception(ConstraintException ex)
@Mapping(target = "params", ignore = true)
@Mapping(target = "invalidParams", ignore = true)
@Mapping(target = "removeInvalidParamsItem", ignore = true)
public abstract ProblemDetailResponseDTO exception(String errorCode, String detail);
ProblemDetailResponseDTO exception(String errorCode, String detail);

public List<ProblemDetailParamDTO> map(Map<String, Object> params) {
default List<ProblemDetailParamDTO> map(Map<String, Object> params) {
if (params == null) {
return List.of();
}
Expand All @@ -54,18 +56,26 @@ public List<ProblemDetailParamDTO> map(Map<String, Object> params) {
}).toList();
}

public abstract List<ProblemDetailInvalidParamDTO> createErrorValidationResponse(
List<ProblemDetailInvalidParamDTO> createErrorValidationResponse(
Set<ConstraintViolation<?>> constraintViolation);

@Mapping(target = "name", source = "propertyPath")
@Mapping(target = "message", source = "message")
public abstract ProblemDetailInvalidParamDTO createError(ConstraintViolation<?> constraintViolation);
ProblemDetailInvalidParamDTO createError(ConstraintViolation<?> constraintViolation);

public String mapPath(Path path) {
default String mapPath(Path path) {
return path.toString();
}

@LogService(log = false)
default RestResponse<ProblemDetailResponseDTO> optimisticLock(OptimisticLockException ex) {
var dto = exception(ErrorKeys.OPTIMISTIC_LOCK.name(), ex.getMessage());
return RestResponse.status(Response.Status.BAD_REQUEST, dto);
}

public enum ErrorKeys {

OPTIMISTIC_LOCK,
CONSTRAINT_VIOLATIONS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ void updateRoleTest() {
.then()
.statusCode(OK.getStatusCode());

// update Role with old modificationCount
var exception = given()
.contentType(APPLICATION_JSON)
.body(requestDto)
.when()
.put("r11")
.then()
.statusCode(BAD_REQUEST.getStatusCode())
.extract().as(ProblemDetailResponseDTO.class);

Assertions.assertNotNull(exception);
Assertions.assertEquals(ExceptionMapper.ErrorKeys.OPTIMISTIC_LOCK.name(), exception.getErrorCode());
Assertions.assertEquals(
"Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [org.tkit.onecx.permission.domain.models.Role#r11]",
exception.getDetail());

// download Role
dto = given().contentType(APPLICATION_JSON)
.when()
Expand Down

0 comments on commit 98647db

Please sign in to comment.