@Test(invocationCount = 3)
  public void testCanTagCatalogItemAfterRebind() {
    assertEquals(Iterables.size(origManagementContext.getCatalog().getCatalogItems()), 1);
    CatalogItem<Object, Object> toTag =
        Iterables.getOnlyElement(origManagementContext.getCatalog().getCatalogItems());
    final String tag = "tag1";
    toTag.tags().addTag(tag);
    assertTrue(toTag.tags().containsTag(tag));

    rebindAndAssertCatalogsAreEqual();

    toTag = Iterables.getOnlyElement(newManagementContext.getCatalog().getCatalogItems());
    assertTrue(toTag.tags().containsTag(tag));
    toTag.tags().removeTag(tag);
  }
 @Test(invocationCount = 3)
 public void testDeletedCatalogItemIsNotPersisted() {
   assertEquals(Iterables.size(origManagementContext.getCatalog().getCatalogItems()), 1);
   CatalogItem<Object, Object> toRemove =
       Iterables.getOnlyElement(origManagementContext.getCatalog().getCatalogItems());
   // Must make sure that the original catalogue item is not managed and unmanaged in the same
   // persistence window. Because BrooklynMementoPersisterToObjectStore applies writes/deletes
   // asynchronously the winner is down to a race and the test might pass or fail.
   origManagementContext.getRebindManager().forcePersistNow(false, null);
   origManagementContext
       .getCatalog()
       .deleteCatalogItem(toRemove.getSymbolicName(), toRemove.getVersion());
   assertEquals(Iterables.size(origManagementContext.getCatalog().getCatalogItems()), 0);
   rebindAndAssertCatalogsAreEqual();
   assertEquals(Iterables.size(newManagementContext.getCatalog().getCatalogItems()), 0);
 }
  private boolean isABrooklynPolicy(Optional<String> policyType, ManagementContext mgmt) {
    if (!policyType.isPresent()) {
      return false;
    }

    Class clazz;
    CatalogItem catalogItem = CatalogUtils.getCatalogItemOptionalVersion(mgmt, policyType.get());
    if (catalogItem != null) {
      clazz = catalogItem.getCatalogItemJavaType();
    } else {
      try {
        clazz = Class.forName(policyType.get());
      } catch (ClassNotFoundException e) {
        return false;
      }
    }
    return Policy.class.isAssignableFrom(clazz);
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  public <T, SpecT extends AbstractBrooklynObjectSpec<? extends T, SpecT>> SpecT createCatalogSpec(
      CatalogItem<T, SpecT> item, Set<String> encounteredTypes) {
    // Ignore old-style java type catalog items
    if (item.getPlanYaml() == null) {
      throw new PlanNotRecognizedException("Old style catalog item " + item + " not supported.");
    }
    if (encounteredTypes.contains(item.getSymbolicName())) {
      throw new IllegalStateException(
          "Already encountered types "
              + encounteredTypes
              + " must not contain catalog item being resolver "
              + item.getSymbolicName());
    }

    // Not really clear what should happen to the top-level attributes, ignored until a good use
    // case appears.
    return (SpecT) CampCatalogUtils.createSpec(mgmt, (CatalogItem) item, encounteredTypes);
  }
  /**
   * @deprecated since it was introduced in 0.9.0; for backwards compatibility only, may be removed
   *     at any point
   */
  @Deprecated
  public static RegisteredType of(CatalogItem<?, ?> item) {
    if (item == null) return null;
    TypeImplementationPlan impl = null;
    if (item.getPlanYaml() != null) {
      impl = new BasicTypeImplementationPlan(null, item.getPlanYaml());
    } else if (item.getJavaType() != null) {
      impl = new JavaClassNameTypeImplementationPlan(item.getJavaType());
    } else {
      throw new IllegalStateException(
          "Unsupported catalog item " + item + " when trying to create RegisteredType");
    }

    BasicRegisteredType type =
        (BasicRegisteredType)
            spec(item.getSymbolicName(), item.getVersion(), impl, item.getCatalogItemJavaType());
    type.displayName = item.getDisplayName();
    type.description = item.getDescription();
    type.iconUrl = item.getIconUrl();

    type.disabled = item.isDisabled();
    type.deprecated = item.isDeprecated();
    if (item.getLibraries() != null) type.bundles.addAll(item.getLibraries());
    // aliases aren't on item
    if (item.tags() != null) type.tags.addAll(item.tags().getTags());

    // these things from item we ignore: javaType, specType, registeredTypeName ...
    return type;
  }
 public void unregisterCatalogItem(CatalogItem<?, ?> item) {
   catalogItems.remove(item.getId());
 }
 /** @deprecated since 0.7.0, see {@link #newBasicMemento(BrooklynObject)} */
 @Deprecated
 public static CatalogItemMemento newCatalogItemMemento(CatalogItem<?, ?> catalogItem) {
   if (catalogItem instanceof CatalogItemDo<?, ?>) {
     catalogItem = ((CatalogItemDo<?, ?>) catalogItem).getDto();
   }
   BasicCatalogItemMemento.Builder builder = BasicCatalogItemMemento.builder();
   populateBrooklynObjectMementoBuilder(catalogItem, builder);
   builder
       .catalogItemJavaType(catalogItem.getCatalogItemJavaType())
       .catalogItemType(catalogItem.getCatalogItemType())
       .description(catalogItem.getDescription())
       .iconUrl(catalogItem.getIconUrl())
       .javaType(catalogItem.getJavaType())
       .libraries(catalogItem.getLibraries())
       .symbolicName(catalogItem.getSymbolicName())
       .specType(catalogItem.getSpecType())
       .version(catalogItem.getVersion())
       .planYaml(catalogItem.getPlanYaml())
       .deprecated(catalogItem.isDeprecated())
       .disabled(catalogItem.isDisabled());
   return builder.build();
 }