Skip to content

Commit

Permalink
[PPD-238] call iuv generator: remittanceInformation + check property
Browse files Browse the repository at this point in the history
type
  • Loading branch information
aacitelli committed Jul 15, 2022
2 parents 76e805e + fe18809 commit b6cb21f
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class Service {
@PartitionKey
private String transferCategory; // tassonomia

@NotBlank(message = "remittance information is required")
private String remittanceInformation; // causale

@CreatedDate
private LocalDateTime insertedDate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import javax.validation.constraints.NotBlank;

import it.gov.pagopa.spontaneouspayment.model.enumeration.PropertyType;


@Getter
@Setter
Expand All @@ -17,7 +19,7 @@ public class ServiceProperty {
@NotBlank
private String name;

private String type;
private PropertyType type;

private boolean isRequired;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ServiceRef {

@NotBlank(message = "segregation code is required")
private String segregationCode;

@NotBlank(message = "remittance information is required")
private String remittanceInformation; // causale

private String postalIban;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum AppError {
ORGANIZATION_SERVICE_NOT_FOUND(HttpStatus.NOT_FOUND, "Not found a relation between organization and service", "Not found a relation between Organization Fiscal Code %s and Service Id %s"),
SERVICE_NOT_FOUND(HttpStatus.NOT_FOUND, "The service configuration was not found", "Not found a service configuration for Service Id %s"),
ENROLLMENT_TO_SERVICE_NOT_FOUND(HttpStatus.NOT_FOUND, "Not found the enrollment for the specified id", "Not found an enrollment to the Service Id %s for the Organization Fiscal Code %s"),
PROPERTY_MISSING(HttpStatus.BAD_REQUEST, "A property configured for the service is not present in the request", "Not found in the request the configured property %s"),
PROPERTY_MISSING_OR_WRONG(HttpStatus.BAD_REQUEST, "A property configured for the service is not present in the request or its type is wrong", "Not found in the request the configured property %s or its type is wrong [expected type = %s]"),
ENTITY_DUPLICATED(HttpStatus.CONFLICT, "Entity with the specified id already exists in the system", "%s"),
ENROLLMENT_TO_SERVICE_DUPLICATED(HttpStatus.CONFLICT, "An enrollment for the specified id already exists", "Already exists an enrollment between Organization Fiscal Code %s and Service Id %s"),
ENTITY_VALIDATION_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "Error during entity validation", "%s"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package it.gov.pagopa.spontaneouspayment.model.enumeration;

public enum PropertyType {
STRING, NUMBER
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.time.Year;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Predicate;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.math.NumberUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -20,7 +22,9 @@
import it.gov.pagopa.spontaneouspayment.exception.AppException;
import it.gov.pagopa.spontaneouspayment.model.IuvGenerationModel;
import it.gov.pagopa.spontaneouspayment.model.ServiceModel;
import it.gov.pagopa.spontaneouspayment.model.ServicePropertyModel;
import it.gov.pagopa.spontaneouspayment.model.SpontaneousPaymentModel;
import it.gov.pagopa.spontaneouspayment.model.enumeration.PropertyType;
import it.gov.pagopa.spontaneouspayment.model.response.IuvGenerationModelResponse;
import it.gov.pagopa.spontaneouspayment.model.response.PaymentOptionModel;
import it.gov.pagopa.spontaneouspayment.model.response.PaymentPositionModel;
Expand Down Expand Up @@ -79,16 +83,27 @@ public PaymentPositionModel createSpontaneousPayment(@NotBlank String organizati
* @param serviceConfiguration the serviceConfiguration in the DB
* @throws AppException if a service configuration is not present in the DB
*/
private void checkServiceProperties(SpontaneousPaymentModel spontaneousPayment, it.gov.pagopa.spontaneouspayment.entity.Service serviceConfiguration) {
for (ServiceProperty confProp : serviceConfiguration.getProperties()) {
var isPresent = spontaneousPayment.getService().getProperties()
.parallelStream()
.anyMatch(o -> o.getName().equals(confProp.getName()));
if (!isPresent) {
throw new AppException(AppError.PROPERTY_MISSING, confProp.getName());
}
}
}
private void checkServiceProperties(SpontaneousPaymentModel spontaneousPayment, it.gov.pagopa.spontaneouspayment.entity.Service serviceConfiguration) {
for (ServiceProperty confProp : serviceConfiguration.getProperties()) {
if (confProp.isRequired()) {
Predicate<ServicePropertyModel> checkName = o -> o.getName().equals(confProp.getName());
Predicate<ServicePropertyModel> checkType = this.getCheckPropertyType(confProp);
var isPresentAndValid = spontaneousPayment.getService().getProperties().parallelStream()
.anyMatch(checkName.and(checkType));
if (!isPresentAndValid) {
throw new AppException(AppError.PROPERTY_MISSING_OR_WRONG, confProp.getName(), confProp.getType());
}
}
}
}

private Predicate<ServicePropertyModel> getCheckPropertyType(ServiceProperty confProp) {
if (confProp.getType().equals(PropertyType.NUMBER)) {
return o -> NumberUtils.isCreatable (o.getValue());
}
// Default is always true condition (no check type)
return o -> true;
}

private PaymentPositionModel createDebtPosition(String organizationFiscalCode,
Organization orgConfiguration, it.gov.pagopa.spontaneouspayment.entity.Service serviceConfiguration, SpontaneousPaymentModel spontaneousPayment) {
Expand Down Expand Up @@ -146,9 +161,4 @@ private Organization getCreditorInstitution(String organizationFiscalCode) {
return orgRepository.findByFiscalCode(organizationFiscalCode)
.orElseThrow(() -> new AppException(AppError.ORGANIZATION_NOT_FOUND, organizationFiscalCode));
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static PaymentPositionModel getPaymentPositionModel() {
public static SpontaneousPaymentModel getSpontaneousPaymentModel() {

List<ServicePropertyModel> props = new ArrayList<>();
props.add(ServicePropertyModel.builder().name("propName1").value("1000").build());
props.add(ServicePropertyModel.builder().name("propName1").value("string value").build());

return SpontaneousPaymentModel.builder()
.debtor(DebtorModel.builder()
Expand Down Expand Up @@ -194,16 +194,35 @@ public static SpontaneousPaymentModel getSpontaneousPaymentModel_BadProperty() {
.build();
}

public static SpontaneousPaymentModel getSpontaneousPaymentModel_BadPropertyType() {

List<ServicePropertyModel> props = new ArrayList<>();
// the type of the property propName2 must be a number but it is a string
props.add(ServicePropertyModel.builder().name("propName2").value("string value").build());

return SpontaneousPaymentModel.builder()
.debtor(DebtorModel.builder()
.type(Type.F)
.fiscalCode("mockFiscalCode")
.fullName("mockFullName")
.email("mockEmail@mock.it").build())
.service(ServiceModel.builder()
.id("id-servizio-1")
.properties(props)
.build())
.build();
}

public static List<Service> getMockServices() {
List<Service> services = new ArrayList<>();
services.add(Service.builder().id("mockId1").name("mockName1").transferCategory("mockTransferCategory1").remittanceInformation("mockRemittanceInformation1").build());
services.add(Service.builder().id("mockId2").name("mockName2").transferCategory("mockTransferCategory2").remittanceInformation("mockRemittanceInformation2").build());
services.add(Service.builder().id("mockId1").name("mockName1").transferCategory("mockTransferCategory1").build());
services.add(Service.builder().id("mockId2").name("mockName2").transferCategory("mockTransferCategory2").build());

return services;
}

public static Service getMockService() {
return Service.builder().id("mockId").name("mockName").transferCategory("mockTransferCategory").remittanceInformation("mockRemittanceInformation").build();
return Service.builder().id("mockId").name("mockName").transferCategory("mockTransferCategory").build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.gov.pagopa.spontaneouspayment.exception.AppException;
import it.gov.pagopa.spontaneouspayment.model.EnrollmentModel;
import it.gov.pagopa.spontaneouspayment.model.OrganizationModel;
import it.gov.pagopa.spontaneouspayment.model.enumeration.PropertyType;
import it.gov.pagopa.spontaneouspayment.model.enumeration.Status;
import it.gov.pagopa.spontaneouspayment.repository.OrganizationRepository;
import it.gov.pagopa.spontaneouspayment.repository.ServiceRepository;
Expand Down Expand Up @@ -100,57 +101,55 @@ public void setUp() throws IOException, KeyStoreException, NoSuchAlgorithmExcept
Service s1 = new Service();
s1.setId("id-servizio-1");
s1.setTransferCategory("tassonomia-1");
s1.setRemittanceInformation("causale-1");
s1.setBasePath("base-path-1");
s1.setEndpoint("endpont-1");

ServiceProperty sp1 = new ServiceProperty("propName1", "number", true);
ServiceProperty sp1 = new ServiceProperty("propName1", PropertyType.STRING, true);
List<ServiceProperty> properties1 = new ArrayList<>();
properties1.add(sp1);
s1.setProperties(properties1);

Service s2 = new Service();
s2.setId("id-servizio-2");
s2.setTransferCategory("tassonomia-2");
s2.setRemittanceInformation("causale-2");
s2.setBasePath("base-path-2");
s2.setEndpoint("endpont-2");

ServiceProperty sp2 = new ServiceProperty("propName2", "string", true);
ServiceProperty sp2 = new ServiceProperty("propName2", PropertyType.STRING, true);
List<ServiceProperty> properties2 = new ArrayList<>();
properties2.add(sp2);
s2.setProperties(properties2);

Service s3 = new Service();
s3.setId("id-servizio-3");
s3.setTransferCategory("tassonomia-3");
s3.setRemittanceInformation("causale-3");
s3.setBasePath("base-path-3");
s3.setEndpoint("endpont-3");

ServiceProperty sp3 = new ServiceProperty("propName3", "url", true);
ServiceProperty sp3 = new ServiceProperty("propName3", PropertyType.STRING, true);
List<ServiceProperty> properties3 = new ArrayList<>();
properties3.add(sp3);
s3.setProperties(properties3);

Service s4 = new Service();
s4.setId("id-servizio-4");
s4.setTransferCategory("tassonomia-4");
s4.setRemittanceInformation("causale-4");
s4.setBasePath("base-path-4");
s4.setEndpoint("endpont-4");

ServiceProperty sp4 = new ServiceProperty("propName4", "rule", true);
ServiceProperty sp4 = new ServiceProperty("propName4", PropertyType.STRING, true);
List<ServiceProperty> properties4 = new ArrayList<>();
properties4.add(sp4);
s4.setProperties(properties4);

ServiceRef ref1 = new ServiceRef();
ref1.setServiceId("id-servizio-1");
ref1.setIban("iban-1");
ref1.setRemittanceInformation("causale-1");
ServiceRef ref2 = new ServiceRef();
ref2.setServiceId("id-servizio-2");
ref2.setIban("iban-2");
ref2.setRemittanceInformation("causale-2");
List<ServiceRef> servicesRef = new ArrayList<>();
servicesRef.add(ref1);
servicesRef.add(ref2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import it.gov.pagopa.spontaneouspayment.exception.AppException;
import it.gov.pagopa.spontaneouspayment.model.IuvGenerationModel;
import it.gov.pagopa.spontaneouspayment.model.SpontaneousPaymentModel;
import it.gov.pagopa.spontaneouspayment.model.enumeration.PropertyType;
import it.gov.pagopa.spontaneouspayment.model.enumeration.Status;
import it.gov.pagopa.spontaneouspayment.model.response.IuvGenerationModelResponse;
import it.gov.pagopa.spontaneouspayment.model.response.PaymentPositionModel;
Expand Down Expand Up @@ -121,47 +122,43 @@ public void setUp() throws IOException, KeyStoreException, NoSuchAlgorithmExcept
Service s1 = new Service();
s1.setId("id-servizio-1");
s1.setTransferCategory("tassonomia-1");
s1.setRemittanceInformation("causale-1");
s1.setBasePath("base-path-1");
s1.setEndpoint("endpont-1");

ServiceProperty sp1 = new ServiceProperty("propName1", "number", true);
ServiceProperty sp1 = new ServiceProperty("propName1", PropertyType.STRING, true);
List<ServiceProperty> properties1 = new ArrayList<>();
properties1.add(sp1);
s1.setProperties(properties1);

Service s2 = new Service();
s2.setId("id-servizio-2");
s2.setTransferCategory("tassonomia-2");
s2.setRemittanceInformation("causale-2");
s2.setBasePath("base-path-2");
s2.setEndpoint("endpont-2");

ServiceProperty sp2 = new ServiceProperty("propName2", "string", true);
ServiceProperty sp2 = new ServiceProperty("propName2", PropertyType.NUMBER, true);
List<ServiceProperty> properties2 = new ArrayList<>();
properties2.add(sp2);
s2.setProperties(properties2);

Service s3 = new Service();
s3.setId("id-servizio-3");
s3.setTransferCategory("tassonomia-3");
s3.setRemittanceInformation("causale-3");
s3.setBasePath("base-path-3");
s3.setEndpoint("endpont-3");

ServiceProperty sp3 = new ServiceProperty("propName3", "url", true);
ServiceProperty sp3 = new ServiceProperty("propName3", PropertyType.STRING, true);
List<ServiceProperty> properties3 = new ArrayList<>();
properties3.add(sp3);
s3.setProperties(properties3);

Service s4 = new Service();
s4.setId("id-servizio-4");
s4.setTransferCategory("tassonomia-4");
s4.setRemittanceInformation("causale-4");
s4.setBasePath("base-path-4");
s4.setEndpoint("endpont-4");

ServiceProperty sp4 = new ServiceProperty("propName4", "rule", true);
ServiceProperty sp4 = new ServiceProperty("propName4", PropertyType.STRING, true);
List<ServiceProperty> properties4 = new ArrayList<>();
properties4.add(sp4);
s4.setProperties(properties4);
Expand All @@ -170,10 +167,12 @@ public void setUp() throws IOException, KeyStoreException, NoSuchAlgorithmExcept
ref1.setServiceId("id-servizio-1");
ref1.setIban("iban-1");
ref1.setSegregationCode("47");
ref1.setRemittanceInformation("causale-1");
ServiceRef ref2 = new ServiceRef();
ref2.setServiceId("id-servizio-2");
ref2.setIban("iban-2");
ref2.setSegregationCode("5");
ref2.setRemittanceInformation("causale-2");
List<ServiceRef> servicesRef = new ArrayList<>();
servicesRef.add(ref1);
servicesRef.add(ref2);
Expand Down Expand Up @@ -275,6 +274,18 @@ void createSpontaneousPayment_400() {
} catch (Exception e) {
fail();
}

spontaneousPaymentModel_badProperty = TestUtil.getSpontaneousPaymentModel_BadPropertyType();
try {
// the request contains a string value but a number was expected -> must raise a 400
// exception
paymentsService.createSpontaneousPayment("organizationTest", spontaneousPaymentModel_badProperty);
fail();
} catch (AppException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getHttpStatus());
} catch (Exception e) {
fail();
}
}

}
Loading

0 comments on commit b6cb21f

Please sign in to comment.