From 044845e183ed6c8e3181d2af11e7376b054596e4 Mon Sep 17 00:00:00 2001
From: Miroslav Blasko <blcham@gmail.com>
Date: Thu, 15 Aug 2024 09:42:45 +0200
Subject: [PATCH] x

---
 .../cz/cvut/spin/SpinIntegrationTest.java     | 92 ++++++++++++++-----
 1 file changed, 69 insertions(+), 23 deletions(-)

diff --git a/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java b/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java
index bed44fc8..5c0b78c5 100644
--- a/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java
+++ b/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java
@@ -1,10 +1,10 @@
- package cz.cvut.spin;
+package cz.cvut.spin;
 
-import cz.cvut.spipes.engine.PipelineFactory;
 import org.apache.jena.ontology.OntModelSpec;
 import org.apache.jena.query.*;
 import org.apache.jena.rdf.model.*;
 import org.apache.jena.util.FileUtils;
+import org.jetbrains.annotations.NotNull;
 import org.junit.jupiter.api.Test;
 import org.topbraid.spin.model.SPINFactory;
 import org.topbraid.spin.system.SPINModuleRegistry;
@@ -12,31 +12,63 @@
 import org.topbraid.spin.vocabulary.SP;
 
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class SpinIntegrationTest {
 
-
     @Test
-    public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEncodingException {
+    public void executeCustomSPINRDFFunctionWithinQuery() {
+        // load custom function definition from RDF
+        Model funcDefModel = getCustomSPINRDFFunctionModel();
 
-        // load custom function definition
-        Model funcDefModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
-        // Model funcDefModel = ModelFactory.createDefaultModel(); // TODO this does not work
+        // register custom function
+        //SPINModuleRegistry.get().init();
+        SPINModuleRegistry.get().registerAll(funcDefModel, null);
 
-        final InputStream funcDefIs = this.getClass().getResourceAsStream("/spin/spin-function.spin.ttl");
+        String repositoryUrl = "http://repository.org";
+        String graphId = "http://graphid.org";
+
+        String queryString = String.format("""
+            PREFIX kbss-spif: <http://onto.fel.cvut.cz/ontologies/lib/spin-function/>
+            SELECT ?sparqlServiceUrl 
+            WHERE {
+            BIND(kbss-spif:create-sparql-service-url(
+                "%s", 
+                "%s"
+            ) AS ?sparqlServiceUrl)
+            }
+        """, repositoryUrl, graphId);
 
-        funcDefModel.read(funcDefIs, null, FileUtils.langTurtle);
+        Model model = ModelFactory.createDefaultModel();
+
+        Query query = QueryFactory.create(queryString);
+
+        QueryExecution qexec = QueryExecutionFactory.create(query, model);
+        ResultSet results = qexec.execSelect();
+
+        assertTrue(results.hasNext(), "No results found");
+
+        QuerySolution soln = results.nextSolution();
+        assertEquals(
+            soln.getResource("sparqlServiceUrl").getURI(),
+            constructServiceUrl(repositoryUrl, graphId)
+        );
+
+    }
+
+    @Test
+    public void executeSPINExpressionWithCustomSPINRDFFunction()  {
+        // load custom function definition from RDF
+        Model funcDefModel = getCustomSPINRDFFunctionModel();
 
         // register custom function
         //SPINModuleRegistry.get().init();
         SPINModuleRegistry.get().registerAll(funcDefModel, null);
 
-
         // load custom function call
         Model funcCallModel = ModelFactory.createDefaultModel();
 
@@ -51,20 +83,17 @@ public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEnco
         // evaluate SPIN expression
         QuerySolutionMap bindings = new QuerySolutionMap();
         String repositoryUrl = "http://repository.org";
-        String reportGraphId = "http://graphid.org";
-        bindings.add("repositoryUrl", ResourceFactory.createPlainLiteral(repositoryUrl));
-        bindings.add("reportGraphId", ResourceFactory.createPlainLiteral(reportGraphId));
-
+        String graphId = "http://graphid.org";
+        bindings.add("sparqlEndpoint", ResourceFactory.createPlainLiteral(repositoryUrl));
+        bindings.add("defaultGraphUri", ResourceFactory.createPlainLiteral(graphId));
 
         RDFNode node = SPINExpressions.evaluate(callExpr, callExpr.getModel(), bindings); //TODO resource.getModel() should be part o context
 
-
-        assertEquals(node.toString(), repositoryUrl + "?default-graph-uri=" + URLEncoder.encode(reportGraphId, StandardCharsets.UTF_8) );
+        assertEquals(node.toString(), constructServiceUrl(repositoryUrl, graphId));
     }
 
     @Test
     public void executeSPINQueryWithCustomJavaFunction() {
-        PipelineFactory pipelineFactory = new PipelineFactory();
 
         String queryString = """
                 PREFIX kbss-timef: <http://onto.fel.cvut.cz/ontologies/lib/function/time/>
@@ -78,13 +107,30 @@ public void executeSPINQueryWithCustomJavaFunction() {
 
         Query query = QueryFactory.create(queryString);
 
-
         QueryExecution qexec = QueryExecutionFactory.create(query, model);
         ResultSet results = qexec.execSelect();
 
-        if (results.hasNext()) {
-            QuerySolution soln = results.nextSolution();
-            assertEquals(soln.getLiteral("nextDay").getString(), "2022-01-02");
-        }
+        assertTrue(results.hasNext(), "No results found");
+
+        QuerySolution soln = results.nextSolution();
+        assertEquals(soln.getLiteral("nextDay").getString(), "2022-01-02");
+    }
+
+    @NotNull
+    private Model getCustomSPINRDFFunctionModel() {
+        // load custom function definition
+        Model funcDefModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
+        // Model funcDefModel = ModelFactory.createDefaultModel(); // TODO this does not work
+
+        final InputStream funcDefIs = this.getClass().getResourceAsStream("/spin/spin-function.spin.ttl");
+
+        funcDefModel.read(funcDefIs, null, FileUtils.langTurtle);
+
+        return funcDefModel;
+    }
+
+    @NotNull
+    private String constructServiceUrl(String repositoryUrl, String graphId) {
+        return String.format("%s?default-graph-uri=%s", repositoryUrl, URLEncoder.encode(graphId, StandardCharsets.UTF_8));
     }
 }