From 8a4e27900073b020f86f2ef01f276c223d19c5fc Mon Sep 17 00:00:00 2001 From: Fabien Date: Mon, 16 Dec 2024 11:57:59 +0100 Subject: [PATCH] chore: coverage tests --- .../service/DocumentServiceImplTest.java | 133 ++++++++++++++++++ pom.xml | 40 ++++++ 2 files changed, 173 insertions(+) create mode 100644 dossierfacile-api-tenant/src/test/java/fr/dossierfacile/api/front/service/DocumentServiceImplTest.java diff --git a/dossierfacile-api-tenant/src/test/java/fr/dossierfacile/api/front/service/DocumentServiceImplTest.java b/dossierfacile-api-tenant/src/test/java/fr/dossierfacile/api/front/service/DocumentServiceImplTest.java new file mode 100644 index 000000000..c65636810 --- /dev/null +++ b/dossierfacile-api-tenant/src/test/java/fr/dossierfacile/api/front/service/DocumentServiceImplTest.java @@ -0,0 +1,133 @@ +package fr.dossierfacile.api.front.service; + +import fr.dossierfacile.api.front.amqp.Producer; +import fr.dossierfacile.api.front.repository.DocumentRepository; +import fr.dossierfacile.common.entity.Document; +import fr.dossierfacile.common.enums.DocumentCategory; +import fr.dossierfacile.common.enums.DocumentStatus; +import fr.dossierfacile.common.repository.DocumentAnalysisReportRepository; +import fr.dossierfacile.common.service.interfaces.FileStorageService; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@MockitoSettings(strictness = Strictness.LENIENT) +class DocumentServiceImplTest { + { + MockitoAnnotations.openMocks(this); + Mockito.lenient(); + } + + @Mock + private DocumentRepository documentRepository; + @Mock + private DocumentAnalysisReportRepository documentAnalysisReportRepository; + @Mock + private FileStorageService fileStorageService; + @Mock + private Producer producer; + @InjectMocks + private DocumentServiceImpl documentService; + + public DocumentServiceImplTest() { + MockitoAnnotations.openMocks(this); + Mockito.lenient(); + } + + @Test + void testResetValidatedOrInProgressDocumentsForValidatedDocument() { + List documentList = new ArrayList<>(); + Document document = new Document(); + document.setId(1L); + document.setDocumentCategory(DocumentCategory.PROFESSIONAL); + document.setDocumentStatus(DocumentStatus.VALIDATED); + documentList.add(document); + + List categoriesToChange = List.of(DocumentCategory.PROFESSIONAL); + + when(documentRepository.save(any(Document.class))).thenReturn(document); + + documentService.resetValidatedOrInProgressDocumentsAccordingCategories(documentList, categoriesToChange); + + assertEquals(DocumentStatus.TO_PROCESS, document.getDocumentStatus()); + assertNull(document.getDocumentDeniedReasons()); + } + + @Test + void testResetValidatedOrInProgressDocumentsForNoDocumentWithWatermark() { + List documentList = new ArrayList<>(); + Document document = new Document(); + document.setId(1L); + document.setDocumentCategory(DocumentCategory.PROFESSIONAL); + document.setDocumentStatus(DocumentStatus.VALIDATED); + document.setNoDocument(true); + document.setWatermarkFile(null); + documentList.add(document); + + List categoriesToChange = List.of(DocumentCategory.PROFESSIONAL); + + when(documentRepository.save(any(Document.class))).thenReturn(document); + + documentService.resetValidatedOrInProgressDocumentsAccordingCategories(documentList, categoriesToChange); + + assertEquals(DocumentStatus.TO_PROCESS, document.getDocumentStatus()); + assertNull(document.getWatermarkFile()); + + } + + @Test + void testResetValidatedOrInProgressDocumentsForNonMatchingCategory() { + List documentList = new ArrayList<>(); + Document document = new Document(); + document.setId(1L); + document.setDocumentCategory(DocumentCategory.IDENTIFICATION); + document.setDocumentStatus(DocumentStatus.VALIDATED); + documentList.add(document); + + List categoriesToChange = List.of(DocumentCategory.PROFESSIONAL); + + documentService.resetValidatedOrInProgressDocumentsAccordingCategories(documentList, categoriesToChange); + + verifyNoInteractions(documentRepository); + verifyNoInteractions(fileStorageService); + verifyNoInteractions(producer); + assertEquals(DocumentStatus.VALIDATED, document.getDocumentStatus()); + } + + @Test + void testResetValidatedOrInProgressDocumentsForEmptyList() { + List documentList = new ArrayList<>(); + List categoriesToChange = List.of(DocumentCategory.PROFESSIONAL); + + documentService.resetValidatedOrInProgressDocumentsAccordingCategories(documentList, categoriesToChange); + + verifyNoInteractions(documentRepository); + verifyNoInteractions(fileStorageService); + verifyNoInteractions(producer); + verifyNoMoreInteractions(documentRepository, fileStorageService, producer); + } + + @Test + void testResetValidatedOrInProgressDocumentsForNullList() { + List categoriesToChange = List.of(DocumentCategory.PROFESSIONAL); + + documentService.resetValidatedOrInProgressDocumentsAccordingCategories(null, categoriesToChange); + + verifyNoInteractions(documentRepository); + verifyNoInteractions(fileStorageService); + verifyNoInteractions(producer); + verifyNoMoreInteractions(documentRepository, fileStorageService, producer); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index b07e9656a..6578effd5 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ 1.4.0 1.78.1 1.78.1 + ${project.basedir}/report-aggregate/target/site/jacoco-aggregate/jacoco.xml @@ -137,4 +138,43 @@ + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M7 + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + prepare-agent + + + + report + prepare-package + + report + + + + aggregate-report + verify + + report-aggregate + + + ${project.build.directory}/jacoco-aggregate + + + + + +