Java Code Examples for org.eclipse.rdf4j.repository.RepositoryException#getCause()

The following examples show how to use org.eclipse.rdf4j.repository.RepositoryException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSHACLTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void runTest() throws Exception {
	try {
		upload(dataRep, dataGraph);
		assertTrue(conforms);
	} catch (RepositoryException exc) {
		if (conforms || !(exc.getCause() instanceof SailException)) {
			throw exc;
		}
	}
}
 
Example 2
Source File: StatementsController.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Delete data from the repository.
 */
private ModelAndView getDeleteDataResult(Repository repository, HttpServletRequest request,
		HttpServletResponse response) throws ServerHTTPException, ClientHTTPException, HTTPException {
	ProtocolUtil.logRequestParameters(request);

	ValueFactory vf = repository.getValueFactory();

	Resource subj = ProtocolUtil.parseResourceParam(request, SUBJECT_PARAM_NAME, vf);
	IRI pred = ProtocolUtil.parseURIParam(request, PREDICATE_PARAM_NAME, vf);
	Value obj = ProtocolUtil.parseValueParam(request, OBJECT_PARAM_NAME, vf);
	Resource[] contexts = ProtocolUtil.parseContextParam(request, CONTEXT_PARAM_NAME, vf);

	try (RepositoryConnection repositoryCon = RepositoryInterceptor.getRepositoryConnection(request)) {
		repositoryCon.remove(subj, pred, obj, contexts);

		return new ModelAndView(EmptySuccessView.getInstance());
	} catch (RepositoryException e) {
		if (e.getCause() != null && e.getCause() instanceof HTTPException) {
			// custom signal from the backend, throw as HTTPException
			// directly
			// (see SES-1016).
			throw (HTTPException) e.getCause();
		} else {
			throw new ServerHTTPException("Repository update error: " + e.getMessage(), e);
		}
	}
}
 
Example 3
Source File: ModifyShapesPostInitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = ShaclSailValidationException.class)
public void testAddingShapesAfterData() throws Throwable {
	ShaclSail shaclSail = new ShaclSail(new MemoryStore());

	SailRepository sailRepository = new SailRepository(shaclSail);
	sailRepository.init();

	try (SailRepositoryConnection connection = sailRepository.getConnection()) {

		add(connection, "ex:pete a ex:Person .");

		connection.begin();

		StringReader shaclRules = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix sh: <http://www.w3.org/ns/shacl#> .",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",

				"ex:PersonShape",
				"        a sh:NodeShape  ;",
				"        sh:targetClass ex:Person ;",
				"        sh:property [",
				"                sh:path ex:age ;",
				"                sh:minCount 1 ;",
				"        ] ."));

		connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);

		try {
			connection.commit();
		} catch (RepositoryException e) {
			throw e.getCause();
		}

	}
}
 
Example 4
Source File: TempTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expected = ShaclSailValidationException.class)
public void testUndefinedTargetClassValidatesAllSubjects() throws Throwable {

	SailRepository shaclRepository = Utils.getInitializedShaclRepository("shacleNoTargetClass.ttl", true);

	try (SailRepositoryConnection connection = shaclRepository.getConnection()) {

		connection.begin();

		StringReader shaclRules = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix sh: <http://www.w3.org/ns/shacl#> .",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",

				"ex:PersonShape",
				"	a sh:NodeShape  ;",
				// " sh:targetClass foaf:Person ;",
				"	sh:property ex:PersonShapeProperty .",

				"ex:PersonShapeProperty ",
				"	sh:path foaf:age ;",
				"	sh:datatype xsd:int ;",
				"  sh:maxCount 1 ;",
				"  sh:minCount 1 ."));

		connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
		connection.commit();

		connection.begin();

		StringReader invalidSampleData = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",

				"ex:peter a foaf:Person ;",
				"	foaf:age 20, \"30\"^^xsd:int  ."

		));
		connection.add(invalidSampleData, "", RDFFormat.TURTLE);

		try {
			connection.commit();
		} catch (RepositoryException e) {
			if (e.getCause() != null) {
				throw e.getCause();
			}
			throw e;
		}
	}

}
 
Example 5
Source File: TempTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expected = ShaclSailValidationException.class)
public void testUndefinedTargetClassValidatesAllSubjects2() throws Throwable {

	SailRepository shaclRepository = Utils.getInitializedShaclRepository("shacleNoTargetClass.ttl", true);

	try (SailRepositoryConnection connection = shaclRepository.getConnection()) {

		connection.begin();

		StringReader shaclRules = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix sh: <http://www.w3.org/ns/shacl#> .",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",

				"ex:PersonShape",
				"	a sh:NodeShape  ;",
				// " sh:targetClass foaf:Person ;",
				"	sh:property ex:PersonShapeProperty .",

				"ex:PersonShapeProperty ",
				"	sh:path foaf:age ;",
				"	sh:datatype xsd:int ;",
				"  sh:maxCount 1 ;",
				"  sh:minCount 1 ."));

		connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);

		StringReader invalidSampleData = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",

				"ex:peter a foaf:Person ;",
				"	foaf:age 20, \"30\"^^xsd:int  ."

		));
		connection.add(invalidSampleData, "", RDFFormat.TURTLE);

		try {
			connection.commit();
		} catch (RepositoryException e) {
			if (e.getCause() != null) {
				throw e.getCause();
			}
			throw e;
		}
	}

}
 
Example 6
Source File: ValidationReportTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void simpleFirstTest() throws IOException {
	SailRepository shaclSail = Utils.getInitializedShaclRepository("shacl.ttl", false);

	try (SailRepositoryConnection connection = shaclSail.getConnection()) {

		connection.begin();
		connection.add(RDFS.RESOURCE, RDF.TYPE, RDFS.RESOURCE);
		connection.add(RDFS.RESOURCE, RDFS.LABEL, connection.getValueFactory().createLiteral("a"));
		connection.add(RDFS.CLASS, RDF.TYPE, RDFS.RESOURCE);
		connection.add(RDF.SUBJECT, RDF.TYPE, RDFS.RESOURCE);

		connection.commit();
		fail();

	} catch (RepositoryException e) {
		ShaclSailValidationException cause = (ShaclSailValidationException) e.getCause();
		Model actual = cause.validationReportAsModel();

		actual.setNamespace(RDF.PREFIX, RDF.NAMESPACE);
		actual.setNamespace(RDFS.PREFIX, RDFS.NAMESPACE);
		actual.setNamespace("ex", "http://example.com/ns#");

		Rio.write(actual, System.out, RDFFormat.TURTLE);

		Model expected = Rio.parse(new StringReader("" + "@prefix ex: <http://example.com/ns#> .\n"
				+ "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
				+ "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
				+ "@prefix sh: <http://www.w3.org/ns/shacl#> .\n" + "\n"
				+ "_:node1d1gi0h02x12 a sh:ValidationReport;\n" + "  sh:conforms false;\n"
				+ "  sh:result _:node1d1gi0h02x13 .\n" + "\n" + "_:node1d1gi0h02x13 a sh:ValidationResult;\n"
				+ "  sh:focusNode rdf:subject;\n" + "  sh:resultPath rdfs:label;\n"
				+ "  sh:sourceConstraintComponent sh:MinCountConstraintComponent;\n"
				+ "  sh:sourceShape ex:PersonShapeProperty .\n" + "\n"
				+ "_:node1d1gi0h02x12 sh:result _:node1d1gi0h02x14 .\n" + "\n"
				+ "_:node1d1gi0h02x14 a sh:ValidationResult;\n" + "  sh:focusNode rdfs:Class;\n"
				+ "  sh:resultPath rdfs:label;\n"
				+ "  sh:sourceConstraintComponent sh:MinCountConstraintComponent;\n"
				+ "  sh:sourceShape ex:PersonShapeProperty ." + ""), "", RDFFormat.TURTLE);

		assertTrue(Models.isomorphic(expected, actual));

	}
}
 
Example 7
Source File: ValidationReportTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void withoutPathTest() throws IOException {
	SailRepository shaclSail = Utils.getInitializedShaclRepository("shaclValidateTarget.ttl", false);

	try (SailRepositoryConnection connection = shaclSail.getConnection()) {

		connection.begin();
		connection.add(vf.createIRI("http://example.com/ns#", "node1"), RDF.TYPE,
				vf.createIRI("http://example.com/ns#", "SecondTarget"));
		connection.commit();

		fail();

	} catch (RepositoryException e) {
		ShaclSailValidationException cause = (ShaclSailValidationException) e.getCause();
		Model actual = cause.validationReportAsModel();

		actual.setNamespace(RDF.PREFIX, RDF.NAMESPACE);
		actual.setNamespace(RDFS.PREFIX, RDFS.NAMESPACE);
		actual.setNamespace("ex", "http://example.com/ns#");

		Rio.write(actual, System.out, RDFFormat.TURTLE);

		Model expected = Rio.parse(new StringReader("" +
				"@prefix ex: <http://example.com/ns#> .\n" +
				"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n" +
				"@prefix sh: <http://www.w3.org/ns/shacl#> .\n" +
				"\n" +
				"_:node1e4dsta0ax19 a sh:ValidationReport;\n" +
				"  sh:conforms false;\n" +
				"  sh:result _:node1e4dsta0ax20 .\n" +
				"\n" +
				"_:node1e4dsta0ax20 a sh:ValidationResult;\n" +
				"  sh:focusNode ex:node1;\n" +
				"  sh:sourceConstraintComponent sh:ClassConstraintComponent;\n" +
				"  sh:sourceShape ex:PersonShape ."
				+ ""), "", RDFFormat.TURTLE);

		assertTrue(Models.isomorphic(expected, actual));

	}
}
 
Example 8
Source File: ValidationReportTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void nestedLogicalOrSupport() throws IOException {

	SailRepository shaclSail = Utils.getInitializedShaclRepository("test-cases/or/datatype/shacl.ttl", false);

	try (SailRepositoryConnection connection = shaclSail.getConnection()) {

		connection.begin();
		connection.prepareUpdate(IOUtils.toString(ValidationReportTest.class.getClassLoader()
				.getResourceAsStream("test-cases/or/datatype/invalid/case1/query1.rq"), StandardCharsets.UTF_8))
				.execute();
		connection.commit();
		fail();

	} catch (RepositoryException e) {
		ShaclSailValidationException cause = (ShaclSailValidationException) e.getCause();
		Model actual = cause.validationReportAsModel();

		actual.setNamespace(RDF.PREFIX, RDF.NAMESPACE);
		actual.setNamespace(RDFS.PREFIX, RDFS.NAMESPACE);
		actual.setNamespace("ex", "http://example.com/ns#");

		WriterConfig writerConfig = new WriterConfig();
		writerConfig.set(BasicWriterSettings.INLINE_BLANK_NODES, true);
		writerConfig.set(BasicWriterSettings.PRETTY_PRINT, true);

		Rio.write(actual, System.out, RDFFormat.TURTLE, writerConfig);

		Model expected = Rio.parse(new StringReader(""
				+ "@prefix ex: <http://example.com/ns#> .\n"
				+ "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
				+ "@prefix sh: <http://www.w3.org/ns/shacl#> .\n" + "\n"
				+ "[] a sh:ValidationReport;\n" +
				"  sh:conforms false;\n" +
				"  sh:result [ a sh:ValidationResult;\n" +
				"      sh:detail [ a sh:ValidationResult;\n" +
				"          sh:detail [ a sh:ValidationResult;\n" +
				"              sh:focusNode ex:validPerson1;\n" +
				"              sh:resultPath ex:age;\n" +
				"              sh:sourceConstraintComponent sh:DatatypeConstraintComponent;\n" +
				"              sh:sourceShape ex:personShapeAgeLong\n" +
				"            ];\n" +
				"          sh:focusNode ex:validPerson1;\n" +
				"          sh:resultPath ex:age;\n" +
				"          sh:sourceConstraintComponent sh:DatatypeConstraintComponent;\n" +
				"          sh:sourceShape ex:personShapeAgeInteger\n" +
				"        ];\n" +
				"      sh:focusNode ex:validPerson1;\n" +
				"      sh:resultPath ex:age;\n" +
				"      sh:sourceConstraintComponent sh:OrConstraintComponent;\n" +
				"      sh:sourceShape ex:personShapeOr\n" +
				"    ] ." + ""), "", RDFFormat.TURTLE);

		assertTrue(Models.isomorphic(expected, actual));

	}
}
 
Example 9
Source File: ModifyShapesPostInitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expected = ShaclSailValidationException.class)
public void testUpdatingShapesViolation() throws Throwable {
	ShaclSail shaclSail = new ShaclSail(new MemoryStore());

	SailRepository sailRepository = new SailRepository(shaclSail);
	sailRepository.init();

	try (SailRepositoryConnection connection = sailRepository.getConnection()) {

		connection.begin();

		StringReader shaclRules = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix sh: <http://www.w3.org/ns/shacl#> .",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",

				"ex:PersonShape",
				"        a sh:NodeShape  ;",
				"        sh:targetClass ex:Person ;",
				"        sh:property [",
				"                sh:path ex:age ;",
				"                sh:datatype xsd:integer ;",
				"        ] ."));

		connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
		connection.commit();

		add(connection, "ex:pete a ex:Person .");

		StringReader extraShaclRules = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix sh: <http://www.w3.org/ns/shacl#> .",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",

				"ex:PersonShape",
				"        a sh:NodeShape  ;",
				"        sh:targetClass ex:Person ;",
				"        sh:property [",
				"                sh:path ex:age ;",
				"                sh:minCount 1 ;",
				"        ] ."));

		try {
			connection.add(extraShaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
		} catch (RepositoryException e) {
			throw e.getCause();
		}
	}
}
 
Example 10
Source File: ModifyShapesPostInitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expected = SailConflictException.class)
public void testDeadlockDetection() throws Throwable {

	ShaclSail shaclSail = new ShaclSail(new MemoryStore());

	SailRepository sailRepository = new SailRepository(shaclSail);
	sailRepository.init();

	SailRepositoryConnection connection1 = sailRepository.getConnection();
	SailRepositoryConnection connection2 = sailRepository.getConnection();

	try {
		connection2.begin();

		StringReader shaclRules = new StringReader(String.join("\n", "",
				"@prefix ex: <http://example.com/ns#> .",
				"@prefix sh: <http://www.w3.org/ns/shacl#> .",
				"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
				"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",

				"ex:PersonShape",
				"        a sh:NodeShape  ;",
				"        sh:targetClass ex:Person ;",
				"        sh:property [",
				"                sh:path ex:age ;",
				"                sh:minCount 1 ;",
				"        ] ."));

		connection2.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
		connection1.begin();

		addInTransaction(connection1, "ex:steve a ex:Person .");

		try {
			connection1.commit();
		} catch (RepositoryException e) {
			throw e.getCause();
		}

	} finally {
		connection2.close();
		connection1.close();

		sailRepository.shutDown();
	}

}