From 34d89a87ba08fc7f2b4251e0b232cca6a19f06b9 Mon Sep 17 00:00:00 2001 From: Andrea De Lella Date: Wed, 16 Oct 2024 16:45:07 +0200 Subject: [PATCH] feat: add mandatory tests for issuers --- .../handler/ControllerExceptionHandler.java | 25 +++++++++++++++++++ .../iniclient/dto/IssuerCreateRequestDTO.java | 6 +++++ .../fse2/ms/iniclient/enums/TestTypeEnum.java | 10 ++++++++ .../repository/entity/IssuerETY.java | 10 ++++++++ .../ms/iniclient/service/impl/IssuerSRV.java | 7 ++++++ 5 files changed, 58 insertions(+) create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/iniclient/enums/TestTypeEnum.java diff --git a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/controller/handler/ControllerExceptionHandler.java b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/controller/handler/ControllerExceptionHandler.java index a12d2d4b..33c46f5d 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/controller/handler/ControllerExceptionHandler.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/controller/handler/ControllerExceptionHandler.java @@ -24,6 +24,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.lang.NonNull; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -31,8 +32,12 @@ import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; + import static it.finanze.sanita.fse2.ms.iniclient.enums.ErrorClassEnum.*; +import java.util.Arrays; + /** * Exceptions Handler. */ @@ -118,4 +123,24 @@ protected ResponseEntity handleGenericException(final Exceptio return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + @Override + protected ResponseEntity handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { + + LogTraceInfoDTO traceInfo = getLogTraceInfo(); + + ErrorDTO error = new ErrorDTO(); + if (ex.getCause() instanceof InvalidFormatException) { + InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause(); + if (invalidFormatException.getTargetType().isEnum()) { + String invalidValue = invalidFormatException.getValue().toString(); + String errorMessage = String.format("Invalid value '%s' for enum. Allowed values are: %s", + invalidValue, Arrays.toString(invalidFormatException.getTargetType().getEnumConstants())); + error = new ErrorDTO(INVALID_INPUT.getType(), INVALID_INPUT.getTitle(), errorMessage, INVALID_INPUT.getInstance()); + } + } + + ErrorResponseDTO response = new ErrorResponseDTO(traceInfo, error); + return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); + } + } \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/dto/IssuerCreateRequestDTO.java b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/dto/IssuerCreateRequestDTO.java index d5e30cfa..3e756b1d 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/dto/IssuerCreateRequestDTO.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/dto/IssuerCreateRequestDTO.java @@ -1,9 +1,13 @@ package it.finanze.sanita.fse2.ms.iniclient.dto; import com.mongodb.lang.Nullable; + +import it.finanze.sanita.fse2.ms.iniclient.enums.TestTypeEnum; import it.finanze.sanita.fse2.ms.iniclient.validators.ValidMiddlewareIssuer; import lombok.*; +import java.util.List; + import javax.validation.constraints.NotBlank; @Data @@ -35,4 +39,6 @@ public class IssuerCreateRequestDTO { private String pazienteCf; private boolean readyToScan; + + private List mandatoryTests; } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/enums/TestTypeEnum.java b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/enums/TestTypeEnum.java new file mode 100644 index 00000000..ac51607a --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/enums/TestTypeEnum.java @@ -0,0 +1,10 @@ +package it.finanze.sanita.fse2.ms.iniclient.enums; + +public enum TestTypeEnum { + + CREATE, + REPLACE, + UPDATE, + DELETE; + +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/repository/entity/IssuerETY.java b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/repository/entity/IssuerETY.java index fe1449fe..54a562b9 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/repository/entity/IssuerETY.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/repository/entity/IssuerETY.java @@ -1,8 +1,14 @@ package it.finanze.sanita.fse2.ms.iniclient.repository.entity; import com.mongodb.lang.Nullable; + +import it.finanze.sanita.fse2.ms.iniclient.enums.TestTypeEnum; import lombok.Data; import lombok.NoArgsConstructor; + +import java.util.Arrays; +import java.util.List; + import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; @@ -21,6 +27,7 @@ public class IssuerETY { public static final String PAZIENTE_CF = "pazienteCf"; public static final String EMAIL_SENT = "isEmailSent"; public static final String READY_TO_SCAN = "readyToScan"; + public static final String MANDATORY_TESTS = "mandatoryTests"; @@ -55,5 +62,8 @@ public class IssuerETY { @Field(name = EMAIL_SENT) private boolean emailSent; + + @Field(name = MANDATORY_TESTS) + private List mandatoryTests = Arrays.asList(TestTypeEnum.values()); } \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/service/impl/IssuerSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/service/impl/IssuerSRV.java index 282b0fd8..e4867711 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/iniclient/service/impl/IssuerSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/iniclient/service/impl/IssuerSRV.java @@ -15,6 +15,7 @@ import it.finanze.sanita.fse2.ms.iniclient.dto.IssuersDTO; import it.finanze.sanita.fse2.ms.iniclient.dto.response.IssuerDeleteResponseDTO; import it.finanze.sanita.fse2.ms.iniclient.dto.response.IssuerResponseDTO; +import it.finanze.sanita.fse2.ms.iniclient.enums.TestTypeEnum; import it.finanze.sanita.fse2.ms.iniclient.exceptions.base.BadRequestException; import it.finanze.sanita.fse2.ms.iniclient.exceptions.base.BusinessException; import it.finanze.sanita.fse2.ms.iniclient.exceptions.base.InputValidationException; @@ -61,6 +62,9 @@ public IssuerResponseDTO createIssuer(IssuerCreateRequestDTO issuerDTO) { entity.setEtichettaRegione(issuerDTO.getEtichettaRegione()); entity.setPazienteCf(issuerDTO.getPazienteCf()); entity.setReadyToScan(issuerDTO.isReadyToScan()); + List mandatoryTests = issuerDTO.getMandatoryTests(); + if(mandatoryTests!=null && !mandatoryTests.isEmpty()) + entity.setMandatoryTests(mandatoryTests); IssuerETY asl = null; if (!StringUtility.isNullOrEmpty(issuerDTO.getNomeDocumentRepository())) { @@ -116,6 +120,9 @@ public IssuerResponseDTO updateIssuer(IssuerETY issuerETY, IssuerCreateRequestDT entity.setPazienteCf(issuerDTO.getPazienteCf()); entity.setReadyToScan(issuerDTO.isReadyToScan()); entity.setEmailSent(issuerETY.isEmailSent()); + List mandatoryTests = issuerDTO.getMandatoryTests(); + if(mandatoryTests!=null && !mandatoryTests.isEmpty()) + entity.setMandatoryTests(mandatoryTests); if (issuerDTO.getNomeDocumentRepository() != null && entity.getMiddleware()) throw new BadRequestException("Sono giĆ  presenti documenti con asl. Impossibile caricare il middleware");