public void apply(EntitySpec<?> entitySpec, String nodeId, ToscaApplication toscaApplication) { LOG.info("Generating EntityInitializers for static attributes on " + entitySpec); Map<String, Object> resolvedAttributes = getToscaFacade().getResolvedAttributes(nodeId, toscaApplication); for (final Map.Entry<String, Object> attribute : resolvedAttributes.entrySet()) { entitySpec.addInitializer(new StaticSensor<>(ConfigBag.newInstance() .configure(StaticSensor.SENSOR_NAME, attribute.getKey()) .configure(StaticSensor.STATIC_VALUE, attribute.getValue())) ); } } }
private EntitySpec<TestEntity> buildEntitySpec(AddEffector... effectors) { EntitySpec<TestEntity> testEntitySpec = EntitySpec.create(TestEntity.class); for (AddEffector effector : effectors) { testEntitySpec.addInitializer(effector); } return testEntitySpec; }
private EntitySpec<TestEntity> buildEntitySpec(HttpCommandEffector httpCommandEffector) { return EntitySpec.create(TestEntity.class).addInitializer(httpCommandEffector); }
public static EntitySpec<? extends Entity> spec() { return EntitySpec.create(BasicStartable.class).addInitializer(MySqlEntityInitializer.class); }
@Test public void testCustomiserIncluded() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(testCustomizer)); assertCustomizers(entity, 1); }
@Test public void testEffectorRemovedDuringInit() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(new EntityInitializer() { @Override public void apply(EntityLocal entity) { ((EntityInternal) entity).getMutableEntityType().removeEffector(TestEntity.IDENTITY_EFFECTOR); } })); assertFalse(entity.getMutableEntityType().getEffectors().containsKey(TestEntity.IDENTITY_EFFECTOR.getName())); }
@Test public void testEffectorAddedDuringInit() { BasicEntity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(new EntityInitializer() { @Override public void apply(EntityLocal entity) { ((EntityInternal) entity).getMutableEntityType().addEffector(EffectorTaskTest.DOUBLE_1); } })); assertEquals(entity.invoke(EffectorTaskTest.DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 5)).getUnchecked(), (Integer) 10); }
@Test public void testInitializerSetsConfigKeyOnEntity() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(DefaultConnectivityResolver.class)); final ConfigKey<Object> key = BrooklynConfigKeys.PROVISIONING_PROPERTIES.subKey(JcloudsLocationConfig.CONNECTIVITY_RESOLVER.getName()); final Object value = entity.config().get(key); assertNotNull(value, "no value on " + entity + " for " + key); assertEquals(value.getClass(), DefaultConnectivityResolver.class); }
@Test public void testAddsStaticSensorOfTypeInteger() { BasicEntity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(new StaticSensor<Integer>(ConfigBag.newInstance(ImmutableMap.of( StaticSensor.SENSOR_NAME, "myname", StaticSensor.SENSOR_TYPE, Integer.class.getName(), StaticSensor.STATIC_VALUE, "1"))))); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newSensor(Integer.class, "myname"), 1); } }
@Test public void testUsingDeprecatedNameInEntityInitializer() throws Exception { Entity entity = app.addChild(EntitySpec.create(BasicEntity.class) .addInitializer(new MyEntityInitializer(ConfigBag.newInstance(ImmutableMap.of("oldKey1", "myval"))))); assertEquals(entity.config().get(MyEntityInitializer.KEY_1), "myval"); }
@Test public void testAddsStaticSensorOfTypeString() { BasicEntity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(new StaticSensor<String>(ConfigBag.newInstance(ImmutableMap.of( StaticSensor.SENSOR_NAME, "myname", StaticSensor.SENSOR_TYPE, String.class.getName(), StaticSensor.STATIC_VALUE, "myval"))))); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newSensor(String.class, "myname"), "myval"); }
@Test public void testCustomizersMerged() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(testCustomizer) .configure( BrooklynConfigKeys.PROVISIONING_PROPERTIES.subKey(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS.getName()), ImmutableList.of(new BasicJcloudsLocationCustomizer()))); assertCustomizers(entity, 2); }
@Test public void testHappyPath() { TestEntity a = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ProxyEffector proxyEffector = new ProxyEffector(ImmutableMap.of( AddEffector.EFFECTOR_NAME, "proxy-effector", ProxyEffector.TARGET_ENTITY, a, ProxyEffector.TARGET_EFFECTOR_NAME, "identityEffector")); // BasicEntity doesn't have an identityEffector. EntityInternal b = Entities.deproxy(app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(proxyEffector))); Object output = b.invoke(b.getEffector("proxy-effector"), ImmutableMap.of("arg", "value")) .getUnchecked(Duration.ONE_MINUTE); assertEquals(output, "value"); }
@Test public void testInitialiserAppliedToMembersOfCluster() { final EntitySpec<TestEntity> memberSpec = EntitySpec.create(TestEntity.class) .addInitializer(testCustomizer); DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.INITIAL_SIZE, 3) .configure(DynamicCluster.MEMBER_SPEC, memberSpec)); app.start(ImmutableList.of(app.newSimulatedLocation())); for (Entity member : cluster.getMembers()) { assertCustomizers(member, 1); } }
@Override @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { super.setUp(); onAddedParameters = new LinkedBlockingQueue<>(); onRemovedParameters = new LinkedBlockingQueue<>(); onAddedEffector = Effectors.effector(Void.class, "on-added-effector") .impl(new RecordingEffector(onAddedParameters)) .build(); onRemovedEffector = Effectors.effector(Void.class, "on-removed-effector") .impl(new RecordingEffector(onRemovedParameters)) .build(); testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(new AddEffector(onAddedEffector)) .addInitializer(new AddEffector(onRemovedEffector))); }
@Test public void testAddsStaticSensorOfTypeString() { BasicEntity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(new MaxConcurrencySensor(ConfigBag.newInstance(ImmutableMap.of( MaxConcurrencySensor.SENSOR_NAME, "myname", MaxConcurrencySensor.MAX_CONCURRENCY, DependentConfiguration.formatString("%d", DependentConfiguration.attributeWhenReady(app, MAX_PERMITS))))))); int actualPermits = 10; app.sensors().set(MAX_PERMITS, actualPermits); EntityAsserts.assertAttributeEventuallyNonNull(entity, (AttributeSensor<?>)entity.getEntityType().getSensor("myname")); }
@Test public void testThrowsIfTargetEffectorDoesntExist() { TestEntity a = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ProxyEffector proxyEffector = new ProxyEffector(ImmutableMap.of( AddEffector.EFFECTOR_NAME, "proxy-effector", ProxyEffector.TARGET_ENTITY, a, ProxyEffector.TARGET_EFFECTOR_NAME, "kajnfksjdnfkjsdnf")); EntityInternal b = Entities.deproxy(app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(proxyEffector))); try { b.invoke(b.getEffector("proxy-effector"), ImmutableMap.of("arg", "value")) .getUnchecked(Duration.ONE_MINUTE); Asserts.shouldHaveFailedPreviously("expected exception when invoking effector that does not exist"); } catch (Exception e) { Asserts.expectedFailureOfType(e, NoSuchElementException.class); } }
@Test public void testRebind() throws Exception { String response = generateCounterReponse("mycounter", "myval"); RecordingWinRmTool.setCustomResponse(".*mycounter.*", new RecordingWinRmTool.CustomResponse(0, response, "")); Entity entity = app().createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(new WindowsPerformanceCounterSensors(ConfigBag.newInstance(ImmutableMap.of( WindowsPerformanceCounterSensors.PERIOD, "1ms", WindowsPerformanceCounterSensors.PERFORMANCE_COUNTERS, ImmutableSet.of( ImmutableMap.of( "name", "mysensor", "sensorType", java.lang.String.class.getName(), //FIXME "counter", "\\mycounter"))))))); app().start(ImmutableList.of(loc)); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor("mysensor"), "myval"); rebind(); String response2 = generateCounterReponse("mycounter", "myval2"); RecordingWinRmTool.setCustomResponse(".*mycounter.*", new RecordingWinRmTool.CustomResponse(0, response2, "")); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor("mysensor"), "myval2"); }
@Test public void testCreateGetContainsAndRemoveTags() throws Exception { entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .tag(2) .addInitializer(EntityInitializers.addingTags("foo"))); entity.tags().addTag(app); Assert.assertTrue(entity.tags().containsTag(app)); Assert.assertTrue(entity.tags().containsTag("foo")); Assert.assertTrue(entity.tags().containsTag(2)); Assert.assertFalse(entity.tags().containsTag("bar")); Assert.assertEquals(entity.tags().getTags(), MutableSet.of(app, "foo", 2)); entity.tags().removeTag("foo"); Assert.assertFalse(entity.tags().containsTag("foo")); Assert.assertTrue(entity.tags().containsTag(entity.getParent())); Assert.assertFalse(entity.tags().containsTag(entity)); entity.tags().removeTag(2); Assert.assertEquals(entity.tags().getTags(), MutableSet.of(app)); }
@Test public void testRebind() throws Exception { RecordingWinRmTool.setCustomResponse(".*mycommand.*", new RecordingWinRmTool.CustomResponse(0, "myval", "")); Entity entity = app().createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(new WinRmCommandSensor<String>(ConfigBag.newInstance(ImmutableMap.of( WinRmCommandSensor.SENSOR_PERIOD, "1ms", WinRmCommandSensor.SENSOR_COMMAND, "mycommand", WinRmCommandSensor.SENSOR_NAME, "mysensor"))))); app().start(ImmutableList.of(loc)); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor("mysensor"), "myval"); rebind(); RecordingWinRmTool.setCustomResponse(".*mycommand.*", new RecordingWinRmTool.CustomResponse(0, "myval2", "")); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor("mysensor"), "myval2"); }