Search in sources :

Example 31 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j-documentation by neo4j.

the class UniquenessOfPathsDocTest method pathUniquenessExample.

@Graph({ "Pet0 descendant Pet1", "Pet0 descendant Pet2", "Pet0 descendant Pet3", "Principal1 owns Pet1", "Principal2 owns Pet2", "Principal1 owns Pet3" })
@Test
@Documented(UNIQUENESS_OF_PATHS_DOC)
public void pathUniquenessExample() {
    Node start = data.get().get("Pet0");
    gen.get().addSnippet("graph", createGraphVizWithNodeId("Descendants example graph", graphdb(), gen.get().getTitle()));
    gen.get();
    gen.get().addTestSourceSnippets(this.getClass(), "traverser", "traverseNodeGlobal");
    // tag::traverser[]
    Node dataTarget = data.get().get("Principal1");
    String output = "";
    int count = 0;
    try (Transaction transaction = graphdb().beginTx()) {
        start = transaction.getNodeById(start.getId());
        final Node target = transaction.getNodeById(dataTarget.getId());
        TraversalDescription td = transaction.traversalDescription().uniqueness(Uniqueness.NODE_PATH).evaluator(new Evaluator() {

            @Override
            public Evaluation evaluate(Path path) {
                boolean endNodeIsTarget = path.endNode().equals(target);
                return Evaluation.of(endNodeIsTarget, !endNodeIsTarget);
            }
        });
        Traverser results = td.traverse(start);
        for (Path path : results) {
            count++;
            output += path.toString() + "\n";
        }
    }
    gen.get().addSnippet("output", createOutputSnippet(output));
    assertEquals(2, count);
    String output2 = "";
    count = 0;
    try (Transaction tx = graphdb().beginTx()) {
        start = tx.getNodeById(start.getId());
        final Node target = tx.getNodeById(dataTarget.getId());
        // tag::traverseNodeGlobal[]
        TraversalDescription nodeGlobalTd = tx.traversalDescription().uniqueness(Uniqueness.NODE_PATH).evaluator(new Evaluator() {

            @Override
            public Evaluation evaluate(Path path) {
                boolean endNodeIsTarget = path.endNode().equals(target);
                return Evaluation.of(endNodeIsTarget, !endNodeIsTarget);
            }
        }).uniqueness(Uniqueness.NODE_GLOBAL);
        Traverser results = nodeGlobalTd.traverse(start);
        // we should get two paths back, through Pet1 and Pet3
        for (Path path : results) {
            count++;
            output2 += path.toString() + "\n";
        }
    }
    gen.get().addSnippet("outNodeGlobal", createOutputSnippet(output2));
    assertEquals(1, count);
}
Also used : Path(org.neo4j.graphdb.Path) Evaluation(org.neo4j.graphdb.traversal.Evaluation) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.traversal.Traverser) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) Graph(org.neo4j.doc.test.GraphDescription.Graph) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.Test)

Example 32 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j-documentation by neo4j.

the class TestData method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    final Title title = description.getAnnotation(Title.class);
    final Documented doc = description.getAnnotation(Documented.class);
    GraphDescription.Graph g = description.getAnnotation(GraphDescription.Graph.class);
    if (g == null) {
        g = description.getTestClass().getAnnotation(GraphDescription.Graph.class);
    }
    final GraphDescription graph = GraphDescription.create(g);
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            product.set(create(graph, title == null ? null : title.value(), doc == null ? null : doc.value(), description.getMethodName()));
            try {
                base.evaluate();
            } finally {
                product.set(null);
            }
        }
    };
}
Also used : Documented(org.neo4j.annotations.documented.Documented) Statement(org.junit.runners.model.Statement)

Example 33 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class TestData method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    final Title title = description.getAnnotation(Title.class);
    final Documented doc = description.getAnnotation(Documented.class);
    GraphDescription.Graph g = description.getAnnotation(GraphDescription.Graph.class);
    if (g == null) {
        g = description.getTestClass().getAnnotation(GraphDescription.Graph.class);
    }
    final GraphDescription graph = GraphDescription.create(g);
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            product.set(create(graph, title == null ? null : title.value(), doc == null ? null : doc.value(), description.getMethodName()));
            try {
                base.evaluate();
            } finally {
                product.set(null);
            }
        }
    };
}
Also used : Documented(org.neo4j.annotations.documented.Documented) Statement(org.junit.runners.model.Statement)

Example 34 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class AuthenticationIT method successful_authentication.

@Test
@Documented("Authenticate to access the server\n" + "\n" + "Authenticate by sending a username and a password to Neo4j using HTTP Basic Auth.\n" + "Requests should include an +Authorization+ header, with a value of +Basic <payload>+,\n" + "where \"payload\" is a base64 encoded string of \"username:password\".")
public void successful_authentication() throws JsonParseException, IOException {
    // Given
    startServerWithConfiguredUser();
    // Then
    HTTP.Response response = HTTP.withBasicAuth("neo4j", "secret").POST(txCommitURL("system"), query("SHOW USERS"));
    assertThat(response.status()).isEqualTo(200);
    final JsonNode jsonNode = getResultRow(response);
    assertThat(jsonNode.get(0).asText()).isEqualTo("neo4j");
    assertThat(jsonNode.get(1).asBoolean()).isEqualTo(false);
}
Also used : HTTP(org.neo4j.test.server.HTTP) JsonNode(com.fasterxml.jackson.databind.JsonNode) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Example 35 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class AuthenticationIT method missing_authorization.

@Test
@Documented("Missing authorization\n" + "\n" + "If an +Authorization+ header is not supplied, the server will reply with an error.")
public void missing_authorization() throws JsonParseException, IOException {
    // Given
    startServerWithConfiguredUser();
    // Document
    RESTRequestGenerator.ResponseEntity response = gen.get().expectedStatus(401).expectedHeader("WWW-Authenticate", "Basic realm=\"Neo4j\"").get(databaseURL());
    // Then
    JsonNode data = JsonHelper.jsonNode(response.entity());
    JsonNode firstError = data.get("errors").get(0);
    assertThat(firstError.get("code").asText()).isEqualTo(Status.Security.Unauthorized.code().serialize());
    assertThat(firstError.get("message").asText()).isEqualTo("No authentication header supplied.");
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) RESTRequestGenerator(org.neo4j.server.rest.RESTRequestGenerator) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Aggregations

Documented (org.neo4j.annotations.documented.Documented)43 Test (org.junit.Test)23 Test (org.junit.jupiter.api.Test)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)13 ResponseEntity (org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity)13 ResponseEntity (org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity)12 Graph (org.neo4j.doc.test.GraphDescription.Graph)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 Transaction (org.neo4j.graphdb.Transaction)4 Map (java.util.Map)3 HTTP (org.neo4j.doc.server.HTTP)3 JavaTestDocsGenerator (org.neo4j.doc.tools.JavaTestDocsGenerator)3 Node (org.neo4j.graphdb.Node)3 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)3 JsonHelper.jsonToMap (org.neo4j.server.rest.domain.JsonHelper.jsonToMap)3 HTTP (org.neo4j.test.server.HTTP)3 Statement (org.junit.runners.model.Statement)2 RESTDocsGenerator (org.neo4j.doc.server.rest.RESTDocsGenerator)2