|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 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 | + |
| 12 | +package org.eclipse.rdf4j.sail.shacl.ast; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | +import static org.mockito.ArgumentMatchers.any; |
| 17 | +import static org.mockito.Mockito.when; |
| 18 | + |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import org.eclipse.rdf4j.model.*; |
| 22 | +import org.eclipse.rdf4j.model.util.Values; |
| 23 | +import org.eclipse.rdf4j.sail.shacl.wrapper.shape.ShapeSource; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | +import org.mockito.InjectMocks; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 31 | + |
| 32 | +@ExtendWith(MockitoExtension.class) |
| 33 | +public class ShaclPropertiesCastingTest { |
| 34 | + |
| 35 | + public static final Literal DUMMY_VALUE = Values.literal("dummy value"); |
| 36 | + public static final IRI IRI = Values.iri("http://example.org/iri"); |
| 37 | + @Mock |
| 38 | + private Statement statement; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private ShapeSource shapeSource; |
| 42 | + |
| 43 | + @InjectMocks |
| 44 | + private ShaclProperties shaclProperties; |
| 45 | + |
| 46 | + @ParameterizedTest |
| 47 | + @MethodSource("provideArgumentsForTest") |
| 48 | + public void testConstructor(IRI predicate, Value value, String exceptedMessage) { |
| 49 | + String exceptedMessageWithPredicate = String.format(exceptedMessage, predicate); |
| 50 | + |
| 51 | + when(shapeSource.getAllStatements(any(Resource.class))).thenReturn(Stream.of(statement)); |
| 52 | + when(statement.getPredicate()).thenReturn(predicate); |
| 53 | + when(statement.getObject()).thenReturn(value); |
| 54 | + |
| 55 | + ShaclShapeParsingException exception = assertThrows(ShaclShapeParsingException.class, |
| 56 | + () -> new ShaclProperties(Values.iri("http://example.org/shape1"), shapeSource) |
| 57 | + ); |
| 58 | + |
| 59 | + assertEquals(exceptedMessageWithPredicate, exception.getMessage()); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static Stream<Arguments> provideArgumentsForTest() { |
| 64 | + return Stream.of( |
| 65 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#or"), DUMMY_VALUE, |
| 66 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 67 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#xone"), DUMMY_VALUE, |
| 68 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 69 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#and"), DUMMY_VALUE, |
| 70 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 71 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#not"), DUMMY_VALUE, |
| 72 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 73 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#property"), DUMMY_VALUE, |
| 74 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 75 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#node"), DUMMY_VALUE, |
| 76 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 77 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#message"), IRI, |
| 78 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 79 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#severity"), DUMMY_VALUE, |
| 80 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 81 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#languageIn"), DUMMY_VALUE, |
| 82 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 83 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#nodeKind"), DUMMY_VALUE, |
| 84 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 85 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#datatype"), DUMMY_VALUE, |
| 86 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 87 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#minCount"), IRI, |
| 88 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 89 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#maxCount"), IRI, |
| 90 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 91 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#minLength"), IRI, |
| 92 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 93 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#maxLength"), IRI, |
| 94 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 95 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#minExclusive"), IRI, |
| 96 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 97 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#maxExclusive"), IRI, |
| 98 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 99 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#minInclusive"), IRI, |
| 100 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 101 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#maxInclusive"), IRI, |
| 102 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 103 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#pattern"), IRI, |
| 104 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 105 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#class"), DUMMY_VALUE, |
| 106 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 107 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#targetNode"), Values.bnode("bnode1"), |
| 108 | + "Expected predicate <%s> to have a Literal or an IRI as object, but found BNode for _:bnode1 - Caused by shape with id: <http://example.org/shape1>"), |
| 109 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#targetClass"), DUMMY_VALUE, |
| 110 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 111 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#targetSubjectsOf"), DUMMY_VALUE, |
| 112 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 113 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#targetObjectsOf"), DUMMY_VALUE, |
| 114 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 115 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#deactivated"), IRI, |
| 116 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 117 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#uniqueLang"), IRI, |
| 118 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 119 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#closed"), IRI, |
| 120 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 121 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#ignoredProperties"), DUMMY_VALUE, |
| 122 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 123 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#flags"), IRI, |
| 124 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 125 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#path"), DUMMY_VALUE, |
| 126 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 127 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#in"), DUMMY_VALUE, |
| 128 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 129 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#equals"), DUMMY_VALUE, |
| 130 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 131 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#disjoint"), DUMMY_VALUE, |
| 132 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 133 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#lessThan"), DUMMY_VALUE, |
| 134 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 135 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#lessThanOrEquals"), DUMMY_VALUE, |
| 136 | + "Expected predicate <%s> to have an IRI as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 137 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#target"), DUMMY_VALUE, |
| 138 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 139 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#qualifiedValueShape"), DUMMY_VALUE, |
| 140 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 141 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#qualifiedValueShapesDisjoint"), IRI, |
| 142 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 143 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#qualifiedMinCount"), IRI, |
| 144 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 145 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#qualifiedMaxCount"), IRI, |
| 146 | + "Expected predicate <%s> to have a Literal as object, but found IRI for http://example.org/iri - Caused by shape with id: <http://example.org/shape1>"), |
| 147 | + Arguments.of(Values.iri("http://datashapes.org/dash#hasValueIn"), DUMMY_VALUE, |
| 148 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 149 | + Arguments.of(Values.iri("http://rdf4j.org/shacl-extensions#targetShape"), DUMMY_VALUE, |
| 150 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>"), |
| 151 | + Arguments.of(Values.iri("http://www.w3.org/ns/shacl#sparql"), DUMMY_VALUE, |
| 152 | + "Expected predicate <%s> to have a Resource as object, but found Literal for \"dummy value\" - Caused by shape with id: <http://example.org/shape1>") |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments