@Test public void testUsesDownloadAddonUrlsAttribute() throws Exception { entity.config().set(BrooklynConfigKeys.SUGGESTED_VERSION, "myentityversion"); entity.sensors().set(Attributes.DOWNLOAD_ADDON_URLS, ImmutableMap.of("myaddon", "acme.com/addon=${addon},version=${addonversion},type=${type},simpletype=${simpletype}")); String expectedFilename = String.format("addon=%s,version=%s,type=%s,simpletype=%s", "myaddon", "myaddonversion", TestEntity.class.getName(), "TestEntity"); String expectedLocalRepo = String.format("file://$HOME/.brooklyn/repository/%s/%s/%s", "TestEntity", "myentityversion", expectedFilename); String expectedDownloadUrl = String.format("acme.com/%s", expectedFilename); String expectedCloudsoftRepo = String.format("http://downloads.cloudsoftcorp.com/brooklyn/repository/%s/%s/%s", "TestEntity", "myentityversion", expectedFilename); DownloadResolver actual = mgmt.getEntityDownloadsManager().newDownloader(driver, "myaddon", ImmutableMap.of("addonversion", "myaddonversion")); assertEquals(actual.getTargets(), ImmutableList.of(expectedLocalRepo, expectedDownloadUrl, expectedCloudsoftRepo), "actual="+actual); }
@Test public void testAttribute() throws Exception { entity.sensors().set(TestEntity.NAME, "myname"); assertEquals(EntityFunctions.attribute(TestEntity.NAME).apply(entity), "myname"); assertEquals(EntityFunctions.attribute(TestEntity.NAME.getName()).apply(entity), "myname"); assertEquals(EntityFunctions.attribute(TestEntity.NAME, "%s - suffix").apply(entity), "myname - suffix"); assertEquals(EntityFunctions.attribute(TestEntity.NAME.getName(), "%s - suffix").apply(entity), "myname - suffix"); assertNull(EntityFunctions.attribute(TestEntity.SEQUENCE).apply(entity)); }
@Test public void shouldAssertAttribute() { final String before = "before"; entity.sensors().set(TestEntity.NAME, before); EntityAsserts.assertAttribute(entity, TestEntity.NAME, Predicates.equalTo(before)); }
@Test public void testAttributeWhenReadyWithPredicate() throws Exception { final Task<String> t = submit(DependentConfiguration.attributeWhenReady(entity, TestEntity.NAME, Predicates.equalTo("myval2"))); entity.sensors().set(TestEntity.NAME, "myval"); assertNotDoneContinually(t); entity.sensors().set(TestEntity.NAME, "myval2"); assertEquals(assertDoneEventually(t), "myval2"); }
@Test public void testRebindsSubscriptions() throws Exception { MyEntity2 origE = origApp.createAndManageChild(EntitySpec.create(MyEntity2.class).configure("subscribe", true)); newApp = rebind(); MyEntity2 newE = (MyEntity2) Iterables.find(newApp.getChildren(), Predicates.instanceOf(MyEntity2.class)); newApp.sensors().set(TestApplication.MY_ATTRIBUTE, "mysensorval"); Asserts.eventually(Suppliers.ofInstance(newE.getEvents()), Predicates.<List<String>>equalTo(ImmutableList.of("mysensorval"))); Assert.assertEquals(newE, origE); }
@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 <T> void testCoercesSensorName() throws Exception { AttributeSensor<String> sensor = Sensors.newStringSensor("mysensor"); entity.sensors().set(sensor, "127.0.0.1:1234"); portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost", 5678), machine, 1234); entity.addLocations(ImmutableList.of(machine)); entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) .configure(OnPublicNetworkEnricher.SENSORS.getName(), ImmutableList.of("mysensor"))); assertAttributeEqualsEventually("mysensor.mapped.public", "mypublichost:5678"); }
@Test public void shouldAssertAttributeEquals() { final String myName = "myname"; entity.sensors().set(TestEntity.NAME, myName); EntityAsserts.assertAttributeEquals(entity, TestEntity.NAME, myName); }
@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 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 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 validThenNull() { addEnricher(); app.sensors().set(currentSensor, 50d); app.sensors().set(totalSensor, 100d); EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0.5d); app.sensors().set(totalSensor, null); EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0.5d); }
@Test public void testRemoveSensorThroughEntity() throws Exception{ entity.sensors().set(TEST_SENSOR, "abc"); entity.removeAttribute(TEST_SENSOR); assertFalse(entity.getEntityType().getSensors().contains(TEST_SENSOR), "sensors="+entity.getEntityType().getSensors()); assertEquals(entity.getAttribute(TEST_SENSOR), null); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(SENSOR_ADDED, entity, TEST_SENSOR), BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, TEST_SENSOR))); }
private static void setExpectedState(Entity entity, Lifecycle state, boolean waitBrieflyForServiceUpIfRunning) { if (waitBrieflyForServiceUpIfRunning) { waitBrieflyForServiceUpIfStateIsRunning(entity, state); } ((EntityInternal)entity).sensors().set(Attributes.SERVICE_STATE_EXPECTED, new Lifecycle.Transition(state, new Date())); Maybe<Enricher> enricher = EntityAdjuncts.tryFindWithUniqueTag(entity.enrichers(), ComputeServiceState.DEFAULT_ENRICHER_UNIQUE_TAG); if (enricher.isPresent() && enricher.get() instanceof ComputeServiceState) { ((ComputeServiceState)enricher.get()).onEvent(null); } }
@Override public Void call() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class)); entity.sensors().set(TestEntity.SERVICE_UP, true); entity.sensors().set(TestEntity.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); Thread.sleep(10); assertEquals(entity.sensors().get(TestEntity.SERVICE_UP), Boolean.TRUE); assertEquals(entity.sensors().get(TestEntity.SERVICE_STATE_ACTUAL), Lifecycle.RUNNING); Entities.unmanage(entity); return null; }}); futures.add(future);
@Test public void testCanOverrideFilenameFromDownloadUrl() throws Exception { entity.config().set(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); entity.sensors().set(Attributes.DOWNLOAD_URL, "http://myhost.com/download/"); DownloadResolver actual = mgmt.getEntityDownloadsManager().newDownloader(driver, ImmutableMap.of("filename", "overridden.filename.tar.gz")); assertEquals(actual.getFilename(), "overridden.filename.tar.gz"); }
@Test public void testInfersFilenameFromDownloadUrl() throws Exception { entity.config().set(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); entity.sensors().set(Attributes.DOWNLOAD_URL, "http://myhost.com/myfile-${version}.tar.gz"); DownloadResolver actual = mgmt.getEntityDownloadsManager().newDownloader(driver); assertEquals(actual.getFilename(), "myfile-myversion.tar.gz"); }