From bf15472705442875c351c4dfc71b041885b6514e Mon Sep 17 00:00:00 2001 From: Miroslav Blasko Date: Wed, 2 Jun 2021 12:28:26 +0200 Subject: [PATCH] [New] Test OntoDocManager document update --- .../spipes/manager/OntoDocManagerTest.java | 68 ++++++++++++++++++- .../manager/file-update/directly-imported.ttl | 17 +++++ .../file-update/indirectly-imported.ttl | 12 ++++ .../manager/file-update/loading-test.ttl | 14 ++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 s-pipes-core/src/test/resources/manager/file-update/directly-imported.ttl create mode 100644 s-pipes-core/src/test/resources/manager/file-update/indirectly-imported.ttl create mode 100644 s-pipes-core/src/test/resources/manager/file-update/loading-test.ttl diff --git a/s-pipes-core/src/test/java/cz/cvut/spipes/manager/OntoDocManagerTest.java b/s-pipes-core/src/test/java/cz/cvut/spipes/manager/OntoDocManagerTest.java index b3fb0d74d..264129f56 100644 --- a/s-pipes-core/src/test/java/cz/cvut/spipes/manager/OntoDocManagerTest.java +++ b/s-pipes-core/src/test/java/cz/cvut/spipes/manager/OntoDocManagerTest.java @@ -1,22 +1,26 @@ package cz.cvut.spipes.manager; import cz.cvut.spipes.TestConstants; +import org.apache.jena.ontology.OntClass; import org.apache.jena.ontology.OntDocumentManager; import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModelSpec; import org.apache.jena.util.LocationMapper; +import org.apache.jena.util.ResourceUtils; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.nio.file.Path; +import java.util.Arrays; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; public class OntoDocManagerTest { - static Path managerDirPath = TestConstants.TEST_RESOURCES_DIR_PATH.resolve("manager").toAbsolutePath(); @Test @@ -101,6 +105,68 @@ public void registerDocumentsToLoadImportClosure2() { assertEquals(3, loadedClassNames.size()); } + @Test //TODO move to a "jena experiment project" + public void jenaOntDocumentManagerLoadsDocumentChanges() throws IOException { + + OntDocumentManager docManager = OntDocumentManager.getInstance(); + String testOntologyPrefix = "http://onto.fel.cvut.cz/ontologies/test/"; + + Path sourceDirPath = managerDirPath.resolve("file-update"); + List ontologyNames = Arrays.asList( + "directly-imported", + "indirectly-imported", + "loading-test" + ); + + String loadingTestOntologyUri = testOntologyPrefix + "loading-test-ontology"; + String indirectlyImportedOntologyUri = testOntologyPrefix + "indirectly-imported-ontology"; + + ontologyNames.forEach( on -> { + docManager.addAltEntry( + testOntologyPrefix + on + "-ontology", + String.valueOf(sourceDirPath.resolve(on + ".ttl").toUri().toURL()) + ); + }); + + OntModel model = docManager.getOntology(loadingTestOntologyUri, OntModelSpec.OWL_MEM); + + assertTrue(containClassWithLocalName(model, "directly-imported-class")); + assertTrue(containClassWithLocalName(model,"indirectly-imported-class")); + assertTrue(containClassWithLocalName(model,"loading-test-class")); + + assertEquals(3, getOntologyClassesLocalNames(model).size()); + + OntModel indirectlyImportedModel = docManager.getOntology(indirectlyImportedOntologyUri, OntModelSpec.OWL_MEM); + + OntClass resource = indirectlyImportedModel + .listClasses() + .filterKeep(r -> r.isResource() && r.asResource().getLocalName().equals("indirectly-imported-class")) + .next(); + ResourceUtils.renameResource(resource, resource.getNameSpace() + "new-indirectly-imported-class"); + + // check locally modified model + assertFalse(containClassWithLocalName(indirectlyImportedModel,"indirectly-imported-class")); + assertTrue(containClassWithLocalName(indirectlyImportedModel,"new-indirectly-imported-class")); + assertEquals(1, getOntologyClassesLocalNames(indirectlyImportedModel).size()); + + // update the model globally + docManager.getFileManager().addCacheModel(indirectlyImportedOntologyUri, indirectlyImportedModel); + + // check new model is updated + assertTrue(containClassWithLocalName(model, "directly-imported-class")); + assertTrue(containClassWithLocalName(model,"new-indirectly-imported-class")); + assertTrue(containClassWithLocalName(model,"loading-test-class")); + assertEquals(3, getOntologyClassesLocalNames(model).size()); + } + + private List getOntologyClassesLocalNames(OntModel model) { + return model.listClasses().mapWith(c -> c.asResource().getLocalName()).toList(); + } + + private boolean containClassWithLocalName(OntModel model, String localName) { + return getOntologyClassesLocalNames(model).contains(localName); + } + private int getLocationMapperEntriesCount(OntologyDocumentManager ontoDocManager) { diff --git a/s-pipes-core/src/test/resources/manager/file-update/directly-imported.ttl b/s-pipes-core/src/test/resources/manager/file-update/directly-imported.ttl new file mode 100644 index 000000000..5d90a819d --- /dev/null +++ b/s-pipes-core/src/test/resources/manager/file-update/directly-imported.ttl @@ -0,0 +1,17 @@ +@prefix : . +@prefix test: . +@prefix owl: . +@prefix rdf: . +@prefix xml: . +@prefix xsd: . +@prefix rdfs: . +@base . + +test:directly-imported-ontology rdf:type owl:Ontology ; + owl:imports test:indirectly-imported-ontology ; +. + +:directly-imported-class rdf:type owl:Class . + + +### Generated by the OWL API (version 4.2.5.20160517-0735) https://github.com/owlcs/owlapi diff --git a/s-pipes-core/src/test/resources/manager/file-update/indirectly-imported.ttl b/s-pipes-core/src/test/resources/manager/file-update/indirectly-imported.ttl new file mode 100644 index 000000000..deffd1ff1 --- /dev/null +++ b/s-pipes-core/src/test/resources/manager/file-update/indirectly-imported.ttl @@ -0,0 +1,12 @@ +@prefix : . +@prefix test: . +@prefix owl: . +@prefix rdf: . +@prefix xml: . +@prefix xsd: . +@prefix rdfs: . +@base . + +test:indirectly-imported-ontology rdf:type owl:Ontology . + +:indirectly-imported-class rdf:type owl:Class . \ No newline at end of file diff --git a/s-pipes-core/src/test/resources/manager/file-update/loading-test.ttl b/s-pipes-core/src/test/resources/manager/file-update/loading-test.ttl new file mode 100644 index 000000000..b980c0e1e --- /dev/null +++ b/s-pipes-core/src/test/resources/manager/file-update/loading-test.ttl @@ -0,0 +1,14 @@ +@prefix test: . +@prefix : . +@prefix owl: . +@prefix rdf: . +@prefix xml: . +@prefix xsd: . +@prefix rdfs: . +@base . + +test:loading-test-ontology rdf:type owl:Ontology ; + owl:imports test:directly-imported-ontology ; +. + +:loading-test-class rdf:type owl:Class .