Skip to content

Commit

Permalink
fix: Returning ingestionFlowFileId (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 authored Jan 17, 2025
1 parent 6c87af7 commit 96e3c2c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 41 deletions.
15 changes: 13 additions & 2 deletions openapi/p4pa-fileshare.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -84,3 +88,10 @@ components:
- PORTAL
- SIL
- PAGOPA
UploadIngestionFlowFileResponseDTO:
type: object
required:
- ingestionFlowFileId
properties:
ingestionFlowFileId:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,9 +21,9 @@ public IngestionFlowFilesController(
}

@Override
public ResponseEntity<Void> uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType ingestionFlowFileType, FileOrigin fileOrigin, MultipartFile ingestionFlowFile) {
ingestionFlowFileService.uploadIngestionFlowFile(organizationId, ingestionFlowFileType, fileOrigin, ingestionFlowFile, SecurityUtils.getLoggedUser(),
public ResponseEntity<UploadIngestionFlowFileResponseDTO> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,61 @@
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;

private IngestionFlowFileClient ingestionFlowFileClient;

@BeforeEach
void setUp() {
ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class);
Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken))
.thenReturn(ingestionFlowFileControllerApiMock);

ingestionFlowFileClient = new IngestionFlowFileClient(
processExecutionsApisHolderMock);
}

@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));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,16 +18,17 @@
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)
class IngestionFlowFilesControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@MockitoBean
private IngestionFlowFileService serviceMock;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,21 +56,25 @@ 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))
.thenReturn(filePath);
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);
}
Expand Down

0 comments on commit 96e3c2c

Please sign in to comment.