protected Entity createCluster(Location location, Map flags) { EntitySpec<?> memberSpec = null; if (getMembers().isEmpty()) memberSpec = getFirstMemberSpec(); if (memberSpec == null) memberSpec = getMemberSpec(); if (memberSpec == null) { throw new IllegalStateException("No member spec nor entity factory supplied for dynamic fabric "+this); } EntitySpec<?> specConfigured = EntitySpec.create(memberSpec).configure(flags); if (location!=null) specConfigured.location(location); return addChild(specConfigured); } }
@Test public void testDoesNotAddConditionalWhenConfigured() throws Exception { optional = app.addChild(EntitySpec.create(ConditionalEntity.class) .configure(ConditionalEntity.CREATE_CONDITIONAL_ENTITY, false) .configure(ConditionalEntity.CONDITIONAL_ENTITY_SPEC, EntitySpec.create(TestEntity.class))); app.start(ImmutableList.of(loc1)); assertEquals(optional.getChildren().size(), 0); }
@Test public void testPrefersMemberSpecLocation() throws Exception { @SuppressWarnings("deprecation") DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class) .location(loc2)) .configure(DynamicCluster.INITIAL_SIZE, 1)); cluster.start(ImmutableList.of(loc)); assertEquals(ImmutableList.copyOf(cluster.getLocations()), ImmutableList.of(loc)); Entity member = Iterables.getOnlyElement(cluster.getMembers()); assertEquals(ImmutableList.copyOf(member.getLocations()), ImmutableList.of(loc2)); }
@Test public void testConfigureFromKey() throws Exception { MyBaseEntity entity = app.addChild(EntitySpec.create(MyBaseEntity.class) .configure(MyBaseEntity.SUPER_KEY_1, "changed")); assertEquals(entity.getConfig(MyBaseEntity.SUPER_KEY_1), "changed"); }
@Test public void testCreateRespectsFlags() { TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class). configure("confName", "boo")); assertEquals(entity2.getConfig(TestEntity.CONF_NAME), "boo"); }
@Test public void testUsingDeprecatedName() throws Exception { EntityInternal entity = app.addChild(EntitySpec.create(MyEntity.class) .configure("oldConfMapDeepMerge", ImmutableMap.of("mykey", "myval"))); assertEquals(entity.config().get(MyEntity.CONF_MAP_DEEP_MERGE), ImmutableMap.of("mykey", "myval")); }
@Test public void testConfigSetToGroovyTruthFalseIsAvailable() throws Exception { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .configure(intKeyWithDefault, 0)); assertEquals(entity.getConfig(intKeyWithDefault), (Integer)0); }
@Test public void testConfigAtConstructionOverridesParentValue() throws Exception { TestEntity parent = app.createAndManageChild(EntitySpec.create(TestEntity.class) .configure(strKey, "aval")); TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class) .configure(strKey, "diffval")); assertEquals(entity.getConfig(strKey), "diffval"); }
private FailingEntity createFailingEntity() { FailingEntity entity = app.createAndManageChild(EntitySpec.create(FailingEntity.class) .configure(FailingEntity.FAIL_ON_START, true)); return entity; }
@Test public void testAllClusterMemberIdsAddedInOrderOnPositiveResize() throws Exception { int clusterSize = 5; DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure("memberSpec", EntitySpec.create(TestEntity.class)) .configure("initialSize", clusterSize)); cluster.start(ImmutableList.of(loc)); int positiveResizeDelta = 3; cluster.resizeByDelta(positiveResizeDelta); assertMemberIdSensors(cluster, ImmutableList.of(0, 1, 2, 3, 4, 5, 6, 7)); }
@Test public void usingEntitySpecResizeFromZeroToOneStartsANewEntityAndSetsItsParent() throws Exception { DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure("memberSpec", EntitySpec.create(TestEntity.class))); cluster.start(ImmutableList.of(loc)); cluster.resize(1); TestEntity entity = (TestEntity) Iterables.getOnlyElement(cluster.getMembers()); assertEquals(entity.getCount(), 1); assertEquals(entity.getParent(), cluster); assertEquals(entity.getApplication(), app); }
@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 testBrooklynAccessibleAddressFindsPreexistingMapping() throws Exception { final int privatePort = 8080; final String publicNatIp = "1.2.3.4"; final int publicNatPort = 12000; SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure(SshMachineLocation.TCP_PORT_MAPPINGS, ImmutableMap.of(privatePort, publicNatIp+":"+publicNatPort))); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .configure(BrooklynAccessUtils.PORT_FORWARDING_MANAGER, pfm) .location(machine)); assertEquals(BrooklynAccessUtils.getBrooklynAccessibleAddress(entity, privatePort), HostAndPort.fromParts(publicNatIp, publicNatPort)); }
@Test public void testChildInheritsFromParent() throws Exception { EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class) .configure("myentity.myconfig", "myval1")); EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class) .parent(entity)); assertEquals(child.config().getBag().getAllConfigAsConfigKeyMap(), ImmutableMap.of(MyEntity.MY_CONFIG, "myval1")); assertEquals(child.config().getBag().getAllConfig(), ImmutableMap.of("myentity.myconfig", "myval1")); assertEquals(child.config().getLocalBag().getAllConfig(), ImmutableMap.of()); }
@Test public void testGetConfigMapWithSubKeys() throws Exception { TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .configure(TestEntity.CONF_MAP_THING.subKey("mysub"), "myval")); assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "myval")); assertEquals(entity.config().getNonBlocking(TestEntity.CONF_MAP_THING).get(), ImmutableMap.of("mysub", "myval")); assertEquals(entity.config().get(TestEntity.CONF_MAP_THING.subKey("mysub")), "myval"); assertEquals(entity.config().getNonBlocking(TestEntity.CONF_MAP_THING.subKey("mysub")).get(), "myval"); }
@DataProvider(name = "brooklynObjects") public Object[][] createBrooklynObjects() throws Exception { EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution()); setUp(); TestApplication app = mgmt.getEntityManager().createEntity(appSpec); EntityRequiringConfigKeyInRange entity = app.createAndManageChild(EntitySpec.create(EntityRequiringConfigKeyInRange.class) .configure(EntityRequiringConfigKeyInRange.RANGE, 5)); Policy policy = entity.policies().add(PolicySpec.create(TestPolicy.class)); Location location = app.newSimulatedLocation(); return new Object[][]{{entity}, {policy}, {location}}; }
@Test public void testGroupWithMatchingFilterReturnsOnlyMatchingMembers() throws Exception { group = app.addChild(EntitySpec.create(SequenceGroup.class) .configure(SequenceGroup.ENTITY_FILTER, EntityPredicates.displayNameEqualTo("test-one"))); createTestEntities(); app.start(ImmutableList.of(loc1)); assertAttributeEqualsEventually(group, SequenceGroup.RUNNING, true); assertEqualsIgnoringOrder(group.getMembers(), ImmutableList.of(e1)); assertAttributeEquals(e1, SequenceGroup.SEQUENCE_VALUE, 1); AtomicInteger state = group.sensors().get(SequenceGroup.SEQUENCE_STATE); assertEquals(state.get(), 2); }
public void testOnlyChildFailsToStartCausesAppToFail() throws Exception { TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) .child(EntitySpec.create(FailingEntity.class) .configure(FailingEntity.FAIL_ON_START, true))); FailingEntity child = (FailingEntity) Iterables.get(app.getChildren(), 0); startAndAssertException(app, ImmutableList.<Location>of()); assertHealthEventually(child, Lifecycle.ON_FIRE, false); assertHealthEventually(app, Lifecycle.ON_FIRE, false); }
@Override protected TestApplication createApp() { EntitySpec<TestApplication> spec = EntitySpec.create(TestApplication.class, TestApplicationNoEnrichersImpl.class); if (shouldSkipOnBoxBaseDirResolution()!=null) { spec.configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution()); } return origManagementContext.getEntityManager().createEntity(spec); }
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); final LocationManager lm = mgmt.getLocationManager(); loc1 = lm.createLocation(LocationSpec.create(SimulatedLocation.class).configure("displayName", "newloc1")); loc2 = lm.createLocation(LocationSpec.create(SimulatedLocation.class).configure("displayName", "newloc2")); loc3 = lm.createLocation(LocationSpec.create(SimulatedLocation.class).configure("displayName", "newloc3")); fabric = app.createAndManageChild(EntitySpec.create(DynamicRegionsFabric.class) .configure("memberSpec", EntitySpec.create(TestEntity.class))); }