@Test public void testTransformingCastsResult() { entity.enrichers().add(Enrichers.builder() .transforming(NUM1) .publishing(LONG1) .computing(Functions.constant(Long.valueOf(1))) .build()); entity.sensors().set(NUM1, 123); EntityAsserts.assertAttributeEqualsEventually(entity, LONG1, Long.valueOf(1)); }
@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 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); }
public static final EnricherSpec<?> newEnricherForServiceUpIfNotUpIndicatorsEmpty() { // The function means: if the serviceNotUpIndicators is not null, then serviceNotUpIndicators.size()==0; // otherwise return the default value. return Enrichers.builder() .transforming(SERVICE_NOT_UP_INDICATORS).<Object>publishing(Attributes.SERVICE_UP) .suppressDuplicates(true) .computing( Functionals.<Map<String,?>> ifNotEquals(null).<Object>apply(Functions.forPredicate(CollectionFunctionals.<String>mapSizeEquals(0))) .defaultValue(Entities.UNCHANGED) ) .uniqueTag(DEFAULT_ENRICHER_UNIQUE_TAG) .build(); }
@Override public void init() { super.init(); enrichers().add(Enrichers.builder() .aggregating(Changeable.GROUP_SIZE) .publishing(FABRIC_SIZE) .fromMembers() .computingSum() .valueToReportIfNoSensors(0) .build()); sensors().set(SERVICE_UP, false); }
@Test public void testPropagatingAsEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(METRIC1, METRIC2)) .from(origEntity) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); EntityAsserts.assertAttributeEqualsEventually(newApp, METRIC2, "myval"); }
@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); }
private <T> void propagateMasterAttribute(AttributeSensor<T> att) { enrichers().add(Enrichers.builder() .aggregating(att) .publishing(att) .computing(IfFunctions.<Collection<T>>ifPredicate(CollectionFunctionals.notEmpty()) .apply(CollectionFunctionals.firstElement()) .defaultValue(null)) .entityFilter(MySqlClusterUtils.IS_MASTER) .build()); }
@Test public void testSensorPropagatedWhenMappingUsedSameNameButDifferentType() throws Exception { AttributeSensor<String> origSensor = Sensors.newSensor(String.class, "origSensor"); AttributeSensor<Object> sourceSensorFromYaml = Sensors.newSensor(Object.class, "origSensor"); AttributeSensor<Object> targetSensor = Sensors.newSensor(Object.class, "newSensor"); app.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(sourceSensorFromYaml, targetSensor)) .from(entity) .build()); entity.sensors().set(origSensor, "myval"); EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, "myval"); }
@Test public void testPropagatingEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .propagating(METRIC1) .from(origEntity) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); EntityAsserts.assertAttributeEqualsEventually(newApp, METRIC1, "myval"); }
@Test public void testPropagatingAllEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .propagatingAll() .from(origEntity) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); EntityAsserts.assertAttributeEqualsEventually(newApp, METRIC1, "myval"); }
@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 testPropagating() { entity.enrichers().add(Enrichers.builder() .propagating(ImmutableList.of(STR1)) .from(entity2) .build()); entity2.sensors().set(STR1, "myval"); EntityAsserts.assertAttributeEqualsEventually(entity, STR1, "myval"); entity2.sensors().set(STR1, null); EntityAsserts.assertAttributeEqualsEventually(entity, STR1, null); }
@Test public void testAveragingEnricherWhenNoProducersDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromChildren() .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testUpdatingMap2() { entity.enrichers().add(Enrichers.builder() .updatingMap((AttributeSensor)MAP2) .from(LONG1) .computing(Functionals.ifEquals(-1L).value("-1 is not allowed")) .build()); doUpdatingMapChecks(MAP2); }
@Test public void testSummingEnricherWithNoProducers() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .defaultValueForUnreportedSensors(11) .valueToReportIfNoSensors(40) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 40); }
@Test public void testTransformingEnricher() throws Exception { //ensure previous values get picked up producer.sensors().set(intSensorA, 3); producer.enrichers().add(Enrichers.builder() .transforming(intSensorA) .computing(MathFunctions.times(2)) .publishing(target) // note: if `computing` comes later, we lose some type inference, have to give explicit types or go unchecked //.computing((Function)MathFunctions.times(2)) .build()); EntityAsserts.assertAttributeEqualsEventually(producer, target, 6L); }
@Test public void testAveragingEnricherWhenNoSensors() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromChildren() .defaultValueForUnreportedSensors(3) .valueToReportIfNoSensors(5) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 5d); }
@Test public void testFromEntity() { entity.enrichers().add(Enrichers.builder() .transforming(NUM1) .publishing(NUM1) .computing(Functions.<Integer>identity()) .from(entity2) .build()); entity2.sensors().set(NUM1, 2); EntityAsserts.assertAttributeEqualsEventually(entity, NUM1, 2); }
@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); }