private void assertQueryGetsValue( ServerControls server, String query, long value ) throws Throwable { HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), quotedJson( "{ 'statements': [ { 'statement': '" + query + "' } ] }" ) ); assertEquals( "[]", response.get( "errors" ).toString() ); JsonNode result = response.get( "results" ).get( 0 ); assertEquals( "value", result.get( "columns" ).get( 0 ).asText() ); assertEquals( value, result.get( "data" ).get( 0 ).get( "row" ).get( 0 ).asLong() ); }
if (!object.has(TYPE) || !object.get(TYPE).isTextual() || !object.get(TYPE).getTextValue().equals("record")) { throw new IllegalArgumentException("The data provided is not a record!"); if (!object.has(NAMESPACE) || !object.get(NAMESPACE).isTextual()) { throw new IllegalArgumentException("No namespace specified!"); } else if (!object.has(NAME) || !object.get(NAME).isTextual()) { throw new IllegalArgumentException("No name specified!"); } else { fqn = object.get(NAMESPACE).getTextValue() + "." + object.get(NAME).getTextValue(); if (!object.has(VERSION) || !object.get(VERSION).isInt()) { object.put(VERSION, 1); schema.setVersion(object.get(VERSION).asInt()); if (!object.has(DEPENDENCIES)) { schema.setDependencySet(dependencies); } else if (!object.get(DEPENDENCIES).isArray()) { throw new IllegalArgumentException("Illegal dependencies format!"); } else { for (JsonNode child : object.get(DEPENDENCIES)) { if (!child.isObject() || !child.has(FQN) || !child.get(FQN).isTextual() || !child.has(VERSION) || !child.get(VERSION).isInt()) { throw new IllegalArgumentException("Illegal dependency format!"); } else { String dependencyFqn = child.get(FQN).asText(); int dependencyVersion = child.get(VERSION).asInt();
@Override public int getIntValue() throws IOException, JsonParseException { return currentNumericNode().getIntValue(); }
private String parseDocNode(JsonNode json) { JsonNode nameNode = json.get("doc"); if (nameNode == null) return null; // no doc defined return nameNode.getTextValue(); }
/** * Returns the value of the named, string-valued property in this schema. * Returns <tt>null</tt> if there is no string-valued property with that name. */ public String getProp(String name) { JsonNode value = getJsonProp(name); return value != null && value.isTextual() ? value.getTextValue() : null; }
/** * Removes UUIDs from a json tree. * * @param json json tree node */ public static void removeUuids(JsonNode json) { boolean containerWithId = json.isContainerNode() && json.has(UUID_FIELD); boolean isArray = json.isArray(); boolean childIsNotArray = !(json.size() == 1 && json.getElements().next().isArray()); if (containerWithId && !isArray && childIsNotArray) { ((ObjectNode) json).remove(UUID_FIELD); } for (JsonNode node : json) { if (node.isContainerNode()) { removeUuids(node); } } } }
@Test public void shouldFixturesWork() throws Exception { // Given the rule in the beginning of this class // When I run this test // Then HTTP.Response response = HTTP.POST( neo4j.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get( 0 ).get( "data" ).size(), equalTo( 2 ) ); }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode rest = serialize( out, json, new RestRepresentationWriter( URI.create( "localhost" ) ) ); MatcherAssert.assertThat( rest.size(), equalTo( 1 ) ); JsonNode firstCell = rest.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
private void assertQueryGetsError( ServerControls server, String query, String error ) throws Throwable { HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), quotedJson( "{ 'statements': [ { 'statement': '" + query + "' } ] }" ) ); assertThat( response.get( "errors" ).toString(), containsString( error ) ); } }
private void assertDBConfig( ServerControls server, String expected, String key ) throws JsonParseException { JsonNode beans = HTTP.GET( server.httpURI().toString() + "db/manage/server/jmx/domain/org.neo4j/" ).get( "beans" ); JsonNode configurationBean = findNamedBean( beans, "Configuration" ).get( "attributes" ); boolean foundKey = false; for ( JsonNode attribute : configurationBean ) { if ( attribute.get( "name" ).asText().equals( key ) ) { assertThat( attribute.get( "value" ).asText(), equalTo( expected ) ); foundKey = true; break; } } if ( !foundKey ) { fail( "No config key '" + key + "'." ); } }
@Test public void txEndpointShouldReplyWithHttpsWhenItReturnsURLs() throws Exception { startServer(); String baseUri = server.baseUri().toString(); HTTP.Response response = POST( baseUri + "db/data/transaction", quotedJson( "{'statements':[]}" ) ); assertThat( response.location(), startsWith( baseUri ) ); assertThat( response.get( "commit" ).asText(), startsWith( baseUri ) ); }
@Test public void testRecord() throws DataConversionException, IOException { Iterable<String> records = converter.convertRecord(null, sampleRecord, state); Iterator<String> recordIt = records.iterator(); ObjectMapper objectMapper = new ObjectMapper(); String record = recordIt.next(); Assert.assertFalse(recordIt.hasNext()); JsonNode parsedRecord = objectMapper.readValue(record, JsonNode.class); Assert.assertEquals(parsedRecord.get("field1").getTextValue(), sampleRecord.get("field1").toString()); } }
@Test public void shouldHandleMapParametersCorrectly() throws Exception { Response response = http.POST( "db/data/transaction/commit", quotedJson("{ 'statements': [ { 'statement': " + "'WITH {map} AS map RETURN map[0]', 'parameters':{'map':[{'index':0,'name':'a'},{'index':1,'name':'b'}]} } ] }")); // then assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ); JsonNode row = data.get( "data" ).get( 0 ).get( "row" ); assertThat( row.size(), equalTo( 1 ) ); assertThat( row.get(0).get("index").asInt(), equalTo( 0 ) ); assertThat( row.get(0).get("name").asText(), equalTo( "a" ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
Schema.Type fieldType = schema.getType(); String expectedVal = null; switch(fieldType) { case NULL: if(!defaultJson.isNull()) { expectedVal = "null"; if(!defaultJson.isBoolean()) { expectedVal = "boolean"; if(!defaultJson.isInt()) { expectedVal = "int"; if(!defaultJson.isInt() && !defaultJson.isLong()) { expectedVal = "long"; case FLOAT: case DOUBLE: if(!defaultJson.isNumber()) { expectedVal = "number"; if(defaultJson.isTextual()) { break; break; case STRING: if(!defaultJson.isTextual()) { expectedVal = "string";
} else if (jsonNode.isNull()) { return JsonProperties.NULL_VALUE; } else if (jsonNode.isBoolean()) { return jsonNode.asBoolean(); } else if (jsonNode.isInt()) { if (schema == null || schema.getType().equals(Schema.Type.INT)) { return jsonNode.asInt(); } else if (schema.getType().equals(Schema.Type.LONG)) { return jsonNode.asLong(); } else if (jsonNode.isLong()) { return jsonNode.asLong(); } else if (jsonNode.isDouble()) { if (schema == null || schema.getType().equals(Schema.Type.DOUBLE)) { return jsonNode.asDouble(); } else if (schema.getType().equals(Schema.Type.FLOAT)) { return (float) jsonNode.asDouble(); } else if (jsonNode.isTextual()) { return jsonNode.asText(); try { return jsonNode.getTextValue().getBytes(BYTES_CHARSET); } catch (UnsupportedEncodingException e) { throw new AvroRuntimeException(e); } else if (jsonNode.isArray()) { List l = new ArrayList();
assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ).get( "data" ); assertTrue( "data is a list", data.isArray() ); assertEquals( "one entry", initialData + 1, data.size() ); JsonNode entry = data.get( 0 ); assertTrue( "entry has row", entry.has( "row" ) ); assertTrue( "entry has graph", entry.has( "graph" ) ); JsonNode nodes = entry.get( "graph" ).get( "nodes" ); JsonNode rels = entry.get( "graph" ).get( "relationships" ); assertTrue( "nodes is a list", nodes.isArray() ); assertTrue( "relationships is a list", rels.isArray() ); assertEquals( "one node", 1, nodes.size() ); assertEquals( "no relationships", 0, rels.size() ); Set<String> labels = new HashSet<>(); for ( JsonNode node : nodes.get( 0 ).get( "labels" ) ) labels.add( node.getTextValue() );
@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; } }
public static void encode(Encoder e, Schema s, JsonNode n) throws IOException { switch (s.getType()) { case RECORD: for (Field f : s.getFields()) { String name = f.name(); JsonNode v = n.get(name); if (v == null) { v = f.defaultValue(); e.writeEnum(s.getEnumOrdinal(n.getTextValue())); break; case ARRAY: e.writeArrayStart(); e.setItemCount(n.size()); case MAP: e.writeMapStart(); e.setItemCount(n.size()); Schema v = s.getValueType(); for (Iterator<String> it = n.getFieldNames(); it.hasNext();) { e.startItem(); String key = it.next(); e.writeString(key); encode(e, v, n.get(key)); break; case FIXED: if (!n.isTextual())
@Test public void shouldReturnReadOnlyStatusWhenCreatingNodes() throws Exception { // Given HTTP.Response response = http.POST( "db/data/transaction/commit", quotedJson( "{ 'statements': [ { 'statement': 'CREATE (node)' } ] }" ) ); // Then JsonNode error = response.get( "errors" ).get( 0 ); String code = error.get( "code" ).asText(); String message = error.get( "message" ).asText(); assertEquals( "Neo.ClientError.General.ForbiddenOnReadOnlyDatabase", code ); assertThat( message, containsString( "This is a read only Neo4j instance" ) ); }
private static boolean isValidDefault(Schema schema, JsonNode defaultValue) { if (defaultValue == null) return false; switch (schema.getType()) { case STRING: case BYTES: case ENUM: case FIXED: return defaultValue.isTextual(); case INT: case LONG: case FLOAT: case DOUBLE: return defaultValue.isNumber(); case BOOLEAN: return defaultValue.isBoolean(); case NULL: return defaultValue.isNull(); case ARRAY: if (!defaultValue.isArray()) return false; for (JsonNode element : defaultValue) if (!isValidDefault(schema.getElementType(), element)) return false; return true; case MAP: if (!defaultValue.isObject()) return false; for (JsonNode value : defaultValue)