Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/coverage #928

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading