Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/P4ADEV-1346-create-activity-veri…
Browse files Browse the repository at this point in the history
…fy-authorization' into P4ADEV-1347-validation-activity

# Conflicts:
#	build.gradle.kts
#	src/main/java/it/gov/pagopa/payhub/activities/dto/NationDTO.java
#	src/main/java/it/gov/pagopa/payhub/activities/exception/ActivitiesException.java
  • Loading branch information
Benedetta-fabbri committed Nov 14, 2024
2 parents e3d317b + ac76a0f commit c0ea240
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 28 deletions.
16 changes: 15 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
java
id("org.springframework.boot") version "3.3.5"
Expand Down Expand Up @@ -40,6 +42,7 @@ val hibernateValidatorVersion = "8.0.1.Final"
val commonsCompressVersion = "1.27.1"
val commonsLang3Version = "3.17.0"
val commonsTextVersion = "1.12.0"
val jacksonModuleVersion = "2.18.1"


dependencies {
Expand All @@ -48,14 +51,16 @@ dependencies {
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
implementation("org.hibernate.validator:hibernate-validator:$hibernateValidatorVersion")

// apache commons
//apache commons
implementation("org.apache.commons:commons-compress:$commonsCompressVersion")
implementation("org.apache.commons:commons-lang3:$commonsLang3Version")
implementation("org.apache.commons:commons-text:$commonsTextVersion")

// Security fixes
implementation("org.yaml:snakeyaml:$snakeYamlVersion")

implementation("com.fasterxml.jackson.module:jackson-module-parameter-names:$jacksonModuleVersion")

// Testing
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.junit.jupiter:junit-jupiter-api")
Expand Down Expand Up @@ -116,3 +121,12 @@ publishing {
}
}
}

tasks.withType<Jar> {
enabled = true
}

tasks.withType<BootJar> {
enabled = true
}

2 changes: 1 addition & 1 deletion force-release
Original file line number Diff line number Diff line change
@@ -1 +1 @@
001
003

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package it.gov.pagopa.payhub.activities.activity;

import it.gov.pagopa.payhub.activities.dao.OrganizationInstallmentTypeDao;
import it.gov.pagopa.payhub.activities.dto.InstallmentsOperatorDTO;
import it.gov.pagopa.payhub.activities.dto.OrganizationInstallmentTypeDTO;
import it.gov.pagopa.payhub.activities.exception.custom.ValidatorException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VerifyAuthorizationInstallments {

private final OrganizationInstallmentTypeDao organizationInstallmentTypeDao;

public VerifyAuthorizationInstallments(OrganizationInstallmentTypeDao organizationInstallmentTypeDao) {
this.organizationInstallmentTypeDao = organizationInstallmentTypeDao;
}

public OrganizationInstallmentTypeDTO verifyAuth(InstallmentsOperatorDTO installmentsOperatorDTO, String username, Long mygovEnteId){

List<OrganizationInstallmentTypeDTO> organizationInstallmentTypeList =
organizationInstallmentTypeDao.getByMygovEnteIdAndOperatoreUsername(mygovEnteId, username);

return organizationInstallmentTypeList.stream()
.filter(etd -> etd.getTypeCode().equals(installmentsOperatorDTO.getOrganizationTypeInstallment().getTypeCode()))
.findFirst()
.orElseThrow(() -> new ValidatorException("OrganizationInstallmentType is not active for this operator"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package it.gov.pagopa.payhub.activities.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

@Configuration
public class JsonConfig {
public static final String DATE_FORMAT = "yyyy/MM/dd";
public static final String DATE_TIME_FORMAT = "yyyy/MM/dd-HH:mm:ss";

@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.DEFAULT));
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY);
mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY);
mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setTimeZone(TimeZone.getDefault());

SimpleModule module = new SimpleModule();
module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
module.addSerializer(Date.class, new DateSerializer(null, new SimpleDateFormat(DATE_TIME_FORMAT)));

module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));

mapper.registerModule(module);
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package it.gov.pagopa.payhub.activities.constants;

public class Constants {
private Constants() {
}

public static final String TIPO_DOVUTO_MARCA_BOLLO_DIGITALE = "MARCA_BOLLO_DIGITALE";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.payhub.activities.dao;

import it.gov.pagopa.payhub.activities.dto.OrganizationInstallmentTypeDTO;

import java.util.List;

public interface OrganizationInstallmentTypeDao {

List<OrganizationInstallmentTypeDTO> getByMygovEnteIdAndOperatoreUsername(Long mygovEnteId, String operatoreUsername);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.time.LocalDateTime;

@Data
@Builder
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class InstallmentOperatorDTO implements Serializable {
Expand Down Expand Up @@ -45,7 +45,7 @@ public class InstallmentOperatorDTO implements Serializable {
private boolean flagGenerateIuv;
private String iuf;

//datails elaborated installment
//details elaborated installment
private LocalDateTime transactionStartDate;
private String transactionId;
private String holder;
Expand All @@ -61,7 +61,7 @@ public class InstallmentOperatorDTO implements Serializable {
private InstallmentPrimaryOrganizationDTO installmentPrimaryOrgDetail;
private ElaboratedInstallmentPrimaryOrganizationDTO elaboratedInstallmentPrimaryOrgDetail;

//multibeneficiary detail
//multi beneficiary detail
private MultiBeneficiaryInstallmentDTO multiBeneficiaryInstallmentDetail;
private ElaboratedMultiBeneficiaryInstallmentDTO elaboratedMultiBeneficiaryInstallmentDetail;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class InstallmentPrimaryOrganizationDTO {
public class InstallmentPrimaryOrganizationDTO implements Serializable {

private String beneficiaryName;
private String uniqueIdentificationCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
@NoArgsConstructor
@AllArgsConstructor
public class NationDTO implements Serializable {

private Long nationId;
private String nationName;
private String codeIsoAlpha2;

public boolean hasProvince(){
public boolean hasProvince(){
return StringUtils.equalsIgnoreCase(codeIsoAlpha2, "it");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@AllArgsConstructor
public class OrganizationInstallmentTypeDTO implements Serializable {


private Long installmentTypeId;
private OrganizationDTO orgId;
private String typeCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,4 @@ public class ActivitiesException extends RuntimeException {
public ActivitiesException(String message) {
super(message);
}

public ActivitiesException(String message, Throwable cause) {
super(message, cause);
}

public ActivitiesException(Throwable cause) {
super(cause);
}

public ActivitiesException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.gov.pagopa.payhub.activities.exception.custom;

import it.gov.pagopa.payhub.activities.exception.ActivitiesException;


public class ValidatorException extends ActivitiesException {

public ValidatorException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package it.gov.pagopa.payhub.activities.activity;

import it.gov.pagopa.payhub.activities.dao.OrganizationInstallmentTypeDao;
import it.gov.pagopa.payhub.activities.dto.InstallmentsOperatorDTO;
import it.gov.pagopa.payhub.activities.dto.OrganizationInstallmentTypeDTO;
import it.gov.pagopa.payhub.activities.dto.OrganizationTypeInstallmentDTO;
import it.gov.pagopa.payhub.activities.exception.custom.ValidatorException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class VerifyAuthorizationInstallmentsTest {

@Mock
private OrganizationInstallmentTypeDao organizationInstallmentTypeDao;

private VerifyAuthorizationInstallments verifyAuthorizationInstallments;

@BeforeEach
void init() {
verifyAuthorizationInstallments = new VerifyAuthorizationInstallments(organizationInstallmentTypeDao);
}

@Test
void givenVerifyAuthThenSuccess() {
String username = "username";
Long mygovEnteId = 1L;
InstallmentsOperatorDTO installmentsOperatorDTO = new InstallmentsOperatorDTO();
OrganizationTypeInstallmentDTO organizationTypeInstallmentDTO = new OrganizationTypeInstallmentDTO();

installmentsOperatorDTO.setOrganizationTypeInstallment(organizationTypeInstallmentDTO);
organizationTypeInstallmentDTO.setTypeCode("TYPE_CODE");

OrganizationInstallmentTypeDTO organizationInstallmentTypeDTO = new OrganizationInstallmentTypeDTO();
organizationInstallmentTypeDTO.setTypeCode("TYPE_CODE");

when(organizationInstallmentTypeDao.getByMygovEnteIdAndOperatoreUsername(mygovEnteId, username))
.thenReturn(List.of(organizationInstallmentTypeDTO));

OrganizationInstallmentTypeDTO result = verifyAuthorizationInstallments.verifyAuth(installmentsOperatorDTO, username, mygovEnteId);

assertEquals(organizationInstallmentTypeDTO, result);
}

@Test
void givenVerifyAuthWhenNotEqualTypeCodeThenThrowValidatorException() {
String username = "username";
Long mygovEnteId = 1L;
InstallmentsOperatorDTO installmentsOperatorDTO = new InstallmentsOperatorDTO();

OrganizationInstallmentTypeDTO organizationInstallmentTypeDTO = new OrganizationInstallmentTypeDTO();
organizationInstallmentTypeDTO.setTypeCode("TYPE_CODE");

when(organizationInstallmentTypeDao.getByMygovEnteIdAndOperatoreUsername(mygovEnteId, username))
.thenReturn(List.of());

assertThrows(ValidatorException.class, () ->
verifyAuthorizationInstallments.verifyAuth(installmentsOperatorDTO, username, mygovEnteId));
}
}

0 comments on commit c0ea240

Please sign in to comment.