Example usage for java.util Optional ifPresent

List of usage examples for java.util Optional ifPresent

Introduction

In this page you can find the example usage for java.util Optional ifPresent.

Prototype

public void ifPresent(Consumer<? super T> action) 

Source Link

Document

If a value is present, performs the given action with the value, otherwise does nothing.

Usage

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@Test
public void testInvalidInsertedContentRelation() {
    final List<IRI> found = models.stream()
            .filter(type -> svc.constrainedBy(type, asGraph("/hasInsertedContent.ttl", domain + "foo"), domain)
                    .findFirst().isPresent())
            .collect(toList());//from w w w.  ja  va  2  s . co  m

    assertTrue(found.contains(LDP.Container), "ldp:Container not present!");
    assertFalse(found.contains(LDP.DirectContainer), "ldp:DirectContainer not expected!");
    assertFalse(found.contains(LDP.IndirectContainer), "ldp:IndirectContainer not expected!");

    models.stream().forEach(type -> {
        final String subject = domain + "foo";
        final Optional<ConstraintViolation> res = svc
                .constrainedBy(type, asGraph("/hasInsertedContent.ttl", subject), domain).findFirst();
        if (type.equals(LDP.IndirectContainer) || type.equals(LDP.DirectContainer)) {
            assertFalse(res.isPresent(), "constraint violation not expected for " + type);
        } else {
            assertTrue(res.isPresent(), "constraint violation not found for " + type);
            res.ifPresent(violation -> {
                assertEquals(Trellis.InvalidProperty, violation.getConstraint(),
                        "no InvalidProperty violation!");
                assertTrue(
                        violation.getTriples().contains(
                                rdf.createTriple(rdf.createIRI(subject), LDP.hasMemberRelation, DC.isPartOf)),
                        "Triple missing from violation!");
            });
        }
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@Test
public void testInvalidLdpProps() {
    final List<IRI> found = models.stream()
            .filter(type -> svc.constrainedBy(type, asGraph("/basicContainer.ttl", domain + "foo"), domain)
                    .findFirst().isPresent())
            .collect(toList());//from   ww  w  .j a  va2s. com

    assertTrue(found.contains(LDP.Container), "ldp:Container not present!");
    assertFalse(found.contains(LDP.DirectContainer), "ldp:DirectContainer not expected!");
    assertFalse(found.contains(LDP.IndirectContainer), "ldp:IndirectContainer not expected!");

    models.stream().forEach(type -> {
        final String subject = domain + "foo";
        final Optional<ConstraintViolation> res = svc
                .constrainedBy(type, asGraph("/basicContainer.ttl", subject), domain).findFirst();
        if (type.equals(LDP.DirectContainer) || type.equals(LDP.IndirectContainer)) {
            assertFalse(res.isPresent(), "constraint violation not expected for " + type);
        } else {
            assertTrue(res.isPresent(), "constraint violation not found for " + type);
            res.ifPresent(violation -> {
                assertEquals(Trellis.InvalidProperty, violation.getConstraint(),
                        "no InvalidProperty violation!");
                assertTrue(
                        violation.getTriples().contains(
                                rdf.createTriple(rdf.createIRI(subject), LDP.hasMemberRelation, DC.isPartOf)),
                        "Triple missing from violation!");
            });
        }
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@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(//from  ww  w .j  a  v  a  2s  . c  o m
                violation.getTriples()
                        .contains(rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.inbox,
                                rdf.createLiteral("invalid range", "en"))),
                "Triple not found in constraint violation!");
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@Test
public void testBasicConstraints1() {
    final Optional<ConstraintViolation> res = svc
            .constrainedBy(LDP.Container, asGraph("/invalidContainer1.ttl", domain + "foo"), domain)
            .findFirst();//from w  w  w  . j  ava2  s. c  om
    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!");
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@Test
public void testBasicConstraints2() {
    final Optional<ConstraintViolation> res = svc
            .constrainedBy(LDP.Container, asGraph("/invalidContainer2.ttl", domain + "foo"), domain)
            .findFirst();//w ww .ja v  a2 s  . c  om
    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!");
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@Test
public void testBasicConstraints3() {
    final Optional<ConstraintViolation> res = svc
            .constrainedBy(LDP.Container, asGraph("/invalidContainer3.ttl", domain + "foo"), domain)
            .findFirst();/*from  w  w  w.  j a v  a2 s .  c  o m*/
    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!");
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@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(/*from   w  ww .  java 2 s .  c  o  m*/
                violation.getTriples().contains(
                        rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.hasMemberRelation, LDP.contains)),
                "Expected triple not found in violation!");
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@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(/*from   ww w . j a va2s . co m*/
                violation.getTriples().contains(
                        rdf.createTriple(rdf.createIRI(domain + "foo"), LDP.isMemberOfRelation, LDP.contains)),
                "Expected triple not found in violation!");
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@Test
public void testCardinality() {
    assertEquals(0L,/*from w  w  w . j  ava  2  s .  co  m*/
            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"));
    });
}

From source file:org.trellisldp.constraint.LdpConstraintsTest.java

@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!"));
}