BindIterableAsPayloadToDeleteRequest implements Binder { @SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument(checkNotNull(input, "input is null") instanceof Iterable, "this binder is only valid for an Iterable"); checkNotNull(request, "request is null"); Iterable<String> keys = (Iterable<String>) input; StringBuilder builder = new StringBuilder(); for (String key : keys) { builder.append(String.format("<Object><Key>%s</Key></Object>", key)); } final String objects = builder.toString(); checkArgument(!objects.isEmpty(), "The list of keys should not be empty."); final String content = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Delete>%s</Delete>", objects); Payload payload = Payloads.newStringPayload(content); payload.getContentMetadata().setContentType(MediaType.TEXT_XML); byte[] md5 = md5().hashString(content, UTF_8).asBytes(); payload.getContentMetadata().setContentMD5(md5); request.setPayload(payload); return request; }  @SuppressWarnings("unchecked") @Override R bindToRequest(R request, Object input);  }
@Test public void testWithASmallSet() { HttpRequest result = binder.bindToRequest(request, ImmutableSet.of("key1", "key2")); Payload payload = Payloads .newStringPayload("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Delete>" + "<Object><Key>key1</Key></Object><Object><Key>key2</Key></Object></Delete>"); payload.getContentMetadata().setContentType(MediaType.TEXT_XML); assertEquals(result.getPayload(), payload); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptySetThrowsException() { binder.bindToRequest(request, ImmutableSet.of()); }
@Test(expectedExceptions = NullPointerException.class) public void testFailsOnNullSet() { binder.bindToRequest(request, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testExpectedASetInstance() { binder.bindToRequest(request, ImmutableList.of()); }