@Test public void testManageFailsIfEntityDeleted() throws Exception { TestEntity child = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .parent(app)); Entities.unmanage(child); try { Entities.manage(child); fail("Managed deleted entity "+child+" in "+mgmt); } catch (IllegalArgumentException e) { if (!(e.toString().contains("Can't manage"))) throw e; } }
@Test public void testAddChildWhenMatchesParentSetInSpec() { Entity e = app.addChild(EntitySpec.create(TestEntity.class).parent(app)); assertEquals(e.getParent(), app); assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); }
childSpec.parent(entity); Entity child = createEntityAndDescendantsUninitialized(childSpec, Optional.absent(), entitiesByEntityId, specsByEntityId); entity.addChild(child);
@Test public void testNewEntityWithParent() throws Exception { TestEntity e = app.addChild(EntitySpec.create(TestEntity.class) .parent(app)); assertTrue(Entities.isManaged(e)); assertEquals(e.getParent(), app); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); }
@Test public void testSpecDeclaresParent() { Entity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class).parent(app)); assertEquals(e.getParent(), app); assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); }
@Test public void testAddingSameChildAgainIsNoop() throws Exception { TestEntity e = app.addChild(EntitySpec.create(TestEntity.class) .parent(app)); app.addChild(e); assertTrue(Entities.isManaged(e)); assertEquals(e.getParent(), app); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); }
/** * Creates an entity using the given spec, and adds it as a child of this entity. * * @see #addChild(Entity) * @see EntityManager#createEntity(EntitySpec) * * @throws IllegalArgumentException If {@code spec.getParent()} is set and is different from this entity */ @Override public <T extends Entity> T addChild(EntitySpec<T> spec) { if (spec.getParent()==null) { spec = EntitySpec.create(spec).parent(getProxyIfAvailable()); } if (!this.equals(spec.getParent())) { throw new IllegalArgumentException("Attempt to create child of "+this+" with entity spec "+spec+ " failed because spec has different parent: "+spec.getParent()); } // The spec now includes this as the parent, so no need to call addChild; // that is done by InternalEntityFactory. return getEntityManager().createEntity(spec); }
@Test public void testManageIsNoop() throws Exception { TestEntity child = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .parent(app)); Entities.manage(child); assertTrue(Entities.isManaged(child)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, child))); }
@Test public void testSetParentWhenMatchesParentSetInSpec() { Entity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class).parent(app)); e.setParent(app); assertEquals(e.getParent(), app); assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); }
@Test public void testAddChildWhenDifferentParentSetInSpec() { TestApplication app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); try { Entity e = app.addChild(EntitySpec.create(TestEntity.class).parent(app2)); Asserts.shouldHaveFailedPreviously("entity="+e); } catch (Exception ex) { Exception cause = Exceptions.getFirstThrowableOfType(ex, IllegalArgumentException.class); if (cause == null || !cause.toString().contains("failed because spec has different parent")) { throw ex; } } }
@Test public void testCommandExecutedWithEnvironment() throws Exception { TestEntity member = entityManager.createEntity(EntitySpec.create(TestEntity.class).parent(group)); assertExecSizeEventually(1); assertEquals(RecordingSshTool.getLastExecCmd().commands, ImmutableList.of("echo ignored")); Map<?, ?> env = RecordingSshTool.getLastExecCmd().env; assertTrue(env.containsKey(SshCommandMembershipTrackingPolicy.EVENT_TYPE)); assertEquals(env.get(SshCommandMembershipTrackingPolicy.EVENT_TYPE), EventType.ENTITY_ADDED.name()); assertTrue(env.containsKey(SshCommandMembershipTrackingPolicy.MEMBER_ID)); assertEquals(env.get(SshCommandMembershipTrackingPolicy.MEMBER_ID), member.getId()); assertTrue(env.containsKey("TEST")); assertEquals(env.get("TEST"), "test"); member.sensors().set(Startable.SERVICE_UP, true); Duration.seconds(1).countdownTimer().waitForExpiry(); assertExecSizeEventually(2); assertEquals(RecordingSshTool.getLastExecCmd().env.get(SshCommandMembershipTrackingPolicy.EVENT_TYPE), EventType.ENTITY_CHANGE.name()); member.clearParent(); assertExecSizeEventually(3); assertEquals(RecordingSshTool.getLastExecCmd().env.get(SshCommandMembershipTrackingPolicy.EVENT_TYPE), EventType.ENTITY_REMOVED.name()); }
private TestEntity createAndManageChildOf(Entity parent) { EntityManager entityManager = app.getManagementContext().getEntityManager(); TestEntity result = entityManager.createEntity(EntitySpec.create(TestEntity.class).parent(parent)); return result; }
@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 testChildCanOverrideConfigUsingFlagName() throws Exception { EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class) .configure(MyChildEntity.MY_CHILD_CONFIG_WITH_FLAGNAME, "myval")); assertEquals(entity.config().getBag().getAllConfigAsConfigKeyMap(), ImmutableMap.of(MyChildEntity.MY_CHILD_CONFIG_WITH_FLAGNAME, "myval")); EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class) .parent(entity) .configure("mychildconfigflagname", "overrideMyval")); assertEquals(child.config().getBag().getAllConfigAsConfigKeyMap(), ImmutableMap.of(MyChildEntity.MY_CHILD_CONFIG_WITH_FLAGNAME, "overrideMyval")); assertEquals(child.config().getBag().getAllConfig(), ImmutableMap.of("mychildentity.myconfigwithflagname", "overrideMyval")); assertEquals(child.config().getLocalBag().getAllConfig(), ImmutableMap.of("mychildentity.myconfigwithflagname", "overrideMyval")); }
@Test public void testSetParentWhenDiffersFromParentSetInSpec() { Entity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class).parent(app)); Entity e2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); try { e.setParent(e2); Asserts.shouldHaveFailedPreviously(); } catch (Exception ex) { Exception uoe = Exceptions.getFirstThrowableOfType(ex, UnsupportedOperationException.class); if (uoe == null || !uoe.toString().contains("Cannot change parent")) { throw ex; } } }
@Test public void testNewEntityHierarchyWithParent() throws Exception { TestEntity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .child(EntitySpec.create(TestEntity.class) .child(EntitySpec.create(TestEntity.class))) .parent(app)); TestEntity e2 = (TestEntity) Iterables.getOnlyElement(e.getChildren()); TestEntity e3 = (TestEntity) Iterables.getOnlyElement(e2.getChildren()); assertTrue(Entities.isManaged(e)); assertTrue(Entities.isManaged(e2)); assertTrue(Entities.isManaged(e3)); assertEquals(e.getParent(), app); assertEquals(e2.getParent(), e); assertEquals(e3.getParent(), e2); listener.assertEventsEqualsEventually(ImmutableList.of( new ChangeEvent(ChangeType.ADDED, e), new ChangeEvent(ChangeType.ADDED, e2), new ChangeEvent(ChangeType.ADDED, e3))); }
@Test public void testChildConfigBagInheritsUnmatchedAtParent() throws Exception { EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class) .configure("mychildentity.myconfig", "myval1") .configure("mychildconfigflagname", "myval2") .configure("notThere", "notThereVal")); EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class) .parent(entity)); assertEquals(child.config().getBag().getAllConfigAsConfigKeyMap(), ImmutableMap.of(MyChildEntity.MY_CHILD_CONFIG, "myval1", ConfigKeys.newConfigKey(Object.class, "mychildconfigflagname"), "myval2", ConfigKeys.newConfigKey(Object.class, "notThere"), "notThereVal")); assertEquals(child.config().getBag().getAllConfig(), ImmutableMap.of("mychildentity.myconfig", "myval1", "mychildconfigflagname", "myval2", "notThere", "notThereVal")); assertEquals(child.config().getLocalBag().getAllConfig(), ImmutableMap.of()); }
@Test public void testChildCanOverrideConfigUsingKeyName() throws Exception { EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class) .configure("mychildentity.myconfigwithflagname", "myval") .configure("notThere", "notThereVal")); EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class) .parent(entity) .configure("mychildentity.myconfigwithflagname", "overrideMyval") .configure("notThere", "overrideNotThereVal")); assertEquals(child.config().getBag().getAllConfigAsConfigKeyMap(), ImmutableMap.of(MyChildEntity.MY_CHILD_CONFIG_WITH_FLAGNAME, "overrideMyval", ConfigKeys.newConfigKey(Object.class, "notThere"), "overrideNotThereVal")); assertEquals(child.config().getBag().getAllConfig(), ImmutableMap.of("mychildentity.myconfigwithflagname", "overrideMyval", "notThere", "overrideNotThereVal")); assertEquals(child.config().getLocalBag().getAllConfig(), ImmutableMap.of("mychildentity.myconfigwithflagname", "overrideMyval", "notThere", "overrideNotThereVal")); }
@Override protected EntitySpec<T> copyFrom(EntitySpec<T> otherSpec) { super.copyFrom(otherSpec) .additionalInterfaces(otherSpec.getAdditionalInterfaces()) .policySpecs(otherSpec.getPolicySpecs()) .policies(otherSpec.getPolicies()) .enricherSpecs(otherSpec.getEnricherSpecs()) .enrichers(otherSpec.getEnrichers()) .addInitializers(otherSpec.getInitializers()) .children(copyFromSpecs(otherSpec.getChildren())) .members(otherSpec.getMembers()) .groups(otherSpec.getGroups()) .locationSpecs(otherSpec.getLocationSpecs()) .locations(otherSpec.getLocations()); if (otherSpec.getParent() != null) parent(otherSpec.getParent()); if (otherSpec.getImplementation() != null) impl(otherSpec.getImplementation()); return this; }