From 4a382a6c681ff7792b2bed6d9b794dd8297c17f0 Mon Sep 17 00:00:00 2001 From: "Nazarov, Rodion" Date: Fri, 4 Aug 2023 14:39:09 +0200 Subject: [PATCH 1/4] [Upd] Add assertion in the RetrievePrefixesModule to compare actual and expected models --- .../modules/RetrievePrefixesModuleTest.java | 42 +++++++++++++++---- .../retrieve-prefixes/expected-output.ttl | 39 +++++++++++++++++ 2 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl diff --git a/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java b/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java index a7bcc42d..8de7c2af 100644 --- a/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java +++ b/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java @@ -3,11 +3,12 @@ import cz.cvut.spipes.engine.ExecutionContext; import cz.cvut.spipes.engine.ExecutionContextFactory; import cz.cvut.spipes.manager.OntologyDocumentManager; +import cz.cvut.spipes.util.JenaUtils; import org.apache.jena.ontology.OntDocumentManager; import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModelSpec; +import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.shared.PrefixMapping; import org.apache.jena.util.FileManager; import org.apache.jena.util.FileUtils; import org.apache.jena.vocabulary.OWL; @@ -17,11 +18,17 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.LinkedHashMap; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.doReturn; @@ -31,9 +38,11 @@ class RetrievePrefixesModuleTest { @Mock OntologyDocumentManager ontoDocManager; + private static final Logger LOG = LoggerFactory.getLogger(RetrievePrefixesModuleTest.class); + private final static String[] ontologyResourcePaths = new String[]{ - "/manager/import-closure/indirect-import.ttl", - "/manager/import-closure/direct-import.ttl" + "/manager/import-closure/indirect-import.ttl", + "/manager/import-closure/direct-import.ttl" }; HashMap uri2ontModel; @@ -45,14 +54,14 @@ void setUp() { OntModel model = loadOntModel(ontologyPath); String iri = getOntologyIri(model); uri2ontModel.put( - iri, - model + iri, + model ); } } @Test - void testExecuteSelf() { + void executeSelfReturnPrefixes() throws URISyntaxException { given(ontoDocManager.getRegisteredOntologyUris()).willReturn(uri2ontModel.keySet()); uri2ontModel.forEach((key, value) -> { doReturn(value).when(ontoDocManager).getOntology(key); @@ -65,7 +74,12 @@ void testExecuteSelf() { retrievePrefixesModule.setInputContext(inputExecutionContext); ExecutionContext outputExecutionContext = retrievePrefixesModule.executeSelf(); - outputExecutionContext.getDefaultModel().write(System.out, FileUtils.langTurtle, null); + Model actualModel = outputExecutionContext.getDefaultModel(); + + Model expectedModel = ModelFactory.createDefaultModel() + .read(getFilePath("module/retrieve-prefixes/expected-output.ttl").toString()); + + assertIsomorphic(actualModel, expectedModel); } @@ -92,4 +106,18 @@ private static OntModel loadOntModel(InputStream inputStream) { dm.loadImports(ontModel); return ontModel; } + + void assertIsomorphic(Model actualModel, Model expectedModel){ + if (! actualModel.isIsomorphicWith(expectedModel)) { + LOG.debug("Saving actual model ... "); + JenaUtils.saveModelToTemporaryFile(actualModel); + LOG.debug("Saving expected model ... "); + JenaUtils.saveModelToTemporaryFile(expectedModel); + fail("Actual model is not isomorphic with expected model (see additional information above)."); + } + } + + public Path getFilePath(String fileName) throws URISyntaxException { + return Paths.get(getClass().getResource("/" + fileName).toURI()); + } } \ No newline at end of file diff --git a/s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl b/s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl new file mode 100644 index 00000000..aac67747 --- /dev/null +++ b/s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl @@ -0,0 +1,39 @@ +@prefix ja: . + + ja:prefixMapping [ a ja:SinglePrefixMapping ; + ja:namespace "http://onto.fel.cvut.cz/ontologies/test/loading-test#" ; + ja:prefix "" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/2002/07/owl#" ; + ja:prefix "owl" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + ja:prefix "rdf" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/XML/1998/namespace" ; + ja:prefix "xml" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/2001/XMLSchema#" ; + ja:prefix "xsd" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/2000/01/rdf-schema#" ; + ja:prefix "rdfs" ] . + + ja:prefixMapping [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/XML/1998/namespace" ; + ja:prefix "xml" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/2001/XMLSchema#" ; + ja:prefix "xsd" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/2000/01/rdf-schema#" ; + ja:prefix "rdfs" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://onto.fel.cvut.cz/ontologies/test/loading-test#" ; + ja:prefix "" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/2002/07/owl#" ; + ja:prefix "owl" ], + [ a ja:SinglePrefixMapping ; + ja:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + ja:prefix "rdf" ] . \ No newline at end of file From ad7f15ae9a179faf62aaef297316c924b848aac4 Mon Sep 17 00:00:00 2001 From: "Nazarov, Rodion" Date: Fri, 4 Aug 2023 15:29:18 +0200 Subject: [PATCH 2/4] [Upd] Update documentation of RetrievePrefixesModule --- .../modules/RetrievePrefixesModule.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java index e077f5c6..819c04d1 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java @@ -13,6 +13,33 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Module retrieves metadata of all prefixes from scripts. + * Metadata is represented as RDF triple: + * ja:PrefixMapping + * [ a ja:SinglePrefixMapping ; + * ja:namespace namespaceURI ; + * ja:prefix prefix + * ] ; + * Example script content: + * @prefix : . + * @prefix owl: . + * + * + * :my-ontology a owl:Ontology . + * + * Example output: + * @prefix : . + * @prefix ja: + * :my-ontology + * ja:prefixMapping + * [ a ja:SinglePrefixMapping ; + * ja:namespace + * "http://www.w3.org/2002/07/owl#" ; + * ja:prefix + * "owl" + * ] ; + */ public class RetrievePrefixesModule extends AbstractModule { private static final Logger LOG = LoggerFactory.getLogger(RetrievePrefixesModule.class.getName()); @@ -38,13 +65,13 @@ ExecutionContext executeSelf() { outputModel.add(ontology, JA.prefixMapping, singlePrefixMapping); outputModel.add( - singlePrefixMapping, RDF.type, JA.SinglePrefixMapping + singlePrefixMapping, RDF.type, JA.SinglePrefixMapping ); outputModel.add( - singlePrefixMapping, JA.prefix, key + singlePrefixMapping, JA.prefix, key ); outputModel.add( - singlePrefixMapping, JA.namespace, value + singlePrefixMapping, JA.namespace, value ); }); } From ea059e36d16b049cd3137886b9b9ab0a484b6c2d Mon Sep 17 00:00:00 2001 From: Miroslav Blasko Date: Fri, 4 Aug 2023 23:19:03 +0200 Subject: [PATCH 3/4] [Upd] Refactor to remove duplicit code --- .../java/cz/cvut/spipes/util/JenaUtils.java | 23 ++++++++------- .../modules/RetrievePrefixesModuleTest.java | 13 +-------- .../spipes/modules/TabularModuleTest.java | 16 +++------- s-pipes-test/pom.xml | 5 +++- .../cz/cvut/spipes/test/JenaTestUtils.java | 29 +++++++++++++++++++ 5 files changed, 50 insertions(+), 36 deletions(-) diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java b/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java index a02f34cb..13c5fc77 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java @@ -1,14 +1,5 @@ package cz.cvut.spipes.util; -import java.io.ByteArrayInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.stream.Stream; import org.apache.commons.codec.digest.DigestUtils; import org.apache.jena.graph.compose.MultiUnion; import org.apache.jena.rdf.model.Model; @@ -18,9 +9,19 @@ import org.apache.jena.util.FileUtils; import org.apache.jena.vocabulary.OWL; import org.apache.jena.vocabulary.RDF; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Stream; + public class JenaUtils { private static Logger LOG = LoggerFactory.getLogger(JenaUtils.class); @@ -105,11 +106,11 @@ public static Model createUnion(Model... model) { return outputModel; } - public static void saveModelToTemporaryFile(Model model) { + public static void saveModelToTemporaryFile(@NotNull Model model) { try { Path file = Files.createTempFile("model-output-", ".ttl"); LOG.debug("Saving model to temporary file " + file.toString() + " ..."); - model.write(new FileOutputStream(file.toFile()), FileUtils.langTurtle); + model.write(Files.newOutputStream(file.toFile().toPath()), FileUtils.langTurtle); } catch (IOException e) { e.printStackTrace(); } diff --git a/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java b/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java index 8de7c2af..2e71464c 100644 --- a/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java +++ b/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java @@ -3,7 +3,6 @@ import cz.cvut.spipes.engine.ExecutionContext; import cz.cvut.spipes.engine.ExecutionContextFactory; import cz.cvut.spipes.manager.OntologyDocumentManager; -import cz.cvut.spipes.util.JenaUtils; import org.apache.jena.ontology.OntDocumentManager; import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModelSpec; @@ -28,7 +27,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; -import static org.junit.jupiter.api.Assertions.fail; +import static cz.cvut.spipes.test.JenaTestUtils.assertIsomorphic; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.doReturn; @@ -82,7 +81,6 @@ void executeSelfReturnPrefixes() throws URISyntaxException { assertIsomorphic(actualModel, expectedModel); } - private static String getOntologyIri(OntModel model) { return model.listResourcesWithProperty(RDF.type, OWL.Ontology).nextResource().toString(); } @@ -107,15 +105,6 @@ private static OntModel loadOntModel(InputStream inputStream) { return ontModel; } - void assertIsomorphic(Model actualModel, Model expectedModel){ - if (! actualModel.isIsomorphicWith(expectedModel)) { - LOG.debug("Saving actual model ... "); - JenaUtils.saveModelToTemporaryFile(actualModel); - LOG.debug("Saving expected model ... "); - JenaUtils.saveModelToTemporaryFile(expectedModel); - fail("Actual model is not isomorphic with expected model (see additional information above)."); - } - } public Path getFilePath(String fileName) throws URISyntaxException { return Paths.get(getClass().getResource("/" + fileName).toURI()); diff --git a/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java b/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java index 2ca2948e..11208334 100644 --- a/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java +++ b/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java @@ -7,7 +7,6 @@ import cz.cvut.spipes.exception.ResourceNotUniqueException; import cz.cvut.spipes.modules.exception.TableSchemaException; import cz.cvut.spipes.test.JenaTestUtils; -import cz.cvut.spipes.util.JenaUtils; import cz.cvut.spipes.util.StreamResourceUtils; import org.apache.jena.rdf.model.*; import org.junit.jupiter.api.BeforeEach; @@ -18,14 +17,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.*; +import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static cz.cvut.spipes.test.JenaTestUtils.assertIsomorphic; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; class TabularModuleTest extends AbstractModuleTestHelper { @@ -228,15 +229,6 @@ void executeSelfWithHTMLFileInput() throws URISyntaxException, IOException { } } - void assertIsomorphic(Model actualModel, Model expectedModel){ - if (! actualModel.isIsomorphicWith(expectedModel)) { - LOG.debug("Saving actual model ... "); - JenaUtils.saveModelToTemporaryFile(actualModel); - LOG.debug("Saving expected model ... "); - JenaUtils.saveModelToTemporaryFile(expectedModel); - fail("Actual model is not isomorphic with expected model (see additional information above)."); - } - } @Override public String getModuleName() { diff --git a/s-pipes-test/pom.xml b/s-pipes-test/pom.xml index f8040869..daa4a803 100644 --- a/s-pipes-test/pom.xml +++ b/s-pipes-test/pom.xml @@ -29,7 +29,10 @@ org.apache.jena jena-core - ${org.apache.jena} + + + org.junit.jupiter + junit-jupiter-api \ No newline at end of file diff --git a/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java b/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java index 23044acc..c8e6b58e 100644 --- a/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java +++ b/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java @@ -7,11 +7,20 @@ import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.util.FileManager; import org.apache.jena.util.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.fail; public class JenaTestUtils { + private static Logger LOG = LoggerFactory.getLogger(JenaTestUtils.class); + public static void mapLocalSPipesDefinitionFiles() { OntDocumentManager dm = OntDocumentManager.getInstance(); dm.setFileManager(FileManager.getInternal()); @@ -52,4 +61,24 @@ public static Model laodModelFromResource(String path) { return model; } + + public static void assertIsomorphic(Model actualModel, Model expectedModel) { + if (!actualModel.isIsomorphicWith(expectedModel)) { + LOG.debug("Saving actual model ... "); + saveModelToTemporaryFile(actualModel); + LOG.debug("Saving expected model ... "); + saveModelToTemporaryFile(expectedModel); + fail("Actual model is not isomorphic with expected model (see additional information above)."); + } + } + + private static void saveModelToTemporaryFile(Model model) { + try { + Path file = Files.createTempFile("model-output-", ".ttl"); + LOG.debug("Saving model to temporary file " + file.toString() + " ..."); + model.write(Files.newOutputStream(file.toFile().toPath()), FileUtils.langTurtle); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 1f97020df4e77bf73f094ee178112f18841bffbd Mon Sep 17 00:00:00 2001 From: Rodion Nazarov <130922166+rodionnv@users.noreply.github.com> Date: Sat, 5 Aug 2023 12:04:33 +0200 Subject: [PATCH 4/4] [Upd] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Miroslav Blaško --- .../modules/RetrievePrefixesModule.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java index 819c04d1..2f8a018b 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java @@ -14,31 +14,38 @@ import org.slf4j.LoggerFactory; /** - * Module retrieves metadata of all prefixes from scripts. - * Metadata is represented as RDF triple: - * ja:PrefixMapping - * [ a ja:SinglePrefixMapping ; - * ja:namespace namespaceURI ; - * ja:prefix prefix - * ] ; - * Example script content: + * Module returns prefix mappings of all loaded scripts. + * Individual mappings are represented by following graph pattern: + * + * @prefix ja: . + * + * ?ontology ja:prefixMapping + * [ a ja:SinglePrefixMapping ; + * ja:namespace ?namespaceIRI ; + * ja:prefix ?prefix ] + * . + * + * As an example let's assume we loaded only one script: + * * @prefix : . * @prefix owl: . * - * * :my-ontology a owl:Ontology . * - * Example output: + * + * The output of this module for the example script would be: + * * @prefix : . - * @prefix ja: - * :my-ontology - * ja:prefixMapping - * [ a ja:SinglePrefixMapping ; - * ja:namespace - * "http://www.w3.org/2002/07/owl#" ; - * ja:prefix - * "owl" - * ] ; + * @prefix ja: . + * + * :my-ontology ja:prefixMapping + * [ a ja:SinglePrefixMapping ; + * ja:namespace "http://example.org/" ; + * ja:prefix "" ], + * [ a ja:SinglePrefixMapping ; + * ja:namespace "http://www.w3.org/2002/07/owl#" ; + * ja:prefix "owl" ] +* . */ public class RetrievePrefixesModule extends AbstractModule { private static final Logger LOG = LoggerFactory.getLogger(RetrievePrefixesModule.class.getName());