Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from pagopa/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
galales authored Sep 4, 2023
2 parents 1575b61 + a20a6cd commit 72aefa5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import feign.Response;
import it.pagopa.interop.probing.probingapi.dtos.ChangeEserviceStateRequest;
import it.pagopa.interop.probing.probingapi.dtos.ChangeProbingFrequencyRequest;
import it.pagopa.interop.probing.probingapi.dtos.ChangeProbingStateRequest;
Expand All @@ -33,19 +34,18 @@ ResponseEntity<SearchEserviceBEResponse> searchEservices(
@RequestParam(value = "state", required = false) List<EserviceStateFE> state);

@PostMapping("/{eserviceId}/versions/{versionId}/updateFrequency")
ResponseEntity<Void> updateEserviceFrequency(@PathVariable("eserviceId") UUID eserviceId,
Response updateEserviceFrequency(@PathVariable("eserviceId") UUID eserviceId,
@PathVariable("versionId") UUID versionId,
@Valid @RequestBody ChangeProbingFrequencyRequest changeProbingFrequencyRequest)
throws EserviceNotFoundException;
@Valid @RequestBody ChangeProbingFrequencyRequest changeProbingFrequencyRequest);

@PostMapping("/{eserviceId}/versions/{versionId}/probing/updateState")
ResponseEntity<Void> updateEserviceProbingState(@PathVariable("eserviceId") UUID eserviceId,
Response updateEserviceProbingState(@PathVariable("eserviceId") UUID eserviceId,
@PathVariable("versionId") UUID versionId,
@RequestBody ChangeProbingStateRequest changeProbingStateRequest)
throws EserviceNotFoundException;

@PostMapping("/{eserviceId}/versions/{versionId}/updateState")
ResponseEntity<Void> updateEserviceState(@PathVariable("eserviceId") UUID eserviceId,
Response updateEserviceState(@PathVariable("eserviceId") UUID eserviceId,
@PathVariable("versionId") UUID versionId,
@RequestBody ChangeEserviceStateRequest changeEserviceStateRequest)
throws EserviceNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

public class EserviceNotFoundException extends Exception {

private static final long serialVersionUID = 3431456002092605147L;
private static final long serialVersionUID = 3431456002092605147L;

public EserviceNotFoundException(String message) {
super(message);
}
public EserviceNotFoundException(String message) {
super(message);
}

public EserviceNotFoundException() {
super();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package it.pagopa.interop.probing.probingapi.exception;

import java.util.List;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.amazonaws.xray.AWSXRay;
import it.pagopa.interop.probing.probingapi.dtos.Problem;
import it.pagopa.interop.probing.probingapi.dtos.ProblemError;
import it.pagopa.interop.probing.probingapi.util.constant.ErrorMessages;
import it.pagopa.interop.probing.probingapi.util.logging.Logger;
import it.pagopa.interop.probing.probingapi.util.logging.LoggingPlaceholders;

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
Expand Down Expand Up @@ -54,6 +54,22 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotRead
return ResponseEntity.status(status).body(problemResponse);
}

/**
* Manages the {@link MethodArgumentNotValidException} creating a new {@link ResponseEntity} and
* sending it to the client with error code 400 and information about the error
*
* @param ex The intercepted exception
* @return A new {@link ResponseEntity} with {@link Problem} body
*/
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
log.logMessageException(ex);
Problem problemResponse =
createProblem(HttpStatus.BAD_REQUEST, ErrorMessages.BAD_REQUEST, ErrorMessages.BAD_REQUEST);
return ResponseEntity.status(status).body(problemResponse);
}

/**
* Creates an instance of type {@link Problem} following the RFC 7807 standard
*
Expand All @@ -67,7 +83,7 @@ private Problem createProblem(HttpStatus responseCode, String titleMessage,
ProblemError errorDetails =
ProblemError.builder().code(responseCode.toString()).detail(detailMessage).build();
return Problem.builder().status(responseCode.value()).title(titleMessage).detail(detailMessage)
.traceId(MDC.get(LoggingPlaceholders.TRACE_ID_XRAY_PLACEHOLDER))
.errors(List.of(errorDetails)).build();
.traceId(AWSXRay.getCurrentSegment().getTraceId().toString()).errors(List.of(errorDetails))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void updateEserviceProbingState(UUID eserviceId, UUID versionId,
*
* @param inputData the input data DTO containing the e-service id, version id, the new frequency
* and new time interval for polling
* @return
* @throws EserviceNotFoundException if the e-service isn't found in the database
*/
void updateEserviceFrequency(UUID eserviceId, UUID versionId,
Expand All @@ -55,7 +56,7 @@ void updateEserviceFrequency(UUID eserviceId, UUID versionId,
* @param state the e service state
* @return the SearchEserviceResponse which contain eserviceList and pagination parameter
*/
public SearchEserviceResponse searchEservices(Integer limit, Integer offset, String eserviceName,
SearchEserviceResponse searchEservices(Integer limit, Integer offset, String eserviceName,
String producerName, Integer versionNumber, List<EserviceStateFE> state);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.List;
import java.util.UUID;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.amazonaws.xray.spring.aop.XRayEnabled;
import feign.Response;
import it.pagopa.interop.probing.probingapi.client.EserviceClient;
import it.pagopa.interop.probing.probingapi.dtos.ChangeEserviceStateRequest;
import it.pagopa.interop.probing.probingapi.dtos.ChangeProbingFrequencyRequest;
Expand Down Expand Up @@ -35,27 +37,33 @@ public class EserviceServiceImpl implements EserviceService {
@Override
public void updateEserviceState(UUID eserviceId, UUID versionId,
ChangeEserviceStateRequest changeEserviceStateRequest) throws EserviceNotFoundException {
eserviceClient.updateEserviceState(eserviceId, versionId, changeEserviceStateRequest);
Response response =
eserviceClient.updateEserviceState(eserviceId, versionId, changeEserviceStateRequest);
logger.logMessageEserviceStateUpdated(eserviceId, versionId,
changeEserviceStateRequest.geteServiceState());
checkStatusNotFound(response);
}

@Override
public void updateEserviceProbingState(UUID eserviceId, UUID versionId,
ChangeProbingStateRequest changeProbingStateRequest) throws EserviceNotFoundException {
eserviceClient.updateEserviceProbingState(eserviceId, versionId, changeProbingStateRequest);
Response response =
eserviceClient.updateEserviceProbingState(eserviceId, versionId, changeProbingStateRequest);
logger.logMessageEserviceProbingStateUpdated(eserviceId, versionId,
changeProbingStateRequest.getProbingEnabled());
checkStatusNotFound(response);
}

@Override
public void updateEserviceFrequency(UUID eserviceId, UUID versionId,
ChangeProbingFrequencyRequest changeProbingFrequencyRequest)
throws EserviceNotFoundException {
eserviceClient.updateEserviceFrequency(eserviceId, versionId, changeProbingFrequencyRequest);
Response response = eserviceClient.updateEserviceFrequency(eserviceId, versionId,
changeProbingFrequencyRequest);
logger.logMessageEservicePollingConfigUpdated(eserviceId, versionId,
changeProbingFrequencyRequest.getStartTime(), changeProbingFrequencyRequest.getEndTime(),
changeProbingFrequencyRequest.getFrequency());
checkStatusNotFound(response);
}

@Override
Expand Down Expand Up @@ -86,4 +94,10 @@ public ProbingDataEserviceResponse getEserviceProbingData(Long eserviceRecordId)
return mapper.toProbingDataEserviceResponse(
eserviceClient.getEserviceProbingData(eserviceRecordId).getBody());
}

private void checkStatusNotFound(Response response) throws EserviceNotFoundException {
if (response.status() == HttpStatus.SC_NOT_FOUND) {
throw new EserviceNotFoundException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -22,6 +24,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import feign.Request;
import feign.Response;
import it.pagopa.interop.probing.probingapi.client.EserviceClient;
import it.pagopa.interop.probing.probingapi.dtos.ChangeEserviceStateRequest;
import it.pagopa.interop.probing.probingapi.dtos.ChangeProbingFrequencyRequest;
Expand Down Expand Up @@ -118,8 +122,10 @@ void setup() {
void testUpdateEserviceState_whenGivenCorrectEserviceIdAndVersionIdAndState_thenEserviceStateIsUpdated()
throws EserviceNotFoundException {
Mockito
.when(eserviceClient.updateEserviceState(eserviceId, versionId, changeEserviceStateRequest))
.thenReturn(ResponseEntity.ok(null));
.when(eserviceClient.updateEserviceState(eserviceId, versionId,
changeEserviceStateRequest))
.thenReturn(Response.builder().status(200).request(Request.create(Request.HttpMethod.POST,
"foo/foo/bar/v1/delete-data-user", new HashMap<>(), null, null, null)).build());
service.updateEserviceState(eserviceId, versionId, changeEserviceStateRequest);
verify(eserviceClient).updateEserviceState(eserviceId, versionId, changeEserviceStateRequest);
}
Expand All @@ -144,9 +150,11 @@ void updateEserviceState_shouldThrowExceptionIfEserviceNotFound()
@DisplayName("e-service probing gets enabled")
void testEserviceProbingState_whenGivenCorrectEserviceIdAndVersionId_thenEserviceProbingIsEnabled()
throws EserviceNotFoundException {
Mockito.when(
eserviceClient.updateEserviceProbingState(eserviceId, versionId, changeProbingStateRequest))
.thenReturn(ResponseEntity.ok(null));
Mockito
.when(eserviceClient.updateEserviceProbingState(eserviceId, versionId,
changeProbingStateRequest))
.thenReturn(Response.builder().status(200).request(Request.create(Request.HttpMethod.POST,
"foo/foo/bar/v1/delete-data-user", new HashMap<>(), null, null, null)).build());
service.updateEserviceProbingState(eserviceId, versionId, changeProbingStateRequest);
verify(eserviceClient).updateEserviceProbingState(eserviceId, versionId,
changeProbingStateRequest);
Expand All @@ -172,8 +180,11 @@ void testEserviceProbingState_whenNoEServiceIsFound_thenThrowsException()
@DisplayName("e-service frequency correctly updated with new state")
void testUpdateEserviceFrequencyDto_whenGivenCorrectEserviceIdAndVersionIdAndState_thenEserviceStateIsUpdated()
throws EserviceNotFoundException {
Mockito.when(eserviceClient.updateEserviceFrequency(eserviceId, versionId,
changeProbingFrequencyRequest)).thenReturn(ResponseEntity.ok(null));
Mockito
.when(eserviceClient.updateEserviceFrequency(eserviceId, versionId,
changeProbingFrequencyRequest))
.thenReturn(Response.builder().status(200).request(Request.create(Request.HttpMethod.POST,
"foo/foo/bar/v1/delete-data-user", new HashMap<>(), null, null, null)).build());
service.updateEserviceFrequency(eserviceId, versionId, changeProbingFrequencyRequest);
verify(eserviceClient).updateEserviceFrequency(eserviceId, versionId,
changeProbingFrequencyRequest);
Expand All @@ -186,13 +197,16 @@ void testUpdateEserviceFrequencyDto_whenNoEServiceIsFound_thenThrowsException()
UUID eserviceIdRandom = UUID.randomUUID();
UUID versionIdRandom = UUID.randomUUID();

Mockito.doThrow(new EserviceNotFoundException("Eservice not found")).when(eserviceClient)
.updateEserviceFrequency(eserviceIdRandom, versionIdRandom, changeProbingFrequencyRequest);
Mockito
.when(eserviceClient.updateEserviceFrequency(eserviceId, versionId,
changeProbingFrequencyRequest))
.thenReturn(Response.builder().status(404).request(Request.create(Request.HttpMethod.POST,
"foo/foo/bar/v1/delete-data-user", new HashMap<>(), null, null, null)).build());

assertThrows(EserviceNotFoundException.class,
() -> service.updateEserviceFrequency(eserviceIdRandom, versionIdRandom,
changeProbingFrequencyRequest),
"e-service should not be found and an EserviceNotFoundException should be thrown");
Response response = eserviceClient.updateEserviceFrequency(eserviceId, versionId,
changeProbingFrequencyRequest);

assertEquals(HttpStatus.SC_NOT_FOUND, response.status());
}

@Test
Expand Down

0 comments on commit 72aefa5

Please sign in to comment.