From 620b245837612f690ea9448ecdbe1a1d7dbca1e0 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Wed, 20 Nov 2024 11:27:13 +0000 Subject: [PATCH 1/2] feat: save document updated data as a string --- .../documents/DocumentsUtils.java | 7 +- .../bauhaus_services/rdf_utils/RdfUtils.java | 9 +- .../code_list/CodeListServiceImplTest.java | 4 +- .../documents/DocumentsUtilsTest.java | 85 +++++++++++++++++-- .../rdf_utils/RdfServiceStubber.java | 11 +++ 5 files changed, 99 insertions(+), 17 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java index e813cead9..ebf3d3dc6 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java @@ -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); } repoGestion.loadSimpleObject(docUri, model); } @@ -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"); } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java index edfa25923..7e72db4d4 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java @@ -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; @@ -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); diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImplTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImplTest.java index e16a74fc2..0c4573b7b 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImplTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImplTest.java @@ -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.*; @@ -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 diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java index a3b4e56b3..040fefbb3 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java @@ -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; @@ -14,12 +22,18 @@ 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 { @@ -27,15 +41,26 @@ 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); @@ -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); }); @@ -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 rdfUtilsMockedStatic = Mockito.mockStatic(RdfUtils.class); + MockedStatic 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 = 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()); + } + } } \ No newline at end of file diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfServiceStubber.java b/src/test/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfServiceStubber.java index 6b1e416ea..8032d9ad8 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfServiceStubber.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfServiceStubber.java @@ -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; + } } From 25508fd3598fdcb09ec15faa6bfc8af56725c649 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Thu, 28 Nov 2024 18:50:22 +0000 Subject: [PATCH 2/2] fix: review --- .../operations/documentations/documents/DocumentsUtils.java | 2 +- .../documentations/documents/DocumentsUtilsTest.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java index ebf3d3dc6..548a7265f 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java @@ -545,7 +545,7 @@ private void writeRdfDocument(Document document, IRI docUri) throws RmesExceptio if (StringUtils.isNotEmpty(document.getDateMiseAJour())) { var dateMiseAJour = document.getDateMiseAJour(); logger.debug("Add to {} PAV.LASTREFRESHEDON {}", docUri, dateMiseAJour); - RdfUtils.addTripleString(docUri, PAV.LASTREFRESHEDON, dateMiseAJour, model, graph); + RdfUtils.addTripleDate(docUri, PAV.LASTREFRESHEDON, dateMiseAJour, model, graph); } repoGestion.loadSimpleObject(docUri, model); } diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java index 040fefbb3..71dd9c9c7 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtilsTest.java @@ -136,6 +136,8 @@ void testIfDateMiseAJourSavedAsString() throws RmesException { ) { 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.toString(any())).thenReturn(documentIRIString); rdfUtilsMockedStatic.when(() -> RdfUtils.toURI(any())).thenReturn(documentIRI); @@ -146,7 +148,7 @@ void testIfDateMiseAJourSavedAsString() throws RmesException { ArgumentCaptor 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()); + Assertions.assertEquals("[(http://document/1, http://purl.org/pav/lastRefreshedOn, \"2024-11-20\"^^, http://documents/graph) [http://documents/graph]]", model.getValue().toString()); } } } \ No newline at end of file