|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.rdf4j.spring.dao.support; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +import org.eclipse.rdf4j.model.vocabulary.SKOS; |
| 16 | +import org.eclipse.rdf4j.spring.RDF4JSpringTestBase; |
| 17 | +import org.eclipse.rdf4j.spring.dao.support.RelationMapBuilder; |
| 18 | +import org.eclipse.rdf4j.spring.domain.model.EX; |
| 19 | +import org.eclipse.rdf4j.spring.support.RDF4JTemplate; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | + |
| 23 | +public class RelationMapBuilderTests extends RDF4JSpringTestBase { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + RDF4JTemplate rdf4JTemplate; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testRelationOnly() { |
| 30 | + RelationMapBuilder rmb = new RelationMapBuilder(this.rdf4JTemplate, SKOS.BROADER); |
| 31 | + assertEquals(normalize( |
| 32 | + "SELECT DISTINCT ( ?rel_subject AS ?rel_key ) ( ?rel_object AS ?rel_value )\n" |
| 33 | + + "WHERE { ?rel_subject <http://www.w3.org/2004/02/skos/core#broader> ?rel_object . }"), |
| 34 | + normalize(rmb.makeQueryString())); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testRelationWithConstraints() { |
| 39 | + RelationMapBuilder rmb = new RelationMapBuilder(this.rdf4JTemplate, EX.creatorOf) |
| 40 | + .constraints(RelationMapBuilder._relSubject.isA( |
| 41 | + EX.Artist)); |
| 42 | + assertEquals(normalize("SELECT DISTINCT ( ?rel_subject AS ?rel_key ) ( ?rel_object AS ?rel_value )\n" |
| 43 | + + "WHERE { ?rel_subject a <http://example.org/Artist> .\n" |
| 44 | + + "?rel_subject <http://example.org/creatorOf> ?rel_object . }"), normalize(rmb.makeQueryString())); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testOptionalRelationWithPattern() { |
| 49 | + RelationMapBuilder rmb = new RelationMapBuilder(this.rdf4JTemplate, EX.creatorOf) |
| 50 | + .constraints(RelationMapBuilder._relSubject.isA( |
| 51 | + EX.Artist)) |
| 52 | + .relationIsOptional(); |
| 53 | + assertEquals(normalize( |
| 54 | + "SELECT DISTINCT ( ?rel_subject AS ?rel_key ) ( ?rel_object AS ?rel_value )\n" |
| 55 | + + "WHERE { ?rel_subject a <http://example.org/Artist> .\n" |
| 56 | + + "OPTIONAL { ?rel_subject <http://example.org/creatorOf> ?rel_object . } }\n"), |
| 57 | + normalize(rmb.makeQueryString())); |
| 58 | + } |
| 59 | + |
| 60 | + private String normalize(String s) { |
| 61 | + return s.replaceAll("\n", " ").replaceAll("\\s+", " ").trim(); |
| 62 | + } |
| 63 | +} |
0 commit comments