Skip to content

Commit

Permalink
P4ADEV-1545 FdRIngestionActity resolved sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
macacia committed Nov 27, 2024
1 parent 476d008 commit 474ea2f
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,133 @@
import it.gov.pagopa.payhub.activities.activity.paymentsreporting.service.IngestionFileHandlerService;
import it.gov.pagopa.payhub.activities.activity.paymentsreporting.service.IngestionFileValidatorService;
import it.gov.pagopa.payhub.activities.activity.paymentsreporting.service.IngestionFlowRetrieverService;
import it.gov.pagopa.payhub.activities.dto.reportingflow.FdRIngestionActivityResult;
import it.gov.pagopa.payhub.activities.dto.reportingflow.IngestionFlowDTO;
import it.gov.pagopa.payhub.activities.exception.InvalidIngestionFileException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.Test;

@ExtendWith(MockitoExtension.class)
class FdRIngestionActivityImplTest {
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@Mock
private IngestionFlowRetrieverService ingestionFlowRetrieverServiceMock;
@Mock
private IngestionFileValidatorService ingestionFileValidatorServiceMock;
@Mock
private IngestionFileHandlerService ingestionFileHandlerServiceMock;
class FdRIngestionActivityImplTest {

private FdRIngestionActivityImpl activity;
private IngestionFlowRetrieverService ingestionFlowRetrieverService;
private IngestionFileValidatorService ingestionFileValidatorService;
private IngestionFileHandlerService ingestionFileHandlerService;
private FdRIngestionActivityImpl ingestionActivity;

@BeforeEach
void init() {
activity = new FdRIngestionActivityImpl(ingestionFlowRetrieverServiceMock, ingestionFileValidatorServiceMock, ingestionFileHandlerServiceMock);
void setUp() {
ingestionFlowRetrieverService = mock(IngestionFlowRetrieverService.class);
ingestionFileValidatorService = mock(IngestionFileValidatorService.class);
ingestionFileHandlerService = mock(IngestionFileHandlerService.class);

ingestionActivity = new FdRIngestionActivityImpl(
ingestionFlowRetrieverService,
ingestionFileValidatorService,
ingestionFileHandlerService
);
}

@Test
void processFile_SuccessfulFlow() throws Exception {
// Given
String ingestionFlowId = "123";
IngestionFlowDTO mockFlowDTO = new IngestionFlowDTO();
mockFlowDTO.setFilePathName("/valid/path");
mockFlowDTO.setFileName("valid-file.zip");
mockFlowDTO.setRequestTokenCode("valid-token");

when(ingestionFlowRetrieverService.getIngestionFlow(Long.valueOf(ingestionFlowId)))
.thenReturn(mockFlowDTO);

// When
FdRIngestionActivityResult result = ingestionActivity.processFile(ingestionFlowId);

// Then
assertTrue(result.isSuccess());
assertNotNull(result.getIufs());
verify(ingestionFlowRetrieverService, times(1))
.getIngestionFlow(Long.valueOf(ingestionFlowId));
verify(ingestionFileValidatorService, times(1))
.validate(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName(), mockFlowDTO.getRequestTokenCode());
verify(ingestionFileHandlerService, times(1))
.setUpProcess(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName());
}

@Test
void processFile_FlowRetrieverFails() {
// Given
String ingestionFlowId = "123";
when(ingestionFlowRetrieverService.getIngestionFlow(Long.valueOf(ingestionFlowId)))
.thenThrow(new RuntimeException("Flow retriever failed"));

// When
FdRIngestionActivityResult result = ingestionActivity.processFile(ingestionFlowId);

// Then
assertFalse(result.isSuccess());
verify(ingestionFlowRetrieverService, times(1))
.getIngestionFlow(Long.valueOf(ingestionFlowId));
verifyNoInteractions(ingestionFileValidatorService);
verifyNoInteractions(ingestionFileHandlerService);
}

@Test
void processFile_ValidationFails() throws Exception {
// Given
String ingestionFlowId = "123";
IngestionFlowDTO mockFlowDTO = new IngestionFlowDTO();
mockFlowDTO.setFilePathName("/valid/path");
mockFlowDTO.setFileName("valid-file.zip");
mockFlowDTO.setRequestTokenCode("valid-token");

when(ingestionFlowRetrieverService.getIngestionFlow(Long.valueOf(ingestionFlowId)))
.thenReturn(mockFlowDTO);

doThrow(new InvalidIngestionFileException("Validation failed"))
.when(ingestionFileValidatorService)
.validate(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName(), mockFlowDTO.getRequestTokenCode());

// When
FdRIngestionActivityResult result = ingestionActivity.processFile(ingestionFlowId);

// Then
assertFalse(result.isSuccess());
verify(ingestionFlowRetrieverService, times(1))
.getIngestionFlow(Long.valueOf(ingestionFlowId));
verify(ingestionFileValidatorService, times(1))
.validate(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName(), mockFlowDTO.getRequestTokenCode());
verifyNoInteractions(ingestionFileHandlerService);
}

}
@Test
void processFile_SetupProcessFails() throws Exception {
// Given
String ingestionFlowId = "123";
IngestionFlowDTO mockFlowDTO = new IngestionFlowDTO();
mockFlowDTO.setFilePathName("/valid/path");
mockFlowDTO.setFileName("valid-file.zip");
mockFlowDTO.setRequestTokenCode("valid-token");

when(ingestionFlowRetrieverService.getIngestionFlow(Long.valueOf(ingestionFlowId)))
.thenReturn(mockFlowDTO);

doThrow(new RuntimeException("Setup process failed"))
.when(ingestionFileHandlerService)
.setUpProcess(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName());

// When
FdRIngestionActivityResult result = ingestionActivity.processFile(ingestionFlowId);

// Then
assertFalse(result.isSuccess());
verify(ingestionFlowRetrieverService, times(1))
.getIngestionFlow(Long.valueOf(ingestionFlowId));
verify(ingestionFileValidatorService, times(1))
.validate(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName(), mockFlowDTO.getRequestTokenCode());
verify(ingestionFileHandlerService, times(1))
.setUpProcess(mockFlowDTO.getFilePathName(), mockFlowDTO.getFileName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

class IngestionFileValidatorServiceTest {

private static final String TEMP_PATH = "temp";

private IngestionFileValidatorService validatorService;

@BeforeEach
Expand All @@ -28,7 +30,7 @@ void setUp() {
@Test
void validate_ValidFile_Success() throws IOException, NoSuchAlgorithmException {
// Given
Path tempDir = Files.createTempDirectory("testDir");
Path tempDir = Files.createTempDirectory(TEMP_PATH);
Path filePath = tempDir.resolve("test.zip");
Path md5Path = tempDir.resolve("test.md5");
Path authPath = tempDir.resolve("test.auth");
Expand All @@ -46,7 +48,7 @@ void validate_ValidFile_Success() throws IOException, NoSuchAlgorithmException {
Files.writeString(md5Path, calculatedMd5);

// When Then
assertDoesNotThrow(() -> validatorService.validate(tempDir.toString(), "test.zip", requestTokenCode));
assertDoesNotThrow(() -> validatorService.validate(TEMP_PATH, "test.zip", requestTokenCode));

// Cleanup
Files.deleteIfExists(filePath);
Expand All @@ -56,9 +58,9 @@ void validate_ValidFile_Success() throws IOException, NoSuchAlgorithmException {
}

@Test
void validate_InvalidMD5_ThrowsException() throws IOException, NoSuchAlgorithmException {
void validate_InvalidMD5_ThrowsException() throws IOException {
// Given
Path tempDir = Files.createTempDirectory("testDir");
Path tempDir = Files.createTempDirectory(TEMP_PATH);
Path filePath = tempDir.resolve("test.zip");
Path md5Path = tempDir.resolve("test.md5");
Path authPath = tempDir.resolve("test.auth");
Expand All @@ -70,7 +72,7 @@ void validate_InvalidMD5_ThrowsException() throws IOException, NoSuchAlgorithmEx

// When Then
assertThrows(InvalidIngestionFileException.class,
() -> validatorService.validate(tempDir.toString(), "test.zip", requestTokenCode)
() -> validatorService.validate(TEMP_PATH, "test.zip", requestTokenCode)
);
// Cleanup
Files.deleteIfExists(filePath);
Expand All @@ -82,7 +84,7 @@ void validate_InvalidMD5_ThrowsException() throws IOException, NoSuchAlgorithmEx
@Test
void validate_InvalidAuth_ThrowsException() throws IOException {
// Given
Path tempDir = Files.createTempDirectory("testDir");
Path tempDir = Files.createTempDirectory(TEMP_PATH);
Path filePath = tempDir.resolve("test.zip");
Path md5Path = tempDir.resolve("test.md5");
Path authPath = tempDir.resolve("test.auth");
Expand All @@ -94,7 +96,7 @@ void validate_InvalidAuth_ThrowsException() throws IOException {

// When Then
assertThrows(InvalidIngestionFileException.class,
() -> validatorService.validate(tempDir.toString(), "test.zip", requestTokenCode)
() -> validatorService.validate(TEMP_PATH, "test.zip", requestTokenCode)
);

// Cleanup
Expand All @@ -107,12 +109,12 @@ void validate_InvalidAuth_ThrowsException() throws IOException {
@Test
void validate_FileNotFound_ThrowsException() {
// Given
Path tempDir = Paths.get("non-existent-dir");
String tempDir = "non-existent-dir";
String filename = "non-existent.zip";

// When Then
assertThrows(InvalidIngestionFileException.class,
() -> validatorService.validate(tempDir.toString(), filename, "valid-token")
() -> validatorService.validate(tempDir, filename, "valid-token")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static org.junit.jupiter.api.Assertions.*;

public class FileUtilsTest {
class FileUtilsTest {
@TempDir
Path tempDir;

Expand Down

0 comments on commit 474ea2f

Please sign in to comment.