/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.brooklyn.enricher.stock; import java.util.concurrent.atomic.AtomicReference; import org.apache.brooklyn.api.entity.EntitySpec; import org.apache.brooklyn.api.sensor.AttributeSensor; import org.apache.brooklyn.api.sensor.SensorEvent; import org.apache.brooklyn.api.sensor.SensorEventListener; import org.apache.brooklyn.core.sensor.Sensors; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.core.test.entity.TestEntity; import org.apache.brooklyn.enricher.stock.SensorPropagatingEnricher; import org.apache.brooklyn.test.Asserts; import org.apache.brooklyn.test.EntityTestUtils; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.javalang.AtomicReferences; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; public class SensorPropagatingEnricherDeprecatedTest extends BrooklynAppUnitTestSupport { private TestEntity entity; @BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); } @Test public void testPropagatesSpecificSensor() { app.enrichers().add(SensorPropagatingEnricher.newInstanceListeningTo(entity, TestEntity.NAME)); // name propagated entity.sensors().set(TestEntity.NAME, "foo"); EntityTestUtils.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); // sequence not propagated entity.sensors().set(TestEntity.SEQUENCE, 2); EntityTestUtils.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); } @Test public void testPropagatesAllSensors() { app.enrichers().add(SensorPropagatingEnricher.newInstanceListeningToAllSensors(entity)); // all attributes propagated entity.sensors().set(TestEntity.NAME, "foo"); entity.sensors().set(TestEntity.SEQUENCE, 2); EntityTestUtils.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); EntityTestUtils.assertAttributeEqualsEventually(app, TestEntity.SEQUENCE, 2); // notification-sensor propagated final AtomicReference<Integer> notif = new AtomicReference<Integer>(); app.subscriptions().subscribe(app, TestEntity.MY_NOTIF, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { notif.set(event.getValue()); }}); entity.sensors().emit(TestEntity.MY_NOTIF, 7); Asserts.eventually(AtomicReferences.supplier(notif), Predicates.equalTo(7)); } @Test public void testPropagatesAllBut() { app.enrichers().add(SensorPropagatingEnricher.newInstanceListeningToAllSensorsBut(entity, TestEntity.SEQUENCE)) ; // name propagated entity.sensors().set(TestEntity.NAME, "foo"); EntityTestUtils.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); // sequence not propagated entity.sensors().set(TestEntity.SEQUENCE, 2); EntityTestUtils.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); } @Test public void testPropagatingAsDifferentSensor() { final AttributeSensor<String> ANOTHER_ATTRIBUTE = Sensors.newStringSensor("another.attribute", ""); app.enrichers().add(SensorPropagatingEnricher.newInstanceRenaming(entity, ImmutableMap.of(TestEntity.NAME, ANOTHER_ATTRIBUTE))); // name propagated as different attribute entity.sensors().set(TestEntity.NAME, "foo"); EntityTestUtils.assertAttributeEqualsEventually(app, ANOTHER_ATTRIBUTE, "foo"); } }