Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/poc-controlli' into feat…
Browse files Browse the repository at this point in the history
…ure/poc
  • Loading branch information
CriMDev97 committed Nov 22, 2023
2 parents a59ac2f + dafc7ff commit 2739449
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/openapi/push-signal_v1_sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ components:
eserviceId:
type: string
description: eservice Id
indexSignal:
signalId:
type: integer
format: int64
example: 1
Expand All @@ -151,7 +151,7 @@ components:
- signalType
- eserviceId
- objectType
- indexSignal
- signalId
SignalType:
type: string
description: type of signal
Expand Down
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@
<version>5.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.1.2</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import it.pagopa.interop.signalhub.push.service.exception.JWTException;
import it.pagopa.interop.signalhub.push.service.exception.PDNDGenericException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
Expand All @@ -25,6 +26,7 @@

import static it.pagopa.interop.signalhub.push.service.exception.ExceptionTypeEnum.*;

@Slf4j
@AllArgsConstructor
public class JWTConverter implements Function<ServerWebExchange, Mono<DecodedJWT>> {
private static final String TYPE = "at+jwt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,46 @@

import com.fasterxml.jackson.databind.JsonMappingException;
import it.pagopa.interop.signalhub.push.service.dto.Problem;
import it.pagopa.interop.signalhub.push.service.dto.ProblemError;
import it.pagopa.interop.signalhub.push.service.mapper.ProblemErrorMapper;
import it.pagopa.interop.signalhub.push.service.mapper.SignalMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.support.WebExchangeBindException;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice

public class RestExceptionHandler {

@Autowired
private ProblemErrorMapper problemErrorMapper;

@ExceptionHandler(JsonMappingException.class)
public void handle(JsonMappingException e) {
log.error("Returning HTTP 400 Bad Request {}", e.getMessage());
public Mono<ResponseEntity<Problem>> handle(JsonMappingException exception) {
log.error("Returning HTTP 400 Bad Request {}", exception.getMessage());
final Problem problem = new Problem();
problem.setTitle(HttpStatus.BAD_REQUEST.name());
problem.setDetail(exception.getCause().getMessage());
problem.setStatus(HttpStatus.BAD_REQUEST.value());

return Mono.just(ResponseEntity.status(HttpStatus.valueOf(400)).body(problem));

}

@ExceptionHandler(PDNDGenericException.class)
Expand All @@ -28,7 +51,26 @@ public Mono<ResponseEntity<Problem>> handleResponseEntityException(final PDNDGen
problem.setTitle(exception.getExceptionType().getTitle());
problem.setDetail(exception.getMessage());
problem.setStatus(exception.getHttpStatus().value());

return Mono.just(ResponseEntity.status(exception.getHttpStatus()).body(problem));
}

@ExceptionHandler(WebExchangeBindException.class)
public Mono<ResponseEntity<Problem>> handleResponseEntityException(final WebExchangeBindException exception){
log.warn(exception.toString());

List<ProblemError> errors= new ArrayList<>();
for(FieldError error : exception.getFieldErrors()) {
errors.add(problemErrorMapper.toEvent(error));
}

final Problem problem = new Problem();
problem.setTitle(HttpStatus.valueOf(exception.getStatusCode().value()).name());
problem.setDetail(HttpStatus.valueOf(exception.getStatusCode().value()).getReasonPhrase());
problem.setStatus(exception.getStatusCode().value());
problem.setErrors(errors);

return Mono.just(ResponseEntity.status(exception.getStatusCode()).body(problem));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.pagopa.interop.signalhub.push.service.mapper;

import it.pagopa.interop.signalhub.push.service.dto.ProblemError;
import it.pagopa.interop.signalhub.push.service.dto.Signal;
import it.pagopa.interop.signalhub.push.service.dto.SignalRequest;
import it.pagopa.interop.signalhub.push.service.queue.model.SignalEvent;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.validation.FieldError;

@Mapper(componentModel = "spring")
public interface ProblemErrorMapper {

@Mapping(target = "code", source= "signal.field")
@Mapping(target = "detail", source= "signal.defaultMessage")
ProblemError toEvent(FieldError signal);



}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface SignalMapper {

SignalEvent toEvent(SignalRequest signal);

@Mapping(target = "signalId", source= "signalRequest.indexSignal")
Signal toSignal(SignalRequest signalRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class SignalEvent {

private String eserviceId;

private Long indexSignal;
private Long signalId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public Mono<SignalEvent> push(SignalEvent event) {
String correlationId = UUID.randomUUID().toString();
Message<String> message = MessageBuilder.withPayload(convertToJson(event))
.setHeader("correlationId", correlationId).build();
log.info("[{} - {}] Pushing signal on internal queue", event.getIndexSignal(), correlationId);
log.info("[{} - {}] Pushing signal on internal queue", event.getSignalId(), correlationId);
return Mono.fromFuture(this.sqsTemplate.sendAsync(message))
.doOnNext(response -> log.info("[{} - {}] Signal pushed on internal queue", event.getIndexSignal(), correlationId))
.doOnNext(response -> log.info("[{} - {}] Signal pushed on internal queue", event.getSignalId(), correlationId))
.thenReturn(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class SignalServiceImpl implements SignalService {
public Mono<Signal> pushSignal(String producerId, SignalRequest signalRequest) {
return organizationEService.getEService(signalRequest.getEserviceId(), producerId)
.switchIfEmpty(Mono.error(new PDNDGenericException(ExceptionTypeEnum.CORRESPONDENCE_NOT_FOUND, ExceptionTypeEnum.CORRESPONDENCE_NOT_FOUND.getMessage().concat(signalRequest.getEserviceId()), HttpStatus.FORBIDDEN)))
.flatMap(eservice -> signalRepository.findBySignalIdAndEServiceId(signalRequest.getIndexSignal(), signalRequest.getEserviceId()))
.flatMap(eservice -> signalRepository.findBySignalIdAndEServiceId(signalRequest.getSignalId(), signalRequest.getEserviceId()))
.flatMap(eservice -> {
log.debug("eservice = {}, SIGNALID_ALREADY_EXISTS", eservice);
return Mono.error(new PDNDGenericException(ExceptionTypeEnum.SIGNALID_ALREADY_EXISTS, ExceptionTypeEnum.SIGNALID_ALREADY_EXISTS.getMessage()
.concat(" ")
.concat(signalRequest.getEserviceId()), HttpStatus.FORBIDDEN));
.concat(signalRequest.getEserviceId()), HttpStatus.BAD_REQUEST));
}).switchIfEmpty(Mono.defer(() ->{
log.debug("SignalRequest = {}, push signal", signalRequest);
SignalEvent event = signalMapper.toEvent(signalRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class SignalMapperTest {
@Test
void toEvent() {
SignalRequest signalRequest= new SignalRequest();
signalRequest.setIndexSignal(1L);
signalRequest.setSignalId(1L);
SignalEvent signal= service.toEvent(signalRequest);
assertEquals(signalRequest.getIndexSignal(), signal.getIndexSignal() );
assertEquals(signalRequest.getSignalId(), signal.getSignalId() );
}

@Test
Expand All @@ -34,9 +34,9 @@ void whenCallToEventAndSignalRequestIsNull() {
@Test
void toSignal() {
SignalRequest signalRequest= new SignalRequest();
signalRequest.setIndexSignal(1L);
signalRequest.setSignalId(1L);
Signal signal= service.toSignal(signalRequest);
assertEquals(signal.getSignalId(), signalRequest.getIndexSignal());
assertEquals(signal.getSignalId(), signalRequest.getSignalId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private SignalRequest getSignalRequest(){
SignalRequest request = new SignalRequest();
request.setSignalType(SignalType.CREATE);
request.setEserviceId("123");
request.setIndexSignal(Long.valueOf(2));
request.setSignalId(Long.valueOf(2));
request.setObjectId("test");
request.setObjectType("test");
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private SignalRequest getSignalRequest(){
SignalRequest request = new SignalRequest();
request.setSignalType(SignalType.CREATE);
request.setEserviceId("123");
request.setIndexSignal(1L);
request.setSignalId(1L);
request.setObjectId("test");
request.setObjectType("test");
return request;
Expand Down

0 comments on commit 2739449

Please sign in to comment.