.aggregator(new ElementAggregator.Builder() .select(TestPropertyNames.COUNT) .execute(new Max()) .build()) .postAggregationFilter(new ElementFilter.Builder()
.execute(new Min()) .build()) .build())
public SelectedBuilder select(final String... selection) { final TupleAdaptedBinaryOperator<String, Object> current = new TupleAdaptedBinaryOperator<>(); current.setSelection(selection); return new SelectedBuilder(aggregator, current); }
@Test public void shouldAggregatePropertiesWithMultipleSelection() { // Given 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); // When Properties state = aggregator.apply(properties1, properties2); state = aggregator.apply(state, properties3); // Then assertEquals(1000, state.get("max")); assertEquals(10, state.get("min")); assertEquals(1000 - 10, state.get("range")); }
@Test public void shouldAggregatePropertiesWithMultipleOfFunctions() { // Given 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); // When Properties state = aggregator.apply(properties1, properties2); state = aggregator.apply(state, properties3); // Then assertEquals(1000, state.get("max")); assertEquals(10, state.get("min")); }
@Test public void shouldAggregateWithTuple2BinaryOperator() { // Given 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"); // When Properties state = props1; state = aggregator.apply(state, props2); state = aggregator.apply(state, props3); // Then assertEquals(props2, state); }
@Test public void shouldAggregateElementUsingKorypheBinaryOperator() { // Given 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(); // When final Element result = aggregator.apply(edge1, edge2); // Then assertEquals(3, result.getProperty(reference)); }
@Test public void shouldAggregateElementUsingMockBinaryOperator() { // Given 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 = new Edge.Builder() .property(reference, 1) .build(); final Edge edge2 = new Edge.Builder() .property(reference, 2) .build(); // When final Element result = aggregator.apply(edge1, edge2); // Then assertEquals(valueResult, result.getProperty(reference)); }
@Test public void shouldAggregateProperties() { // Given 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); // When final Properties result = aggregator.apply(properties1, properties2); // Then assertEquals(valueResult, result.get(reference)); }
@Test public void shouldAggregateElementUsingLambdaBinaryOperator() { // Given final String reference = "reference1"; final BinaryOperator<String> function = (a, b) -> a + "," + b; final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Edge edge1 = new Edge.Builder() .property(reference, "value1") .build(); final Edge edge2 = new Edge.Builder() .property(reference, "value2") .build(); // When final Element result = aggregator.apply(edge1, edge2); // Then assertEquals("value1,value2", result.getProperty(reference)); }
public SelectedBuilder select(final String... selection) { final TupleAdaptedBinaryOperator<String, Object> current = new TupleAdaptedBinaryOperator<>(); current.setSelection(selection); return new SelectedBuilder(aggregator, current); }