Java Code Examples for org.apache.brooklyn.api.entity.Entity#addChild()

The following examples show how to use org.apache.brooklyn.api.entity.Entity#addChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OwnedChildrenDeprecatedTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testParentalLoopForbiddenViaAddChild() {
    Entity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class));
    Entity e2 = e.addChild(EntitySpec.create(TestEntity.class));
    try {
        e2.addChild(e);
        Asserts.shouldHaveFailedPreviously();
    } catch (Exception ex) {
        Exception cause = Exceptions.getFirstThrowableOfType(ex, IllegalStateException.class);
        if (cause == null || !cause.toString().contains("loop detected trying to add child")) {
            throw ex;
        }
    }
    assertEqualsIgnoringOrder(e.getChildren(), ImmutableList.of(e2));
    assertEqualsIgnoringOrder(e2.getChildren(), ImmutableList.of());
    assertEquals(e.getParent(), null);
    assertEquals(e2.getParent(), e);
}
 
Example 2
Source File: OwnedChildrenDeprecatedTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testParentalLoopForbiddenViaSetParent() {
    Entity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class));
    Entity e2 = e.addChild(EntitySpec.create(TestEntity.class));
    try {
        e.setParent(e2);
        Asserts.shouldHaveFailedPreviously();
    } catch (Exception ex) {
        Exception cause = Exceptions.getFirstThrowableOfType(ex, IllegalStateException.class);
        if (cause == null || !cause.toString().contains("loop detected trying to set parent")) {
            throw ex;
        }
    }
    assertEqualsIgnoringOrder(e.getChildren(), ImmutableList.of(e2));
    assertEqualsIgnoringOrder(e2.getChildren(), ImmutableList.of());
    assertEquals(e.getParent(), null);
    assertEquals(e2.getParent(), e);
}
 
Example 3
Source File: OwnedChildrenDeprecatedTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testChildingOneselfForbidden() {
    Entity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class));
    try {
        e.addChild(e);
        Asserts.shouldHaveFailedPreviously();
    } catch (Exception ex) {
        Exception cause = Exceptions.getFirstThrowableOfType(ex, IllegalStateException.class);
        if (cause == null || !cause.toString().contains("cannot own itself")) {
            throw ex;
        }
    }
    
    assertNull(e.getParent());
    assertEquals(e.getChildren(), ImmutableList.of());
}
 
Example 4
Source File: OwnedChildrenTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testHierarchyOfOwners() {
    Entity e = app.addChild(EntitySpec.create(TestEntity.class));
    Entity e2 = e.addChild(EntitySpec.create(TestEntity.class));
    Entity e3 = e2.addChild(EntitySpec.create(TestEntity.class));
    
    assertEquals(app.getParent(), null);
    assertEquals(e.getParent(), app);
    assertEquals(e2.getParent(), e);
    assertEquals(e3.getParent(), e2);
    
    assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e));
    assertEqualsIgnoringOrder(e.getChildren(), ImmutableList.of(e2));
    assertEqualsIgnoringOrder(e2.getChildren(), ImmutableList.of(e3));
    assertEqualsIgnoringOrder(e3.getChildren(), ImmutableList.of());
}
 
Example 5
Source File: OsgiVersionMoreEntityTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testMoreEntitiesV1() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH);

    RegisteredType c2 = addMoreEntityV1(mgmt, TEST_VERSION);
    
    // test load and instantiate
    Entity me = addItemFromCatalog(c2);
    Assert.assertEquals(me.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION));
    
    assertV1MethodCall(me);
    assertV1EffectorCall(me);
    
    // test adding a child gets the right type; this time by entity parent hierarchy
    @SuppressWarnings({ "unchecked" })
    Entity me2 = me.addChild( mgmt.getTypeRegistry().createSpec(c2, null, EntitySpec.class) );
    Assert.assertEquals(me2.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION));
}
 
Example 6
Source File: AbstractEntity.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Adds this as a child of the given entity; registers with application if necessary.
 */
@Override
public AbstractEntity setParent(Entity entity) {
    if (!parent.isNull()) {
        // If we are changing to the same parent...
        if (parent.contains(entity)) return this;
        // If we have a parent but changing to orphaned...
        if (entity==null) { clearParent(); return this; }
        
        // We have a parent and are changing to another parent...
        throw new UnsupportedOperationException("Cannot change parent of "+this+" from "+parent+" to "+entity+" (parent change not supported)");
    }
    // If we have previously had a parent and are trying to change to another one...
    if (previouslyOwned && entity != null)
        throw new UnsupportedOperationException("Cannot set a parent of "+this+" as "+entity+" because it has previously had a parent");
    // We don't have a parent, never have and are changing to having a parent...

    //make sure there is no loop
    if (this.equals(entity)) throw new IllegalStateException("entity "+this+" cannot own itself");
    //this may be expensive, but preferable to throw before setting the parent!
    if (Entities.isDescendant(this, entity))
        throw new IllegalStateException("loop detected trying to set parent of "+this+" as "+entity+", which is already a descendent");
    
    parent.set(entity);
    entity.addChild(getProxyIfAvailable());
    config().refreshInheritedConfig();
    previouslyOwned = true;
    
    getApplication();
    
    return this;
}
 
Example 7
Source File: EntityManagementUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** adds entities from the given yaml, under the given parent; but does not start them */
public static List<Entity> addChildrenUnstarted(final Entity parent, String yaml) {
    log.debug("Creating child of "+parent+" from yaml:\n{}", yaml);

    ManagementContext mgmt = parent.getApplication().getManagementContext();

    EntitySpec<? extends Application> specA = createEntitySpecForApplication(mgmt, yaml);

    // see whether we can promote children
    List<EntitySpec<?>> specs = MutableList.of();
    if (!canUnwrapEntity(specA)) {
        // if not promoting, set a nice name if needed
        if (Strings.isEmpty(specA.getDisplayName())) {
            int size = specA.getChildren().size();
            String childrenCountString = size+" "+(size!=1 ? "children" : "child");
            specA.displayName("Dynamically added "+childrenCountString);
        }
    }
    
    specs.add(unwrapEntity(specA));

    final List<Entity> children = MutableList.of();
    for (EntitySpec<?> spec: specs) {
        Entity child = parent.addChild(spec);
        children.add(child);
    }

    return children;
}
 
Example 8
Source File: EntityConfigTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testInheritedDefault() {
    final Entity e1 = app.addChild(EntitySpec.create(MyBaseEntity.class));
    final Entity e2 = e1.addChild(EntitySpec.create(BasicEntity.class));
    Assert.assertEquals(e2.config().get(MyBaseEntity.SUPER_KEY_1), MyBaseEntity.SUPER_KEY_1.getDefaultValue());
    Assert.assertEquals(e2.config().get(ConfigKeys.newStringConfigKey(MyBaseEntity.SUPER_KEY_1.getName())), MyBaseEntity.SUPER_KEY_1.getDefaultValue());
}
 
Example 9
Source File: OwnedChildrenTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
  public void testIsAncestor() {
      Entity e = app.addChild(EntitySpec.create(TestEntity.class));
      Entity e2 = e.addChild(EntitySpec.create(TestEntity.class));
      
assertTrue(Entities.isAncestor(e2, app));
assertTrue(Entities.isAncestor(e2, e));
assertFalse(Entities.isAncestor(e2, e2));
      assertFalse(Entities.isAncestor(e, e2));
  }
 
Example 10
Source File: OwnedChildrenTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
  public void testIsDescendant() {
      Entity e = app.addChild(EntitySpec.create(TestEntity.class));
      Entity e2 = e.addChild(EntitySpec.create(TestEntity.class));

assertTrue(Entities.isDescendant(app, e));
assertTrue(Entities.isDescendant(app, e2));
assertFalse(Entities.isDescendant(e2, e));
      assertFalse(Entities.isDescendant(e2, e2));
  }