@Test
 public void testWrapsEntityIfDifferentTopLevelName() throws Exception {
   Entity app =
       createAndStartApplication(
           "name: topLevel",
           "services:",
           "- type: " + BasicApplication.class.getName(),
           "  name: bottomLevel");
   assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertEquals(app.getDisplayName(), "topLevel");
   assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicApplication);
   assertTrue(Iterables.getOnlyElement(app.getChildren()).getChildren().isEmpty());
   assertEquals(Iterables.getOnlyElement(app.getChildren()).getDisplayName(), "bottomLevel");
 }
 @Test
 public void testWrapsEntity() throws Exception {
   Entity app = createAndStartApplication("services:", "- type: " + BasicEntity.class.getName());
   assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicEntity);
 }
  private JsonNode recursiveTreeFromEntity(Entity entity) {
    ObjectNode aRoot = entityBase(entity);

    if (!entity.getChildren().isEmpty())
      aRoot.put("children", childEntitiesRecursiveAsArray(entity));

    return aRoot;
  }
 @Test
 public void testDoesNotWrapApp() throws Exception {
   Entity app =
       createAndStartApplication("services:", "- type: " + BasicApplication.class.getName());
   assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertTrue(app.getChildren().isEmpty());
 }
  @Test
  public void testParametersCoercedOnSetAndReferences() throws Exception {
    Integer testValue = Integer.valueOf(55);
    addCatalogItems(
        "brooklyn.catalog:",
        "  id: " + SYMBOLIC_NAME,
        "  version: " + TEST_VERSION,
        "  itemType: entity",
        "  item:",
        "    type: " + BasicApplication.class.getName(),
        "    brooklyn.parameters:",
        "    - name: num",
        "      type: integer",
        "    brooklyn.children:",
        "    - type: " + ConfigEntityForTest.class.getName(),
        "      brooklyn.config:",
        "        refConfig: $brooklyn:scopeRoot().config(\"num\")",
        "    - type: " + ConfigEntityForTest.class.getName(),
        "      brooklyn.config:",
        "        refConfig: $brooklyn:config(\"num\")"); // inherited config

    Entity app =
        createAndStartApplication(
            "services:",
            "- type: " + BasicApplication.class.getName(),
            "  brooklyn.children:",
            "  - type: " + ver(SYMBOLIC_NAME),
            "    brooklyn.config:",
            "      num: \"" + testValue + "\"");

    Entity scopeRoot = Iterables.getOnlyElement(app.getChildren());

    ConfigKey<Object> numKey = ConfigKeys.newConfigKey(Object.class, "num");
    assertEquals(scopeRoot.config().get(numKey), testValue);

    ConfigKey<Object> refConfigKey = ConfigKeys.newConfigKey(Object.class, "refConfig");

    Iterator<Entity> childIter = scopeRoot.getChildren().iterator();
    Entity c1 = childIter.next();
    assertEquals(c1.config().get(refConfigKey), testValue);
    Entity c2 = childIter.next();
    assertEquals(c2.config().get(refConfigKey), testValue);
    assertFalse(childIter.hasNext());
  }
 private ArrayNode childEntitiesRecursiveAsArray(Entity entity) {
   ArrayNode node = mapper().createArrayNode();
   for (Entity e : entity.getChildren()) {
     if (Entitlements.isEntitled(
         mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, entity)) {
       node.add(recursiveTreeFromEntity(e));
     }
   }
   return node;
 }
 @Test
 public void testDoesNotWrapsEntityIfNoNameOnService() throws Exception {
   Entity app =
       createAndStartApplication(
           "name: topLevel", "services:", "- type: " + BasicApplication.class.getName());
   assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertTrue(app.getChildren().isEmpty());
   assertEquals(app.getDisplayName(), "topLevel");
 }
  // TODO when applicationTree can be removed, replace this with an extension to EntitySummary
  // (without links)
  private JsonNode fromEntity(Entity entity) {
    ObjectNode aRoot = entityBase(entity);

    aRoot.put("applicationId", entity.getApplicationId());

    if (entity.getParent() != null) {
      aRoot.put("parentId", entity.getParent().getId());
    }

    if (!entity.groups().isEmpty()) aRoot.put("groupIds", entitiesIdAsArray(entity.groups()));

    if (!entity.getChildren().isEmpty())
      aRoot.put("children", entitiesIdAndNameAsArray(entity.getChildren()));

    if (entity instanceof Group) {
      // use attribute instead of method in case it is read-only
      Collection<Entity> members = entity.getAttribute(AbstractGroup.GROUP_MEMBERS);
      if (members != null && !members.isEmpty())
        aRoot.put("members", entitiesIdAndNameAsArray(members));
    }

    return aRoot;
  }