public void testListTypesByNamesWhenResponseIs2xx() throws Exception {
    HttpRequest listWithOptions =
        HttpRequest.builder()
            .method("POST")
            .endpoint("https://elasticloadbalancing.us-east-1.amazonaws.com/")
            .addHeader("Host", "elasticloadbalancing.us-east-1.amazonaws.com")
            .payload(
                payloadFromStringWithContentType(
                    "Action=DescribeLoadBalancerPolicyTypes"
                        + "&PolicyTypeNames.member.1=moo"
                        + "&Signature=cX8twwn2E6%2B7V3CGZ4ac69NhyolJLsV1nzpQl3wQXW8%3D"
                        + "&SignatureMethod=HmacSHA256"
                        + "&SignatureVersion=2"
                        + "&Timestamp=2009-11-08T15%3A54%3A08.897Z"
                        + "&Version=2012-06-01"
                        + "&AWSAccessKeyId=identity",
                    "application/x-www-form-urlencoded"))
            .build();

    HttpResponse listWithOptionsResponse =
        HttpResponse.builder()
            .statusCode(200)
            .payload(payloadFromResourceWithContentType("/describe_policy_types.xml", "text/xml"))
            .build();

    ELBApi apiWhenWithOptionsExist = requestSendsResponse(listWithOptions, listWithOptionsResponse);

    assertEquals(
        apiWhenWithOptionsExist.getPolicyApi().listTypes(ImmutableSet.of("moo")).toString(),
        new DescribeLoadBalancerPolicyTypesResponseTest().expected().toString());
  }
  public void testListWithOptionsWhenResponseIs2xx() throws Exception {
    HttpRequest listWithOptions =
        HttpRequest.builder()
            .method("POST")
            .endpoint("https://elasticloadbalancing.us-east-1.amazonaws.com/")
            .addHeader("Host", "elasticloadbalancing.us-east-1.amazonaws.com")
            .payload(
                payloadFromStringWithContentType(
                    "Action=DescribeLoadBalancerPolicies"
                        + "&LoadBalancerName=moo"
                        + "&Signature=c8PG1b5wI5YMU0motVEo5Mz7d5w8gy8u51kfCR6SnRI%3D"
                        + "&SignatureMethod=HmacSHA256"
                        + "&SignatureVersion=2"
                        + "&Timestamp=2009-11-08T15%3A54%3A08.897Z"
                        + "&Version=2012-06-01"
                        + "&AWSAccessKeyId=identity",
                    "application/x-www-form-urlencoded"))
            .build();

    HttpResponse listWithOptionsResponse =
        HttpResponse.builder()
            .statusCode(200)
            .payload(payloadFromResourceWithContentType("/describe_policies.xml", "text/xml"))
            .build();

    ELBApi apiWhenWithOptionsExist = requestSendsResponse(listWithOptions, listWithOptionsResponse);

    assertEquals(
        apiWhenWithOptionsExist.getPolicyApi().list(loadBalancerName("moo")).toString(),
        new DescribeLoadBalancerPoliciesResponseTest().expected().toString());
  }
  public void testGetWhenResponseIs404() throws Exception {

    HttpResponse getResponse = HttpResponse.builder().statusCode(404).build();

    ELBApi apiWhenDontExist = requestSendsResponse(get, getResponse);

    assertNull(apiWhenDontExist.getPolicyApi().get("name"));
  }
  public void testListTypesWhenResponseIs404() throws Exception {

    HttpResponse listResponse = HttpResponse.builder().statusCode(404).build();

    ELBApi apiWhenDontExist = requestSendsResponse(listTypes, listResponse);

    apiWhenDontExist.getPolicyApi().listTypes();
  }
  public void testGetWhenResponseIs2xx() throws Exception {

    HttpResponse getResponse =
        HttpResponse.builder()
            .statusCode(200)
            .payload(payloadFromResourceWithContentType("/describe_policy.xml", "text/xml"))
            .build();

    ELBApi apiWhenExist = requestSendsResponse(get, getResponse);

    assertEquals(
        apiWhenExist.getPolicyApi().get("name").toString(),
        new GetPolicyResponseTest().expected().toString());
  }
  public void testListTypeWhenResponseIs2xx() throws Exception {

    HttpResponse listResponse =
        HttpResponse.builder()
            .statusCode(200)
            .payload(payloadFromResourceWithContentType("/describe_policy_types.xml", "text/xml"))
            .build();

    ELBApi apiWhenExist = requestSendsResponse(listTypes, listResponse);

    assertEquals(
        apiWhenExist.getPolicyApi().listTypes().toString(),
        new DescribeLoadBalancerPolicyTypesResponseTest().expected().toString());
  }
  @Override
  public LoadBalancerMetadata createLoadBalancerInLocation(
      Location location,
      String name,
      String protocol,
      int loadBalancerPort,
      int instancePort,
      Iterable<? extends NodeMetadata> nodes) {
    checkNotNull(location, "location");
    String region = getRegionFromLocationOrNull(location);

    Set<String> zonesDesired =
        ImmutableSet.copyOf(
            transform(
                nodes,
                new Function<NodeMetadata, String>() {

                  @Override
                  public String apply(NodeMetadata from) {
                    return from.getLocation().getId();
                  }
                }));

    logger.debug(">> creating loadBalancer(%s) in zones(%s)", name, zonesDesired);
    try {
      String dnsName =
          api.getLoadBalancerApiForRegion(region)
              .createListeningInAvailabilityZones(
                  name,
                  ImmutableSet.of(
                      Listener.builder()
                          .port(loadBalancerPort)
                          .instancePort(instancePort)
                          .protocol(Protocol.valueOf(protocol))
                          .build()),
                  zonesDesired);
      logger.debug("<< created loadBalancer(%s) dnsName(%s)", name, dnsName);
    } catch (IllegalStateException e) {
      logger.debug("<< converging zones(%s) in loadBalancer(%s)", zonesDesired, name);
      Set<String> currentZones = api.getLoadBalancerApi().get(name).getAvailabilityZones();
      Set<String> zonesToAdd = Sets.difference(zonesDesired, currentZones);
      if (zonesToAdd.size() > 0)
        currentZones =
            api.getAvailabilityZoneApi().addAvailabilityZonesToLoadBalancer(zonesToAdd, name);
      Set<String> zonesToRemove = Sets.difference(currentZones, zonesDesired);
      if (zonesToRemove.size() > 0)
        api.getAvailabilityZoneApi().removeAvailabilityZonesFromLoadBalancer(zonesToRemove, name);
    }

    Set<String> instanceIds =
        ImmutableSet.copyOf(
            transform(
                nodes,
                new Function<NodeMetadata, String>() {

                  @Override
                  public String apply(NodeMetadata from) {
                    return from.getProviderId();
                  }
                }));

    logger.debug(">> converging loadBalancer(%s) to instances(%s)", name, instanceIds);
    Set<String> registeredInstanceIds =
        api.getInstanceApiForRegion(region).registerInstancesWithLoadBalancer(instanceIds, name);

    Set<String> instancesToRemove = filter(registeredInstanceIds, not(in(instanceIds)));
    if (instancesToRemove.size() > 0) {
      logger.debug(">> deregistering instances(%s) from loadBalancer(%s)", instancesToRemove, name);
      api.getInstanceApiForRegion(region)
          .deregisterInstancesFromLoadBalancer(instancesToRemove, name);
    }
    logger.debug("<< converged loadBalancer(%s) ", name);

    return converter.apply(
        new LoadBalancerInRegion(api.getLoadBalancerApiForRegion(region).get(name), region));
  }