From 1fe00577924a50f0330ddd5e40bb7ff9393f4732 Mon Sep 17 00:00:00 2001 From: gioelemella <128155546+gioelemella@users.noreply.github.com> Date: Tue, 14 May 2024 11:44:24 +0200 Subject: [PATCH] [VAS-1034] feat: Add check on create public bundle request (#105) * [VAS-1034] added check for generic attribute on public bundle request creation * [VAS-1034] updated unit tests --------- Co-authored-by: giomella --- .../afm/marketplacebe/exception/AppError.java | 1 + .../service/BundleRequestService.java | 72 +++++--- .../service/BundleRequestServiceTest.java | 165 ++++++++++++------ 3 files changed, 160 insertions(+), 78 deletions(-) diff --git a/src/main/java/it/pagopa/afm/marketplacebe/exception/AppError.java b/src/main/java/it/pagopa/afm/marketplacebe/exception/AppError.java index 0fa30a99..c8709177 100644 --- a/src/main/java/it/pagopa/afm/marketplacebe/exception/AppError.java +++ b/src/main/java/it/pagopa/afm/marketplacebe/exception/AppError.java @@ -34,6 +34,7 @@ public enum AppError { CI_BUNDLE_ID_NOT_FOUND(HttpStatus.NOT_FOUND, "No CI-BUNDLE relationship found", "CI-Bundle %s not found."), BUNDLE_REQUEST_BAD_REQUEST(HttpStatus.BAD_REQUEST, "Bundle request bad request", "Bundle request with id %s not configured with %s."), + BUNDLE_REQUEST_BAD_ATTRIBUTE(HttpStatus.BAD_REQUEST, "Bundle request bad attribute", "Bundle request with id %s has invalid attributes: %s."), BUNDLE_REQUEST_BAD_DATE(HttpStatus.BAD_REQUEST, "Bundle request bad request", "Date %s is not valid."), BUNDLE_REQUEST_NOT_FOUND(HttpStatus.NOT_FOUND, "Bundle request not found", "Bundle request with id %s not found."), BUNDLE_REQUEST_CONFLICT(HttpStatus.CONFLICT, "Bundle request conflict", "Bundle request with id %s. %s"), diff --git a/src/main/java/it/pagopa/afm/marketplacebe/service/BundleRequestService.java b/src/main/java/it/pagopa/afm/marketplacebe/service/BundleRequestService.java index 9ab95035..5d969399 100644 --- a/src/main/java/it/pagopa/afm/marketplacebe/service/BundleRequestService.java +++ b/src/main/java/it/pagopa/afm/marketplacebe/service/BundleRequestService.java @@ -10,6 +10,7 @@ import it.pagopa.afm.marketplacebe.exception.AppException; import it.pagopa.afm.marketplacebe.model.PageInfo; import it.pagopa.afm.marketplacebe.model.request.BundleRequestId; +import it.pagopa.afm.marketplacebe.model.request.CiBundleAttributeModel; import it.pagopa.afm.marketplacebe.model.request.PublicBundleRequest; import it.pagopa.afm.marketplacebe.model.request.CiBundleSubscriptionRequest; import it.pagopa.afm.marketplacebe.model.request.PublicBundleRequests; @@ -62,11 +63,16 @@ public BundleRequestService( this.modelMapper = modelMapper; } + /** + * Create a public bundle's request with the provided info + * + * @param ciFiscalCode creditor institution's tax code + * @param ciBundleSubscriptionRequest object that contain the request info + * @return bundle request id + */ public BundleRequestId createBundleRequest(String ciFiscalCode, CiBundleSubscriptionRequest ciBundleSubscriptionRequest) { // retrieve bundle by idBundle and check if it is public - String idBundle = ciBundleSubscriptionRequest.getIdBundle(); - Bundle bundle = getBundle(idBundle); // a bundle request is acceptable if validityDateTo is after now @@ -78,29 +84,13 @@ public BundleRequestId createBundleRequest(String ciFiscalCode, CiBundleSubscrip throw new AppException(AppError.BUNDLE_OFFER_CONFLICT, idBundle, "Type not public"); } - // rule R15: attribute payment amount should be lower than bundle one - if (ciBundleSubscriptionRequest.getCiBundleAttributeModelList() != null) { - ciBundleSubscriptionRequest.getCiBundleAttributeModelList().parallelStream().forEach(attribute -> { - if (attribute.getMaxPaymentAmount().compareTo(bundle.getPaymentAmount()) > 0) { - throw new AppException(AppError.BUNDLE_REQUEST_BAD_REQUEST, idBundle, "Payment amount should be lower than or equal to bundle payment amount."); - } - }); + List attributeModelList = ciBundleSubscriptionRequest.getCiBundleAttributeModelList(); + List attributes = new ArrayList<>(); + if (attributeModelList != null && !attributeModelList.isEmpty()) { + validateCIBundleAttributes(attributeModelList, bundle, idBundle); + attributes = buildCiBundleAttributes(idBundle, attributeModelList); } - List attributes = (ciBundleSubscriptionRequest.getCiBundleAttributeModelList() != null - && !ciBundleSubscriptionRequest.getCiBundleAttributeModelList().isEmpty()) ? - ciBundleSubscriptionRequest.getCiBundleAttributeModelList() - .stream() - .map(attribute -> - CiBundleAttribute.builder() - .id(idBundle + "-" + UUID.randomUUID()) - .insertedDate(LocalDateTime.now()) - .maxPaymentAmount(attribute.getMaxPaymentAmount()) - .transferCategory(attribute.getTransferCategory()) - .transferCategoryRelation(attribute.getTransferCategoryRelation()) - .build() - ).toList() : new ArrayList<>(); - BundleRequestEntity request = BundleRequestEntity.builder() .idBundle(bundle.getId()) .idPsp(bundle.getIdPsp()) @@ -108,8 +98,7 @@ public BundleRequestId createBundleRequest(String ciFiscalCode, CiBundleSubscrip .ciBundleAttributes(attributes) .build(); - bundleRequestRepository.save(request); - + this.bundleRequestRepository.save(request); return BundleRequestId.builder().idBundleRequest(request.getId()).build(); } @@ -283,4 +272,37 @@ private Bundle getBundle(String idBundle) { private int calculateTotalPages(Integer limit, double totalItems) { return (int) Math.ceil(totalItems / limit); } + + private List buildCiBundleAttributes(String idBundle, List ciBundleAttributeModelList) { + return ciBundleAttributeModelList.parallelStream() + .map(attribute -> CiBundleAttribute.builder() + .id(String.format("%s-%s", idBundle, UUID.randomUUID())) + .insertedDate(LocalDateTime.now()) + .maxPaymentAmount(attribute.getMaxPaymentAmount()) + .transferCategory(attribute.getTransferCategory()) + .transferCategoryRelation(attribute.getTransferCategoryRelation()) + .build()).toList(); + } + + private void validateCIBundleAttributes(List attributeModelList, Bundle bundle, String idBundle) { + attributeModelList.parallelStream().forEach( + attribute -> { + // rule R15: attribute payment amount should be lower than bundle one + if (attribute.getMaxPaymentAmount().compareTo(bundle.getPaymentAmount()) > 0) { + throw new AppException( + AppError.BUNDLE_REQUEST_BAD_REQUEST, + idBundle, + "Payment amount should be lower than or equal to bundle payment amount." + ); + } + if (attribute.getTransferCategory() == null && attributeModelList.size() > 1) { + throw new AppException( + AppError.BUNDLE_REQUEST_BAD_ATTRIBUTE, + idBundle, + "Only one attribute can be specified if the attribute has transfer category null" + ); + } + + }); + } } diff --git a/src/test/java/it/pagopa/afm/marketplacebe/service/BundleRequestServiceTest.java b/src/test/java/it/pagopa/afm/marketplacebe/service/BundleRequestServiceTest.java index 8fa246f2..b884426a 100644 --- a/src/test/java/it/pagopa/afm/marketplacebe/service/BundleRequestServiceTest.java +++ b/src/test/java/it/pagopa/afm/marketplacebe/service/BundleRequestServiceTest.java @@ -7,8 +7,10 @@ import it.pagopa.afm.marketplacebe.entity.BundleRequestEntity; import it.pagopa.afm.marketplacebe.entity.BundleType; import it.pagopa.afm.marketplacebe.entity.CiBundle; +import it.pagopa.afm.marketplacebe.entity.TransferCategoryRelation; import it.pagopa.afm.marketplacebe.exception.AppException; import it.pagopa.afm.marketplacebe.model.request.BundleRequestId; +import it.pagopa.afm.marketplacebe.model.request.CiBundleAttributeModel; import it.pagopa.afm.marketplacebe.model.request.CiBundleSubscriptionRequest; import it.pagopa.afm.marketplacebe.model.request.PublicBundleRequests; import it.pagopa.afm.marketplacebe.repository.ArchivedBundleRequestRepository; @@ -18,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -26,6 +27,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -33,10 +35,11 @@ import static it.pagopa.afm.marketplacebe.TestUtil.getMockBundle; import static it.pagopa.afm.marketplacebe.TestUtil.getMockBundleRequestE; import static it.pagopa.afm.marketplacebe.TestUtil.getMockCiBundle; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -44,6 +47,8 @@ @SpringBootTest(classes = {BundleRequestService.class, MappingsConfiguration.class}) class BundleRequestServiceTest { + + private static final String FISCAL_CODE = "ciTaxCode"; @MockBean private BundleRepository bundleRepository; @@ -65,7 +70,6 @@ class BundleRequestServiceTest { @ParameterizedTest @ValueSource(strings = {"PUBLIC"}) void shouldCreateBundleRequest(String bundleType) { - CiBundle ciBundle = getMockCiBundle(); Bundle bundle = getMockBundle(); bundle.setValidityDateTo(null); bundle.setType(BundleType.fromValue(bundleType)); @@ -73,12 +77,11 @@ void shouldCreateBundleRequest(String bundleType) { ciBundleSubscriptionRequest.setIdBundle(bundle.getId()); // Preconditions - Mockito.when(bundleRepository.findById(bundle.getId())) - .thenReturn(Optional.of(bundle)); + when(bundleRepository.findById(bundle.getId())).thenReturn(Optional.of(bundle)); - bundleRequestService.createBundleRequest(ciBundle.getCiFiscalCode(), ciBundleSubscriptionRequest); + assertDoesNotThrow(() -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest)); - verify(bundleRequestRepository, times(1)).save(Mockito.any()); + verify(bundleRequestRepository).save(any()); } @Test @@ -90,10 +93,12 @@ void shouldRaiseExceptionBundleTypeCreateBundleRequest_1() { ciBundleSubscriptionRequest.setIdBundle(bundle.getId()); // Preconditions - Mockito.when(bundleRepository.findById(bundle.getId())) - .thenReturn(Optional.of(bundle)); + when(bundleRepository.findById(bundle.getId())).thenReturn(Optional.of(bundle)); + + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest)); - createBundleRequest_ko(ciBundleSubscriptionRequest, HttpStatus.CONFLICT); + assertEquals(HttpStatus.CONFLICT, e.getHttpStatus()); } @Test @@ -106,10 +111,12 @@ void shouldRaiseExceptionBundleTypeCreateBundleRequest_2() { ciBundleSubscriptionRequest.getCiBundleAttributeModelList().get(0).setMaxPaymentAmount(20000L); // Preconditions - Mockito.when(bundleRepository.findById(bundle.getId())) - .thenReturn(Optional.of(bundle)); + when(bundleRepository.findById(bundle.getId())).thenReturn(Optional.of(bundle)); - createBundleRequest_ko(ciBundleSubscriptionRequest, HttpStatus.BAD_REQUEST); + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest)); + + assertEquals(HttpStatus.BAD_REQUEST, e.getHttpStatus()); } @Test @@ -121,10 +128,32 @@ void shouldRaiseExceptionBundleNotFoundCreateBundleRequest() { ciBundleSubscriptionRequest.setIdBundle(bundle.getId()); // Preconditions - Mockito.when(bundleRepository.findById(bundle.getId())) + when(bundleRepository.findById(bundle.getId())) .thenReturn(Optional.empty()); - createBundleRequest_ko(ciBundleSubscriptionRequest, HttpStatus.NOT_FOUND); + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest)); + + assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); + } + + @Test + void shouldRaiseExceptionOnBundleAttributesCreateBundleRequest() { + Bundle bundle = TestUtil.getMockBundle(); + bundle.setType(BundleType.PUBLIC); + + CiBundleSubscriptionRequest ciBundleSubscriptionRequest = CiBundleSubscriptionRequest.builder() + .idBundle(bundle.getId()) + .ciBundleAttributeModelList(buildBadCIBundleAttributeModelList()) + .build(); + + // Preconditions + when(bundleRepository.findById(bundle.getId())).thenReturn(Optional.of(bundle)); + + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest)); + + assertEquals(HttpStatus.BAD_REQUEST, e.getHttpStatus()); } @Test @@ -132,8 +161,7 @@ void shouldRemoveBundleRequest() { BundleRequestEntity bundleRequest = getMockBundleRequestE(); // Preconditions - Mockito.when(bundleRequestRepository.findById(bundleRequest.getId())) - .thenReturn(Optional.of(bundleRequest)); + when(bundleRequestRepository.findById(bundleRequest.getId())).thenReturn(Optional.of(bundleRequest)); bundleRequestService.removeBundleRequest(bundleRequest.getCiFiscalCode(), bundleRequest.getId()); verify(bundleRequestRepository, times(1)).delete(bundleRequest); @@ -145,8 +173,7 @@ void shouldRaiseBadRequestFiscalCodeBundleRequest() { String ciFiscalCode = "ABC"; // Preconditions - Mockito.when(bundleRequestRepository.findById(bundleRequest.getId())) - .thenReturn(Optional.of(bundleRequest)); + when(bundleRequestRepository.findById(bundleRequest.getId())).thenReturn(Optional.of(bundleRequest)); removeBundleRequest_ko(ciFiscalCode, bundleRequest.getId(), HttpStatus.BAD_REQUEST); } @@ -156,8 +183,7 @@ void shouldRaiseBadRequestNoRequest() { BundleRequestEntity bundleRequest = getMockBundleRequestE(); // Preconditions - Mockito.when(bundleRequestRepository.findById(bundleRequest.getId())) - .thenReturn(Optional.empty()); + when(bundleRequestRepository.findById(bundleRequest.getId())).thenReturn(Optional.empty()); removeBundleRequest_ko(TestUtil.getMockCiFiscalCode(), bundleRequest.getId(), HttpStatus.NOT_FOUND); } @@ -167,7 +193,7 @@ void shouldRaiseBadRequestNoRequest() { void shouldGetRequestsByPsp_1() { List bundleRequests = List.of(getMockBundleRequestE()); - Mockito.when(bundleRequestRepository.findByIdPspAndFiscalCodeAndIdBundle(bundleRequests.get(0).getIdPsp(), null, null, 0 ,100)) + when(bundleRequestRepository.findByIdPspAndFiscalCodeAndIdBundle(bundleRequests.get(0).getIdPsp(), null, null, 0 ,100)) .thenReturn(bundleRequests); PublicBundleRequests requests = bundleRequestService.getPublicBundleRequests(bundleRequests.get(0).getIdPsp(), @@ -181,7 +207,7 @@ void shouldGetRequestsByPsp_1() { void shouldGetRequestsByPsp_2() { List bundleRequests = List.of(getMockBundleRequestE()); - Mockito.when(bundleRequestRepository.findByIdPspAndFiscalCodeAndIdBundle(bundleRequests.get(0).getIdPsp(), bundleRequests.get(0).getCiFiscalCode(), null, 0,100)) + when(bundleRequestRepository.findByIdPspAndFiscalCodeAndIdBundle(bundleRequests.get(0).getIdPsp(), bundleRequests.get(0).getCiFiscalCode(), null, 0,100)) .thenReturn(bundleRequests); PublicBundleRequests requests = bundleRequestService.getPublicBundleRequests(bundleRequests.get(0).getIdPsp(), @@ -195,7 +221,7 @@ void shouldGetRequestsByPsp_2() { void shouldGetRequestsByPsp_3() { List bundleRequests = List.of(getMockBundleRequestE()); - Mockito.when(bundleRequestRepository.findByIdPspAndFiscalCodeAndIdBundle(bundleRequests.get(0).getIdPsp(), bundleRequests.get(0).getCiFiscalCode(), + when(bundleRequestRepository.findByIdPspAndFiscalCodeAndIdBundle(bundleRequests.get(0).getIdPsp(), bundleRequests.get(0).getCiFiscalCode(), bundleRequests.get(0).getIdBundle(), 0,100)).thenReturn(bundleRequests); PublicBundleRequests requests = bundleRequestService.getPublicBundleRequests(bundleRequests.get(0).getIdPsp(), @@ -212,9 +238,9 @@ void shouldAcceptRequest() { ciBundle.setIdBundle(bundleRequest.getIdBundle()); // Preconditions - Mockito.when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) + when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) .thenReturn(Optional.of(bundleRequest)); - Mockito.when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( + when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( bundleRequest.getIdBundle(), bundleRequest.getCiFiscalCode() )) .thenReturn(Optional.of(ciBundle)); @@ -233,9 +259,9 @@ void shouldThrowExceptionAlreadyAcceptedRequest() { ciBundle.setIdBundle(bundleRequest.getIdBundle()); // Preconditions - Mockito.when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) + when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) .thenReturn(Optional.of(bundleRequest)); - Mockito.when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( + when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( bundleRequest.getIdBundle(), bundleRequest.getCiFiscalCode() )) .thenReturn(Optional.of(ciBundle)); @@ -252,9 +278,9 @@ void shouldThrowExceptionAlreadyRejectedRequest() { ciBundle.setIdBundle(bundleRequest.getIdBundle()); // Preconditions - Mockito.when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) + when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) .thenReturn(Optional.of(bundleRequest)); - Mockito.when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( + when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( bundleRequest.getIdBundle(), bundleRequest.getCiFiscalCode() )) .thenReturn(Optional.of(ciBundle)); @@ -269,16 +295,16 @@ void shouldRejectRequest() { ciBundle.setIdBundle(bundleRequest.getIdBundle()); // Preconditions - Mockito.when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) + when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) .thenReturn(Optional.of(bundleRequest)); - Mockito.when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( + when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( bundleRequest.getIdBundle(), bundleRequest.getCiFiscalCode() )) .thenReturn(Optional.of(ciBundle)); bundleRequestService.rejectRequest(bundleRequest.getIdPsp(), bundleRequest.getId()); - verify(bundleRequestRepository, times(1)).delete(Mockito.any()); + verify(bundleRequestRepository, times(1)).delete(any()); } @Test @@ -290,9 +316,9 @@ void shouldThrowExceptionAlreadyRejectAcceptedRequest() { ciBundle.setIdBundle(bundleRequest.getIdBundle()); // Preconditions - Mockito.when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) + when(bundleRequestRepository.findByIdAndIdPsp(bundleRequest.getId(), bundleRequest.getIdPsp())) .thenReturn(Optional.of(bundleRequest)); - Mockito.when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( + when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( bundleRequest.getIdBundle(), bundleRequest.getCiFiscalCode() )) .thenReturn(Optional.of(ciBundle)); @@ -311,9 +337,9 @@ void shouldThrowExceptionRejectAlreadyRejectedRequest() { // Preconditions String idPsp = bundleRequest.getIdPsp(); String requestId = bundleRequest.getId(); - Mockito.when(bundleRequestRepository.findByIdAndIdPsp(requestId, idPsp)) + when(bundleRequestRepository.findByIdAndIdPsp(requestId, idPsp)) .thenReturn(Optional.of(bundleRequest)); - Mockito.when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( + when(ciBundleRepository.findByIdBundleAndCiFiscalCodeAndValidityDateToIsNull( bundleRequest.getIdBundle(), bundleRequest.getCiFiscalCode() )) .thenReturn(Optional.of(ciBundle)); @@ -370,9 +396,15 @@ void createBundleRequest_ok_4() { void createBundleRequest_ko_1() { // request for a global bundle Bundle bundle = TestUtil.getMockBundle(); + CiBundleSubscriptionRequest ciBundleSubscriptionRequest = TestUtil.getMockCiBundleSubscriptionRequest(); + when(bundleRepository.findById(anyString())).thenReturn(Optional.of(bundle)); - createBundleRequest_ko(TestUtil.getMockCiBundleSubscriptionRequest(), HttpStatus.CONFLICT); + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest) + ); + + assertEquals(HttpStatus.CONFLICT, e.getHttpStatus()); } @ParameterizedTest @@ -381,15 +413,26 @@ void createBundleRequest_ko_2(String bundleType) { // request for a private|global bundle Bundle bundle = TestUtil.getMockBundle(); bundle.setType(BundleType.fromValue(bundleType)); + CiBundleSubscriptionRequest ciBundleSubscriptionRequest = TestUtil.getMockCiBundleSubscriptionRequest(); when(bundleRepository.findById(anyString())).thenReturn(Optional.of(bundle)); - createBundleRequest_ko(TestUtil.getMockCiBundleSubscriptionRequest(), HttpStatus.CONFLICT); + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest) + ); + + assertEquals(HttpStatus.CONFLICT, e.getHttpStatus()); } @Test void createBundleRequest_ko_3() { + CiBundleSubscriptionRequest ciBundleSubscriptionRequest = TestUtil.getMockCiBundleSubscriptionRequest(); when(bundleRepository.findById(anyString())).thenReturn(Optional.empty()); - createBundleRequest_ko(TestUtil.getMockCiBundleSubscriptionRequest(), HttpStatus.NOT_FOUND); + + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest) + ); + + assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); } @Test @@ -398,22 +441,14 @@ void createBundleRequest_ko_4() { Bundle bundle = TestUtil.getMockBundle(); bundle.setValidityDateFrom(LocalDate.now().minusDays(7)); bundle.setValidityDateTo(LocalDate.now()); + CiBundleSubscriptionRequest ciBundleSubscriptionRequest = TestUtil.getMockCiBundleSubscriptionRequest(); when(bundleRepository.findById(anyString())).thenReturn(Optional.of(bundle)); - createBundleRequest_ko(TestUtil.getMockCiBundleSubscriptionRequest(), HttpStatus.BAD_REQUEST); - } - + AppException e = assertThrows(AppException.class, + () -> bundleRequestService.createBundleRequest(FISCAL_CODE, ciBundleSubscriptionRequest) + ); - void createBundleRequest_ko(CiBundleSubscriptionRequest ciBundleSubscriptionRequest, HttpStatus status) { - String fiscalCode = TestUtil.getMockCiFiscalCode(); - try { - bundleRequestService.createBundleRequest(fiscalCode, ciBundleSubscriptionRequest); - fail(); - } catch (AppException e) { - assertEquals(status, e.getHttpStatus()); - } catch (Exception e) { - fail(); - } + assertEquals(HttpStatus.BAD_REQUEST, e.getHttpStatus()); } void removeBundleRequest_ko(String fiscalCode, String bundleRequestId, HttpStatus status) { @@ -451,4 +486,28 @@ void getBundleRequest_ko(int limit, int pageNumber, HttpStatus status) { assertEquals(status, appException.getHttpStatus()); } + + private List buildBadCIBundleAttributeModelList() { + CiBundleAttributeModel attribute1 = CiBundleAttributeModel.builder() + .maxPaymentAmount(100L) + .transferCategory("taxonomy1") + .transferCategoryRelation(TransferCategoryRelation.EQUAL) + .build(); + CiBundleAttributeModel attribute2 = CiBundleAttributeModel.builder() + .maxPaymentAmount(100L) + .build(); + List attributeModelList = new ArrayList<>(); + attributeModelList.add(attribute1); + attributeModelList.add(attribute2); + return attributeModelList; + } + + private List buildCIBundleAttributeModelList() { + CiBundleAttributeModel attribute = CiBundleAttributeModel.builder() + .maxPaymentAmount(100L) + .transferCategory("taxonomy1") + .transferCategoryRelation(TransferCategoryRelation.EQUAL) + .build(); + return Collections.singletonList(attribute); + } }