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

feat: save document updated data as a string #811

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -543,8 +543,9 @@ private void writeRdfDocument(Document document, IRI docUri) throws RmesExceptio
RdfUtils.addTripleString(docUri, DC.LANGUAGE, document.getLangue(), model, graph);
}
if (StringUtils.isNotEmpty(document.getDateMiseAJour())) {
logger.debug("Add to {} PAV.LASTREFRESHEDON {}", docUri, document.getDateMiseAJour());
RdfUtils.addTripleDateTime(docUri, PAV.LASTREFRESHEDON, document.getDateMiseAJour(), model, graph);
var dateMiseAJour = document.getDateMiseAJour();
logger.debug("Add to {} PAV.LASTREFRESHEDON {}", docUri, dateMiseAJour);
RdfUtils.addTripleString(docUri, PAV.LASTREFRESHEDON, dateMiseAJour, model, graph);
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
}
repoGestion.loadSimpleObject(docUri, model);
}
Expand Down Expand Up @@ -572,7 +573,7 @@ private String getIdFromUri(String uri) {

private String createFileUrl(String name) throws RmesException {
Path gestionStorageFolder=Path.of(config.getDocumentsStorageGestion());
if (! filesOperations.dirExists(gestionStorageFolder)){
if (!filesOperations.dirExists(gestionStorageFolder)){
throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Storage folder not found",
"config.DOCUMENTS_STORAGE");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import fr.insee.rmes.utils.DateUtils;
import fr.insee.rmes.utils.XhtmlToMarkdownUtils;
import org.eclipse.rdf4j.model.*;
import org.eclipse.rdf4j.model.impl.SimpleIRI;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.XSD;
Expand Down Expand Up @@ -222,13 +221,9 @@ public static IRI toURI(String string) {
}

public static String toString(IRI iri) {
return ((SimpleIRI)iri).toString();
return iri.toString();
}

/**
* Utils to create triples if data exist
*/


public static void addTripleString(IRI objectURI, IRI predicat, String value, Model model, Resource graph) {
if (value != null && !value.isEmpty()) {
model.add(objectURI, predicat, RdfUtils.setLiteralString(value), graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -126,9 +128,7 @@ void addCodeFromCodeList() throws RmesException {

assertEquals("code", result);
Assertions.assertEquals("[(http://lastCodeUriSegment/code, http://www.w3.org/2004/02/skos/core#notation, \"code\", http://codesListGraph) [http://codesListGraph]]", model.getValue().toString());

}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package fr.insee.rmes.bauhaus_services.operations.documentations.documents;

import fr.insee.rmes.Stubber;
import fr.insee.rmes.bauhaus_services.FilesOperations;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryGestion;
import fr.insee.rmes.config.Config;
import fr.insee.rmes.config.ConfigStub;
import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService;
import fr.insee.rmes.exceptions.RmesException;
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 @@ -14,28 +22,45 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
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 static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class DocumentsUtilsTest {

@Mock
RepositoryGestion repositoryGestion;

DocumentsUtils documentsUtils=new DocumentsUtils(null, null);
@Mock
Config config;

@Mock
StampsRestrictionsService stampsRestrictionsService;

@Mock
FilesOperations filesOperations;



@BeforeAll
static void initGenericQueries(){
static void initGenericQueries() {
GenericQueries.setConfig(new ConfigStub());
}

@Test
void shouldReturnAListOfDocuments() throws RmesException {
DocumentsUtils documentsUtils = new DocumentsUtils(null, filesOperations);

JSONObject document = new JSONObject().put("id", "1");
JSONArray array = new JSONArray();
array.put(document);
Expand All @@ -57,9 +82,11 @@ void shouldReturnAListOfDocuments() throws RmesException {
"'...', '{\"code\":362,\"details\":\"...\",\"message\":\"FileName contains forbidden characters, please use only Letters, Numbers, Underscores and Hyphens\"}'",
"'-', '{\"code\":362,\"details\":\"-\",\"message\":\"FileName contains forbidden characters, please use only Letters, Numbers, Underscores and Hyphens\"}'",
"'-.', '{\"code\":362,\"details\":\"-.\",\"message\":\"FileName contains forbidden characters, please use only Letters, Numbers, Underscores and Hyphens\"}'",
})
})
@ParameterizedTest
void test_checkFileNameValidity_throwsWhenNameInvalid(String fileName, String exceptionDetail) {
DocumentsUtils documentsUtils = new DocumentsUtils(null, filesOperations);

RmesNotAcceptableException exception = assertThrows(RmesNotAcceptableException.class, () -> {
documentsUtils.checkFileNameValidity(fileName);
});
Expand All @@ -69,9 +96,57 @@ void test_checkFileNameValidity_throwsWhenNameInvalid(String fileName, String ex

@Test
void testFileNameIsValid() {
DocumentsUtils documentsUtils = new DocumentsUtils(null, filesOperations);

String fileName = "valid_file-name.txt";
assertDoesNotThrow(() -> {
documentsUtils.checkFileNameValidity(fileName);
});
}

@Test
void testIfDateMiseAJourSavedAsString() throws RmesException {
DocumentsUtils documentsUtils = new DocumentsUtils(null, filesOperations);

Stubber.forRdfService(documentsUtils).injectStampsRestrictionsService(stampsRestrictionsService);
Stubber.forRdfService(documentsUtils).injectRepoGestion(repositoryGestion);
Stubber.forRdfService(documentsUtils).injectConfig(config);

when(config.getDocumentsStorageGestion()).thenReturn("/path/");
when(stampsRestrictionsService.canManageDocumentsAndLinks()).thenReturn(true);
when(repositoryGestion.getResponseAsBoolean(any())).thenReturn(false);
when(repositoryGestion.getResponseAsObject(any())).thenReturn(new JSONObject());
when(filesOperations.dirExists(any(Path.class))).thenReturn(true);

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

String documentIRIString = "http://document/1";
SimpleValueFactory valueFactory = SimpleValueFactory.getInstance();
IRI documentIRI = valueFactory.createIRI(documentIRIString);
IRI graph = valueFactory.createIRI("http://documents/graph");

try (MockedStatic<RdfUtils> rdfUtilsMockedStatic = Mockito.mockStatic(RdfUtils.class);
MockedStatic<DocumentsQueries> documentQueriesMockedStatic = Mockito.mockStatic(DocumentsQueries.class)
) {
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.documentsGraph()).thenReturn(graph);
rdfUtilsMockedStatic.when(() -> RdfUtils.toString(any())).thenReturn(documentIRIString);
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://documents/graph) [http://documents/graph]]", model.getValue().toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package fr.insee.rmes.bauhaus_services.rdf_utils;

import fr.insee.rmes.config.Config;
import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService;

public record RdfServiceStubber(RdfService rdfService) {

public void injectRepoGestion(RepositoryGestion repoGestion) {
rdfService.repoGestion=repoGestion;
}

public void injectStampsRestrictionsService(StampsRestrictionsService stampsRestrictionsService) {
rdfService.stampsRestrictionsService=stampsRestrictionsService;
}

public void injectConfig(Config config) {
rdfService.config=config;
}
}