BindMetadataToQueryParams implements Binder { @SuppressWarnings("unchecked") public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument(checkNotNull(input, "input") instanceof Map, "this binder is only valid for Maps!"); Map<String, String> userMetadata = (Map<String, String>) input; List<String> metadata = Lists.newArrayList(); for (Entry<String, String> entry : userMetadata.entrySet()) { metadata.add(String.format("%s:%s", entry.getKey().toLowerCase(), entry.getValue())); } return ModifyRequest.addQueryParam(request, "metadata", metadata, builder.get()); } @Inject  BindMetadataToQueryParams(Provider<UriBuilder> builder); @SuppressWarnings("unchecked") R bindToRequest(R request, Object input);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testMustBeMap() { BindMetadataToQueryParams binder = new BindMetadataToQueryParams(null); HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http: binder.bindToRequest(request, new File("foo")); }
@Test public void testCorrect() throws SecurityException, NoSuchMethodException { HttpRequest request = new HttpRequest("GET", URI.create("http: BindMetadataToQueryParams binder = new BindMetadataToQueryParams(new Provider<UriBuilder>() { @Override public UriBuilder get() { return new UriBuilderImpl(); } }); request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2")); assertEquals(request.getRequestLine(), "GET http: }
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class }) public void testNullIsBad() { BindMetadataToQueryParams binder = new BindMetadataToQueryParams(null); GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class); binder.bindToRequest(request, null); }