Skip to content

Commit

Permalink
chore: coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien committed Dec 16, 2024
1 parent b30796f commit 8a4e279
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Document> documentList = new ArrayList<>();
Document document = new Document();
document.setId(1L);
document.setDocumentCategory(DocumentCategory.PROFESSIONAL);
document.setDocumentStatus(DocumentStatus.VALIDATED);
documentList.add(document);

List<DocumentCategory> 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<Document> 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<DocumentCategory> 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<Document> documentList = new ArrayList<>();
Document document = new Document();
document.setId(1L);
document.setDocumentCategory(DocumentCategory.IDENTIFICATION);
document.setDocumentStatus(DocumentStatus.VALIDATED);
documentList.add(document);

List<DocumentCategory> categoriesToChange = List.of(DocumentCategory.PROFESSIONAL);

documentService.resetValidatedOrInProgressDocumentsAccordingCategories(documentList, categoriesToChange);

verifyNoInteractions(documentRepository);
verifyNoInteractions(fileStorageService);
verifyNoInteractions(producer);
assertEquals(DocumentStatus.VALIDATED, document.getDocumentStatus());
}

@Test
void testResetValidatedOrInProgressDocumentsForEmptyList() {
List<Document> documentList = new ArrayList<>();
List<DocumentCategory> categoriesToChange = List.of(DocumentCategory.PROFESSIONAL);

documentService.resetValidatedOrInProgressDocumentsAccordingCategories(documentList, categoriesToChange);

verifyNoInteractions(documentRepository);
verifyNoInteractions(fileStorageService);
verifyNoInteractions(producer);
verifyNoMoreInteractions(documentRepository, fileStorageService, producer);
}

@Test
void testResetValidatedOrInProgressDocumentsForNullList() {
List<DocumentCategory> categoriesToChange = List.of(DocumentCategory.PROFESSIONAL);

documentService.resetValidatedOrInProgressDocumentsAccordingCategories(null, categoriesToChange);

verifyNoInteractions(documentRepository);
verifyNoInteractions(fileStorageService);
verifyNoInteractions(producer);
verifyNoMoreInteractions(documentRepository, fileStorageService, producer);
}
}
40 changes: 40 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<jai-imageio-jpeg2000.version>1.4.0</jai-imageio-jpeg2000.version>
<bcprov-jdk18on.version>1.78.1</bcprov-jdk18on.version>
<bcpkix-jdk18on.version>1.78.1</bcpkix-jdk18on.version>
<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/report-aggregate/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -137,4 +138,43 @@
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>aggregate-report</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jacoco-aggregate</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 8a4e279

Please sign in to comment.