@Test
  public void testRebindAfterItemDeprecated() {
    String symbolicName = "rebind-yaml-catalog-item-test";
    String yaml =
        Joiner.on("\n")
            .join(
                "brooklyn.catalog:",
                "  items:",
                "  - id: " + symbolicName,
                "    version: " + TEST_VERSION,
                "    itemType: entity",
                "    item:",
                "      type: io.camp.mock:AppServer");
    CatalogEntityItemDto item =
        CatalogItemBuilder.newEntity(symbolicName, TEST_VERSION)
            .displayName(symbolicName)
            .plan(yaml)
            .build();
    origManagementContext.getCatalog().addItem(item);
    assertNotNull(item, "catalogItem");
    BasicBrooklynCatalog catalog = (BasicBrooklynCatalog) origManagementContext.getCatalog();

    item.setDeprecated(true);
    catalog.persist(item);
    rebindAndAssertCatalogsAreEqual();
    RegisteredType catalogItemAfterRebind =
        newManagementContext.getTypeRegistry().get("rebind-yaml-catalog-item-test", TEST_VERSION);
    assertTrue(catalogItemAfterRebind.isDeprecated(), "Expected item to be deprecated");
  }
 @Test
 public void testSameCatalogItemIdRemovalAndAdditionRebinds() throws Exception {
   // The test is not reliable on Windows (doesn't catch the pre-fix problem) -
   // the store is unable to delete still locked files so the bug doesn't manifest.
   // TODO investigate if locked files not caused by unclosed streams!
   String symbolicName = "rebind-yaml-catalog-item-test";
   String yaml =
       Joiner.on("\n")
           .join(
               "brooklyn.catalog:",
               "  items:",
               "  - id: " + symbolicName,
               "    version: " + TEST_VERSION,
               "    itemType: entity",
               "    item:",
               "      type: io.camp.mock:AppServer");
   BasicBrooklynCatalog catalog = (BasicBrooklynCatalog) origManagementContext.getCatalog();
   CatalogEntityItemDto item =
       CatalogItemBuilder.newEntity(symbolicName, TEST_VERSION)
           .displayName(symbolicName)
           .plan(yaml)
           .build();
   catalog.addItem(item);
   String catalogXml = catalog.toXmlString();
   catalog.reset(CatalogDto.newDtoFromXmlContents(catalogXml, "Test reset"));
   rebindAndAssertCatalogsAreEqual();
 }
 @Test
 public void testAddAndRebindPolicy() {
   // Doesn't matter that SamplePolicy doesn't exist
   String symbolicName = "Test Policy";
   String yaml =
       Joiner.on("\n")
           .join(
               "brooklyn.catalog:",
               "  items:",
               "  - id: " + symbolicName,
               "    version: " + TEST_VERSION,
               "    itemType: policy",
               "    item:",
               "      type: org.apache.brooklyn.core.mgmt.rebind.RebindCatalogItemTest$MyPolicy",
               "      brooklyn.config:",
               "        cfg1: 111",
               "        cfg2: 222");
   CatalogPolicyItemDto item =
       CatalogItemBuilder.newPolicy(symbolicName, TEST_VERSION)
           .displayName(symbolicName)
           .plan(yaml)
           .build();
   origManagementContext.getCatalog().addItem(item);
   LOG.info("Added item to catalog: {}, id={}", item, item.getId());
   rebindAndAssertCatalogsAreEqual();
 }
  private void doTestAddAndRebindAndDeleteLocation(boolean suppressPeriodicCheckpointing)
      throws Exception {
    String symbolicName = "sample_location";
    String yaml =
        Joiner.on("\n")
            .join(
                "brooklyn.catalog:",
                "  items:",
                "  - id: " + symbolicName,
                "    version: " + TEST_VERSION,
                "    itemType: location",
                "    item:",
                "      type: " + LocalhostMachineProvisioningLocation.class.getName(),
                "      brooklyn.config:",
                "        cfg1: 111",
                "        cfg2: 222");
    CatalogLocationItemDto item =
        CatalogItemBuilder.newLocation(symbolicName, TEST_VERSION)
            .displayName(symbolicName)
            .plan(yaml)
            .build();
    origManagementContext.getCatalog().addItem(item);
    assertEquals(item.getCatalogItemType(), CatalogItemType.LOCATION);
    if (!suppressPeriodicCheckpointing) {
      rebindAndAssertCatalogsAreEqual();
    } else {
      LocalManagementContext nmc =
          RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
              .forLive(useLiveManagementContext())
              .emptyCatalog(useEmptyCatalog())
              .persistPeriod(Duration.PRACTICALLY_FOREVER)
              .buildUnstarted();
      rebind(RebindOptions.create().newManagementContext(nmc));
    }

    deleteItem(newManagementContext, item.getSymbolicName(), item.getVersion());

    switchOriginalToNewManagementContext();
    rebindAndAssertCatalogsAreEqual();
  }
 @Test
 public void testAddAndRebindEntityLegacyFormat() throws Exception {
   String symbolicName = "rebind-yaml-catalog-item-test";
   String yaml =
       "name: "
           + symbolicName
           + "\n"
           + "brooklyn.catalog:\n"
           + "  version: "
           + TEST_VERSION
           + "\n"
           + "services:\n"
           + "- type: io.camp.mock:AppServer";
   CatalogEntityItemDto item =
       CatalogItemBuilder.newEntity(symbolicName, TEST_VERSION)
           .displayName(symbolicName)
           .plan(yaml)
           .build();
   origManagementContext.getCatalog().addItem(item);
   LOG.info("Added item to catalog: {}, id={}", item, item.getId());
   rebindAndAssertCatalogsAreEqual();
 }
 @Test
 public void testAddAndRebindEntity() throws Exception {
   String symbolicName = "rebind-yaml-catalog-item-test";
   String yaml =
       Joiner.on("\n")
           .join(
               "brooklyn.catalog:",
               "  items:",
               "  - id: " + symbolicName,
               "    version: " + TEST_VERSION,
               "    itemType: entity",
               "    item:",
               "      type: io.camp.mock:AppServer");
   CatalogEntityItemDto item =
       CatalogItemBuilder.newEntity(symbolicName, TEST_VERSION)
           .displayName(symbolicName)
           .plan(yaml)
           .build();
   origManagementContext.getCatalog().addItem(item);
   LOG.info("Added item to catalog: {}, id={}", item, item.getId());
   rebindAndAssertCatalogsAreEqual();
 }