@Test public void testAdding() { Enricher enr = entity.enrichers().add(Enrichers.builder() .combining(NUM1, NUM2) .publishing(NUM3) .computingSum() .build()); Assert.assertEquals(EntityAdjuncts.getNonSystemEnrichers(entity), ImmutableList.of(enr)); entity.sensors().set(NUM1, 2); entity.sensors().set(NUM2, 3); EntityAsserts.assertAttributeEqualsEventually(entity, NUM3, 5); }
@SuppressWarnings("unchecked") @Test public void testCombiningEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .combining(METRIC1, METRIC2) .from(origEntity) .computing(StringFunctions.joiner(",")) .publishing(METRIC2) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); newEntity.sensors().set(METRIC2, "myval2"); EntityAsserts.assertAttributeEventually(newApp, METRIC2, Predicates.or(Predicates.equalTo("myval,myval2"), Predicates.equalTo("myval2,myval"))); }
@Test public void testCombiningRespectsUnchanged() { entity.enrichers().add(Enrichers.builder() .combining(NUM1, NUM2) .<Object>publishing(NUM3) .computing(new Function<Iterable<Integer>, Object>() { @Override public Object apply(Iterable<Integer> input) { if (input != null && Iterables.contains(input, 123)) { return Enrichers.sum(input, 0, 0, new TypeToken<Integer>(){}); } else { return Entities.UNCHANGED; } }}) .build()); entity.sensors().set(NUM1, 123); entity.sensors().set(NUM2, 3); EntityAsserts.assertAttributeEqualsEventually(entity, NUM3, 126); entity.sensors().set(NUM1, 2); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"), entity, NUM3, 126); }
.combining(RESPONSES_4XX_COUNT, RESPONSES_5XX_COUNT) .publishing(ERROR_COUNT) .computingSum()
@Test public void testCombiningWithCustomFunction() { entity.enrichers().add(Enrichers.builder() .combining(NUM1, NUM2) .publishing(NUM3) .computing(Functions.constant(1)) .build()); entity.sensors().set(NUM1, 2); entity.sensors().set(NUM2, 3); EntityAsserts.assertAttributeEqualsEventually(entity, NUM3, 1); }