@Test(
      description = "PUT /admin/catalog/{id}/owner",
      dependsOnMethods = {"testGetCatalog"})
  public void editCatalogOwner() {
    User newOwnerUser = randomTestUser("testEditCatalogOwner");
    newOwnerUser = adminContext.getApi().getUserApi().addUserToOrg(newOwnerUser, org.getId());
    assertNotNull(newOwnerUser, "failed to add temp user to test editCatalogOwner");

    Owner oldOwner = owner;
    Owner newOwner =
        Owner.builder()
            .type("application/vnd.vmware.vcloud.owner+xml")
            .user(Reference.builder().fromEntity(newOwnerUser).build())
            .build();

    try {
      catalogApi.setOwner(catalog.getId(), newOwner);
      owner = catalogApi.getOwner(catalog.getId());
      Checks.checkOwner(owner);
      assertTrue(
          equal(
              owner.toBuilder().links(ImmutableSet.<Link>of()).build(),
              newOwner.toBuilder().user(newOwner.getUser()).build()),
          String.format(OBJ_FIELD_UPDATABLE, CATALOG, "owner"));
    } finally {
      catalogApi.setOwner(catalog.getId(), oldOwner);
      owner = catalogApi.getOwner(catalog.getId());
      adminContext.getApi().getUserApi().remove(newOwnerUser.getHref());
    }
  }
 @AfterClass(alwaysRun = true)
 protected void tidyUp() {
   if (catalog != null) {
     try {
       catalogApi.remove(catalog.getId());
     } catch (Exception e) {
       logger.warn(e, "Error deleting admin catalog '%s'", catalog.getName());
     }
   }
 }
  @Test(
      description = "POST /org/{id}/catalog/{catalogId}/action/controlAccess",
      dependsOnMethods = {"testAddCatalog"})
  public void testEditAccessControl() {
    // Setup params
    ControlAccessParams params = catalogApi.getAccessControl(catalog.getId());

    // Call the method being tested
    ControlAccessParams modified = catalogApi.editAccessControl(catalog.getId(), params);

    // Check params are well formed
    checkControlAccessParams(modified);
  }
 @Test(
     description = "GET /admin/catalog/{id}/owner",
     dependsOnMethods = {"testGetCatalog"})
 public void testGetCatalogOwner() {
   owner = catalogApi.getOwner(catalog.getId());
   Checks.checkOwner(owner);
 }
  @Test(
      description = "GET /admin/catalog/{id}",
      dependsOnMethods = {"testAddCatalog"})
  public void testGetCatalog() {
    catalog = catalogApi.get(catalog.getId());

    Checks.checkAdminCatalog(catalog);
  }
  @Test(
      description = "GET /org/{id}/catalog/{catalogId}/controlAccess",
      dependsOnMethods = {"testAddCatalog"})
  public void testGetControlAccessControl() {
    // Call the method being tested
    ControlAccessParams params = catalogApi.getAccessControl(catalog.getId());

    // Check params are well formed
    checkControlAccessParams(params);
  }
  @Test(
      description = "DELETE /admin/catalog/{id}",
      dependsOnMethods = {"testAddCatalog"})
  public void testRemoveCatalog() {
    // assertEquals(catalog.getCatalogItems().getCatalogItems().size(), 0,
    // String.format(OBJ_FIELD_EMPTY_TO_DELETE, "Catalog", "CatalogItems",
    // catalog.getCatalogItems().getCatalogItems().toString()));
    AdminCatalog removeCatalog =
        AdminCatalog.builder()
            .name(name("Test Catalog "))
            .description("created by testAddCatalog()")
            .build();
    removeCatalog = catalogApi.addCatalogToOrg(removeCatalog, org.getId());
    catalogApi.remove(removeCatalog.getId());

    removeCatalog = catalogApi.get(removeCatalog.getId());
    assertNull(
        removeCatalog,
        String.format(OBJ_DEL, CATALOG, removeCatalog != null ? removeCatalog.toString() : ""));
  }
  @Test(description = "POST /admin/org/{id}/catalogs")
  public void testAddCatalog() {
    AdminCatalog newCatalog =
        AdminCatalog.builder()
            .name(name("Test Catalog "))
            .description("created by testAddCatalog()")
            .build();
    catalog = catalogApi.addCatalogToOrg(newCatalog, org.getId());

    Checks.checkAdminCatalog(catalog);

    // FIXME: documentation suggests we should wait for a task here
  }
  @Test(
      description = "PUT /admin/catalog/{id}",
      dependsOnMethods = {"testGetCatalogOwner"})
  public void testEditCatalog() {
    String oldName = catalog.getName();
    String newName = "new " + oldName;
    String oldDescription = catalog.getDescription();
    String newDescription = "new " + oldDescription;
    // TODO: can we edit/manage catalogItems directly like this? or does it just do a merge
    // (like metadata)
    // CatalogItems oldCatalogItems = catalog.getCatalogItems();
    // CatalogItems newCatalogItems = CatalogItems.builder().build();

    try {
      catalog =
          catalog
              .toBuilder()
              .name(newName)
              .description(newDescription)
              // .catalogItems(newCatalogItems)
              .build();

      catalog = catalogApi.edit(catalog.getId(), catalog);

      assertTrue(
          equal(catalog.getName(), newName), String.format(OBJ_FIELD_UPDATABLE, CATALOG, "name"));
      assertTrue(
          equal(catalog.getDescription(), newDescription),
          String.format(OBJ_FIELD_UPDATABLE, CATALOG, "description"));
      // assertTrue(equal(catalog.getCatalogItems(), newCatalogItems),
      // String.format(OBJ_FIELD_UPDATABLE, CATALOG, "catalogItems"));

      // TODO negative tests?

      Checks.checkAdminCatalog(catalog);
    } finally {
      catalog =
          catalog
              .toBuilder()
              .name(oldName)
              .description(oldDescription)
              // .catalogItems(oldCatalogItems)
              .build();

      catalog = catalogApi.edit(catalog.getId(), catalog);
    }
  }
  // FIXME fails with a 403
  @Test(
      description = "POST /admin/catalog/{id}/action/publish",
      dependsOnMethods = {"testEditCatalog"})
  public void testPublishCatalog() {
    assertNotNull(catalog, String.format(NOT_NULL_OBJ_FMT, "Catalog"));
    assertFalse(
        catalog.isPublished(),
        String.format(OBJ_FIELD_EQ, CATALOG, "isPublished", false, catalog.isPublished()));

    PublishCatalogParams params = PublishCatalogParams.builder().isPublished(true).build();

    catalogApi.publish(catalog.getId(), params);
    catalog = catalogApi.get(catalog.getId());

    assertTrue(
        catalog.isPublished(),
        String.format(OBJ_FIELD_EQ, CATALOG, "isPublished", true, catalog.isPublished()));
  }
 public static final AdminCatalog catalog() {
   return AdminCatalog.builder()
       .name("QunyingTestCatalog")
       .id("urn:vcloud:catalog:7212e451-76e1-4631-b2de-ba1dfd8080e4")
       .type("application/vnd.vmware.admin.catalog+xml")
       .href(
           URI.create(
               "https://vcloudbeta.bluelock.com/api/admin/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
       .link(
           Link.builder()
               .rel("up")
               .type("application/vnd.vmware.admin.organization+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
               .build())
       .link(
           Link.builder()
               .rel("alternate")
               .type("application/vnd.vmware.vcloud.catalog+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
               .build())
       .link(
           Link.builder()
               .rel("down")
               .type("application/vnd.vmware.vcloud.owner+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4/owner"))
               .build())
       .link(
           Link.builder()
               .rel("add")
               .type("application/vnd.vmware.vcloud.catalogItem+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4/catalogItems"))
               .build())
       .link(
           Link.builder()
               .rel("edit")
               .type("application/vnd.vmware.admin.catalog+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
               .build())
       .link(
           Link.builder()
               .rel("remove")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
               .build())
       .link(
           Link.builder()
               .rel("down")
               .type("application/vnd.vmware.vcloud.metadata+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4/metadata"))
               .build())
       .description("Testing")
       .owner(owner())
       .item(
           Reference.builder()
               .type("application/vnd.vmware.vcloud.catalogItem+xml")
               .name("image")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalogItem/67a469a1-aafe-4b5b-bb31-a6202ad8961f"))
               .build())
       .item(
           Reference.builder()
               .type("application/vnd.vmware.vcloud.catalogItem+xml")
               .name("ubuntu10")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalogItem/a36fdac9-b8c2-43e2-9a4c-2ffaf3ee13df"))
               .build())
       .item(
           Reference.builder()
               .type("application/vnd.vmware.vcloud.catalogItem+xml")
               .name("imageTesting")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalogItem/a9e0afdb-a42b-4688-8409-2ac68cf22939"))
               .build())
       .item(
           Reference.builder()
               .type("application/vnd.vmware.vcloud.catalogItem+xml")
               .name("TestCase")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalogItem/f7598606-aea4-41d7-8f67-2090e28e7876"))
               .build())
       .isPublished(false)
       .build();
 }
 public static final AdminCatalog addCatalogToOrg() {
   return AdminCatalog.builder()
       .name("Test Catalog")
       .id("urn:vcloud:catalog:c56d9159-7838-446f-bb35-9ee12dfbbef3")
       .type("application/vnd.vmware.admin.catalog+xml")
       .description("created by testCreateCatalog()")
       .href(
           URI.create(
               "https://vcloudbeta.bluelock.com/api/admin/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3"))
       .link(
           Link.builder()
               .rel("up")
               .type("application/vnd.vmware.admin.organization+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
               .build())
       .link(
           Link.builder()
               .rel("alternate")
               .type("application/vnd.vmware.vcloud.catalog+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3"))
               .build())
       .link(
           Link.builder()
               .rel("down")
               .type("application/vnd.vmware.vcloud.owner+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3/owner"))
               .build())
       .link(
           Link.builder()
               .rel("add")
               .type("application/vnd.vmware.vcloud.catalogItem+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3/catalogItems"))
               .build())
       .link(
           Link.builder()
               .rel("edit")
               .type("application/vnd.vmware.admin.catalog+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3"))
               .build())
       .link(
           Link.builder()
               .rel("remove")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3"))
               .build())
       .link(
           Link.builder()
               .rel("down")
               .type("application/vnd.vmware.vcloud.metadata+xml")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3/metadata"))
               .build())
       .tasks(
           ImmutableSet.<Task>builder()
               .add(
                   Task.builder()
                       .status("running")
                       .startTime(dateService.iso8601DateParse("2012-03-11T18:43:02.429-04:00"))
                       .operationName("catalogCreateCatalog")
                       .operation(
                           "Creating Catalog Test Catalog(c56d9159-7838-446f-bb35-9ee12dfbbef3)")
                       .expiryTime(dateService.iso8601DateParse("2012-06-09T18:43:02.429-04:00"))
                       .name("task")
                       .id("urn:vcloud:task:20f556f9-9125-4090-9092-0da9f72bedf4")
                       .type("application/vnd.vmware.vcloud.task+xml")
                       .href(
                           URI.create(
                               "https://vcloudbeta.bluelock.com/api/task/20f556f9-9125-4090-9092-0da9f72bedf4"))
                       .link(
                           Link.builder()
                               .rel("task:cancel")
                               .href(
                                   URI.create(
                                       "https://vcloudbeta.bluelock.com/api/task/20f556f9-9125-4090-9092-0da9f72bedf4/action/cancel"))
                               .build())
                       .owner(
                           Reference.builder()
                               .type("application/vnd.vmware.vcloud.catalog+xml")
                               .name("Test Catalog")
                               .href(
                                   URI.create(
                                       "https://vcloudbeta.bluelock.com/api/catalog/c56d9159-7838-446f-bb35-9ee12dfbbef3"))
                               .build())
                       .user(
                           Reference.builder()
                               .type("application/vnd.vmware.admin.user+xml")
                               .name("*****@*****.**")
                               .href(
                                   URI.create(
                                       "https://vcloudbeta.bluelock.com/api/admin/user/ae75edd2-12de-414c-8e85-e6ea10442c08"))
                               .build())
                       .org(
                           Reference.builder()
                               .type("application/vnd.vmware.vcloud.org+xml")
                               .name("JClouds")
                               .href(
                                   URI.create(
                                       "https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
                               .build())
                       .build())
               .build())
       .isPublished(false)
       .build();
 }
 public static final AdminCatalog addCatalogToOrgSource() {
   return AdminCatalog.builder()
       .name("Test Catalog")
       .description("created by testCreateCatalog()")
       .build();
 }