Skip to content

Commit 9926d05

Browse files
committed
GH-5575 handle recursion in shacl better
1 parent 66da95d commit 9926d05

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

core/sail/shacl/src/test/java/org/eclipse/rdf4j/sail/shacl/DeepRecursionValidationTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,100 @@
1010
*******************************************************************************/
1111
package org.eclipse.rdf4j.sail.shacl;
1212

13+
import static org.junit.jupiter.api.Assertions.assertFalse;
1314
import static org.junit.jupiter.api.Assertions.assertNotNull;
1415
import static org.junit.jupiter.api.Assertions.assertThrows;
1516

1617
import java.net.URL;
1718
import java.util.Objects;
1819
import java.util.Set;
1920

21+
import org.eclipse.rdf4j.common.transaction.IsolationLevels;
22+
import org.eclipse.rdf4j.model.Resource;
23+
import org.eclipse.rdf4j.model.util.Values;
2024
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
25+
import org.eclipse.rdf4j.model.vocabulary.RDFS;
2126
import org.eclipse.rdf4j.repository.RepositoryException;
2227
import org.eclipse.rdf4j.repository.sail.SailRepository;
2328
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
2429
import org.eclipse.rdf4j.rio.RDFFormat;
30+
import org.eclipse.rdf4j.sail.memory.MemoryStore;
2531
import org.eclipse.rdf4j.sail.shacl.ast.ShaclShapeParsingException;
2632
import org.junit.jupiter.api.Test;
2733

2834
class DeepRecursionValidationTest {
2935

36+
@Test
37+
void recursiveShapesCanBeRemovedAfterFailedValidation() throws Exception {
38+
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
39+
shaclSail.setShapesGraphs(Set.of(RDF4J.SHACL_SHAPE_GRAPH));
40+
SailRepository shaclRepository = new SailRepository(shaclSail);
41+
shaclRepository.init();
42+
43+
URL shapes = Objects.requireNonNull(
44+
getClass().getClassLoader().getResource("recursion/deep/shacl.trig"));
45+
46+
try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
47+
connection.begin(IsolationLevels.NONE, ShaclSail.TransactionSettings.ValidationApproach.Disabled);
48+
connection.add(shapes, shapes.toString(), RDFFormat.TRIG, RDF4J.SHACL_SHAPE_GRAPH);
49+
connection.commit();
50+
}
51+
52+
try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
53+
connection.begin();
54+
connection.add(RDF4J.SHACL_SHAPE_GRAPH, RDF4J.SHACL_SHAPE_GRAPH, RDF4J.SHACL_SHAPE_GRAPH);
55+
56+
assertThrows(ShaclShapeParsingException.class, connection::commit);
57+
connection.rollback();
58+
}
59+
60+
try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
61+
connection.begin();
62+
connection.clear(RDF4J.SHACL_SHAPE_GRAPH);
63+
connection.commit();
64+
65+
assertFalse(connection.hasStatement(null, null, null, false, RDF4J.SHACL_SHAPE_GRAPH));
66+
} finally {
67+
shaclRepository.shutDown();
68+
}
69+
}
70+
71+
@Test
72+
void recursiveShapesCanBeRemovedAfterFailedValidation2() throws Exception {
73+
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
74+
shaclSail.setShapesGraphs(Set.of(RDF4J.NIL));
75+
76+
SailRepository shaclRepository = new SailRepository(shaclSail);
77+
shaclRepository.init();
78+
79+
URL shapes = Objects.requireNonNull(
80+
getClass().getClassLoader().getResource("recursion/deep/shacl.trig"));
81+
82+
try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
83+
connection.begin(IsolationLevels.NONE, ShaclSail.TransactionSettings.ValidationApproach.Disabled);
84+
connection.add(shapes, shapes.toString(), RDFFormat.TRIG);
85+
connection.commit();
86+
}
87+
88+
try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
89+
connection.begin();
90+
connection.add(Values.bnode(), RDFS.LABEL, Values.literal("This will fail"));
91+
92+
assertThrows(ShaclShapeParsingException.class, connection::commit);
93+
connection.rollback();
94+
}
95+
96+
try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
97+
connection.begin();
98+
connection.clear(new Resource[1]);
99+
connection.commit();
100+
101+
assertFalse(connection.hasStatement(null, null, null, false));
102+
} finally {
103+
shaclRepository.shutDown();
104+
}
105+
}
106+
30107
@Test
31108
void deepRecursionAcrossAllConstraintTypesValidates() throws Exception {
32109
SailRepository shaclRepository = Utils.getInitializedShaclRepository(

0 commit comments

Comments
 (0)