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

Docker Compose + Unit Test #154

Open
wants to merge 2 commits into
base: main
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
17 changes: 17 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: MetaData API
services:
metadata-api:
build: .
ports:
- "8080:8080"
environment:
- fr.insee.rmes.api.sparqlEndpoint=http://graphdb:7200
links:
- graphdb
graphdb:
image: ontotext/graphdb:10.6.4
restart: always
ports:
- "7200:7200"
environment:
GDB_JAVA_OPTS: "-Xmx1g -Xms512M"
2 changes: 1 addition & 1 deletion src/main/resources/rmes-api.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
fr.insee.rmes.api.log.configuration = log4j2.xml

# database
fr.insee.rmes.api.sparqlEndpoint = XXXXX
fr.insee.rmes.api.sparqlEndpoint = http://graphdb:7200
fr.insee.rmes.api.baseHost = http://id.insee.fr

# files storage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package fr.insee.rmes.api.operations.pseudointegrationtest;

import fr.insee.rmes.api.operations.OperationsApiService;
import fr.insee.rmes.config.Configuration;
import fr.insee.rmes.modeles.operations.SimpleObject;
import fr.insee.rmes.modeles.operations.documentations.Document;
import fr.insee.rmes.modeles.operations.documentations.Rubrique;
import fr.insee.rmes.modeles.operations.documentations.RubriqueRichText;
import fr.insee.rmes.utils.FileUtils;
import fr.insee.rmes.utils.Lang;
import fr.insee.rmes.utils.SparqlUtils;
import org.junit.jupiter.api.Assertions;
Expand All @@ -16,12 +18,15 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -42,21 +47,54 @@ class OperationsApiServiceTest {

private static final String MOCK_CSV_GET_DOCUMENTS_EMPTY = "url,labelLg1,labelLg2,dateMiseAJour,langue";

private static List<Rubrique> EXPECTED_RUBRICS ;
private static List<Rubrique> EXPECTED_RUBRICS ;

@BeforeEach
public void init() {
initExpected();
}

@Test
void shouldReturnEmptyMapWhenFileIsEmpty() {
try (MockedStatic<Configuration> mockedConfigurationFactory = Mockito.mockStatic(Configuration.class);
MockedStatic<FileUtils> mockedFileUtilsFactory = Mockito.mockStatic(FileUtils.class)) {

mockedConfigurationFactory.when(() -> Configuration.getFileStorageLocation()).thenReturn("/mock/location");
mockedFileUtilsFactory.when(() -> FileUtils.readFile(eq("/mock/location/storage/exclusionsInseeFr.txt"), eq(";"))).thenReturn(Collections.emptyList());

Map<String, List<String>> result = operationsApiService.readExclusions();

assertEquals(0, result.size());
}
}

@Test
void shouldReturnCorrectMapWhenFileHasValidContent() {
try (MockedStatic<Configuration> mockedConfigurationFactory = Mockito.mockStatic(Configuration.class);
MockedStatic<FileUtils> mockedFileUtilsFactory = Mockito.mockStatic(FileUtils.class)) {

mockedConfigurationFactory.when(() -> Configuration.getFileStorageLocation()).thenReturn("/mock/location");
mockedFileUtilsFactory.when(() -> FileUtils.readFile(eq("/mock/location/storage/exclusionsInseeFr.txt"), eq(";"))).thenReturn(Arrays.asList(
Arrays.asList("TYPE1", "ID1"),
Arrays.asList("TYPE1", "ID2"),
Arrays.asList("TYPE2", "ID3")
));

Map<String, List<String>> result = operationsApiService.readExclusions();

assertEquals(2, result.size());
assertEquals(Arrays.asList("ID1", "ID2"), result.get("TYPE1"));
assertEquals(Collections.singletonList("ID3"), result.get("TYPE2"));
}
}

@ParameterizedTest
@ValueSource(strings = {""})
@NullSource
void givenGetListRubriques_whenQueryReturnNothing_thenResponseIsEmpty(String id) {
when(mockSparqlUtils.executeSparqlQuery(Mockito.anyString())).thenReturn("");
List<Rubrique> liste = operationsApiService.getListRubriques(id);
Assertions.assertEquals(new ArrayList<Rubrique>(),liste);
assertEquals(new ArrayList<Rubrique>(),liste);
}


Expand All @@ -67,7 +105,7 @@ void givenGetListRubriques_whenQueryOk_thenResponseIsOk() {
.thenReturn(MOCK_CSV_GET_DOCUMENTS_EMPTY)//get documents fr
.thenReturn(MOCK_CSV_GET_DOCUMENTS_EMPTY);//get documents en
List<Rubrique> liste = operationsApiService.getListRubriques("1774");
Assertions.assertEquals(EXPECTED_RUBRICS,liste);
assertEquals(EXPECTED_RUBRICS,liste);
}


Expand Down