diff --git a/openapi/p4pa-fileshare.openapi.yaml b/openapi/p4pa-fileshare.openapi.yaml index 1545c9f..a9e6bf7 100644 --- a/openapi/p4pa-fileshare.openapi.yaml +++ b/openapi/p4pa-fileshare.openapi.yaml @@ -40,8 +40,12 @@ paths: type: string format: binary responses: - '200': - description: OK + '201': + description: Created + content: + application/json: + schema: + $ref: '#/components/schemas/UploadIngestionFlowFileResponseDTO' '500': description: Server Error content: @@ -84,3 +88,10 @@ components: - PORTAL - SIL - PAGOPA + UploadIngestionFlowFileResponseDTO: + type: object + required: + - ingestionFlowFileId + properties: + ingestionFlowFileId: + type: string diff --git a/src/main/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClient.java b/src/main/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClient.java index 7db4c5f..771cc6b 100644 --- a/src/main/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClient.java +++ b/src/main/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClient.java @@ -3,6 +3,7 @@ import it.gov.pagopa.pu.fileshare.connector.processexecutions.config.ProcessExecutionsApisHolder; import it.gov.pagopa.pu.p4paprocessexecutions.dto.generated.IngestionFlowFileRequestDTO; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; import org.springframework.web.client.HttpClientErrorException; @@ -16,9 +17,11 @@ public IngestionFlowFileClient( this.processExecutionsApisHolder = processExecutionsApisHolder; } - public void createIngestionFlowFile(IngestionFlowFileRequestDTO ingestionFlowFileDTO, String accessToken) { + public String createIngestionFlowFile(IngestionFlowFileRequestDTO ingestionFlowFileDTO, String accessToken) { try{ - processExecutionsApisHolder.getIngestionFlowFileControllerApi(accessToken).createIngestionFlowFile(ingestionFlowFileDTO); + return processExecutionsApisHolder.getIngestionFlowFileControllerApi(accessToken) + .createIngestionFlowFileWithHttpInfo(ingestionFlowFileDTO) + .getHeaders().getFirst(HttpHeaders.LOCATION); } catch (HttpClientErrorException e) { log.error("Error creating ingestion flow file", e); throw e; diff --git a/src/main/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesController.java b/src/main/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesController.java index aa9caf7..1c4e61e 100644 --- a/src/main/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesController.java +++ b/src/main/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesController.java @@ -3,6 +3,7 @@ import it.gov.pagopa.pu.fileshare.controller.generated.IngestionFlowFileApi; import it.gov.pagopa.pu.fileshare.dto.generated.FileOrigin; import it.gov.pagopa.pu.fileshare.dto.generated.IngestionFlowFileType; +import it.gov.pagopa.pu.fileshare.dto.generated.UploadIngestionFlowFileResponseDTO; import it.gov.pagopa.pu.fileshare.security.SecurityUtils; import it.gov.pagopa.pu.fileshare.service.ingestion.IngestionFlowFileService; import org.springframework.http.ResponseEntity; @@ -20,9 +21,9 @@ public IngestionFlowFilesController( } @Override - public ResponseEntity uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, FileOrigin fileOrigin, MultipartFile ingestionFlowFile) { - ingestionFlowFileService.uploadIngestionFlowFile(organizationId, ingestionFlowFileType, fileOrigin, ingestionFlowFile, SecurityUtils.getLoggedUser(), + public ResponseEntity uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, FileOrigin fileOrigin, MultipartFile ingestionFlowFile) { + String ingestionFlowFileId = ingestionFlowFileService.uploadIngestionFlowFile(organizationId, ingestionFlowFileType, fileOrigin, ingestionFlowFile, SecurityUtils.getLoggedUser(), SecurityUtils.getAccessToken()); - return ResponseEntity.ok(null); + return ResponseEntity.ok(new UploadIngestionFlowFileResponseDTO(ingestionFlowFileId)); } } diff --git a/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileService.java b/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileService.java index 6ba86e9..b5b92d1 100644 --- a/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileService.java +++ b/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileService.java @@ -6,6 +6,6 @@ import org.springframework.web.multipart.MultipartFile; public interface IngestionFlowFileService { - void uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, + String uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, FileOrigin fileOrigin, MultipartFile ingestionFlowFile, UserInfo user, String accessToken); } diff --git a/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImpl.java b/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImpl.java index 776656b..113e161 100644 --- a/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImpl.java +++ b/src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImpl.java @@ -43,13 +43,14 @@ public IngestionFlowFileServiceImpl( } @Override - public void uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, + public String uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, FileOrigin fileOrigin, MultipartFile ingestionFlowFile, UserInfo user, String accessToken) { userAuthorizationService.checkUserAuthorization(organizationId, user, accessToken); fileService.validateFile(ingestionFlowFile, validIngestionFlowFileExt); String filePath = fileStorerService.saveToSharedFolder(ingestionFlowFile, foldersPathsConfig.getIngestionFlowFilePath(ingestionFlowFileType)); - ingestionFlowFileClient.createIngestionFlowFile( + + return ingestionFlowFileClient.createIngestionFlowFile( ingestionFlowFileDTOMapper.mapToIngestionFlowFileDTO(ingestionFlowFile, ingestionFlowFileType, fileOrigin, organizationId, filePath) , accessToken); diff --git a/src/test/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClientTest.java b/src/test/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClientTest.java index ae334be..53ded35 100644 --- a/src/test/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClientTest.java +++ b/src/test/java/it/gov/pagopa/pu/fileshare/connector/processexecutions/client/IngestionFlowFileClientTest.java @@ -12,12 +12,16 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.HttpClientErrorException; +import java.net.URI; + @ExtendWith(MockitoExtension.class) class IngestionFlowFileClientTest { - @Mock - private ProcessExecutionsApisHolder processExecutionsApisHolderMock; + + private final String accessToken = "ACCESSTOKEN"; + @Mock private IngestionFlowFileControllerApi ingestionFlowFileControllerApiMock; @@ -25,6 +29,10 @@ class IngestionFlowFileClientTest { @BeforeEach void setUp() { + ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class); + Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken)) + .thenReturn(ingestionFlowFileControllerApiMock); + ingestionFlowFileClient = new IngestionFlowFileClient( processExecutionsApisHolderMock); } @@ -32,35 +40,33 @@ void setUp() { @AfterEach void verifyNoMoreInteractions() { Mockito.verifyNoMoreInteractions( - processExecutionsApisHolderMock + ingestionFlowFileControllerApiMock ); } @Test void whenCreateIngestionFlowFileThenOK() { // Given - String accessToken = "ACCESSTOKEN"; IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO(); - Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken)) - .thenReturn(ingestionFlowFileControllerApiMock); + String expectedIngestionFlowFileId = "INGESTIONFLOWFILEID"; + Mockito.when(ingestionFlowFileControllerApiMock.createIngestionFlowFileWithHttpInfo(ingestionFlowFileRequestDTO)) + .thenReturn(ResponseEntity.created(URI.create(expectedIngestionFlowFileId)).build()); + // When - ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken); + String result = ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken); // Then - Mockito.verify(ingestionFlowFileControllerApiMock).createIngestionFlowFile(ingestionFlowFileRequestDTO); + Assertions.assertSame(expectedIngestionFlowFileId, result); } @Test void givenGenericHttpExceptionWhenCreateIngestionFlowFileThenThrowIt() { // Given - String accessToken = "ACCESSTOKEN"; HttpClientErrorException expectedException = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR); IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO(); - Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken)) - .thenReturn(ingestionFlowFileControllerApiMock); - Mockito.doThrow(expectedException).when(ingestionFlowFileControllerApiMock).createIngestionFlowFile(ingestionFlowFileRequestDTO); + Mockito.doThrow(expectedException).when(ingestionFlowFileControllerApiMock).createIngestionFlowFileWithHttpInfo(ingestionFlowFileRequestDTO); // When HttpClientErrorException result = Assertions.assertThrows(expectedException.getClass(), () -> ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken)); @@ -72,13 +78,10 @@ void givenGenericHttpExceptionWhenCreateIngestionFlowFileThenThrowIt() { @Test void givenGenericExceptionWhenCreateIngestionFlowFileThenThrowIt() { // Given - String accessToken = "ACCESSTOKEN"; RuntimeException expectedException = new RuntimeException(); IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO(); - Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken)) - .thenReturn(ingestionFlowFileControllerApiMock); - Mockito.doThrow(expectedException).when(ingestionFlowFileControllerApiMock).createIngestionFlowFile(ingestionFlowFileRequestDTO); + Mockito.doThrow(expectedException).when(ingestionFlowFileControllerApiMock).createIngestionFlowFileWithHttpInfo(ingestionFlowFileRequestDTO); // When RuntimeException result = Assertions.assertThrows(expectedException.getClass(), () -> ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken)); diff --git a/src/test/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesControllerTest.java b/src/test/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesControllerTest.java index 5bf4dce..61da0b0 100644 --- a/src/test/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesControllerTest.java +++ b/src/test/java/it/gov/pagopa/pu/fileshare/controller/IngestionFlowFilesControllerTest.java @@ -1,9 +1,5 @@ package it.gov.pagopa.pu.fileshare.controller; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import com.fasterxml.jackson.databind.ObjectMapper; import it.gov.pagopa.pu.fileshare.controller.generated.IngestionFlowFileApi; import it.gov.pagopa.pu.fileshare.dto.generated.FileOrigin; import it.gov.pagopa.pu.fileshare.dto.generated.IngestionFlowFileType; @@ -22,6 +18,10 @@ import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @WebMvcTest(value = IngestionFlowFileApi.class,excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = JwtAuthenticationFilter.class)) @AutoConfigureMockMvc(addFilters = false) @@ -29,9 +29,6 @@ class IngestionFlowFilesControllerTest { @Autowired private MockMvc mockMvc; - @Autowired - private ObjectMapper objectMapper; - @MockitoBean private IngestionFlowFileService serviceMock; @@ -46,16 +43,18 @@ void givenCorrectRequestWhenUploadIngestionFlowFileThenOk() throws Exception { ); TestUtils.addSampleUserIntoSecurityContext(); + Mockito.when(serviceMock.uploadIngestionFlowFile(Mockito.eq(organizationId), + Mockito.eq(IngestionFlowFileType.RECEIPT),Mockito.eq(FileOrigin.PAGOPA),Mockito.eq(file), + Mockito.any(), Mockito.anyString())) + .thenReturn("INGESTIONFLOWFILEID"); + mockMvc.perform(multipart("/ingestionflowfiles/"+organizationId) .file(file) .param("ingestionFlowFileType", IngestionFlowFileType.RECEIPT.toString()) .param("fileOrigin", FileOrigin.PAGOPA.toString()) .contentType(MediaType.MULTIPART_FORM_DATA) - ).andExpect(status().isOk()); - - Mockito.verify(serviceMock).uploadIngestionFlowFile(Mockito.eq(organizationId), - Mockito.eq(IngestionFlowFileType.RECEIPT),Mockito.eq(FileOrigin.PAGOPA),Mockito.eq(file), - Mockito.any(), Mockito.anyString()); + ).andExpect(status().isOk()) + .andExpect(content().json("{\"ingestionFlowFileId\":\"INGESTIONFLOWFILEID\"}")); } @Test diff --git a/src/test/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImplTest.java b/src/test/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImplTest.java index 050e34e..ff47cfc 100644 --- a/src/test/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileServiceImplTest.java @@ -10,6 +10,7 @@ import it.gov.pagopa.pu.fileshare.service.UserAuthorizationService; import it.gov.pagopa.pu.fileshare.util.TestUtils; import it.gov.pagopa.pu.p4paprocessexecutions.dto.generated.IngestionFlowFileRequestDTO; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -55,7 +56,9 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk(){ MediaType.TEXT_PLAIN_VALUE, "this is a test file".getBytes() ); + String expectedIngestionFlowFileId = "INGESTIONFLOWFILEID"; IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO(); + Mockito.when(foldersPathsConfigMock.getIngestionFlowFilePath(IngestionFlowFileType.RECEIPT)) .thenReturn(receiptFilePath); Mockito.when(fileStorerServiceMock.saveToSharedFolder(file,receiptFilePath)) @@ -63,13 +66,15 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk(){ Mockito.when(ingestionFlowFileDTOMapperMock.mapToIngestionFlowFileDTO(file, IngestionFlowFileType.RECEIPT, FileOrigin.PAGOPA, organizationId,filePath)) .thenReturn(ingestionFlowFileRequestDTO); + Mockito.when(ingestionFlowFileClientMock.createIngestionFlowFile(ingestionFlowFileRequestDTO,accessToken)) + .thenReturn(expectedIngestionFlowFileId); - ingestionFlowFileService.uploadIngestionFlowFile(organizationId, IngestionFlowFileType.RECEIPT, FileOrigin.PAGOPA, - file, TestUtils.getSampleUser(),accessToken); + String result = ingestionFlowFileService.uploadIngestionFlowFile(organizationId, IngestionFlowFileType.RECEIPT, FileOrigin.PAGOPA, + file, TestUtils.getSampleUser(), accessToken); + Assertions.assertSame(expectedIngestionFlowFileId, result); Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId,TestUtils.getSampleUser(),accessToken); Mockito.verify(fileServiceMock).validateFile(file,VALID_FILE_EXTENSION); - Mockito.verify(ingestionFlowFileClientMock).createIngestionFlowFile(ingestionFlowFileRequestDTO,accessToken); Mockito.verifyNoMoreInteractions(userAuthorizationServiceMock,fileServiceMock, foldersPathsConfigMock,fileStorerServiceMock,ingestionFlowFileDTOMapperMock,ingestionFlowFileClientMock); }