@Test public void testSummingEnricherWithNoValues() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testSummingEnricherWhenNoSensorValuesYetDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testSummingEnricherWhenNoSensorValuesYet() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .defaultValueForUnreportedSensors(11) .valueToReportIfNoSensors(40) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 11); }
@Test public void testMultipleProducersAverage() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1, producer2, producer3)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); producer1.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); producer2.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 2d); producer3.sensors().set(intSensor, 5); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); producer2.sensors().set(intSensor, 4); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 4d); }
@Test public void testMultipleProducersAverageDefaultingZero() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1, producer2, producer3)) .defaultValueForUnreportedSensors(0) .valueToReportIfNoSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 0d); producer1.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 1d); producer2.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 2d); producer3.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); }
@Test public void testCustomAggregatingFunction() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Function<Collection<Integer>,Integer> aggregator = new Function<Collection<Integer>, Integer>() { @Override public Integer apply(Collection<Integer> input) { int result = 1; for (Integer in : input) result += in*in; return result; } }; entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computing(aggregator) .fromHardcodedProducers(ImmutableList.of(producer1)) .defaultValueForUnreportedSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); // Event by producer producer1.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 5); } }
@Test public void testMultipleProducersSum() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(producer1, producer2, producer3)) .build()); producer3.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); producer1.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 3); producer2.sensors().set(intSensor, 4); EntityAsserts.assertAttributeEqualsEventually(entity, target, 7); }
@Test public void testSummingEnricherWhenDefaultValueForUnreportedSensors() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .defaultValueForUnreportedSensors(3) .valueToReportIfNoSensors(5) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 3); entity.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, 3); entity.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); entity.sensors().set(intSensor, 7); EntityAsserts.assertAttributeEqualsEventually(entity, target, 7); }
@Test public void testAveragingEnricherWhenDefaultValueForUnreportedSensors() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1)) .defaultValueForUnreportedSensors(3) .valueToReportIfNoSensors(5) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); producer1.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, 3d); producer1.sensors().set(intSensor, 4); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 4d); }
@Test public void testAveragingEnricherWhenNoAndNullSensorValues() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); producer1.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); }
@Test public void testSummingEnricherWhenNullSensorValue() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); entity.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testSummingEnricherWithOneValue() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); entity.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); }