Search in sources :

Example 6 with JsonParseException

use of org.neo4j.server.rest.domain.JsonParseException in project neo4j by neo4j.

the class TransactionMatchers method rowContainsDeletedEntitiesInPath.

public static Matcher<? super HTTP.Response> rowContainsDeletedEntitiesInPath(final int nodes, final int rels) {
    return new TypeSafeMatcher<HTTP.Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                Iterator<JsonNode> meta = getJsonNodeWithName(response, "meta").iterator();
                int nodeCounter = 0;
                int relCounter = 0;
                assertTrue("Expected to find a JSON node, but there was none", meta.hasNext());
                JsonNode node = meta.next();
                assertTrue("Expected the node to be a list (for a path)", node.isArray());
                for (JsonNode inner : node) {
                    String type = inner.get("type").getTextValue();
                    switch(type) {
                        case "node":
                            if (inner.get("deleted").asBoolean()) {
                                ++nodeCounter;
                            }
                            break;
                        case "relationship":
                            if (inner.get("deleted").asBoolean()) {
                                ++relCounter;
                            }
                            break;
                        default:
                            fail("Unexpected type: " + type);
                            break;
                    }
                }
                assertEquals(nodes, nodeCounter);
                assertEquals(rels, relCounter);
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) HTTP(org.neo4j.test.server.HTTP) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 7 with JsonParseException

use of org.neo4j.server.rest.domain.JsonParseException in project neo4j by neo4j.

the class TransactionMatchers method graphContainsDeletedNodes.

public static Matcher<? super HTTP.Response> graphContainsDeletedNodes(final int amount) {
    return new TypeSafeMatcher<HTTP.Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                Iterator<JsonNode> nodes = getJsonNodeWithName(response, "graph").get("nodes").iterator();
                for (int i = 0; i < amount; ++i) {
                    assertTrue(nodes.hasNext());
                    JsonNode node = nodes.next();
                    assertThat(node.get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                }
                while (nodes.hasNext()) {
                    JsonNode node = nodes.next();
                    assertNull(node.get("deleted"));
                }
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) HTTP(org.neo4j.test.server.HTTP) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 8 with JsonParseException

use of org.neo4j.server.rest.domain.JsonParseException in project neo4j by neo4j.

the class TransactionMatchers method assertElementAtMetaIndex.

private static boolean assertElementAtMetaIndex(HTTP.Response response, int[] indexes, String element) {
    try {
        Iterator<JsonNode> meta = getJsonNodeWithName(response, "meta").iterator();
        int i = 0;
        for (int metaIndex = 0; meta.hasNext() && i < indexes.length; metaIndex++) {
            JsonNode node = meta.next();
            if (!node.isNull()) {
                String type = node.get("type").getTextValue();
                if (type.equals(element)) {
                    assertEquals("Expected " + element + " to be at indexes " + Arrays.toString(indexes) + ", but found it at " + metaIndex, indexes[i], metaIndex);
                    ++i;
                } else {
                    assertNotEquals("Expected " + element + " at index " + metaIndex + ", but found " + type, indexes[i], metaIndex);
                }
            }
        }
        return i == indexes.length;
    } catch (JsonParseException e) {
        return false;
    }
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 9 with JsonParseException

use of org.neo4j.server.rest.domain.JsonParseException in project neo4j by neo4j.

the class QueryResultsSerializationTest method restContainsNestedDeleted.

/**
     * This matcher is hardcoded to check for a list containing one deleted node and one map with a deleted node mapped to the key `someKey`.
     */
public static Matcher<? super Response> restContainsNestedDeleted() {
    return new TypeSafeMatcher<Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                JsonNode list = TransactionMatchers.getJsonNodeWithName(response, "rest").iterator().next();
                assertThat(list.get(0).get("metadata").get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                assertThat(list.get(1).get("someKey").get("metadata").get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : Response(org.neo4j.test.server.HTTP.Response) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 10 with JsonParseException

use of org.neo4j.server.rest.domain.JsonParseException in project neo4j by neo4j.

the class QueryResultsSerializationTest method setUp.

@Before
public void setUp() {
    // begin
    Response begin = http.POST("/db/data/transaction");
    assertThat(begin.status(), equalTo(201));
    assertHasTxLocation(begin);
    try {
        commitResource = begin.stringFromContent("commit");
    } catch (JsonParseException e) {
        fail("Exception caught when setting up test: " + e.getMessage());
    }
    assertThat(commitResource, equalTo(begin.location() + "/commit"));
}
Also used : Response(org.neo4j.test.server.HTTP.Response) JsonParseException(org.neo4j.server.rest.domain.JsonParseException) Before(org.junit.Before)

Aggregations

JsonParseException (org.neo4j.server.rest.domain.JsonParseException)12 JsonNode (org.codehaus.jackson.JsonNode)8 Description (org.hamcrest.Description)7 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)7 HTTP (org.neo4j.test.server.HTTP)7 Response (org.neo4j.test.server.HTTP.Response)3 Before (org.junit.Before)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Status (org.neo4j.kernel.api.exceptions.Status)1 JmxMBeanRepresentation (org.neo4j.server.rest.management.repr.JmxMBeanRepresentation)1 ListRepresentation (org.neo4j.server.rest.repr.ListRepresentation)1