@Test public void testReducingBuilderWithNamedNonExistentFunction() { try { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("unknown function name", ImmutableMap.<String, Object>of("separator", "-")) .publishing(STR3) .build() ); Asserts.fail("Expected exception when adding reducing enricher with unknown named function"); } catch (Exception e) { if (Exceptions.getFirstThrowableOfType(e, IllegalStateException.class) == null) { throw e; } } }
@Test public void testReducingBuilderWithJoinerFunction() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("joiner", ImmutableMap.<String, Object>of("separator", "-")) .publishing(STR3) .build() ); EntityAsserts.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foo-null"); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foo-bar"); }
@Test public void testReducingBuilderWithFormatStringFunction() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("formatString", ImmutableMap.<String, Object>of("format", "hello, %s and %s")) .publishing(STR3) .build() ); EntityAsserts.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "hello, foo and null"); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "hello, foo and bar"); }
@Test public void testReducingBuilderWithJoinerFunctionAndUnusedParameter() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("joiner", ImmutableMap.<String, Object>of("non.existent.parameter", "-")) .publishing(STR3) .build() ); EntityAsserts.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foo, null"); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foo, bar"); }
@Test public void testReducingBuilderWithConcatenator() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing(new Concatenator()) .publishing(STR3) .build() ); EntityAsserts.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsContinually(entity, STR3, null); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foobar"); }
@Test public void testReducingBuilderWithLengthCalculator() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing(new LengthCalculator()) .publishing(INT1) .build() ); EntityAsserts.assertAttributeEquals(entity, INT1, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsEventually(entity, INT1, 3); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, INT1, 6); }
@Test public void testReducingBuilderWithJoinerFunctionWithDefaultParameter() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("joiner") .publishing(STR3) .build() ); EntityAsserts.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foo, null"); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foo, bar"); }