@Test public void testCreateRespectsConfigKey() { TestEntity entity2 = app.createAndManageChild( TestEntity.Spec.newInstance().configure(TestEntity.CONF_NAME, "foo")); assertEquals(entity2.getConfig(TestEntity.CONF_NAME), "foo"); }
@Test public void testServiceReplacerWorksAfterRebind() throws Exception { Location origLoc = origManagementContext .getLocationManager() .createLocation(LocationSpec.create(SimulatedLocation.class)); DynamicCluster origCluster = origApp.createAndManageChild( EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class)) .configure(DynamicCluster.INITIAL_SIZE, 3)); origApp.start(ImmutableList.<Location>of(origLoc)); origCluster.addPolicy( PolicySpec.create(ServiceReplacer.class) .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED)); // rebind TestApplication newApp = rebind(); final DynamicCluster newCluster = (DynamicCluster) Iterables.find(newApp.getChildren(), Predicates.instanceOf(DynamicCluster.class)); // stimulate the policy final Set<Entity> initialMembers = ImmutableSet.copyOf(newCluster.getMembers()); final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 1); newApp .getManagementContext() .getSubscriptionManager() .subscribe(e1, HASensors.ENTITY_FAILED, eventListener); newApp .getManagementContext() .getSubscriptionManager() .subscribe(e1, HASensors.ENTITY_RECOVERED, eventListener); e1.emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure")); // Expect e1 to be replaced Asserts.succeedsEventually( new Runnable() { @Override public void run() { Set<Entity> newMembers = Sets.difference(ImmutableSet.copyOf(newCluster.getMembers()), initialMembers); Set<Entity> removedMembers = Sets.difference(initialMembers, ImmutableSet.copyOf(newCluster.getMembers())); assertEquals(removedMembers, ImmutableSet.of(e1)); assertEquals(newMembers.size(), 1); assertEquals( ((TestEntity) Iterables.getOnlyElement(newMembers)).getCallHistory(), ImmutableList.of("start")); // TODO e1 not reporting "start" after rebind because callHistory is a field rather than // an attribute, so was not persisted Asserts.assertEqualsIgnoringOrder(e1.getCallHistory(), ImmutableList.of("stop")); assertFalse(Entities.isManaged(e1)); } }); }
@Test public void testDisplayName() { TestEntity result = entity.createAndManageChild(TestEntity.Spec.newInstance().displayName("Boo")); assertIsProxy(result); assertEquals(result.getDisplayName(), "Boo"); }
@Test public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() throws Exception { policy.subscribe(entity, TestEntity.SEQUENCE, listener); policy.subscribe(entity, TestEntity.NAME, listener); policy.subscribe(entity, TestEntity.MY_NOTIF, listener); policy.subscribe(otherEntity, TestEntity.SEQUENCE, listener); policy.unsubscribe(entity); entity.setAttribute(TestEntity.SEQUENCE, 123); entity.setAttribute(TestEntity.NAME, "myname"); entity.emit(TestEntity.MY_NOTIF, 456); otherEntity.setAttribute(TestEntity.SEQUENCE, 789); Thread.sleep(SHORT_WAIT_MS); Asserts.succeedsEventually( new Runnable() { @Override public void run() { assertEquals( listener.events, ImmutableList.of( new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 789))); } }); }
@Test public void testCreateRespectsFlagInMap() { TestEntity entity2 = app.createAndManageChild( TestEntity.Spec.newInstance().configure(MutableMap.of("confName", "baz"))); assertEquals(entity2.getConfig(TestEntity.CONF_NAME), "baz"); }
@Test public void testGetEffector() throws Exception { TestEntity entity2 = app.createAndManageChild(EntitySpecs.spec(TestEntity.class)); Effector<?> effector = entity2.getEntityType().getEffector("myEffector"); Effector<?> effector2 = entity2.getEntityType().getEffector("identityEffector", Object.class); assertEquals(effector.getName(), "myEffector"); assertEquals(effector2.getName(), "identityEffector"); }
@Test public void testCreateAndManageChild() { TestEntity result = entity.createAndManageChild(TestEntity.Spec.newInstance()); assertIsProxy(result); assertIsProxy(Iterables.get(entity.getChildren(), 0)); assertIsProxy(result.getParent()); assertIsProxy(managementContext.getEntityManager().getEntity(result.getId())); }
@Test public void testGetChildrenAndParentsReturnsProxies() { TestEntity child = (TestEntity) Iterables.get(app.getChildren(), 0); Application parent = (Application) child.getParent(); assertIsProxy(child); assertIsProxy(parent); }
@Test public void testRestoresSetConfigKey() throws Exception { origApp.createAndManageChild( EntitySpec.create(TestEntity.class) .configure(TestEntity.CONF_SET_THING.subKey(), "val1") .configure(TestEntity.CONF_SET_THING.subKey(), "val2")); newApp = rebind(); final TestEntity newE = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); assertEquals(newE.getConfig(TestEntity.CONF_SET_THING), ImmutableSet.of("val1", "val2")); }
@Test(groups = "Integration") public void testPollsUrl() throws Exception { entity.setAttribute(TestEntity.SERVICE_UP, true); entity.addEnricher( HttpLatencyDetector.builder() .url(baseUrl) .rollup(500, TimeUnit.MILLISECONDS) .period(100, TimeUnit.MILLISECONDS) .build()); assertLatencyAttributesNonNull(entity); }
@Test(groups = "Integration") public void testGetsSensorIfAlredySetThenPolls() throws Exception { entity.setAttribute(TEST_URL, baseUrl.toString()); entity.addEnricher( HttpLatencyDetector.builder() .url(TEST_URL) .noServiceUp() .rollup(500, TimeUnit.MILLISECONDS) .period(100, TimeUnit.MILLISECONDS) .build()); assertLatencyAttributesNonNull(entity); }
@Test public void testRestoresConfigKeys() throws Exception { origApp.createAndManageChild( EntitySpec.create(TestEntity.class) .configure(TestEntity.CONF_NAME, "nameval") .configure(TestEntity.CONF_LIST_PLAIN, ImmutableList.of("val1", "val2")) .configure(TestEntity.CONF_MAP_PLAIN, ImmutableMap.of("akey", "aval"))); newApp = rebind(); final TestEntity newE = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); assertEquals(newE.getConfig(TestEntity.CONF_NAME), "nameval"); assertEquals(newE.getConfig(TestEntity.CONF_LIST_PLAIN), ImmutableList.of("val1", "val2")); assertEquals(newE.getConfig(TestEntity.CONF_MAP_PLAIN), ImmutableMap.of("akey", "aval")); }
@Test // ListConfigKey deprecated, as order no longer guaranteed public void testRestoresListConfigKey() throws Exception { origApp.createAndManageChild( EntitySpec.create(TestEntity.class) .configure(TestEntity.CONF_LIST_THING.subKey(), "val1") .configure(TestEntity.CONF_LIST_THING.subKey(), "val2")); newApp = rebind(); final TestEntity newE = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); // assertEquals(newE.getConfig(TestEntity.CONF_LIST_THING), ImmutableList.of("val1", "val2")); assertEquals( ImmutableSet.copyOf(newE.getConfig(TestEntity.CONF_LIST_THING)), ImmutableSet.of("val1", "val2")); }
@Test public void testEscapesUriForSensorName() throws IOException { Sensor<String> sensor = Sensors.newStringSensor("name with space"); SensorSummary summary = SensorTransformer.sensorSummary(entity, sensor); URI selfUri = summary.getLinks().get("self"); String expectedUri = "/v1/applications/" + entity.getApplicationId() + "/entities/" + entity.getId() + "/sensors/" + "name%20with%20space"; assertEquals(selfUri, URI.create(expectedUri)); }
@Test public void testSubstitutionUsesDriverBean() throws Exception { String entityid = entity.getId(); String pattern = "id=${driver.entity.id}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, String.format("id=%s", entityid)); }
@Test(groups = "Integration") public void testWaitsForServiceUp() throws Exception { entity.setAttribute(TestEntity.SERVICE_UP, false); entity.addEnricher( HttpLatencyDetector.builder().url(baseUrl).period(100, TimeUnit.MILLISECONDS).build()); // nothing until url is set EntityTestUtils.assertAttributeEqualsContinually( MutableMap.of("timeout", 200), entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT, null); // gets value after url is set, and gets rolling average entity.setAttribute(TestEntity.SERVICE_UP, true); assertLatencyAttributesNonNull(entity); }
@Test public void testInvokeStopEntityOnShutdown() throws Exception { BrooklynShutdownHooks.invokeStopOnShutdown(entity); BrooklynShutdownHooks.BrooklynShutdownHookJob job = BrooklynShutdownHookJob.newInstanceForTesting(); job.run(); assertTrue(entity.getCallHistory().contains("stop")); }
@Test public void testSimpleSubstitution() throws Exception { entity.setConfig(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String pattern = "mykey1=${mykey1},mykey2=${mykey2}"; String result = DownloadSubstituters.substitute( pattern, ImmutableMap.of("mykey1", "myval1", "mykey2", "myval2")); assertEquals(result, "mykey1=myval1,mykey2=myval2"); }
@Test public void testSubstitutionUsesOverrides() throws Exception { entity.setConfig(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String pattern = "version=${version},mykey1=${mykey1}"; BasicDownloadRequirement req = new BasicDownloadRequirement( driver, ImmutableMap.of("version", "overriddenversion", "mykey1", "myval1")); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, "version=overriddenversion,mykey1=myval1"); }
// Should first stop entities, then terminate management contexts @Test public void testInvokeStopEntityAndTerminateManagementContextOnShutdown() throws Exception { BrooklynShutdownHooks.invokeTerminateOnShutdown(managementContext); BrooklynShutdownHooks.invokeStopOnShutdown(entity); BrooklynShutdownHooks.BrooklynShutdownHookJob job = BrooklynShutdownHookJob.newInstanceForTesting(); job.run(); assertTrue(entity.getCallHistory().contains("stop")); assertFalse(managementContext.isRunning()); }
// Previously failed because immutable-map builder threw exception if put same key multiple times, // and the NamedActionWithUrl did not have equals/hashCode @Test public void testSensorWithMultipleOpenUrlActionsRegistered() throws IOException { AttributeSensor<String> sensor = Sensors.newStringSensor("sensor1"); entity.setAttribute(sensor, "http://myval"); RendererHints.register(sensor, RendererHints.namedActionWithUrl()); RendererHints.register(sensor, RendererHints.namedActionWithUrl()); SensorSummary summary = SensorTransformer.sensorSummary(entity, sensor); assertEquals(summary.getLinks().get("action:open"), URI.create("http://myval")); }
@Test public void testServiceRestarterWorksAfterRebind() throws Exception { origEntity.addPolicy( PolicySpec.create(ServiceRestarter.class) .configure(ServiceRestarter.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED)); TestApplication newApp = rebind(); final TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.emit(HASensors.ENTITY_FAILED, new FailureDescriptor(origEntity, "simulate failure")); Asserts.succeedsEventually( new Runnable() { @Override public void run() { assertEquals(newEntity.getCallHistory(), ImmutableList.of("restart")); } }); }
@Test public void testSubstitutionIncludesDefaultSubs() throws Exception { entity.setConfig(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String pattern = "version=${version},type=${type},simpletype=${simpletype}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); String result = DownloadSubstituters.substitute(req, pattern); assertEquals( result, String.format( "version=%s,type=%s,simpletype=%s", "myversion", TestEntity.class.getName(), TestEntity.class.getSimpleName())); }
@Test public void testGetEffectors() throws Exception { TestEntity entity2 = app.createAndManageChild(EntitySpecs.spec(TestEntity.class)); Set<Effector<?>> effectors = entity2.getEntityType().getEffectors(); class MatchesNamePredicate implements Predicate<Effector<?>> { private final String name; public MatchesNamePredicate(String name) { this.name = name; } @Override public boolean apply(@Nullable Effector<?> input) { return name.equals(input.getName()); } }; assertNotNull(Iterables.find(effectors, new MatchesNamePredicate("myEffector")), null); assertNotNull(Iterables.find(effectors, new MatchesNamePredicate("identityEffector")), null); }
@BeforeMethod(alwaysRun = true) @Override public void setUp() throws Exception { super.setUp(); loc = app.newSimulatedLocation(); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); otherEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); listener = new RecordingSensorEventListener(); policy = new AbstractPolicy() {}; entity.addPolicy(policy); app.start(ImmutableList.of(loc)); }
@Test public void testEffectorOnProxyIsRecorded() { Object result = entity.identityEffector("abc"); assertEquals(result, "abc"); Set<Task<?>> tasks = managementContext .getExecutionManager() .getTasksWithAllTags(ImmutableList.of(ManagementContextInternal.EFFECTOR_TAG, entity)); Task<?> task = Iterables.get(tasks, 0); assertEquals(tasks.size(), 1, "tasks=" + tasks); assertTrue(task.getDescription().contains("identityEffector")); }
@Test public void testSubstituter() throws Exception { entity.setConfig(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String baseurl = "version=${version},type=${type},simpletype=${simpletype}"; Map<String, Object> subs = DownloadSubstituters.getBasicEntitySubstitutions(driver); DownloadTargets result = DownloadSubstituters.substituter(Functions.constant(baseurl), Functions.constant(subs)) .apply(new BasicDownloadRequirement(driver)); String expected = String.format( "version=%s,type=%s,simpletype=%s", "myversion", TestEntity.class.getName(), TestEntity.class.getSimpleName()); assertEquals(result.getPrimaryLocations(), ImmutableList.of(expected)); }
@Test public void testServiceFailureDetectorWorksAfterRebind() throws Exception { origEntity.addEnricher(EnricherSpec.create(ServiceFailureDetector.class)); // rebind TestApplication newApp = rebind(); final TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newApp .getManagementContext() .getSubscriptionManager() .subscribe(newEntity, HASensors.ENTITY_FAILED, eventListener); newEntity.setAttribute(TestEntity.SERVICE_UP, true); ServiceStateLogic.setExpectedState(newEntity, Lifecycle.RUNNING); // trigger the failure newEntity.setAttribute(TestEntity.SERVICE_UP, false); assertHasEventEventually(HASensors.ENTITY_FAILED, Predicates.<Object>equalTo(newEntity), null); assertEquals(events.size(), 1, "events=" + events); }
@Test public void testRestoresUnmatchedConfig() throws Exception { TestEntity origE = origApp.createAndManageChild( EntitySpec.create(TestEntity.class).configure("myunmatchedkey", "myunmatchedval")); origE.createAndManageChild(EntitySpec.create(TestEntity.class)); newApp = rebind(); final TestEntity newE = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); final TestEntity newChildE = (TestEntity) Iterables.find(newE.getChildren(), Predicates.instanceOf(TestEntity.class)); assertEquals(newE.getAllConfigBag().getStringKey("myunmatchedkey"), "myunmatchedval"); assertEquals(newE.getLocalConfigBag().getStringKey("myunmatchedkey"), "myunmatchedval"); assertEquals(newChildE.getAllConfigBag().getStringKey("myunmatchedkey"), "myunmatchedval"); assertFalse(newChildE.getLocalConfigBag().containsKey("myunmatchedkey")); }
@Test public void testSubscriptionReceivesEvents() throws Exception { policy.subscribe(entity, TestEntity.SEQUENCE, listener); policy.subscribe(entity, TestEntity.NAME, listener); policy.subscribe(entity, TestEntity.MY_NOTIF, listener); otherEntity.setAttribute(TestEntity.SEQUENCE, 456); entity.setAttribute(TestEntity.SEQUENCE, 123); entity.setAttribute(TestEntity.NAME, "myname"); entity.emit(TestEntity.MY_NOTIF, 789); Asserts.succeedsEventually( new Runnable() { @Override public void run() { assertEquals( listener.events, ImmutableList.of( new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123), new BasicSensorEvent<String>(TestEntity.NAME, entity, "myname"), new BasicSensorEvent<Integer>(TestEntity.MY_NOTIF, entity, 789))); } }); }