@Test public void testSingleConstraint() { final ConstraintViolation violation = new ConstraintViolation(InvalidProperty, triple); assertEquals(InvalidProperty, violation.getConstraint(), "Incorrect constraint IRI"); assertTrue(violation.getTriples().contains(triple), "Problematic triple not found"); assertEquals(1L, violation.getTriples().size(), "Incorrect triple count"); }
@Test public void testToString() { final ConstraintViolation violation = new ConstraintViolation(InvalidProperty, triple); assertEquals("http://www.trellisldp.org/ns/trellis#InvalidProperty: " + "[<ex:subject> <http://www.w3.org/ns/ldp#contains> <ex:object> .]", violation.toString(), "Unexpected serialization of constraint violation"); } }
@Test public void testMembershipTriples1() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.IndirectContainer, asGraph("/invalidMembershipTriple.ttl", domain + "foo"), domain) .filter(v -> v.getConstraint().equals(Trellis.InvalidRange)).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidRange, violation.getConstraint(), "no InvalidRange violation!"); assertTrue(violation.getTriples().contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.hasMemberRelation, LDP.contains)), "Expected triple not found in violation!"); }); }
/** * Check the constraints of a graph. * @param graph the graph * @param type the LDP interaction model * @param syntax the output syntax */ protected void checkConstraint(final Graph graph, final IRI type, final RDFSyntax syntax) { ofNullable(graph).map(g -> constraintServices.stream().parallel().flatMap(svc -> svc.constrainedBy(type, g)).collect(toList())) .filter(violations -> !violations.isEmpty()) .map(violations -> { final ResponseBuilder err = status(CONFLICT); violations.forEach(v -> err.link(v.getConstraint().getIRIString(), LDP.constrainedBy.getIRIString())); final StreamingOutput stream = new StreamingOutput() { @Override public void write(final OutputStream out) throws IOException { getServices().getIOService().write(violations.stream().flatMap(v2 -> v2.getTriples().stream()), out, syntax); } }; return err.entity(stream); }).ifPresent(err -> { throw new ClientErrorException(err.build()); }); }
private Function<Triple, Stream<ConstraintViolation>> checkModelConstraints(final IRI model, final String domain) { requireNonNull(model, "The interaction model must not be null!"); return triple -> { final Stream.Builder<ConstraintViolation> builder = Stream.builder(); of(triple).filter(propertyFilter(model)).map(t -> new ConstraintViolation(Trellis.InvalidProperty, t)) .ifPresent(builder::accept); of(triple).filter(uriRangeFilter).map(t -> new ConstraintViolation(Trellis.InvalidRange, t)) .ifPresent(builder::accept); of(triple).filter(inDomainRangeFilter(domain)).map(t -> new ConstraintViolation(Trellis.InvalidRange, t)) .ifPresent(builder::accept); return builder.build(); }; }
@Test public void testMembershipTriples2() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.DirectContainer, asGraph("/invalidMembershipTriple2.ttl", domain + "foo"), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidRange, violation.getConstraint(), "no InvalidRange violation found!"); assertTrue(violation.getTriples().contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.isMemberOfRelation, LDP.contains)), "Expected triple not found in violation!"); }); }
@Test public void testInvalidType2() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.RDFSource, asGraph("/invalidType.ttl", domain + "foo"), domain) .filter(v -> v.getConstraint().equals(Trellis.InvalidRange)).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> assertEquals(Trellis.InvalidRange, violation.getConstraint(), "no InvalidRange violation!")); }
private Function<Triple, Stream<ConstraintViolation>> checkModelConstraints(final IRI model, final String domain) { requireNonNull(model, "The interaction model must not be null!"); return triple -> { final Stream.Builder<ConstraintViolation> builder = Stream.builder(); of(triple).filter(propertyFilter(model)).map(t -> new ConstraintViolation(Trellis.InvalidProperty, t)) .ifPresent(builder::accept); of(triple).filter(uriRangeFilter).map(t -> new ConstraintViolation(Trellis.InvalidRange, t)) .ifPresent(builder::accept); of(triple).filter(inDomainRangeFilter(domain)).map(t -> new ConstraintViolation(Trellis.InvalidRange, t)) .ifPresent(builder::accept); return builder.build(); }; }
@Test public void testInvalidInbox() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.RDFSource, asGraph("/invalidInbox.ttl", domain + "foo"), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidRange, violation.getConstraint(), "no InvalidRange constraint!"); assertTrue(violation.getTriples().contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.inbox, rdf.createLiteral("invalid range", "en"))), "Triple not found in constraint violation!"); }); }
@Test public void testMultipleConstraint() { final ConstraintViolation violation = new ConstraintViolation(InvalidProperty, asList(triple, triple2)); assertEquals(InvalidProperty, violation.getConstraint(), "Incorrect constraint IRI"); assertTrue(violation.getTriples().contains(triple), "Problematic triple (1) not found"); assertTrue(violation.getTriples().contains(triple2), "Problematic triple (2) not found"); assertEquals(2L, violation.getTriples().size(), "Incorrect triple count"); }
@Test public void testLdpType() { assertFalse(models.stream().map(ldpType -> svc.constrainedBy(ldpType, asGraph("/withLdpType.ttl", domain + "foo"), domain).anyMatch(v -> Trellis.InvalidType.equals(v.getConstraint()))) .reduce(false, (acc, x) -> acc || x), "Unexpected constraint violation!"); }
@Override public Stream<ConstraintViolation> constrainedBy(final IRI model, final Graph graph, final String domain) { return concat(graph.stream().flatMap(checkModelConstraints(model, domain)), Stream.of(graph).filter(checkCardinality(model)) .map(g -> new ConstraintViolation(Trellis.InvalidCardinality, g.stream().collect(toList())))); } }
@Test public void testBasicConstraints2() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.Container, asGraph("/invalidContainer2.ttl", domain + "foo"), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidProperty, violation.getConstraint(), "no InvalidProperty violation!"); assertTrue(violation.getTriples().contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.insertedContentRelation, rdf.createIRI(domain + "resource"))), "Expected triple not found in violation!"); }); }
@Test public void testCardinality() { assertEquals(0L, models.stream() .filter(type -> !svc.constrainedBy(type, asGraph("/invalidCardinality.ttl", domain + "foo"), domain) .findFirst().isPresent()) .count(), "unexpected constraint violation found!"); models.stream().forEach(type -> { final String subject = domain + "foo"; final Optional<ConstraintViolation> res = svc.constrainedBy(type, asGraph("/invalidCardinality.ttl", subject), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found for " + type); res.ifPresent(violation -> assertEquals(Trellis.InvalidCardinality, violation.getConstraint(), "no InvalidCardinality violation")); }); }
@Override public Stream<ConstraintViolation> constrainedBy(final IRI model, final Graph graph, final String domain) { return concat(graph.stream().flatMap(checkModelConstraints(model, domain)), Stream.of(graph).filter(checkCardinality(model)) .map(g -> new ConstraintViolation(Trellis.InvalidCardinality, g.stream().collect(toList())))); } }
@Test public void testBasicConstraints3() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.Container, asGraph("/invalidContainer3.ttl", domain + "foo"), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidProperty, violation.getConstraint(), "no InvalidProperty violation!"); assertTrue(violation.getTriples().contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.membershipResource, rdf.createIRI(domain + "resource"))), "Expected triple not found in violation!"); }); }
@Test public void testTooManyMembershipTriples() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.IndirectContainer, asGraph("/tooManyMembershipTriples.ttl", domain + "foo"), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> assertEquals(Trellis.InvalidCardinality, violation.getConstraint(), "no InvalidCardinality violation!")); }
@Test public void testBasicConstraints1() { final Optional<ConstraintViolation> res = svc.constrainedBy(LDP.Container, asGraph("/invalidContainer1.ttl", domain + "foo"), domain).findFirst(); assertTrue(res.isPresent(), "no constraint violation found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidProperty, violation.getConstraint(), "no InvalidProperty violation!"); assertTrue(violation.getTriples().contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.isMemberOfRelation, rdf.createIRI(domain + "resource"))), "Triple not found in constraint violation!"); }); }
violations.forEach(v -> err.link(v.getConstraint().getIRIString(), LDP.constrainedBy.getIRIString())); throw new ClientErrorException(err.build());
@Test public void testInvalidContainsProperty() { assertTrue(models.stream() .map(type -> svc.constrainedBy(type, asGraph("/hasLdpContainsTriples.ttl", domain + "foo"), domain) .anyMatch(v -> v.getConstraint().equals(Trellis.InvalidProperty))) .reduce(true, (acc, x) -> acc && x), "InvalidProperty constring not found!"); models.stream().forEach(type -> { final String subject = domain + "foo"; final Optional<ConstraintViolation> res = svc.constrainedBy(type, asGraph("/hasLdpContainsTriples.ttl", subject), domain) .filter(v -> v.getConstraint().equals(Trellis.InvalidProperty)).findFirst(); assertTrue(res.isPresent(), "Constraint violation not found!"); res.ifPresent(violation -> { assertEquals(Trellis.InvalidProperty, violation.getConstraint(), "no InvalidProperty violation!"); assertTrue(violation.getTriples().contains( rdf.createTriple(rdf.createIRI(subject), LDP.contains, rdf.createIRI(domain + "bar"))), "Triple missing from violation!"); }); }); }