Skip to content

Commit

Permalink
test(fix test about minio errors)
Browse files Browse the repository at this point in the history
- Object Document with id attribute to be compliant with existing code
  • Loading branch information
Fabrice B committed Dec 11, 2024
1 parent 0d38ca7 commit d1f4d83
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static fr.insee.rmes.bauhaus_services.Constants.GOAL_COMITE_LABEL;
import static fr.insee.rmes.bauhaus_services.Constants.GOAL_RMES;
import static fr.insee.rmes.utils.StreamUtils.*;

@Component
public class DocumentationExport {
Expand Down Expand Up @@ -154,22 +152,23 @@ public ResponseEntity<Resource> exportAsZip(JSONObject sims, Map<String, String>
}
}

private Set<String> exportRubricsDocuments(JSONObject sims, Path directory) throws RmesException, IOException {
private Set<String> exportRubricsDocuments(JSONObject sims, Path directory) throws RmesException {
JSONArray documents = documentsUtils.getDocumentsUriAndUrlForSims(sims.getString("id"));
Set<String> missingDocuments = new HashSet<>();
Consumer<Void> unsafeStream = v -> JSONUtils.stream(documents)
.distinct()
.forEach(safeConsumer(document -> extractDocumentIfExists(directory, missingDocuments, document)));
executeAndThrow(unsafeStream);
Set<Document> documentsSet = JSONUtils.stream(documents)
.map(DocumentsUtils::buildDocumentFromJson)
.collect(Collectors.toSet());
for (Document document : documentsSet) {
extractDocumentIfExists(directory, missingDocuments, document);
}
return missingDocuments;
}

private void extractDocumentIfExists(Path directory, Set<String> missingDocuments, JSONObject documentJson) throws RmesException{
var document = documentsUtils.buildDocumentFromJson(documentJson);
private void extractDocumentIfExists(Path directory, Set<String> missingDocuments, Document document) throws RmesException{
String url = document.url();
logger.debug("Extracting document {}", url);
if (!documentsUtils.existsInStorage(document)) {
missingDocuments.add(documentJson.getString("id"));
missingDocuments.add(document.getId());
} else {
extractExistingDocument(directory, document, url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private void addJsonDocumentToObjectRubric(JSONObject rubric, DocumentationRubri

for (int i = 0; i < documents.length(); i++) {
JSONObject doc = documents.getJSONObject(i);
currentDoc = docUtils.buildDocumentFromJson(doc);
currentDoc = DocumentsUtils.buildDocumentFromJson(doc);
docs.add(currentDoc);
}
if (documentsWithRubricLang.equals(Constants.DOCUMENTS_LG1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ public void checkFileNameValidity(String fileName) throws RmesNotAcceptableExcep
logger.debug("The file name {} is valid", fileName);
}

public Document buildDocumentFromJson(JSONObject jsonDoc) {
public static Document buildDocumentFromJson(JSONObject jsonDoc) {
try {
return Deserializer.deserializeJSONObject(jsonDoc, Document.class);
} catch (RmesException e) {
Expand Down Expand Up @@ -669,11 +669,11 @@ public boolean existsInStorage(Document document) {

private record DocumentBuilder(Document document) {
public Document withId(String id, boolean isLink) {
return new Document(document.labelLg1(), document.labelLg2(), document.descriptionLg1(), document().descriptionLg2(), document.dateMiseAJour(), document.langue(), document.url(), uriFromId(id, isLink));
return new Document(document.labelLg1(), document.labelLg2(), document.descriptionLg1(), document().descriptionLg2(), document.dateMiseAJour(), document.langue(), document.url(), uriFromId(id, isLink), id);
}

public static Document emptyDocument() {
return new Document(null, null, null, null, null, null, null, null);
return new Document(null, null, null, null, null, null, null, null, null);
}

private String uriFromId(String id, boolean isLink) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfUtils;
import org.apache.commons.lang3.StringUtils;

import java.net.URI;
Expand All @@ -17,28 +16,21 @@ public record Document(String labelLg1,
@JsonProperty("lang")
String langue,
String url,
String uri) {
String uri,
// TODO remove this attribute
String id) {


// public Document(String id) {
// this(id, false);
// }
//
// public Document(String id, boolean isLink) {
// this(null, null, null, null, null, null, null, uriFromId(id, isLink));
// }

private static String uriFromId(String id, boolean isLink) {
return (isLink ? RdfUtils.linkIRI(id) :
RdfUtils.documentIRI(id)).toString();
}

public String getId() {
return StringUtils.substringAfter(uri, "/");
String idFromUri = StringUtils.substringAfter(uri, "/");
if (idFromUri != null) {
return idFromUri;
}
return id;
}

public Document withUrl(String url) {
return new Document(this.labelLg1, this.labelLg2, this.descriptionLg1, this.descriptionLg2, this.dateMiseAJour, this.langue, url, this.uri);
return new Document(this.labelLg1, this.labelLg2, this.descriptionLg1, this.descriptionLg2, this.dateMiseAJour, this.langue, url, this.uri, null);
}

public String documentFileName() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/fr/insee/rmes/utils/Deserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ private Deserializer() {

public static <T> T deserializeJsonString(String json, Class<T> target) throws RmesException {
try {
return mapper.readValue(json, target);
T t = mapper.readValue(json, target);
return t;
} catch (IOException e) {
throw new RmesException(HttpStatus.SC_BAD_REQUEST, "while deserializing "+json , e.getMessage());
}
Expand Down
40 changes: 0 additions & 40 deletions src/main/java/fr/insee/rmes/utils/StreamUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -71,6 +72,7 @@ void testExportAsZip_success() throws Exception {
document.put("id", "1");

when(documentsUtils.getDocumentsUriAndUrlForSims("sims123")).thenReturn(new JSONArray().put(document));
when(documentsUtils.existsInStorage(any())).thenReturn(false);
var sims = new JSONObject();
sims.put("id", "sims123");
sims.put("labelLg1", "simsLabel");
Expand All @@ -93,8 +95,8 @@ void testExportAsZip_success() throws Exception {

assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
assertEquals(response.getStatusCode(), HttpStatus.OK);
assertEquals(response.getHeaders().get("X-Missing-Documents").get(0), "1");
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().get("X-Missing-Documents").getFirst()).isEqualTo("1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.insee.rmes.Stubber;
import fr.insee.rmes.bauhaus_services.FilesOperations;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfServicesForRdfUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryGestion;
import fr.insee.rmes.bauhaus_services.rdf_utils.UriUtils;
import fr.insee.rmes.config.Config;
Expand All @@ -13,9 +12,7 @@
import fr.insee.rmes.exceptions.RmesNotAcceptableException;
import fr.insee.rmes.persistance.sparql_queries.GenericQueries;
import fr.insee.rmes.persistance.sparql_queries.operations.documentations.DocumentsQueries;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
Expand All @@ -24,12 +21,19 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;

import java.nio.file.Path;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -121,51 +125,37 @@ void testIfDateMiseAJourSavedAsString() throws RmesException {
when(filesOperations.dirExists(any(Path.class))).thenReturn(true);

var id = "1";
String date = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
var body = new JSONObject()
.put("id", "1")
.put("updatedDate", "2024-11-20").toString();
.put("updatedDate", date).toString();
var isLink = false;
var document = IOUtils.toInputStream("stream");
var name = "documentName";


SimpleValueFactory valueFactory = SimpleValueFactory.getInstance();
IRI documentIRI = valueFactory.createIRI(documentIRIString);
IRI graph = valueFactory.createIRI(DOCUMENTS_GRAPH);

try (MockedStatic<RdfUtils> rdfUtilsMockedStatic = Mockito.mockStatic(RdfUtils.class);
initRdfUtils();
try (
MockedStatic<DocumentsQueries> documentQueriesMockedStatic = Mockito.mockStatic(DocumentsQueries.class)
) {

initRdfUtils();

// rdfUtilsMockedStatic.when(() -> RdfUtils.setLiteralString(anyString())).thenCallRealMethod();
// rdfUtilsMockedStatic.when(() -> RdfUtils.addTripleString(eq(documentIRI), any(IRI.class), any(), any(Model.class), eq(graph))).thenCallRealMethod();
// rdfUtilsMockedStatic.when(() -> RdfUtils.setLiteralDate(any(String.class))).thenCallRealMethod();
// rdfUtilsMockedStatic.when(() -> RdfUtils.addTripleDate(eq(documentIRI), any(IRI.class), any(), any(Model.class), eq(graph))).thenCallRealMethod();
// rdfUtilsMockedStatic.when(RdfUtils::documentsGraph).thenReturn(graph);
// ? rdfUtilsMockedStatic.when(() -> RdfUtils.toURI(any())).thenReturn(documentIRI);
documentQueriesMockedStatic.when(() -> DocumentsQueries.checkLabelUnicity(eq("1"), anyString(), any())).thenReturn(documentIRIString);


documentsUtils.createDocument(id, body, isLink, document, name);
ArgumentCaptor<Model> model = ArgumentCaptor.forClass(Model.class);

verify(repositoryGestion, times(1)).loadSimpleObject(any(), model.capture());
Assertions.assertEquals("[(http://document/1, http://purl.org/pav/lastRefreshedOn, \"2024-11-20\"^^<http://www.w3.org/2001/XMLSchema#date>, http://documents/graph) [http://documents/graph]]", model.getValue().toString());
assertThat( model.getValue().toString()).asString().contains(date);
}
}

private void initRdfUtils() {

// ? rdfUtilsMockedStatic.when(() -> ArgumentMatchers.<IRI>any().toString()).thenReturn(documentIRIString);

RdfServicesForRdfUtils rdfServicesForRdfUtils = new RdfServicesForRdfUtils(new Config(){
@Override
public String getDocumentsGraph() {
return DOCUMENTS_GRAPH;
}
}, new UriUtils("","http://bauhaus/", null));
}, new UriUtils("","http://document/", s-> Optional.empty()));
rdfServicesForRdfUtils.initRdfUtils();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public DocumentsUtils myDocumentsUtils() {
return new DocumentsUtils(null, new FilesOperationStub()){
@Override
protected Document findDocumentById(String id){
return new Document(null, null, null, null, null, null, nomFichier, "http://id.insee.fr/1");
return new Document(null, null, null, null, null, null, nomFichier, "http://id.insee.fr/1", "1");
}
};
}
Expand Down

0 comments on commit d1f4d83

Please sign in to comment.