Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisKina-dev committed Jan 30, 2025
1 parent 19027e2 commit 983e098
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import it.gov.pagopa.pu.fileshare.connector.processexecutions.config.ProcessExecutionsApisHolder;
import it.gov.pagopa.pu.p4paprocessexecutions.controller.generated.IngestionFlowFileControllerApi;
import it.gov.pagopa.pu.p4paprocessexecutions.controller.generated.IngestionFlowFileEntityControllerApi;
import it.gov.pagopa.pu.p4paprocessexecutions.dto.generated.IngestionFlowFile;
import it.gov.pagopa.pu.p4paprocessexecutions.dto.generated.IngestionFlowFileRequestDTO;
import org.junit.jupiter.api.AfterEach;
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;
import org.mockito.Mock;
Expand All @@ -25,68 +25,125 @@ class IngestionFlowFileClientTest {
@Mock
private IngestionFlowFileControllerApi ingestionFlowFileControllerApiMock;

private IngestionFlowFileClient ingestionFlowFileClient;
@Mock
private IngestionFlowFileEntityControllerApi ingestionFlowFileEntityControllerApiMock;

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

ingestionFlowFileClient = new IngestionFlowFileClient(
processExecutionsApisHolderMock);
}
IngestionFlowFileClient ingestionFlowFileClient = new IngestionFlowFileClient(processExecutionsApisHolderMock);

@AfterEach
void verifyNoMoreInteractions() {
Mockito.verifyNoMoreInteractions(
ingestionFlowFileControllerApiMock
);
}

@Test
void whenCreateIngestionFlowFileThenOK() {
// Given
IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO();

String expectedIngestionFlowFileId = "INGESTIONFLOWFILEID";
Mockito.when(ingestionFlowFileControllerApiMock.createIngestionFlowFileWithHttpInfo(ingestionFlowFileRequestDTO))
.thenReturn(ResponseEntity.created(URI.create(expectedIngestionFlowFileId)).build());

// When
String result = ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken);

// Then
Assertions.assertSame(expectedIngestionFlowFileId, result);

Mockito.verifyNoMoreInteractions(ingestionFlowFileControllerApiMock);
}

@Test
void givenGenericHttpExceptionWhenCreateIngestionFlowFileThenThrowIt() {
// Given
ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class);
Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken))
.thenReturn(ingestionFlowFileControllerApiMock);

IngestionFlowFileClient ingestionFlowFileClient = new IngestionFlowFileClient(processExecutionsApisHolderMock);

HttpClientErrorException expectedException = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR);
IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO();

Mockito.doThrow(expectedException).when(ingestionFlowFileControllerApiMock).createIngestionFlowFileWithHttpInfo(ingestionFlowFileRequestDTO);

// When
HttpClientErrorException result = Assertions.assertThrows(expectedException.getClass(), () -> ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken));
HttpClientErrorException result = Assertions.assertThrows(expectedException.getClass(),
() -> ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken));

// Then
Assertions.assertSame(expectedException, result);

Mockito.verifyNoMoreInteractions(ingestionFlowFileControllerApiMock);
}

@Test
void givenGenericExceptionWhenCreateIngestionFlowFileThenThrowIt() {
// Given
ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class);
Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileControllerApi(accessToken))
.thenReturn(ingestionFlowFileControllerApiMock);

IngestionFlowFileClient ingestionFlowFileClient = new IngestionFlowFileClient(processExecutionsApisHolderMock);

RuntimeException expectedException = new RuntimeException();
IngestionFlowFileRequestDTO ingestionFlowFileRequestDTO = new IngestionFlowFileRequestDTO();

Mockito.doThrow(expectedException).when(ingestionFlowFileControllerApiMock).createIngestionFlowFileWithHttpInfo(ingestionFlowFileRequestDTO);

// When
RuntimeException result = Assertions.assertThrows(expectedException.getClass(), () -> ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken));
RuntimeException result = Assertions.assertThrows(expectedException.getClass(),
() -> ingestionFlowFileClient.createIngestionFlowFile(ingestionFlowFileRequestDTO, accessToken));

// Then
Assertions.assertSame(expectedException, result);

Mockito.verifyNoMoreInteractions(ingestionFlowFileControllerApiMock);
}

@Test
void whenGetIngestionFlowFileThenReturnIngestionFlowFile() {
ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class);
Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileEntityControllerApi(accessToken))
.thenReturn(ingestionFlowFileEntityControllerApiMock);

IngestionFlowFileClient ingestionFlowFileClient = new IngestionFlowFileClient(processExecutionsApisHolderMock);

Long ingestionFlowFileId = 123L;
IngestionFlowFile expectedIngestionFlowFile = new IngestionFlowFile();
Mockito.when(ingestionFlowFileEntityControllerApiMock.crudGetIngestionflowfile("123"))
.thenReturn(expectedIngestionFlowFile);

IngestionFlowFile result = ingestionFlowFileClient.getIngestionFlowFile(ingestionFlowFileId, accessToken);

Assertions.assertSame(expectedIngestionFlowFile, result);

Mockito.verifyNoMoreInteractions(ingestionFlowFileEntityControllerApiMock);
}

@Test
void givenGenericExceptionWhenGetIngestionFlowFileThenThrowIt() {
ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class);
Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileEntityControllerApi(accessToken))
.thenReturn(ingestionFlowFileEntityControllerApiMock);

IngestionFlowFileClient ingestionFlowFileClient = new IngestionFlowFileClient(processExecutionsApisHolderMock);

Long ingestionFlowFileId = 123L;
RuntimeException genericException = new RuntimeException("Unexpected error");
Mockito.when(ingestionFlowFileEntityControllerApiMock.crudGetIngestionflowfile("123"))
.thenThrow(genericException);

RuntimeException result = Assertions.assertThrows(RuntimeException.class,
() -> ingestionFlowFileClient.getIngestionFlowFile(ingestionFlowFileId, accessToken));

Assertions.assertSame(genericException, result);
}

@Test
void givenHttpClientErrorExceptionOtherStatusWhenGetIngestionFlowFileThenThrowIt() {
ProcessExecutionsApisHolder processExecutionsApisHolderMock = Mockito.mock(ProcessExecutionsApisHolder.class);
Mockito.when(processExecutionsApisHolderMock.getIngestionFlowFileEntityControllerApi(accessToken))
.thenReturn(ingestionFlowFileEntityControllerApiMock);

IngestionFlowFileClient ingestionFlowFileClient = new IngestionFlowFileClient(processExecutionsApisHolderMock);

Long ingestionFlowFileId = 123L;
HttpClientErrorException genericException = new HttpClientErrorException(HttpStatus.BAD_REQUEST);
Mockito.when(ingestionFlowFileEntityControllerApiMock.crudGetIngestionflowfile("123"))
.thenThrow(genericException);

HttpClientErrorException result = Assertions.assertThrows(HttpClientErrorException.class,
() -> ingestionFlowFileClient.getIngestionFlowFile(ingestionFlowFileId, accessToken));

Assertions.assertSame(genericException, result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import it.gov.pagopa.pu.fileshare.exception.custom.FileUploadException;
import it.gov.pagopa.pu.fileshare.exception.custom.InvalidFileException;
import it.gov.pagopa.pu.fileshare.util.AESUtils;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -15,9 +12,17 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(MockitoExtension.class)
class FileStorerServiceTest {
private FileStorerService fileStorerService;
Expand All @@ -27,15 +32,15 @@ class FileStorerServiceTest {

@BeforeEach
void setUp() {
fileStorerService = new FileStorerService(foldersPathsConfig,FILE_ENCRYPT_PASSWORD);
fileStorerService = new FileStorerService(foldersPathsConfig, FILE_ENCRYPT_PASSWORD);
}

@Test
void givenInvalidFileWhenSaveToSharedFolderThenFileUploadException() {
try (MockedStatic<AESUtils> aesUtilsMockedStatic = Mockito.mockStatic(
AESUtils.class);
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
try {
fileStorerService.saveToSharedFolder(0L, null, "");
Assertions.fail("Expected FileUploadException");
Expand All @@ -58,8 +63,8 @@ void givenInvalidFilenameWhenSaveToSharedFolderThenInvalidFileException() {

try (MockedStatic<AESUtils> aesUtilsMockedStatic = Mockito.mockStatic(
AESUtils.class);
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
try {
fileStorerService.saveToSharedFolder(0L, file, "");
Assertions.fail("Expected InvalidFileException");
Expand All @@ -82,8 +87,8 @@ void givenErrorWhenSaveToSharedFolderThenFileUploadException() {

try (MockedStatic<AESUtils> aesUtilsMockedStatic = Mockito.mockStatic(
AESUtils.class);
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
String sharedFolderPath = "/shared";
Mockito.when(foldersPathsConfig.getShared()).thenReturn(sharedFolderPath);
Mockito.when(AESUtils.encrypt(Mockito.eq(FILE_ENCRYPT_PASSWORD), (InputStream) Mockito.any()))
Expand All @@ -95,7 +100,7 @@ void givenErrorWhenSaveToSharedFolderThenFileUploadException() {
} catch (FileUploadException e) {
Mockito.verify(foldersPathsConfig).getShared();
Mockito.verifyNoMoreInteractions(foldersPathsConfig);
aesUtilsMockedStatic.verify(() -> AESUtils.encrypt(Mockito.anyString(),(InputStream) Mockito.any()));
aesUtilsMockedStatic.verify(() -> AESUtils.encrypt(Mockito.anyString(), (InputStream) Mockito.any()));
aesUtilsMockedStatic.verifyNoMoreInteractions();
filesMockedStatic.verify(() -> Files.exists(Mockito.any()));
filesMockedStatic.verify(() -> Files.createDirectories(Mockito.any()));
Expand All @@ -116,8 +121,8 @@ void givenValidFileWhenSaveToSharedFolderThenOK() {

try (MockedStatic<AESUtils> aesUtilsMockedStatic = Mockito.mockStatic(
AESUtils.class);
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)
) {
String sharedFolderPath = "/shared";
long organizationId = 0L;
Expand All @@ -127,16 +132,16 @@ void givenValidFileWhenSaveToSharedFolderThenOK() {

String result = fileStorerService.saveToSharedFolder(organizationId, file, relativePath);

Assertions.assertEquals(Paths.get(relativePath,filename).toString(),result);
Assertions.assertEquals(Paths.get(relativePath, filename).toString(), result);
Mockito.verify(foldersPathsConfig).getShared();
Mockito.verifyNoMoreInteractions(foldersPathsConfig);
aesUtilsMockedStatic.verify(() -> AESUtils.encrypt(Mockito.anyString(),(InputStream) Mockito.any()));
aesUtilsMockedStatic.verify(() -> AESUtils.encrypt(Mockito.anyString(), (InputStream) Mockito.any()));
aesUtilsMockedStatic.verifyNoMoreInteractions();
filesMockedStatic.verify(() -> Files.exists(Mockito.eq(
Paths.get(sharedFolderPath, organizationFolder, relativePath))));
filesMockedStatic.verify(() -> Files.createDirectories(Mockito.eq(
Paths.get(sharedFolderPath, organizationFolder, relativePath))));
filesMockedStatic.verify(() -> Files.copy((InputStream) Mockito.any(),Mockito.eq(Paths.get(sharedFolderPath, organizationFolder,relativePath,filename)), Mockito.any()));
filesMockedStatic.verify(() -> Files.copy((InputStream) Mockito.any(), Mockito.eq(Paths.get(sharedFolderPath, organizationFolder, relativePath, filename)), Mockito.any()));
filesMockedStatic.verifyNoMoreInteractions();
}
}
Expand All @@ -152,8 +157,8 @@ void givenInvalidPathWhenSaveToSharedFolderThenInvalidFileException() {

try (MockedStatic<AESUtils> aesUtilsMockedStatic = Mockito.mockStatic(
AESUtils.class);
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
try {
fileStorerService.saveToSharedFolder(0L, file, "/relative/../../test");
Assertions.fail("Expected InvalidFileException");
Expand All @@ -164,4 +169,52 @@ void givenInvalidPathWhenSaveToSharedFolderThenInvalidFileException() {
}
}
}

@Test
void givenExistingFileWhenDecryptFileThenReturnInputStreamResource() throws IOException {
String filePath = "C:/shared/organization";
String fileName = "test.txt";
File directory = new File(filePath);

File mockFile = new File(directory, fileName);

try (BufferedWriter writer = new BufferedWriter(new FileWriter(mockFile))) {
writer.write("Encrypted content");
}

assertTrue(mockFile.exists());

InputStream cipherInputStream = new ByteArrayInputStream("Decrypted content".getBytes());

try (MockedStatic<AESUtils> aesUtilsMockedStatic = Mockito.mockStatic(AESUtils.class)) {
Mockito.when(AESUtils.decrypt(Mockito.anyString(), Mockito.any(InputStream.class)))
.thenReturn(cipherInputStream);

InputStreamResource result = fileStorerService.decryptFile(filePath, fileName);

Assertions.assertNotNull(result);
Assertions.assertEquals(cipherInputStream, result.getInputStream());

aesUtilsMockedStatic.verify(() -> AESUtils.decrypt(Mockito.anyString(), Mockito.any(InputStream.class)));
}
}

@Test
void givenNonExistingFileWhenDecryptFileThenReturnNull() {
String filePath = "C:/shared/organization";
String fileName = "nonexistent.txt";
File directory = new File(filePath);
File mockFile = new File(directory, fileName);

assertFalse(mockFile.exists());

InputStreamResource result = fileStorerService.decryptFile(filePath, fileName);

Assertions.assertNull(result);
}

}




Loading

0 comments on commit 983e098

Please sign in to comment.