ElementAggregator extends TupleAdaptedBinaryOperatorComposite<String> { public Element apply(final Element state, final Element element) { if (null == state) { return element; } apply(state.getProperties(), element.getProperties()); return state; }  Element apply(final Element state, final Element element); Properties apply(final Properties state, final Properties properties); @Override List<TupleAdaptedBinaryOperator<String, ?>> getComponents(); void lock(); @Override boolean equals(final Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void shouldAggregateElementUsingMockBinaryOperator() { final String reference = "reference1"; final Integer valueResult = 3; final BinaryOperator<Integer> function = mock(BinaryOperator.class); given(function.apply(1, 2)).willReturn(valueResult); final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Edge edge1 = createEdge(reference, 1); final Edge edge2 = createEdge(reference, 2); final Element result = aggregator.apply(edge1, edge2); assertEquals(valueResult, result.getProperty(reference)); }
@Test public void shouldAggregateElementUsingLambdaBinaryOperator() { final String propertyReference = "reference1"; final BinaryOperator<String> function = (a, b) -> a + "," + b; final ElementAggregator aggregator = new ElementAggregator.Builder() .select(propertyReference) .execute(function) .build(); final Edge edge1 = createEdge(propertyReference, "value1"); final Edge edge2 = createEdge(propertyReference, "value2"); final Element result = aggregator.apply(edge1, edge2); assertEquals("value1,value2", result.getProperty(propertyReference)); }
@Test public void shouldAggregateElementUsingKorypheBinaryOperator() { final String reference = "reference1"; final BinaryOperator<Integer> function = new KorypheBinaryOperator<Integer>() { @Override public Integer _apply(final Integer a, final Integer b) { return a + b; } }; final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Edge edge1 = new Edge.Builder() .property(reference, 1) .build(); final Edge edge2 = new Edge.Builder() .property(reference, 2) .build(); final Element result = aggregator.apply(edge1, edge2); assertEquals(3, result.getProperty(reference)); }
@Test public void shouldAggregateProperties() { final String reference = "reference1"; final Integer value1 = 1; final Integer value2 = 2; final Integer valueResult = 3; final BinaryOperator<Integer> function = mock(BinaryOperator.class); given(function.apply(value1, value2)).willReturn(valueResult); final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Properties properties1 = new Properties(reference, value1); final Properties properties2 = new Properties(reference, value2); final Properties result = aggregator.apply(properties1, properties2); assertEquals(valueResult, result.get(reference)); }
@Test public void shouldAggregatePropertiesWithMultipleOfFunctions() { final BinaryOperator<Integer> max = Math::max; final BinaryOperator<Integer> min = Math::min; final ElementAggregator aggregator = new ElementAggregator.Builder() .select("max") .execute(max) .select("min") .execute(min) .build(); final Properties properties1 = new Properties(); properties1.put("max", 10); properties1.put("min", 10); final Properties properties2 = new Properties(); properties2.put("max", 100); properties2.put("min", 100); final Properties properties3 = new Properties(); properties3.put("max", 1000); properties3.put("min", 1000); Properties state = aggregator.apply(properties1, properties2); state = aggregator.apply(state, properties3); assertEquals(1000, state.get("max")); assertEquals(10, state.get("min")); }
@Test public void shouldAggregatePropertiesWithMultipleSelection() { final BinaryOperator<Tuple3<Integer, Integer, Integer>> maxMinRange = (t1, t2) -> new Tuple3<>( Math.max(t1.get0(), t2.get0()), Math.min(t1.get1(), t2.get1()), Math.max(t1.get0(), t2.get0()) - Math.min(t1.get1(), t2.get1()) ); final ElementAggregator aggregator = new ElementAggregator.Builder() .select("max", "min", "range") .execute(maxMinRange) .build(); final Properties properties1 = new Properties(); properties1.put("max", 10); properties1.put("min", 10); final Properties properties2 = new Properties(); properties2.put("max", 100); properties2.put("min", 100); final Properties properties3 = new Properties(); properties3.put("max", 1000); properties3.put("min", 1000); Properties state = aggregator.apply(properties1, properties2); state = aggregator.apply(state, properties3); assertEquals(1000, state.get("max")); assertEquals(10, state.get("min")); assertEquals(1000 - 10, state.get("range")); }
@Test public void shouldAggregateWithNoPropertiesOrFunctions() { final ElementAggregator aggregator = new ElementAggregator(); final Edge edge1 = new Edge.Builder().group("group").build(); final Edge edge2 = new Edge.Builder().group("group").build(); final Element result = aggregator.apply(edge1, edge2); assertEquals(edge2, result); assertTrue(result.getProperties().isEmpty()); }
@Test public void shouldAggregateWithTuple2BinaryOperator() { final String property1 = "property 1"; final String property2 = "property 2"; final BinaryOperator func1 = new ExampleTuple2BinaryOperator(); final ElementAggregator aggregator = new ElementAggregator.Builder() .select(property1, property2) .execute(func1) .build(); final Properties props1 = new Properties(); props1.put(property1, 1); props1.put(property2, "value1"); final Properties props2 = new Properties(); props2.put(property1, 10); props2.put(property2, "value10"); final Properties props3 = new Properties(); props3.put(property1, 5); props3.put(property2, "value5"); Properties state = props1; state = aggregator.apply(state, props2); state = aggregator.apply(state, props3); assertEquals(props2, state); }