/** Used by other methods to add a custom {@link Task} object. */
  private static Task task(
      String taskId,
      String name,
      String description,
      String status,
      String operation,
      String operationName,
      String startTime) {
    Task task =
        Task.builder()
            .error(Error.builder().build())
            .org(Reference.builder().build())
            .owner(Reference.builder().build())
            .user(Reference.builder().build())
            .params(null)
            .progress(0)
            .status(status)
            .operation(operation)
            .operationName(operationName)
            .startTime(dateService.iso8601DateParse(startTime))
            .endTime(null)
            .expiryTime(null)
            .tasks(Sets.<Task>newLinkedHashSet())
            .description(description)
            .name(name)
            .id("urn:vcloud:" + taskId)
            .href(URI.create(endpoint + "/task/" + taskId))
            .links(Sets.<Link>newLinkedHashSet())
            .build();

    return task;
  }
 public static Task rebootTask() {
   return Task.builder()
       .id("urn:vcloud:task:5bc25a1b-79cb-4904-b53f-4ca09088e434")
       .name("task")
       .href(
           URI.create(
               "https://vcloudbeta.bluelock.com/api/task/5bc25a1b-79cb-4904-b53f-4ca09088e434"))
       .link(
           Link.builder()
               .rel(Link.Rel.TASK_CANCEL)
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/task/5bc25a1b-79cb-4904-b53f-4ca09088e434/action/cancel"))
               .build())
       .org(
           Reference.builder()
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/org/4559b367-8af2-4ee1-8429-a0d39e7df3de"))
               .name("jclouds")
               .type(VCloudDirectorMediaType.ORG)
               .build())
       .operation("Rebooting Virtual Machine damn small(b7e995a7-1468-4873-af39-bc703feefd63)")
       .operationName("vappReboot")
       .progress(0)
       .startTime(dateService.cDateParse("Thu Oct 25 13:39:25 EDT 2012"))
       .expiryTime(dateService.cDateParse("Wed Jan 23 13:39:25 EST 2013"))
       .status(Task.Status.RUNNING)
       .type(VCloudDirectorMediaType.TASK)
       .build();
 }
 public static final User createUserSource() {
   return User.builder()
       .name("test")
       .fullName("testFullName")
       .emailAddress("*****@*****.**")
       .telephone("555-1234")
       .isEnabled(false)
       .im("testIM")
       .isAlertEnabled(false)
       .alertEmailPrefix("testPrefix")
       .alertEmail("*****@*****.**")
       .isExternal(false)
       .isGroupRole(false)
       .role(
           Reference.builder()
               .type("application/vnd.vmware.admin.role+xml")
               .name("vApp User")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"))
               .build())
       .password("password")
       .groups(Collections.<Reference>emptyList())
       .build();
 }
  @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());
    }
  }
  @Test
  public void testUpdateNetwork() {
    VCloudDirectorClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("GET", "/admin/network/b466c0c5-8a5c-4335-b703-a2e2e6b5f3e1")
                .xmlFilePayload(
                    "/network/admin/updateNetworkSource.xml", VCloudDirectorMediaType.ORG_NETWORK)
                .acceptMedia(VCloudDirectorMediaType.TASK)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload(
                    "/network/admin/updateNetworkTask.xml", VCloudDirectorMediaType.TASK)
                .httpResponseBuilder()
                .build());

    Task expected = updateNetworkTask();

    assertEquals(
        client.getAdminNetworkClient().updateNetwork(networkRef.getHref(), updateNetwork()),
        expected);
  }
 public final Task updateNetworkTask() {
   return Task.builder()
       .status("running")
       .startTime(dateService.iso8601DateParse("2012-03-14T12:39:23.720-04:00"))
       .operationName("networkUpdateNetwork")
       .operation("Updating Network ilsolation01-Jclouds(f3ba8256-6f48-4512-aad6-600e85b4dc38)")
       .expiryTime(dateService.iso8601DateParse("2012-06-12T12:39:23.720-04:00"))
       .name("task")
       .id("urn:vcloud:task:49d2e180-7921-4902-ac39-b4ff5406bb94")
       .type("application/vnd.vmware.vcloud.task+xml")
       .href(
           URI.create(
               "https://vcloudbeta.bluelock.com/api/task/49d2e180-7921-4902-ac39-b4ff5406bb94"))
       .link(
           Link.builder()
               .rel("task:cancel")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/task/49d2e180-7921-4902-ac39-b4ff5406bb94/action/cancel"))
               .build())
       .owner(
           Reference.builder()
               .type("application/vnd.vmware.vcloud.network+xml")
               .name("ilsolation01-Jclouds")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
               .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();
 }
  // FIXME "Task error: Unable to perform this action. Contact your cloud administrator."
  @Test(
      description = "PUT /vApp/{id}/networkConnectionSection",
      dependsOnMethods = {"testEditGuestCustomizationSection"})
  public void testEditNetworkConnectionSection() {
    powerOffVm(vmUrn);
    // Look up a network in the Vdc
    Set<Reference> networks = vdc.getAvailableNetworks();
    Reference network = Iterables.getLast(networks);

    // Copy existing section and edit fields
    NetworkConnectionSection oldSection = vmApi.getNetworkConnectionSection(vmUrn);
    NetworkConnectionSection newSection =
        oldSection
            .toBuilder()
            .networkConnection(
                NetworkConnection.builder()
                    .ipAddressAllocationMode(IpAddressAllocationMode.DHCP.toString())
                    .network(network.getName())
                    .build())
            .build();

    // The method under test
    Task editNetworkConnectionSection = vmApi.editNetworkConnectionSection(vmUrn, newSection);
    assertTrue(
        retryTaskSuccess.apply(editNetworkConnectionSection),
        String.format(TASK_COMPLETE_TIMELY, "editNetworkConnectionSection"));

    // Retrieve the modified section
    NetworkConnectionSection modified = vmApi.getNetworkConnectionSection(vmUrn);

    // Check the retrieved object is well formed
    checkNetworkConnectionSection(modified);

    // Check the modified section has an extra network connection
    assertEquals(
        modified.getNetworkConnections().size(), newSection.getNetworkConnections().size() + 1);

    // Check the section was modified correctly
    assertEquals(modified, newSection, String.format(ENTITY_EQUAL, "NetworkConnectionSection"));
  }
 private static Owner owner() {
   return Owner.builder()
       .type("application/vnd.vmware.vcloud.owner+xml")
       .user(
           Reference.builder()
               .type("application/vnd.vmware.admin.user+xml")
               .name("*****@*****.**")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/user/967d317c-4273-4a95-b8a4-bf63b78e9c69"))
               .build())
       .build();
 }
 public final OrgNetwork orgNetwork() {
   return NetworkClientExpectTest.orgNetwork()
       .toBuilder()
       .href(toAdminUri(NetworkClientExpectTest.orgNetwork().getHref()))
       .links(
           ImmutableSet.of(
               Link.builder()
                   .rel("alternate")
                   .type("application/vnd.vmware.vcloud.orgNetwork+xml")
                   .href(
                       URI.create(
                           "https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
                   .build(),
               Link.builder()
                   .rel("edit")
                   .type("application/vnd.vmware.admin.orgNetwork+xml")
                   .href(
                       URI.create(
                           "https://vcloudbeta.bluelock.com/api/admin/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
                   .build(),
               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.builder()
                   .rel("repair")
                   .type("application/vnd.vmware.admin.orgNetwork+xml")
                   .href(
                       URI.create(
                           "https://vcloudbeta.bluelock.com/api/admin/network/f3ba8256-6f48-4512-aad6-600e85b4dc38/action/reset"))
                   .build(),
               Link.builder()
                   .rel("down")
                   .type("application/vnd.vmware.vcloud.metadata+xml")
                   .href(
                       URI.create(
                           "https://vcloudbeta.bluelock.com/api/admin/network/f3ba8256-6f48-4512-aad6-600e85b4dc38/metadata"))
                   .build()))
       .networkPool(
           Reference.builder()
               .type("application/vnd.vmware.admin.networkPool+xml")
               .name("vcdni01")
               .href(
                   URI.create(
                       "https://vcloudbeta.bluelock.com/api/admin/extension/networkPool/e86bfdb5-b3e0-4ece-9125-e764ac64c95c"))
               .build())
       .build();
 }
  @Test
  public void testDeleteUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("DELETE", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer().httpResponseBuilder().statusCode(204).build());

    client.getUserClient().deleteUser(userRef.getHref());
  }
  @Test(
      description = "PUT /vApp/{id}/media/action/ejectMedia",
      dependsOnMethods = {"testInsertMedia"})
  public void testEjectMedia() {
    // Setup media params from configured media id
    MediaInsertOrEjectParams params =
        MediaInsertOrEjectParams.builder()
            .media(Reference.builder().href(lazyGetMedia().getHref()).type(MEDIA).build())
            .build();

    // The method under test
    Task ejectMedia = vmApi.ejectMedia(vmUrn, params);
    assertTrue(
        retryTaskSuccess.apply(ejectMedia), String.format(TASK_COMPLETE_TIMELY, "ejectMedia"));
  }
  // NOTE This test is disabled, as it is not possible to look up datastores using the User API
  @Test(
      description = "POST /vApp/{id}/action/relocate",
      dependsOnMethods = {"testGetVm"})
  public void testRelocate() {
    // Relocate to the last of the available datastores
    QueryResultRecords records = context.getApi().getQueryApi().queryAll("datastore");
    QueryResultRecordType datastore = Iterables.getLast(records.getRecords());
    RelocateParams params =
        RelocateParams.builder()
            .datastore(Reference.builder().href(datastore.getHref()).build())
            .build();

    // The method under test
    Task relocate = vmApi.relocate(vmUrn, params);
    assertTrue(retryTaskSuccess.apply(relocate), String.format(TASK_COMPLETE_TIMELY, "relocate"));
  }
  @Test
  public void testGetUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("GET", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/user/user.xml", VCloudDirectorMediaType.USER)
                .httpResponseBuilder()
                .build());

    User expected = user();

    assertEquals(client.getUserClient().getUser(userRef.getHref()), expected);
  }
  @Test(enabled = false)
  public void testResetNetwork() {
    VCloudDirectorClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand(
                    "POST", "/admin/network/b466c0c5-8a5c-4335-b703-a2e2e6b5f3e1/action/reset")
                .acceptMedia(VCloudDirectorMediaType.TASK)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/network/admin/resetNetworkTask.xml", VCloudDirectorMediaType.TASK)
                .httpResponseBuilder()
                .build());

    Task expected = resetNetworkTask();

    assertEquals(client.getAdminNetworkClient().resetNetwork(networkRef.getHref()), expected);
  }
  @Test
  public void testCreateUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("POST", "/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/users")
                .xmlFilePayload("/user/createUserSource.xml", VCloudDirectorMediaType.USER)
                .acceptMedia(VCloudDirectorMediaType.USER)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/user/createUser.xml", VCloudDirectorMediaType.USER)
                .httpResponseBuilder()
                .build());

    User source = createUserSource();
    User expected = createUser();

    assertEquals(client.getUserClient().createUser(orgRef.getHref(), source), expected);
  }
  @Test(expectedExceptions = ResourceNotFoundException.class)
  public void testUnlockUserFailNotFound() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand(
                    "POST", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6/action/unlock")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .httpResponseBuilder()
                .statusCode(403)
                .payload(
                    payloadFromResourceWithContentType(
                        "/org/error400.xml", VCloudDirectorMediaType.ERROR))
                .build());

    client.getUserClient().unlockUser(userRef.getHref());
  }
 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();
 }
/**
 * Test the {@link AdminCatalogApi} by observing its side effects.
 *
 * @author [email protected], Adrian Cole
 */
@Test(
    groups = {"unit", "admin"},
    singleThreaded = true,
    testName = "AdminCatalogApiExpectTest")
public class AdminCatalogApiExpectTest extends VCloudDirectorAdminApiExpectTest {

  static String catalog = "7212e451-76e1-4631-b2de-ba1dfd8080e4";
  static String catalogUrn = "urn:vcloud:catalog:" + catalog;
  static URI catalogAdminHref = URI.create(endpoint + "/admin/catalog/" + catalog);
  static URI catalogHref = URI.create(endpoint + "/catalog/" + catalog);

  HttpRequest get =
      HttpRequest.builder()
          .method("GET")
          .endpoint(catalogAdminHref)
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .build();

  HttpResponse getResponse =
      HttpResponse.builder()
          .statusCode(200)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/catalog.xml", ADMIN_CATALOG + ";version=1.5"))
          .build();

  @Test
  public void testGetCatalogHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(loginRequest, sessionResponse, get, getResponse);
    assertEquals(api.getCatalogApi().get(catalogAdminHref), catalog());
  }

  HttpRequest resolveCatalog =
      HttpRequest.builder()
          .method("GET")
          .endpoint(endpoint + "/entity/" + catalogUrn)
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .build();

  String catalogEntity =
      asString(
          createXMLBuilder("Entity")
              .a("xmlns", "http://www.vmware.com/vcloud/v1.5")
              .a("name", catalogUrn)
              .a("id", catalogUrn)
              .a("type", ENTITY)
              .a("href", endpoint + "/entity/" + catalogUrn)
              .e("Link")
              .a("rel", "alternate")
              .a("type", CATALOG)
              .a("href", catalogHref.toString())
              .up()
              .e("Link")
              .a("rel", "alternate")
              .a("type", ADMIN_CATALOG)
              .a("href", catalogAdminHref.toString())
              .up());

  HttpResponse resolveCatalogResponse =
      HttpResponse.builder()
          .statusCode(200)
          .payload(payloadFromStringWithContentType(catalogEntity, ENTITY + ";version=1.5"))
          .build();

  @Test
  public void testGetCatalogUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            resolveCatalog,
            resolveCatalogResponse,
            get,
            getResponse);
    assertEquals(api.getCatalogApi().get(catalogUrn), catalog());
  }

  static String org = "7212e451-76e1-4631-b2de-asdasdasd";
  static String orgUrn = "urn:vcloud:org:" + org;
  static URI orgHref = URI.create(endpoint + "/org/" + org);
  static URI orgAdminHref = URI.create(endpoint + "/admin/org/" + org);

  HttpRequest add =
      HttpRequest.builder()
          .method("POST")
          .endpoint(orgAdminHref + "/catalogs")
          .addHeader("Accept", ADMIN_CATALOG)
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/addCatalogSource.xml", VCloudDirectorMediaType.ADMIN_CATALOG))
          .build();

  HttpResponse addResponse =
      HttpResponse.builder()
          .statusCode(200)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/addCatalog.xml", ADMIN_CATALOG + ";version=1.5"))
          .build();

  @Test
  public void testAddCatalogHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(loginRequest, sessionResponse, add, addResponse);
    assertEquals(
        api.getCatalogApi().addCatalogToOrg(addCatalogToOrgSource(), orgAdminHref),
        addCatalogToOrg());
  }

  HttpRequest resolveOrg =
      HttpRequest.builder()
          .method("GET")
          .endpoint(endpoint + "/entity/" + orgUrn)
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .build();

  String orgEntity =
      asString(
          createXMLBuilder("Entity")
              .a("xmlns", "http://www.vmware.com/vcloud/v1.5")
              .a("name", orgUrn)
              .a("id", orgUrn)
              .a("type", ENTITY)
              .a("href", endpoint + "/entity/" + catalogUrn)
              .e("Link")
              .a("rel", "alternate")
              .a("type", ORG)
              .a("href", orgHref.toString())
              .up()
              .e("Link")
              .a("rel", "alternate")
              .a("type", ADMIN_ORG)
              .a("href", orgAdminHref.toString())
              .up());

  HttpResponse resolveOrgResponse =
      HttpResponse.builder()
          .statusCode(200)
          .payload(payloadFromStringWithContentType(orgEntity, ENTITY + ";version=1.5"))
          .build();

  @Test
  public void testAddCatalogUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest, sessionResponse, resolveOrg, resolveOrgResponse, add, addResponse);
    assertEquals(
        api.getCatalogApi().addCatalogToOrg(addCatalogToOrgSource(), orgUrn), addCatalogToOrg());
  }

  HttpRequest edit =
      HttpRequest.builder()
          .method("PUT")
          .endpoint(catalogAdminHref)
          .addHeader("Accept", ADMIN_CATALOG)
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/editCatalogSource.xml", VCloudDirectorMediaType.ADMIN_CATALOG))
          .build();

  HttpResponse editResponse =
      HttpResponse.builder()
          .statusCode(200)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/editCatalog.xml", ADMIN_CATALOG + ";version=1.5"))
          .build();

  @Test
  public void testEditCatalogHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(loginRequest, sessionResponse, edit, editResponse);
    assertEquals(api.getCatalogApi().edit(catalogAdminHref, editCatalog()), editCatalog());
  }

  @Test
  public void testEditCatalogUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            resolveCatalog,
            resolveCatalogResponse,
            edit,
            editResponse);
    assertEquals(api.getCatalogApi().edit(catalogUrn, editCatalog()), editCatalog());
  }

  HttpRequest getOwner =
      HttpRequest.builder()
          .method("GET")
          .endpoint(catalogAdminHref + "/owner")
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .build();

  HttpResponse getOwnerResponse =
      HttpResponse.builder()
          .statusCode(200)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/owner.xml", OWNER + ";version=1.5"))
          .build();

  Owner expectedGetOwner =
      owner()
          .toBuilder()
          .link(
              Link.builder()
                  .href(
                      URI.create(
                          "https://vcloudbeta.bluelock.com/api/admin/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
                  .type("application/vnd.vmware.vcloud.catalog+xml")
                  .rel("up")
                  .build())
          .link(
              Link.builder()
                  .href(
                      URI.create(
                          "https://vcloudbeta.bluelock.com/api/admin/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4/owner"))
                  .type("application/vnd.vmware.vcloud.owner+xml")
                  .rel("edit")
                  .build())
          .build();

  @Test
  public void testGetCatalogOwnerHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(loginRequest, sessionResponse, getOwner, getOwnerResponse);
    assertEquals(api.getCatalogApi().getOwner(catalogAdminHref), expectedGetOwner);
  }

  @Test
  public void testGetCatalogOwnerUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            resolveCatalog,
            resolveCatalogResponse,
            getOwner,
            getOwnerResponse);
    assertEquals(api.getCatalogApi().getOwner(catalogUrn), expectedGetOwner);
  }

  HttpRequest setOwner =
      HttpRequest.builder()
          .method("PUT")
          .endpoint(catalogAdminHref + "/owner")
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/editOwnerSource.xml", OWNER + ";version=1.5"))
          .build();

  HttpResponse setOwnerResponse = HttpResponse.builder().statusCode(204).build();

  Owner ownerToSet =
      Owner.builder()
          .type("application/vnd.vmware.vcloud.owner+xml")
          .user(
              Reference.builder()
                  .type("application/vnd.vmware.admin.user+xml")
                  .name("*****@*****.**")
                  .href(
                      URI.create(
                          "https://vcloudbeta.bluelock.com/api/admin/user/e9eb1b29-0404-4c5e-8ef7-e584acc51da9"))
                  .build())
          .build();

  @Test
  public void testSetCatalogOwnerHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(loginRequest, sessionResponse, setOwner, setOwnerResponse);
    api.getCatalogApi().setOwner(catalogAdminHref, ownerToSet);
  }

  @Test
  public void testSetCatalogOwnerUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            resolveCatalog,
            resolveCatalogResponse,
            setOwner,
            setOwnerResponse);
    api.getCatalogApi().setOwner(catalogUrn, ownerToSet);
  }

  HttpRequest publishCatalog =
      HttpRequest.builder()
          .method("POST")
          .endpoint(catalogAdminHref + "/action/publish")
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .payload(
              payloadFromResourceWithContentType(
                  "/catalog/admin/publishCatalogParams.xml",
                  PUBLISH_CATALOG_PARAMS + ";version=1.5"))
          .build();

  HttpResponse publishCatalogResponse = HttpResponse.builder().statusCode(204).build();

  @Test
  public void testPublishCatalogHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest, sessionResponse, publishCatalog, publishCatalogResponse);
    api.getCatalogApi()
        .publish(catalogAdminHref, PublishCatalogParams.builder().isPublished(true).build());
  }

  @Test
  public void testPublishCatalogUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            resolveCatalog,
            resolveCatalogResponse,
            publishCatalog,
            publishCatalogResponse);
    api.getCatalogApi()
        .publish(catalogUrn, PublishCatalogParams.builder().isPublished(true).build());
  }

  HttpRequest removeCatalog =
      HttpRequest.builder()
          .method("DELETE")
          .endpoint(catalogAdminHref)
          .addHeader("Accept", "*/*")
          .addHeader("x-vcloud-authorization", token)
          .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + token)
          .build();

  HttpResponse removeCatalogResponse = HttpResponse.builder().statusCode(204).build();

  @Test
  public void testRemoveCatalogHref() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(loginRequest, sessionResponse, removeCatalog, removeCatalogResponse);
    api.getCatalogApi().remove(catalogAdminHref);
  }

  @Test
  public void testRemoveCatalogUrn() {
    VCloudDirectorAdminApi api =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            resolveCatalog,
            resolveCatalogResponse,
            removeCatalog,
            removeCatalogResponse);
    api.getCatalogApi().remove(catalogUrn);
  }

  // TODO: tests for access control!

  public static final AdminCatalog addCatalogToOrgSource() {
    return AdminCatalog.builder()
        .name("Test Catalog")
        .description("created by testCreateCatalog()")
        .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 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();
  }

  private static Owner owner() {
    return Owner.builder()
        .type("application/vnd.vmware.vcloud.owner+xml")
        .user(
            Reference.builder()
                .type("application/vnd.vmware.admin.user+xml")
                .name("*****@*****.**")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/admin/user/967d317c-4273-4a95-b8a4-bf63b78e9c69"))
                .build())
        .build();
  }

  public static final AdminCatalog editCatalog() {
    return catalog().toBuilder().name("new QunyingTestCatalog").description("new Testing").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();
 }
/**
 * Test the {@link UserClient} by observing its side effects.
 *
 * @author danikov
 */
@Test(
    groups = {"unit", "admin"},
    singleThreaded = true,
    testName = "UserClientExpectTest")
public class UserClientExpectTest extends VCloudDirectorAdminClientExpectTest {

  private Reference orgRef =
      Reference.builder()
          .href(URI.create(endpoint + "/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
          .build();

  private Reference userRef =
      Reference.builder()
          .href(URI.create(endpoint + "/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
          .build();

  @Test
  public void testCreateUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("POST", "/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/users")
                .xmlFilePayload("/user/createUserSource.xml", VCloudDirectorMediaType.USER)
                .acceptMedia(VCloudDirectorMediaType.USER)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/user/createUser.xml", VCloudDirectorMediaType.USER)
                .httpResponseBuilder()
                .build());

    User source = createUserSource();
    User expected = createUser();

    assertEquals(client.getUserClient().createUser(orgRef.getHref(), source), expected);
  }

  public static final User createUserSource() {
    return User.builder()
        .name("test")
        .fullName("testFullName")
        .emailAddress("*****@*****.**")
        .telephone("555-1234")
        .isEnabled(false)
        .im("testIM")
        .isAlertEnabled(false)
        .alertEmailPrefix("testPrefix")
        .alertEmail("*****@*****.**")
        .isExternal(false)
        .isGroupRole(false)
        .role(
            Reference.builder()
                .type("application/vnd.vmware.admin.role+xml")
                .name("vApp User")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"))
                .build())
        .password("password")
        .groups(Collections.<Reference>emptyList())
        .build();
  }

  public static final User createUser() {
    return createUserSource()
        .toBuilder()
        .id("urn:vcloud:user:b37223f3-8792-477a-820f-334998f61cd6")
        .type("application/vnd.vmware.admin.user+xml")
        .href(
            URI.create(
                "https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
        .link(
            Link.builder()
                .rel("edit")
                .type("application/vnd.vmware.admin.user+xml")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
                .build())
        .link(
            Link.builder()
                .rel("remove")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
                .build())
        .isLocked(false)
        .isDefaultCached(false)
        .storedVmQuota(0)
        .deployedVmQuota(0)
        .password(null)
        .build();
  }

  @Test
  public void testGetUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("GET", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/user/user.xml", VCloudDirectorMediaType.USER)
                .httpResponseBuilder()
                .build());

    User expected = user();

    assertEquals(client.getUserClient().getUser(userRef.getHref()), expected);
  }

  public static final User user() {
    return createUser().toBuilder().nameInSource("test").build();
  }

  @Test
  public void testUpdateUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("PUT", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
                .xmlFilePayload("/user/updateUserSource.xml", VCloudDirectorMediaType.USER)
                .acceptMedia(VCloudDirectorMediaType.USER)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/user/updateUser.xml", VCloudDirectorMediaType.USER)
                .httpResponseBuilder()
                .build());

    User source = updateUserSource();
    User expected = updateUser();

    assertEquals(client.getUserClient().updateUser(userRef.getHref(), source), expected);
  }

  public static final User updateUserSource() {
    return user()
        .toBuilder()
        .fullName("new" + user().getFullName())
        .emailAddress("new" + user().getEmailAddress())
        .telephone("1-" + user().getTelephone())
        .isEnabled(true)
        .im("new" + user().getIM())
        .isAlertEnabled(true)
        .alertEmailPrefix("new" + user().getAlertEmailPrefix())
        .alertEmail("new" + user().getAlertEmail())
        .storedVmQuota(1)
        .deployedVmQuota(1)
        .password("newPassword")
        .build();
  }

  public static final User updateUser() {
    return updateUserSource().toBuilder().password(null).build();
  }

  @Test
  public void testDeleteUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("DELETE", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer().httpResponseBuilder().statusCode(204).build());

    client.getUserClient().deleteUser(userRef.getHref());
  }

  @Test
  public void testUnlockUser() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand(
                    "POST", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6/action/unlock")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer().httpResponseBuilder().statusCode(204).build());

    client.getUserClient().unlockUser(userRef.getHref());
  }

  @Test(expectedExceptions = ResourceNotFoundException.class)
  public void testUnlockUserFailNotFound() {
    VCloudDirectorAdminClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand(
                    "POST", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6/action/unlock")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .httpResponseBuilder()
                .statusCode(403)
                .payload(
                    payloadFromResourceWithContentType(
                        "/org/error400.xml", VCloudDirectorMediaType.ERROR))
                .build());

    client.getUserClient().unlockUser(userRef.getHref());
  }
}
/**
 * Test the {@link GroupClient} by observing its side effects.
 *
 * @author danikov
 */
@Test(
    groups = {"unit", "admin", "network"},
    singleThreaded = true,
    testName = "AdminNetworkClientExpectTest")
public class AdminNetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectTest {

  Reference networkRef =
      Reference.builder()
          .href(URI.create(endpoint + "/admin/network/b466c0c5-8a5c-4335-b703-a2e2e6b5f3e1"))
          .build();

  @Test
  public void testGetNetworkWithOrgNetwork() {
    VCloudDirectorClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("GET", "/admin/network/b466c0c5-8a5c-4335-b703-a2e2e6b5f3e1")
                .acceptAnyMedia()
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload(
                    "/network/admin/orgNetwork.xml", VCloudDirectorMediaType.ORG_NETWORK)
                .httpResponseBuilder()
                .build());

    OrgNetwork expected = orgNetwork();

    assertEquals(client.getAdminNetworkClient().getNetwork(networkRef.getHref()), expected);
  }

  @Test
  public void testUpdateNetwork() {
    VCloudDirectorClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand("GET", "/admin/network/b466c0c5-8a5c-4335-b703-a2e2e6b5f3e1")
                .xmlFilePayload(
                    "/network/admin/updateNetworkSource.xml", VCloudDirectorMediaType.ORG_NETWORK)
                .acceptMedia(VCloudDirectorMediaType.TASK)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload(
                    "/network/admin/updateNetworkTask.xml", VCloudDirectorMediaType.TASK)
                .httpResponseBuilder()
                .build());

    Task expected = updateNetworkTask();

    assertEquals(
        client.getAdminNetworkClient().updateNetwork(networkRef.getHref(), updateNetwork()),
        expected);
  }

  @Test(enabled = false)
  public void testResetNetwork() {
    VCloudDirectorClient client =
        requestsSendResponses(
            loginRequest,
            sessionResponse,
            new VcloudHttpRequestPrimer()
                .apiCommand(
                    "POST", "/admin/network/b466c0c5-8a5c-4335-b703-a2e2e6b5f3e1/action/reset")
                .acceptMedia(VCloudDirectorMediaType.TASK)
                .httpRequestBuilder()
                .build(),
            new VcloudHttpResponsePrimer()
                .xmlFilePayload("/network/admin/resetNetworkTask.xml", VCloudDirectorMediaType.TASK)
                .httpResponseBuilder()
                .build());

    Task expected = resetNetworkTask();

    assertEquals(client.getAdminNetworkClient().resetNetwork(networkRef.getHref()), expected);
  }

  public final OrgNetwork orgNetwork() {
    return NetworkClientExpectTest.orgNetwork()
        .toBuilder()
        .href(toAdminUri(NetworkClientExpectTest.orgNetwork().getHref()))
        .links(
            ImmutableSet.of(
                Link.builder()
                    .rel("alternate")
                    .type("application/vnd.vmware.vcloud.orgNetwork+xml")
                    .href(
                        URI.create(
                            "https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
                    .build(),
                Link.builder()
                    .rel("edit")
                    .type("application/vnd.vmware.admin.orgNetwork+xml")
                    .href(
                        URI.create(
                            "https://vcloudbeta.bluelock.com/api/admin/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
                    .build(),
                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.builder()
                    .rel("repair")
                    .type("application/vnd.vmware.admin.orgNetwork+xml")
                    .href(
                        URI.create(
                            "https://vcloudbeta.bluelock.com/api/admin/network/f3ba8256-6f48-4512-aad6-600e85b4dc38/action/reset"))
                    .build(),
                Link.builder()
                    .rel("down")
                    .type("application/vnd.vmware.vcloud.metadata+xml")
                    .href(
                        URI.create(
                            "https://vcloudbeta.bluelock.com/api/admin/network/f3ba8256-6f48-4512-aad6-600e85b4dc38/metadata"))
                    .build()))
        .networkPool(
            Reference.builder()
                .type("application/vnd.vmware.admin.networkPool+xml")
                .name("vcdni01")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/admin/extension/networkPool/e86bfdb5-b3e0-4ece-9125-e764ac64c95c"))
                .build())
        .build();
  }

  public final OrgNetwork updateNetwork() {
    return orgNetwork().toBuilder().build();
  }

  public final Task resetNetworkTask() {
    return Task.builder().build();
  }

  public final Task updateNetworkTask() {
    return Task.builder()
        .status("running")
        .startTime(dateService.iso8601DateParse("2012-03-14T12:39:23.720-04:00"))
        .operationName("networkUpdateNetwork")
        .operation("Updating Network ilsolation01-Jclouds(f3ba8256-6f48-4512-aad6-600e85b4dc38)")
        .expiryTime(dateService.iso8601DateParse("2012-06-12T12:39:23.720-04:00"))
        .name("task")
        .id("urn:vcloud:task:49d2e180-7921-4902-ac39-b4ff5406bb94")
        .type("application/vnd.vmware.vcloud.task+xml")
        .href(
            URI.create(
                "https://vcloudbeta.bluelock.com/api/task/49d2e180-7921-4902-ac39-b4ff5406bb94"))
        .link(
            Link.builder()
                .rel("task:cancel")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/task/49d2e180-7921-4902-ac39-b4ff5406bb94/action/cancel"))
                .build())
        .owner(
            Reference.builder()
                .type("application/vnd.vmware.vcloud.network+xml")
                .name("ilsolation01-Jclouds")
                .href(
                    URI.create(
                        "https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
                .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();
  }
}