|
| 1 | +/* |
| 2 | + * Copyright 2025 Martynas Jusevičius <martynas@atomgraph.com>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.atomgraph.linkeddatahub.writer.function; |
| 17 | + |
| 18 | +import java.io.StringWriter; |
| 19 | +import java.net.URI; |
| 20 | +import net.sf.saxon.s9api.Processor; |
| 21 | +import net.sf.saxon.s9api.Serializer; |
| 22 | +import net.sf.saxon.s9api.XdmAtomicValue; |
| 23 | +import net.sf.saxon.s9api.XdmMap; |
| 24 | +import net.sf.saxon.s9api.XdmValue; |
| 25 | +import org.apache.jena.query.QueryParseException; |
| 26 | +import org.apache.jena.rdf.model.Model; |
| 27 | +import org.apache.jena.rdf.model.ModelFactory; |
| 28 | +import org.apache.jena.vocabulary.RDF; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import static org.junit.Assert.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Unit tests for the {@link Construct} Saxon extension function. |
| 35 | + * |
| 36 | + * @author Martynas Jusevičius {@literal <martynas@atomgraph.com>} |
| 37 | + */ |
| 38 | +public class ConstructTest |
| 39 | +{ |
| 40 | + |
| 41 | + public static final String CLASS_URI = "http://test/ontology#MyClass"; |
| 42 | + public static final String PROP_URI = "http://test/ontology#prop"; |
| 43 | + public static final String VALUE_URI = "http://test/value"; |
| 44 | + |
| 45 | + public static final String CONSTRUCT_QUERY = |
| 46 | + "CONSTRUCT { ?this <" + PROP_URI + "> <" + VALUE_URI + "> } WHERE {}"; |
| 47 | + |
| 48 | + private Processor processor; |
| 49 | + private Construct construct; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() |
| 53 | + { |
| 54 | + processor = new Processor(false); |
| 55 | + construct = new Construct(processor); |
| 56 | + } |
| 57 | + |
| 58 | + private XdmMap buildMap(String classUri, String... queries) throws Exception |
| 59 | + { |
| 60 | + XdmMap map = new XdmMap(); |
| 61 | + XdmAtomicValue key = new XdmAtomicValue(new URI(classUri)); |
| 62 | + XdmValue value = new XdmValue(java.util.Arrays.stream(queries) |
| 63 | + .map(XdmAtomicValue::new) |
| 64 | + .collect(java.util.stream.Collectors.toList())); |
| 65 | + return map.put(key, value); |
| 66 | + } |
| 67 | + |
| 68 | + private Model parseResult(XdmValue result) throws Exception |
| 69 | + { |
| 70 | + StringWriter sw = new StringWriter(); |
| 71 | + Serializer ser = processor.newSerializer(sw); |
| 72 | + ser.serializeXdmValue(result); |
| 73 | + Model model = ModelFactory.createDefaultModel(); |
| 74 | + model.read(new java.io.StringReader(sw.toString()), null, "RDF/XML"); |
| 75 | + return model; |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testConstruct() throws Exception |
| 80 | + { |
| 81 | + XdmValue result = construct.call(new XdmValue[] { buildMap(CLASS_URI, CONSTRUCT_QUERY) }); |
| 82 | + Model model = parseResult(result); |
| 83 | + |
| 84 | + assertTrue(model.contains(null, RDF.type, model.createResource(CLASS_URI))); |
| 85 | + assertTrue(model.contains(null, model.createProperty(PROP_URI), model.createResource(VALUE_URI))); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testMultipleConstructors() throws Exception |
| 90 | + { |
| 91 | + String prop2 = "http://test/ontology#prop2"; |
| 92 | + String value2 = "http://test/value2"; |
| 93 | + String query2 = "CONSTRUCT { ?this <" + prop2 + "> <" + value2 + "> } WHERE {}"; |
| 94 | + |
| 95 | + XdmValue result = construct.call(new XdmValue[] { buildMap(CLASS_URI, CONSTRUCT_QUERY, query2) }); |
| 96 | + Model model = parseResult(result); |
| 97 | + |
| 98 | + assertTrue(model.contains(null, model.createProperty(PROP_URI), model.createResource(VALUE_URI))); |
| 99 | + assertTrue(model.contains(null, model.createProperty(prop2), model.createResource(value2))); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testEmptyMap() throws Exception |
| 104 | + { |
| 105 | + XdmValue result = construct.call(new XdmValue[] { new XdmMap() }); |
| 106 | + Model model = parseResult(result); |
| 107 | + |
| 108 | + assertTrue(model.isEmpty()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test(expected = QueryParseException.class) |
| 112 | + public void testInvalidSparql() throws Exception |
| 113 | + { |
| 114 | + construct.call(new XdmValue[] { buildMap(CLASS_URI, "NOT VALID SPARQL") }); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments