public static void consumeProperties( PropertyTypeDispatcher<String, Void> dispatcher, PropertyContainer entity ) { for ( Map.Entry<String, Object> property : entity.getAllProperties().entrySet() ) { dispatcher.dispatch( property.getValue(), property.getKey() ); } }
private void writeProperties( JsonGenerator out, PropertyContainer container ) throws IOException { out.writeObjectFieldStart( "properties" ); try { for ( Map.Entry<String, Object> property : container.getAllProperties().entrySet() ) { out.writeObjectField( property.getKey(), property.getValue() ); } } finally { out.writeEndObject(); } }
private void writeNodeOrRelationship( JsonGenerator out, PropertyContainer entity, boolean isDeleted ) throws IOException { out.writeStartObject(); try { if ( !isDeleted ) { for ( Map.Entry<String,Object> property : entity.getAllProperties().entrySet() ) { out.writeObjectField( property.getKey(), property.getValue() ); } } } finally { out.writeEndObject(); } }
static PropertyContainer container( Map<String, Object> values ) { PropertyContainer container = mock( PropertyContainer.class ); when( container.getPropertyKeys() ).thenReturn( values.keySet() ); when( container.getAllProperties() ).thenReturn( values ); for ( Map.Entry<String, Object> entry : values.entrySet() ) { when( container.getProperty( entry.getKey(), null ) ).thenReturn( entry.getValue() ); } return container; } }
private static <T extends PropertyContainer> T mockPropertyContainer( Class<T> type, Properties properties ) { T container = mock( type ); when( container.getProperty( anyString() ) ).thenAnswer( properties ); when( container.getProperty( anyString(), any() ) ).thenAnswer( properties ); when( container.getPropertyKeys() ).thenReturn( properties ); when( container.getAllProperties() ).thenReturn( properties.getProperties() ); return container; } }
@Test public void testNodeWriting() throws IOException { //Given PropertyContainer node = mock( Node.class ); when( node.getAllProperties() ).thenThrow( RuntimeException.class ); //When try { jsonCodec.writeValue( jsonGenerator, node ); } catch ( RuntimeException e ) { // do nothing } //Then verify( jsonGenerator, times( 1 ) ).writeEndObject(); }
@Test public void testIteratorWriting() throws IOException { //Given PropertyContainer propertyContainer = mock( PropertyContainer.class ); when( propertyContainer.getAllProperties() ).thenThrow( RuntimeException.class ); //When try { jsonCodec.writeValue( jsonGenerator, Arrays.asList( propertyContainer ) ); } catch ( Exception ignored ) { } //Then verify( jsonGenerator, times( 1 ) ).writeEndArray(); }
@Test public void testRelationshipWriting() throws IOException { //Given PropertyContainer relationship = mock( Relationship.class ); when( relationship.getAllProperties() ).thenThrow( RuntimeException.class ); //When try { jsonCodec.writeValue( jsonGenerator, relationship ); } catch ( Exception e ) { // do nothing } //Then verify( jsonGenerator, times( 1 ) ).writeEndObject(); }
@Test public void testPathWriting() throws IOException { //Given Path path = mock( Path.class ); PropertyContainer propertyContainer = mock( PropertyContainer.class ); when( propertyContainer.getAllProperties() ).thenThrow( RuntimeException.class ); when( path.iterator() ).thenReturn( Arrays.asList(propertyContainer).listIterator() ); //When try { jsonCodec.writeValue( jsonGenerator, path ); } catch ( Exception ignored ) { } //Then verify( jsonGenerator, times( 1 ) ).writeEndArray(); }
@Test public void testPropertyContainerWriting() throws IOException { //Given PropertyContainer propertyContainer = mock( PropertyContainer.class ); when( propertyContainer.getAllProperties() ).thenThrow( RuntimeException.class ); boolean exceptionThrown = false; //When try { jsonCodec.writeValue( jsonGenerator, propertyContainer ); } catch ( IllegalArgumentException e ) { //Then verify( jsonGenerator, never() ).writeEndObject(); exceptionThrown = true; } assertTrue( exceptionThrown ); }
try ( Transaction tx = db.beginTx() ) listedProperties = lookupPropertyContainer( containerId ).getAllProperties(); tx.success();
private <T extends PropertyContainer> T copyProperties(PropertyContainer source, T target) { return copyProperties(source.getAllProperties(), target); }
private <C extends Collection<T>, T extends PropertyContainer> C fixAggregates(C pcs) { for (PropertyContainer pc : pcs) { pc.getAllProperties().entrySet().forEach((entry) -> { Object v = entry.getValue(); String k = entry.getKey(); if (k.matches("^(min|max|sum)_.+")) { if (v instanceof Number && ((Number) v).doubleValue() == ((Number) v).longValue()) entry.setValue(((Number) v).longValue()); } if (k.matches("^avg_.+") && v instanceof double[]) { double[] values = (double[]) v; entry.setValue(values[1] == 0 ? 0 : values[0] / values[1]); } if (k.matches("^collect_.+") && v instanceof Collection) { entry.setValue(((Collection) v).toArray()); } }); } return pcs; }
@UserFunction("apoc.any.properties") @Description("returns properties for virtual and real, nodes, rels and maps") public Map<String,Object> properties(@Name("thing") Object thing, @Name(value = "keys",defaultValue = "null") List<String> keys) { if (thing == null) return null; if (thing instanceof Map) { Map<String, Object> map = (Map<String, Object>) thing; if (keys != null) map.keySet().retainAll(keys); return map; } if (thing instanceof PropertyContainer) { if (keys == null) return ((PropertyContainer) thing).getAllProperties(); return ((PropertyContainer) thing).getProperties(keys.toArray(new String[keys.size()])); } return null; }
@UserFunction @Description("apoc.convert.toMap(value) | tries it's best to convert the value to a map") public Map<String, Object> toMap(@Name("map") Object map) { if (map instanceof PropertyContainer) { return ((PropertyContainer)map).getAllProperties(); } else if (map instanceof Map) { return (Map<String, Object>) map; } else { return null; } }
public static Map<String,Object> toMap(PropertyContainer pc) { if (pc == null) return null; if (pc instanceof Node) { Node node = (Node) pc; return map("id", node.getId(), "labels",labelStrings(node), "properties",pc.getAllProperties()); } if (pc instanceof Relationship) { Relationship rel = (Relationship) pc; return map("id", rel.getId(), "type", rel.getType().name(), "start", rel.getStartNode().getId(),"end", rel.getEndNode().getId(), "properties",pc.getAllProperties()); } throw new RuntimeException("Invalid graph element "+pc); } public static String toString(Object value) {
@Test public void testToCypher() throws Exception { try (Transaction tx = db.beginTx()) { String stmtmt = "CREATE (f:Foo {foo:'foo',answer:42})-[fb:`F B` {fb:'fb',`an swer`:31}]->(b:`B ar` {bar:'bar',answer:41}) RETURN {f:f,fb:fb,b:b} AS data"; Map<String, PropertyContainer> data = (Map<String, PropertyContainer>) db.execute(stmtmt).columnAs("data").next(); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", data.get("f")), (row) -> assertEquals("(:Foo {answer:42,foo:'foo'})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v,{node:'f'}) AS value", map("v", data.get("f")), (row) -> assertEquals("(f:Foo {answer:42,foo:'foo'})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v,{skipKeys:['answer']}) AS value", map("v", data.get("f")), (row) -> assertEquals("(:Foo {foo:'foo'})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v,{keepKeys:['answer']}) AS value", map("v", data.get("f")), (row) -> assertEquals("(:Foo {answer:42})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v,{keepValues:[42]}) AS value", map("v", data.get("f")), (row) -> assertEquals("(:Foo {answer:42})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v,{skipValues:[42]}) AS value", map("v", data.get("f")), (row) -> assertEquals("(:Foo {foo:'foo'})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", data.get("b")), (row) -> assertEquals("(:`B ar` {answer:41,bar:'bar'})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", data.get("fb")), (row) -> assertEquals("(:Foo {answer:42,foo:'foo'})-[:`F B` {`an swer`:31,fb:'fb'}]->(:`B ar` {answer:41,bar:'bar'})", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v,{start:'f',end:'b', relationship:'fb'}) AS value", map("v", data.get("fb")), (row) -> assertEquals("(f)-[fb:`F B` {`an swer`:31,fb:'fb'}]->(b)", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", data.get("b").getAllProperties()), (row) -> assertEquals("{answer:41,bar:'bar'}", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", data.get("b").getProperties("answer", "bar")), (row) -> assertEquals("{answer:41,bar:'bar'}", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", asList(41,"bar",false,null)), (row) -> assertEquals("[41,'bar',false,null]", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", 41), (row) -> assertEquals("41", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", "bar"), (row) -> assertEquals("'bar'", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", null), (row) -> assertEquals("null", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", true), (row) -> assertEquals("true", row.get("value"))); testCall(db, "RETURN apoc.text.toCypher($v) AS value", map("v", false), (row) -> assertEquals("false", row.get("value"))); } } }
public static void consumeProperties( PropertyTypeDispatcher<String, Void> dispatcher, PropertyContainer entity ) { for ( Map.Entry<String, Object> property : entity.getAllProperties().entrySet() ) { dispatcher.dispatch( property.getValue(), property.getKey() ); } }
private void writeProperties( JsonGenerator out, PropertyContainer container ) throws IOException { out.writeObjectFieldStart( "properties" ); try { for ( Map.Entry<String, Object> property : container.getAllProperties().entrySet() ) { out.writeObjectField( property.getKey(), property.getValue() ); } } finally { out.writeEndObject(); } }
private void writeNodeOrRelationship( JsonGenerator out, PropertyContainer entity, boolean isDeleted ) throws IOException { out.writeStartObject(); try { if ( !isDeleted ) { for ( Map.Entry<String,Object> property : entity.getAllProperties().entrySet() ) { out.writeObjectField( property.getKey(), property.getValue() ); } } } finally { out.writeEndObject(); } }