@Test(timeOut = 5 * 60 * 1000)
 public void testCreateContainer() throws Exception {
   boolean created = false;
   while (!created) {
     privateContainer = containerPrefix + new SecureRandom().nextInt();
     try {
       created =
           client.createContainer(
               privateContainer, withMetadata(ImmutableMultimap.of("foo", "bar")));
     } catch (UndeclaredThrowableException e) {
       HttpResponseException htpe = (HttpResponseException) e.getCause().getCause();
       if (htpe.getResponse().getStatusCode() == 409) continue;
       throw e;
     }
   }
   Set<ContainerProperties> response = client.listContainers(includeMetadata());
   assert null != response;
   long containerCount = response.size();
   assertTrue(containerCount >= 1);
   ListBlobsResponse list = client.listBlobs(privateContainer);
   assertEquals(
       list.getUrl(),
       URI.create(
           String.format("https://%s.blob.core.windows.net/%s", identity, privateContainer)));
   // TODO .. check to see the container actually exists
 }
  public void testExceptionIfNot5xx() {
    FalseOn5xx function = new FalseOn5xx();
    HttpResponse response = EasyMock.createMock(HttpResponse.class);
    HttpResponseException exception = EasyMock.createMock(HttpResponseException.class);

    // Status code is called twice
    expect(response.getStatusCode()).andReturn(600);
    expect(response.getStatusCode()).andReturn(600);
    // Get response gets called twice
    expect(exception.getResponse()).andReturn(response);
    expect(exception.getResponse()).andReturn(response);
    // Get cause is called to determine the root cause
    expect(exception.getCause()).andReturn(null);

    replay(response);
    replay(exception);

    try {
      function.createOrPropagate(exception);
    } catch (Exception ex) {
      assertEquals(ex, exception);
    }

    verify(response);
    verify(exception);
  }
 public SharedIpGroup apply(Exception from) {
   if (from instanceof HttpResponseException) {
     HttpResponseException responseException = (HttpResponseException) from;
     if (responseException.getResponse().getStatusCode() == 404) {
       return SharedIpGroup.NOT_FOUND;
     }
   }
   return null;
 }
 public void testGetImagesDetail() throws Exception {
   Set<Image> response = client.listImages();
   assert null != response;
   long imageCount = response.size();
   assertTrue(imageCount >= 0);
   for (Image image : response) {
     try {
       Image newDetails = client.getImage(image.getId());
       assertEquals(image, newDetails);
     } catch (HttpResponseException e) { // Ticket #9867
       if (e.getResponse().getStatusCode() != 400) throw e;
     }
   }
 }
 public Object apply(Exception from) {
   if (from instanceof HttpResponseException) {
     HttpResponseException responseException = (HttpResponseException) from;
     if (responseException.getResponse() != null)
       switch (responseException.getResponse().getStatusCode()) {
         case 401:
           throw new AuthorizationException(from);
         case 403:
           throw new AuthorizationException(from);
         case 404:
           throw new ResourceNotFoundException(from);
         case 409:
           throw new IllegalStateException(from);
       }
   }
   return propagateOrNull(from);
 }
 @Test(timeOut = 5 * 60 * 1000)
 public void testCreatePublicContainer() throws Exception {
   boolean created = false;
   while (!created) {
     publicContainer = containerPrefix + new SecureRandom().nextInt();
     try {
       created = client.createContainer(publicContainer, withPublicAcl());
     } catch (UndeclaredThrowableException e) {
       HttpResponseException htpe = (HttpResponseException) e.getCause().getCause();
       if (htpe.getResponse().getStatusCode() == 409) continue;
       throw e;
     }
   }
   // TODO
   // URL url = new URL(String.format("http://%s.blob.core.windows.net/%s", identity,
   // publicContainer));
   // Utils.toStringAndClose(url.openStream());
 }
 @Test(timeOut = 5 * 60 * 1000)
 public void testCreateDirectory() throws Exception {
   boolean created = false;
   while (!created) {
     privateDirectory = containerPrefix + new SecureRandom().nextInt();
     try {
       created = connection.createDirectory(privateDirectory) != null;
     } catch (UndeclaredThrowableException e) {
       HttpResponseException htpe = (HttpResponseException) e.getCause().getCause();
       if (htpe.getResponse().getStatusCode() == 409) continue;
       throw e;
     }
   }
   BoundedSortedSet<? extends DirectoryEntry> response = connection.listDirectories();
   for (DirectoryEntry id : response) {
     BoundedSortedSet<? extends DirectoryEntry> r2 = connection.listDirectory(id.getObjectName());
     assert r2 != null;
   }
 }
 @Override
 public Object apply(Exception from) {
   Iterable<HttpResponseException> throwables =
       Iterables.filter(Throwables.getCausalChain(from), HttpResponseException.class);
   HttpResponseException exception = Iterables.getFirst(throwables, null);
   if (exception != null
       && exception.getResponse() != null
       && exception.getResponse().getStatusCode() >= 400
       && exception.getResponse().getStatusCode() < 500) {
     try {
       Error error =
           JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class);
       throw new VCloudDirectorException(error);
     } catch (IOException e) {
       Throwables.propagate(e);
     }
   }
   throw Throwables.propagate(from);
 }
  @Override
  public Object apply(final Exception from) {
    Throwable exception =
        Iterables.find(Throwables.getCausalChain(from), isNotAvailableException(from), null);

    if (exception != null) {
      if (exception instanceof HttpResponseException) {
        HttpResponseException responseException = (HttpResponseException) exception;
        HttpResponse response = responseException.getResponse();

        if (response != null && response.getStatusCode() >= 500 && response.getStatusCode() < 600) {
          return false;
        }
      } else {
        // Will enter here when exception is a ResourceNotFoundException
        return false;
      }
    }

    throw Throwables.propagate(from);
  }
 @Test(enabled = true)
 public void testCreateSlice() throws Exception {
   int imageId = 14362;
   int flavorId = 1;
   Slice slice = null;
   while (slice == null) {
     String sliceName = slicePrefix + "createslice" + new SecureRandom().nextInt();
     try {
       slice = client.createSlice(sliceName, imageId, flavorId);
     } catch (UndeclaredThrowableException e) {
       HttpResponseException htpe = (HttpResponseException) e.getCause().getCause();
       if (htpe.getResponse().getStatusCode() == 400) continue;
       throw e;
     }
   }
   assertNotNull(slice.getRootPassword());
   sliceId = slice.getId();
   rootPassword = slice.getRootPassword();
   assertEquals(slice.getStatus(), Slice.Status.BUILD);
   blockUntilSliceActive(sliceId);
 }
  public void testFalseIf5xx() throws Exception {
    FalseOn5xx function = new FalseOn5xx();
    HttpResponse response = EasyMock.createMock(HttpResponse.class);
    HttpResponseException exception = EasyMock.createMock(HttpResponseException.class);

    // Status code is called twice
    expect(response.getStatusCode()).andReturn(503);
    expect(response.getStatusCode()).andReturn(503);
    // Get response gets called twice
    expect(exception.getResponse()).andReturn(response);
    expect(exception.getResponse()).andReturn(response);
    // Get cause is called to determine the root cause
    expect(exception.getCause()).andReturn(null);

    replay(response);
    replay(exception);

    assertFalse(function.createOrPropagate(exception));

    verify(response);
    verify(exception);
  }
 private RunningInstance createInstance(String imageId) throws UnknownHostException {
   RunningInstance instance = null;
   while (instance == null) {
     try {
       System.out.printf("%d: running instance%n", System.currentTimeMillis());
       Reservation<? extends RunningInstance> reservation =
           client
               .getInstanceServices()
               .runInstancesInRegion(
                   null,
                   null, // allow
                   // ec2
                   // to
                   // chose
                   // an
                   // availability
                   // zone
                   imageId,
                   1, // minimum instances
                   1, // maximum instances
                   withKeyName(keyPair.getKeyName()) // key I created above
                       .asType(InstanceType.M1_SMALL) // smallest instance
                       // size
                       .withSecurityGroup(securityGroupName)); // group I
       // created
       // above
       instance = Iterables.getOnlyElement(reservation);
     } catch (HttpResponseException htpe) {
       if (htpe.getResponse().getStatusCode() == 400) continue;
       throw htpe;
     }
   }
   assertNotNull(instance.getId());
   assertEquals(instance.getInstanceState(), InstanceState.PENDING);
   instance = blockUntilWeCanSshIntoInstance(instance);
   return instance;
 }
  @Test(
      timeOut = 5 * 60 * 1000,
      dependsOnMethods = {"testCreateContainer", "testCreatePublicContainer"})
  public void testObjectOperations() throws Exception {
    String data = "Here is my data";

    // Test PUT with string data, ETag hash, and a piece of metadata
    AzureBlob object = client.newBlob();
    object.getProperties().setName("object");
    object.setPayload(data);
    Payloads.calculateMD5(object);
    object.getProperties().getContentMetadata().setContentType("text/plain");
    object.getProperties().getMetadata().put("mykey", "metadata-value");
    byte[] md5 = object.getProperties().getContentMetadata().getContentMD5();
    String newEtag = client.putBlob(privateContainer, object);
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(object.getProperties().getContentMetadata().getContentMD5()));

    // Test HEAD of missing object
    assert client.getBlobProperties(privateContainer, "non-existent-object") == null;

    // Test HEAD of object
    BlobProperties metadata =
        client.getBlobProperties(privateContainer, object.getProperties().getName());
    // TODO assertEquals(metadata.getName(), object.getProperties().getName());
    // we can't check this while hacking around lack of content-md5, as GET of the first byte will
    // show incorrect length 1, the returned size, as opposed to the real length. This is an ok
    // tradeoff, as a container list will contain the correct size of the objects in an
    // inexpensive fashion
    // http://code.google.com/p/jclouds/issues/detail?id=92
    // assertEquals(metadata.getSize(), data.length());
    assertEquals(metadata.getContentMetadata().getContentType(), "text/plain");
    // Azure doesn't return the Content-MD5 on head request..
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(object.getProperties().getContentMetadata().getContentMD5()));
    assertEquals(metadata.getETag(), newEtag);
    assertEquals(metadata.getMetadata().entrySet().size(), 1);
    assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

    // // Test POST to update object's metadata
    // Multimap<String, String> userMetadata = LinkedHashMultimap.create();
    // userMetadata.put("New-Metadata-1", "value-1");
    // userMetadata.put("New-Metadata-2", "value-2");
    // assertTrue(client.setBlobProperties(privateContainer, object.getProperties().getName(),
    // userMetadata));

    // Test GET of missing object
    assert client.getBlob(privateContainer, "non-existent-object") == null;

    // Test GET of object (including updated metadata)
    AzureBlob getBlob = client.getBlob(privateContainer, object.getProperties().getName());
    assertEquals(Utils.toStringAndClose(getBlob.getPayload().getInput()), data);
    // TODO assertEquals(getBlob.getName(), object.getProperties().getName());
    assertEquals(
        getBlob.getPayload().getContentMetadata().getContentLength(), new Long(data.length()));
    assertEquals(getBlob.getProperties().getContentMetadata().getContentType(), "text/plain");
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(getBlob.getProperties().getContentMetadata().getContentMD5()));
    assertEquals(newEtag, getBlob.getProperties().getETag());
    // wait until we can update metadata
    // assertEquals(getBlob.getProperties().getMetadata().entries().size(), 2);
    // assertEquals(
    // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-1")),
    // "value-1");
    // assertEquals(
    // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-2")),
    // "value-2");
    assertEquals(metadata.getMetadata().entrySet().size(), 1);
    assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

    // test listing
    ListBlobsResponse response =
        client.listBlobs(
            privateContainer,
            ListBlobsOptions.Builder.prefix(
                    object
                        .getProperties()
                        .getName()
                        .substring(0, object.getProperties().getName().length() - 1))
                .maxResults(1)
                .includeMetadata());
    assertEquals(response.size(), 1);
    assertEquals(Iterables.getOnlyElement(response).getName(), object.getProperties().getName());
    assertEquals(
        Iterables.getOnlyElement(response).getMetadata(),
        ImmutableMap.of("mykey", "metadata-value"));

    // Test PUT with invalid ETag (as if object's data was corrupted in transit)
    String correctEtag = newEtag;
    String incorrectEtag = "0" + correctEtag.substring(1);
    object.getProperties().setETag(incorrectEtag);
    try {
      client.putBlob(privateContainer, object);
    } catch (Throwable e) {
      assertEquals(e.getCause().getClass(), HttpResponseException.class);
      assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8"));
    object = client.newBlob();
    object.getProperties().setName("chunked-object");
    object.setPayload(bais);
    object.getPayload().getContentMetadata().setContentLength(new Long(data.getBytes().length));
    newEtag = client.putBlob(privateContainer, object);
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(getBlob.getProperties().getContentMetadata().getContentMD5()));

    // Test GET with options
    // Non-matching ETag
    try {
      client.getBlob(
          privateContainer,
          object.getProperties().getName(),
          GetOptions.Builder.ifETagDoesntMatch(newEtag));
    } catch (Exception e) {
      assertEquals(e.getCause().getClass(), HttpResponseException.class);
      assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 304);
    }

    // Matching ETag TODO this shouldn't fail!!!
    try {
      getBlob =
          client.getBlob(
              privateContainer,
              object.getProperties().getName(),
              GetOptions.Builder.ifETagMatches(newEtag));
      assertEquals(getBlob.getProperties().getETag(), newEtag);
    } catch (HttpResponseException e) {
      assertEquals(e.getResponse().getStatusCode(), 412);
    }

    // Range
    // doesn't work per
    // http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/479fa63f-51df-4b66-96b5-33ae362747b6
    // getBlob = client
    // .getBlob(privateContainer, object.getProperties().getName(),
    // GetOptions.Builder.startAt(8)).get(120,
    // TimeUnit.SECONDS);
    // assertEquals(Utils.toStringAndClose((InputStream) getBlob.getData()), data.substring(8));

    client.deleteBlob(privateContainer, "object");
    client.deleteBlob(privateContainer, "chunked-object");
  }
  @Test
  public void testObjectOperations() throws Exception {
    String containerName = getContainerName();
    try {
      // Test PUT with string data, ETag hash, and a piece of metadata
      String data = "Here is my data";
      String key = "object";
      SwiftObject object = newSwiftObject(data, key);
      byte[] md5 = object.getPayload().getContentMetadata().getContentMD5();
      String newEtag = getApi().putObject(containerName, object);
      assert newEtag != null;

      assertEquals(
          base16().lowerCase().encode(md5),
          base16().lowerCase().encode(object.getPayload().getContentMetadata().getContentMD5()));

      // Test HEAD of missing object
      assert getApi().getObjectInfo(containerName, "non-existent-object") == null;

      // Test HEAD of object
      MutableObjectInfoWithMetadata metadata =
          getApi().getObjectInfo(containerName, object.getInfo().getName());
      assertEquals(metadata.getName(), object.getInfo().getName());

      assertEquals(metadata.getBytes(), Long.valueOf(data.length()));
      assert metadata.getContentType().startsWith("text/plain") : metadata.getContentType();

      assertEquals(
          base16().lowerCase().encode(md5), base16().lowerCase().encode(metadata.getHash()));
      assertEquals(metadata.getHash(), base16().lowerCase().decode(newEtag));
      assertEquals(metadata.getMetadata().entrySet().size(), 1);
      assertEquals(metadata.getMetadata().get("metadata"), "metadata-value");

      // // Test POST to update object's metadata
      Map<String, String> userMetadata = Maps.newHashMap();
      userMetadata.put("New-Metadata-1", "value-1");
      userMetadata.put("New-Metadata-2", "value-2");
      assertTrue(getApi().setObjectInfo(containerName, object.getInfo().getName(), userMetadata));

      // Test GET of missing object
      assert getApi().getObject(containerName, "non-existent-object") == null;
      // Test GET of object (including updated metadata)
      SwiftObject getBlob = getApi().getObject(containerName, object.getInfo().getName());
      assertEquals(Strings2.toString(getBlob.getPayload()), data);
      // TODO assertEquals(getBlob.getName(),
      // object.getMetadata().getName());
      assertEquals(getBlob.getInfo().getBytes(), Long.valueOf(data.length()));
      testGetObjectContentType(getBlob);
      assertEquals(
          base16().lowerCase().encode(md5),
          base16().lowerCase().encode(getBlob.getInfo().getHash()));
      assertEquals(base16().lowerCase().decode(newEtag), getBlob.getInfo().getHash());
      assertEquals(getBlob.getInfo().getMetadata().entrySet().size(), 2);
      assertEquals(getBlob.getInfo().getMetadata().get("new-metadata-1"), "value-1");
      assertEquals(getBlob.getInfo().getMetadata().get("new-metadata-2"), "value-2");

      // Test PUT with invalid ETag (as if object's data was corrupted in
      // transit)
      String correctEtag = newEtag;
      String incorrectEtag = "0" + correctEtag.substring(1);
      object.getInfo().setHash(base16().lowerCase().decode(incorrectEtag));
      try {
        getApi().putObject(containerName, object);
      } catch (HttpResponseException e) {
        assertEquals(e.getResponse().getStatusCode(), 422);
      }

      // Test PUT chunked/streamed upload with data of "unknown" length
      ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
      SwiftObject blob = getApi().newSwiftObject();
      blob.getInfo().setName("chunked-object");
      blob.setPayload(bais);
      newEtag = getApi().putObject(containerName, blob);
      assertEquals(
          base16().lowerCase().encode(md5),
          base16().lowerCase().encode(getBlob.getInfo().getHash()));

      // Test GET with options
      // Non-matching ETag
      try {
        getApi()
            .getObject(
                containerName,
                object.getInfo().getName(),
                GetOptions.Builder.ifETagDoesntMatch(newEtag));
      } catch (HttpResponseException e) {
        assertEquals(e.getResponse().getStatusCode(), 304);
      }

      // Matching ETag
      getBlob =
          getApi()
              .getObject(
                  containerName,
                  object.getInfo().getName(),
                  GetOptions.Builder.ifETagMatches(newEtag));
      assertEquals(getBlob.getInfo().getHash(), base16().lowerCase().decode(newEtag));
      getBlob =
          getApi()
              .getObject(containerName, object.getInfo().getName(), GetOptions.Builder.startAt(8));
      assertEquals(Strings2.toString(getBlob.getPayload()), data.substring(8));

    } finally {
      returnContainer(containerName);
    }
  }