public void testAllOptions() throws IOException {

    String expected =
        Strings2.toStringAndClose(
            getClass().getResourceAsStream("/InstantiateVAppTemplateParams-options-test.xml"));
    Multimap<String, String> headers =
        Multimaps.synchronizedMultimap(HashMultimap.<String, String>create());
    GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
    expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
    expect(request.getArgs())
        .andReturn(
            ImmutableList.<Object>of(
                InstantiateVAppTemplateOptions.Builder.processorCount(2)
                    .memory(512)
                    .inGroup("group")
                    .withPassword("password")
                    .inRow("row")
                    .addNetworkConfig(new NetworkConfig(URI.create("http://network")))))
        .atLeastOnce();
    expect(request.getFirstHeaderOrNull("Content-Type"))
        .andReturn("application/unknown")
        .atLeastOnce();
    expect(request.getHeaders()).andReturn(headers).atLeastOnce();
    request.setPayload(expected);
    replay(request);

    BindInstantiateVAppTemplateParamsToXmlPayload binder =
        injector.getInstance(BindInstantiateVAppTemplateParamsToXmlPayload.class);

    Map<String, String> map = Maps.newHashMap();
    map.put("name", "name");
    map.put("template", "https://vcloud/vAppTemplate/3");
    binder.bindToRequest(request, map);
  }
 public ContainerProperties apply(HttpResponse from) {
   MutableContainerPropertiesWithMetadata to = new MutableContainerPropertiesWithMetadataImpl();
   to.setName(request.getInvocation().getArgs().get(0).toString());
   addUserMetadataTo(from, to);
   parseLastModifiedOrThrowException(from, to);
   addETagTo(from, to);
   to.setUrl(request.getEndpoint());
   return to;
 }
 public static String getNameFor(GeneratedHttpRequest request) {
   checkNotNull(request, "request");
   List<Object> args = request.getInvocation().getArgs();
   // assume first params are container and key
   if (args.size() >= 2 && args.get(0) instanceof String && args.get(1) instanceof String) {
     return args.get(1).toString();
   } else if (args.size() >= 1 && args.get(0) instanceof String) {
     Matcher matcher = keyFromContainer.matcher(args.get(0).toString());
     if (matcher.find()) return matcher.group(1);
   }
   String objectKey = request.getEndpoint().getPath();
   if (objectKey.startsWith("/")) {
     // Trim initial slash from object key name.
     objectKey = objectKey.substring(1);
   }
   return objectKey;
 }
  public void testDefault() throws IOException {
    String expected =
        Strings2.toStringAndClose(getClass().getResourceAsStream("/cloneVApp-default.xml"));

    GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
    expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
    expect(request.getArgs()).andReturn(ImmutableList.<Object>of()).atLeastOnce();
    request.setPayload(expected);
    replay(request);

    BindCloneVCloudExpressVAppParamsToXmlPayload binder =
        injector.getInstance(BindCloneVCloudExpressVAppParamsToXmlPayload.class);

    Map<String, String> map = Maps.newHashMap();
    map.put("newName", "my-vapp");
    map.put("vApp", "https://vcloud.safesecureweb.com/api/v0.8/vapp/4181");
    binder.bindToRequest(request, map);
    verify(request);
  }
  public void testWithDescriptionDeployOn() throws IOException {
    String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/cloneVApp.xml"));

    CloneVAppOptions options =
        new CloneVAppOptions()
            .deploy()
            .powerOn()
            .withDescription("The description of the new vApp");
    GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
    expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
    expect(request.getArgs()).andReturn(ImmutableList.<Object>of(options)).atLeastOnce();
    request.setPayload(expected);
    replay(request);

    BindCloneVCloudExpressVAppParamsToXmlPayload binder =
        injector.getInstance(BindCloneVCloudExpressVAppParamsToXmlPayload.class);

    Map<String, String> map = Maps.newHashMap();
    map.put("newName", "new-linux-server");
    map.put("vApp", "https://vcloud.safesecureweb.com/api/v0.8/vapp/201");
    binder.bindToRequest(request, map);
    verify(request);
  }
  @Test
  public void testResponsePathSchemeLocationOk() throws Exception {
    ParseURIFromListOrLocationHeaderIf20x function =
        new ParseURIFromListOrLocationHeaderIf20x(uriBuilderProvider);
    HttpResponse response = createMock(HttpResponse.class);
    GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class);
    Payload payload = createMock(Payload.class);

    function.setContext(request);
    expect(request.getEndpoint()).andReturn(URI.create("https://new/fd")).atLeastOnce();
    expect(response.getStatusCode()).andReturn(200).atLeastOnce();
    expect(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE)).andReturn("text/plain");
    expect(response.getFirstHeaderOrNull(HttpHeaders.LOCATION)).andReturn("path");
    expect(response.getPayload()).andReturn(payload).atLeastOnce();
    payload.release();

    replay(request);
    replay(payload);
    replay(response);
    assertEquals(function.apply(response), URI.create("https://new/path"));
    verify(request);
    verify(response);
    verify(payload);
  }
 public void setContext(GeneratedHttpRequest<?> request) {
   this.endpoint = request == null ? null : request.getEndpoint();
 }