@Test public void testCreatingEntitySpecFromSpecCreatesDuplicate() { EntitySpec<TestEntity> originalChildSpec = EntitySpec.create(TestEntity.class); EntitySpec<TestEntity> originalEntitySpec = EntitySpec.create(TestEntity.class).child(originalChildSpec); EntitySpec<TestEntity> duplicateEntitySpec = EntitySpec.create(originalEntitySpec); EntitySpec<?> duplicateChildSpec = duplicateEntitySpec.getChildren().get(0); assertEquals(originalEntitySpec, duplicateEntitySpec); assertTrue(originalEntitySpec != duplicateEntitySpec); assertEquals(originalChildSpec, duplicateChildSpec); assertTrue(originalChildSpec != duplicateChildSpec); }
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); } }
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); group = app.createAndManageChild(EntitySpec.create(BasicGroup.class)); }
@Override public void run() { TestEntity e3 = app.addChild(EntitySpec.create(TestEntity.class) .displayName("e3")); e3ref.set(e3); }});
@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 testExplicitDisplayName() { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .displayName("myDisplayName")); assertEquals(entity.getDisplayName(), "myDisplayName"); }
@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"); }
@Test public void testConfigSetOnParentInheritedByExistingChildren() throws Exception { TestEntity parent = app.addChild(EntitySpec.create(TestEntity.class)); TestEntity entity = parent.createChild(EntitySpec.create(TestEntity.class)); parent.config().set(strKey,"aval"); assertEquals(entity.getConfig(strKey), "aval"); }
@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 testFindSubnetIpFromLocationWithoutPrivate() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineWithoutPrivateSpec)); assertEquals(Machines.findSubnetIp(entity).get(), publicAddr); }
@Test public void testStartManagementFailsIfAppDeleted() throws Exception { TestApplication app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); Entities.unmanage(app2); try { Entities.startManagement(app2, mgmt); fail("Managed deleted app "+app2+" in "+mgmt); } catch (IllegalStateException e) { if (!(e.toString().contains("No concrete entity known"))) throw e; } }
@Test public void testConfigFilterPresent() throws Exception { MySubEntity entity = app.addChild(EntitySpec.create(MySubEntity.class)); entity.config().set(MyBaseEntity.SUPER_KEY_1, "s1"); entity.config().set(MySubEntity.SUB_KEY_2, "s2"); Set<ConfigKey<?>> filteredKeys = entity.config().getInternalConfigMap().findKeysPresent(ConfigPredicates.nameMatchesGlob("sup*")); Assert.assertTrue(filteredKeys.contains(MyBaseEntity.SUPER_KEY_1)); Assert.assertFalse(filteredKeys.contains(MySubEntity.SUB_KEY_2)); Assert.assertFalse(filteredKeys.contains(MyBaseEntity.SUPER_KEY_2)); Asserts.assertSize(filteredKeys, 1); }
@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()); }
@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 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); }