Skip to content

Commit

Permalink
Added emd citizen first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitolo-Andrea committed Oct 16, 2024
1 parent 17a5cc5 commit c5cb94d
Show file tree
Hide file tree
Showing 29 changed files with 552 additions and 364 deletions.
60 changes: 13 additions & 47 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<version>3.3.3</version>
</parent>

<groupId>it.gov.pagopa.onboarding.citizen</groupId>
<artifactId>emd-onboarding-citizen</artifactId>
<name>emd-onboarding-citizen</name>
<description>Onboarding Citizen Microservice</description>
<groupId>it.gov.pagopa.citizen</groupId>
<artifactId>emd-citizen</artifactId>
<name>emd-citizen</name>
<description>Citizen Microservice</description>
<version>0.0.1-SNAPSHOT</version>

<properties>
Expand All @@ -24,18 +24,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
Expand All @@ -48,27 +40,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId>
</dependency>

<!-- Da verificare-->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-data-cosmos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
Expand All @@ -83,17 +60,6 @@
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!-- Da verificare-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>13.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.11</version>
</dependency>

<!--TEST-->
<dependency>
Expand All @@ -108,14 +74,8 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-wiremock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring30x</artifactId>
<version>4.11.0</version>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -136,6 +96,12 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package it.gov.pagopa.common.configuration;

import org.bson.Document;
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

public class CustomReactiveMongoHealthIndicator extends AbstractReactiveHealthIndicator {

private final ReactiveMongoTemplate reactiveMongoTemplate;

public CustomReactiveMongoHealthIndicator(ReactiveMongoTemplate reactiveMongoTemplate) {
super("Mongo health check failed");
Assert.notNull(reactiveMongoTemplate, "ReactiveMongoTemplate must not be null");
this.reactiveMongoTemplate = reactiveMongoTemplate;
}

@Override
protected Mono<Health> doHealthCheck(Health.Builder builder) {
Mono<Document> buildInfo = this.reactiveMongoTemplate.executeCommand("{ isMaster: 1 }");
return buildInfo.map(document -> builderUp(builder, document));
}
private Health builderUp(Health.Builder builder, Document document) {
return builder.up().withDetail("maxWireVersion", document.getInteger("maxWireVersion")).build();
}
}
104 changes: 104 additions & 0 deletions src/main/java/it/gov/pagopa/common/configuration/MongoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package it.gov.pagopa.common.configuration;

import com.mongodb.lang.NonNull;
import it.gov.pagopa.common.utils.CommonConstants;
import lombok.Setter;
import org.bson.types.Decimal128;
import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Configuration
public class MongoConfig {

@Configuration
@ConfigurationProperties(prefix = "spring.data.mongodb.config")
public static class MongoDbCustomProperties {
@Setter
ConnectionPoolSettings connectionPool;

@Setter
static class ConnectionPoolSettings {
int maxSize;
int minSize;
long maxWaitTimeMS;
long maxConnectionLifeTimeMS;
long maxConnectionIdleTimeMS;
int maxConnecting;
}

}

@Bean
public MongoClientSettingsBuilderCustomizer customizer(MongoDbCustomProperties mongoDbCustomProperties) {
return builder -> builder.applyToConnectionPoolSettings(
connectionPool -> {
connectionPool.maxSize(mongoDbCustomProperties.connectionPool.maxSize);
connectionPool.minSize(mongoDbCustomProperties.connectionPool.minSize);
connectionPool.maxWaitTime(mongoDbCustomProperties.connectionPool.maxWaitTimeMS, TimeUnit.MILLISECONDS);
connectionPool.maxConnectionLifeTime(mongoDbCustomProperties.connectionPool.maxConnectionLifeTimeMS, TimeUnit.MILLISECONDS);
connectionPool.maxConnectionIdleTime(mongoDbCustomProperties.connectionPool.maxConnectionIdleTimeMS, TimeUnit.MILLISECONDS);
connectionPool.maxConnecting(mongoDbCustomProperties.connectionPool.maxConnecting);
});
}

@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(Arrays.asList(
// BigDecimal support
new BigDecimalDecimal128Converter(),
new Decimal128BigDecimalConverter(),

// OffsetDateTime support
new OffsetDateTimeWriteConverter(),
new OffsetDateTimeReadConverter()
));
}

@WritingConverter
private static class BigDecimalDecimal128Converter implements Converter<BigDecimal, Decimal128> {

@Override
public Decimal128 convert(@NonNull BigDecimal source) {
return new Decimal128(source);
}
}

@ReadingConverter
private static class Decimal128BigDecimalConverter implements Converter<Decimal128, BigDecimal> {

@Override
public BigDecimal convert(@NonNull Decimal128 source) {
return source.bigDecimalValue();
}

}

@WritingConverter
public static class OffsetDateTimeWriteConverter implements Converter<OffsetDateTime, Date> {
@Override
public Date convert(OffsetDateTime offsetDateTime) {
return Date.from(offsetDateTime.toInstant());
}
}

@ReadingConverter
public static class OffsetDateTimeReadConverter implements Converter<Date, OffsetDateTime> {
@Override
public OffsetDateTime convert(Date date) {
return date.toInstant().atZone(CommonConstants.ZONEID).toOffsetDateTime();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package it.gov.pagopa.common.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;

@Configuration
public class MongoHealthConfig {
@Bean
public CustomReactiveMongoHealthIndicator customMongoHealthIndicator(ReactiveMongoTemplate reactiveMongoTemplate) {
return new CustomReactiveMongoHealthIndicator(reactiveMongoTemplate);
}
}

16 changes: 16 additions & 0 deletions src/main/java/it/gov/pagopa/common/utils/CommonConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.gov.pagopa.common.utils;

import java.time.ZoneId;

public class CommonConstants {


public static final class ExceptionCode {
public static final String GENERIC_ERROR = "GENERIC_ERROR";
private ExceptionCode() {}
}

public static final ZoneId ZONEID = ZoneId.of("Europe/Rome");

private CommonConstants(){}
}
7 changes: 2 additions & 5 deletions src/main/java/it/gov/pagopa/common/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package it.gov.pagopa.common.utils;
import it.gov.pagopa.onboarding.citizen.exception.custom.EmdEncryptionException;
import it.gov.pagopa.common.web.exception.EmdEncryptionException;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -30,10 +30,7 @@ public static String createSHA256(String fiscalCode) {
}
}

public static void logInfo(String message){
log.info(inputSanify(message));
}
private static String inputSanify(String message){
public static String inputSanify(String message){
if (message != null)
return message.replaceAll("[\\r\\n]", "");
return "[EMD][WARNING] Null log";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class ClientExceptionNoBody extends ClientException{
public class ClientExceptionNoBody extends ClientException {

public ClientExceptionNoBody(HttpStatus httpStatus, String message) {
super(httpStatus, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.http.HttpStatus;

@Getter
public class ClientExceptionWithBody extends ClientException{
public class ClientExceptionWithBody extends ClientException {
private final String code;

public ClientExceptionWithBody(HttpStatus httpStatus, String code, String message){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package it.gov.pagopa.onboarding.citizen.exception.custom;
package it.gov.pagopa.common.web.exception;

import it.gov.pagopa.common.web.exception.ServiceException;
import it.gov.pagopa.onboarding.citizen.constants.OnboardingCitizenConstants.ExceptionCode;

import it.gov.pagopa.common.utils.CommonConstants.ExceptionCode;

public class EmdEncryptionException extends ServiceException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package it.gov.pagopa.common.web.exception;

import it.gov.pagopa.common.web.dto.ErrorDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -23,7 +23,7 @@ public ErrorManager(@Nullable ErrorDTO defaultErrorDTO) {
}

@ExceptionHandler(RuntimeException.class)
protected ResponseEntity<ErrorDTO> handleException(RuntimeException error, HttpServletRequest request) {
protected ResponseEntity<ErrorDTO> handleException(RuntimeException error, ServerHttpRequest request) {

logClientException(error, request);

Expand All @@ -46,7 +46,7 @@ protected ResponseEntity<ErrorDTO> handleException(RuntimeException error, HttpS
.body(errorDTO);
}
}
public static void logClientException(RuntimeException error, HttpServletRequest request) {
public static void logClientException(RuntimeException error, ServerHttpRequest request) {
Throwable unwrappedException = error.getCause() instanceof ServiceException
? error.getCause()
: error;
Expand All @@ -71,7 +71,7 @@ public static void logClientException(RuntimeException error, HttpServletRequest
}
}

public static String getRequestDetails(HttpServletRequest request) {
return "%s %s".formatted(request.getMethod(), request.getRequestURI());
public static String getRequestDetails(ServerHttpRequest request) {
return "%s %s".formatted(request.getMethod(), request.getURI());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package it.gov.pagopa.common.web.exception;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand All @@ -27,7 +27,7 @@ public ServiceExceptionHandler(ErrorManager errorManager, Map<Class<? extends Se

@SuppressWarnings("squid:S1452")
@ExceptionHandler(ServiceException.class)
protected ResponseEntity<? extends ServiceExceptionPayload> handleException(ServiceException error, HttpServletRequest request) {
protected ResponseEntity<? extends ServiceExceptionPayload> handleException(ServiceException error, ServerHttpRequest request) {
if (null != error.getPayload()) {
return handleBodyProvidedException(error, request);
}
Expand All @@ -45,7 +45,7 @@ private ClientException transcodeException(ServiceException error) {
return new ClientExceptionWithBody(httpStatus, error.getCode(), error.getMessage(), error.isPrintStackTrace(), error);
}

private ResponseEntity<? extends ServiceExceptionPayload> handleBodyProvidedException(ServiceException error, HttpServletRequest request) {
private ResponseEntity<? extends ServiceExceptionPayload> handleBodyProvidedException(ServiceException error, ServerHttpRequest request) {
ClientException clientException = transcodeException(error);
ErrorManager.logClientException(clientException, request);

Expand Down
Loading

0 comments on commit c5cb94d

Please sign in to comment.