/** Extracts text value associated to key from the container JsonNode. */ private static String getOptionalText(JsonNode container, String key) { JsonNode jsonNode = container.get(key); return jsonNode != null ? jsonNode.getTextValue() : null; }
private String parseDocNode(JsonNode json) { JsonNode nameNode = json.get("doc"); if (nameNode == null) return null; // no doc defined return nameNode.getTextValue(); }
/** Return the defined properties that have string values. */ @Deprecated public Map<String,String> getProps() { Map<String,String> result = new LinkedHashMap<String,String>(); for (Map.Entry<String,JsonNode> e : props.entrySet()) if (e.getValue().isTextual()) result.put(e.getKey(), e.getValue().getTextValue()); return result; }
private void parseNamespace(JsonNode json) { JsonNode nameNode = json.get("namespace"); if (nameNode == null) return; // no namespace defined this.namespace = nameNode.getTextValue(); types.space(this.namespace); }
private void parseName(JsonNode json) { JsonNode nameNode = json.get("protocol"); if (nameNode == null) throw new SchemaParseException("No protocol name specified: "+json); this.name = nameNode.getTextValue(); }
private static String getTextField(final JsonNode node, final String fieldName) throws IOException { return getField(node, fieldName).getTextValue(); }
private static JsonNode get( Iterable<JsonNode> jsonNodes, String id ) { for ( JsonNode jsonNode : jsonNodes ) { if ( id.equals( jsonNode.get( "id" ).getTextValue() ) ) { return jsonNode; } } return null; } }
/** * 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; }
static Set<String> parseAliases(JsonNode node) { JsonNode aliasesNode = node.get("aliases"); if (aliasesNode == null) return null; if (!aliasesNode.isArray()) throw new SchemaParseException("aliases not an array: "+node); Set<String> aliases = new LinkedHashSet<String>(); for (JsonNode aliasNode : aliasesNode) { if (!aliasNode.isTextual()) throw new SchemaParseException("alias not a string: "+aliasNode); aliases.add(aliasNode.getTextValue()); } return aliases; }
@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()); } }
private static void assertListEquals( String what, List<String> expected, JsonNode jsonNode ) { assertTrue( what + " - should be a list", jsonNode.isArray() ); List<String> actual = new ArrayList<>( jsonNode.size() ); for ( JsonNode node : jsonNode ) { actual.add( node.getTextValue() ); } assertEquals( what, expected, actual ); }
@Test public void testRecord() throws DataConversionException { Iterable<RecordWithMetadata<JsonNode>> records = converter.convertRecord(null, sampleRecord, state); RecordWithMetadata<JsonNode> node = records.iterator().next(); Assert.assertEquals(node.getMetadata().getGlobalMetadata().getContentType(), "test.name+json"); Assert.assertEquals(node.getRecord().get("field1").getTextValue(), sampleRecord.get("field1").toString()); } }
private static void assertRelationship( String id, JsonNode relationships, String startNodeId, String type, String endNodeId, Property... properties ) { JsonNode relationship = get( relationships, id ); assertEquals( "Relationship[" + id + "].labels", type, relationship.get( "type" ).getTextValue() ); assertEquals( "Relationship[" + id + "].startNode", startNodeId, relationship.get( "startNode" ).getTextValue() ); assertEquals( "Relationship[" + id + "].endNode", endNodeId, relationship.get( "endNode" ).getTextValue() ); JsonNode props = relationship.get( "properties" ); assertEquals( "length( Relationship[" + id + "].properties )", properties.length, props.size() ); for ( Property property : properties ) { assertJsonEquals( "Relationship[" + id + "].properties[" + property.key() + "]", property.value(), props.get( property.key() ) ); } }
@Test public void shouldProduceResultStreamWithLegacyRestFormatAndNestedMaps() throws Exception { // given ByteArrayOutputStream output = new ByteArrayOutputStream(); ExecutionResultSerializer serializer = getSerializerWith( output, "http://base.uri/" ); // when serializer.statementResult( mockExecutionResult( // RETURN {one:{two:['wait for it...', {three: 'GO!'}]}} map( "map", map("one", map( "two", asList("wait for it...", map("three", "GO!") ) ) ) ) ), false, ResultDataContent.rest ); serializer.finish(); // then String result = output.toString( UTF_8.name() ); JsonNode json = jsonNode(result); Map<String, Integer> columns = new HashMap<>(); int col = 0; JsonNode results = json.get( "results" ).get( 0 ); for ( JsonNode column : results.get( "columns" ) ) { columns.put( column.getTextValue(), col++ ); } JsonNode row = results.get( "data" ).get( 0 ).get( "rest" ); JsonNode jsonMap = row.get( columns.get( "map" ) ); assertEquals( "wait for it...", jsonMap.get( "one" ).get( "two" ).get( 0 ).asText() ); assertEquals( "GO!", jsonMap.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asText() ); }
@Test public void testSuccessWithBinary() throws DataConversionException, IOException { ObjectMapper objectMapper = new ObjectMapper(); byte[] record = "aaaa".getBytes(StandardCharsets.UTF_8); Metadata md = new Metadata(); md.getGlobalMetadata().setContentType("application/binary"); RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata(); Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator = converter.convertRecord("", new RecordWithMetadata<>(record, md), null).iterator(); RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next(); JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class); Assert.assertEquals(parsedElement.get("r").getTextValue(), "YWFhYQ=="); } }
@Test public void testSuccessWithInferredPrintableByteArray() throws DataConversionException, IOException { ObjectMapper objectMapper = new ObjectMapper(); byte[] record = "abrac\\adabra".getBytes(StandardCharsets.UTF_8); Metadata md = new Metadata(); md.getGlobalMetadata().setContentType("application/binary"); md.getGlobalMetadata().addTransferEncoding("base64"); RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata(); Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator = converter.convertRecord("", new RecordWithMetadata<>(record, md), null).iterator(); RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next(); JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class); Assert.assertEquals(parsedElement.get("r").getTextValue(), new String(record, StandardCharsets.UTF_8)); }
@Test public void testSuccessWithString() throws DataConversionException, IOException { ObjectMapper objectMapper = new ObjectMapper(); String innerRecord = "abracadabra"; RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata(); RecordWithMetadata<String> record = new RecordWithMetadata<>(innerRecord, new Metadata()); Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator = converter.convertRecord("", record, null).iterator(); RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next(); JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class); Assert.assertEquals(parsedElement.get("mId").getTextValue(), record.getMetadata().getGlobalMetadata().getId()); Assert.assertEquals(parsedElement.get("r").getTextValue(), innerRecord); Assert .assertEquals(recordWithMetadata.getMetadata().getGlobalMetadata().getContentType(), "lnkd+recordWithMetadata"); Assert.assertNull(recordWithMetadata.getMetadata().getGlobalMetadata().getInnerContentType()); }
for ( JsonNode node : nodes.get( 0 ).get( "labels" ) ) labels.add( node.getTextValue() );
private static void assertJsonEquals( String message, Object expected, JsonNode actual ) { if ( expected == null ) { assertTrue( message, actual == null || actual.isNull() ); } else if ( expected instanceof String ) { assertEquals( message, expected, actual.getTextValue() ); } else if ( expected instanceof Number ) { assertEquals( message, expected, actual.getNumberValue() ); } else { fail( message + " - unexpected type - " + expected ); } }
@Test public void shouldReturnCorrectStatusCodeOnDeadlock() throws Exception { // Given try ( Transaction tx = graphdb().beginTx() ) { graphdb().createNode( Label.label( "First" ) ); graphdb().createNode( Label.label( "Second" ) ); tx.success(); } // When I lock node:First HTTP.Response begin = http.POST( "db/data/transaction", quotedJson( "{ 'statements': [ { 'statement': 'MATCH (n:First) SET n.prop=1' } ] }" )); // and I lock node:Second, and wait for a lock on node:First in another transaction otherThread.execute( writeToFirstAndSecond() ); // and I wait for those locks to be pending assertTrue( secondNodeLocked.await( 10, TimeUnit.SECONDS ) ); Thread.sleep( 1000 ); // and I then try and lock node:Second in the first transaction HTTP.Response deadlock = http.POST( begin.location(), quotedJson( "{ 'statements': [ { 'statement': 'MATCH (n:Second) SET n.prop=1' } ] }" )); // Then assertThat( deadlock.get( "errors" ).get( 0 ).get( "code" ).getTextValue(), equalTo( DeadlockDetected.code().serialize() ) ); }