/core/src/test/java/org/jclouds/rest/binders/BindToJsonPayloadWrappedWithTest.java

https://github.com/regularfry/jclouds · Java · 73 lines · 37 code · 13 blank · 23 comment · 0 complexity · 9700eb1b66983e93a0e9b29dc9a9c869 MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.rest.binders;
  20. import static org.testng.Assert.assertEquals;
  21. import java.net.URI;
  22. import org.jclouds.http.HttpRequest;
  23. import org.jclouds.json.config.GsonModule;
  24. import org.testng.annotations.Test;
  25. import com.google.common.collect.ImmutableMap;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Injector;
  28. import com.google.inject.assistedinject.FactoryModuleBuilder;
  29. /**
  30. * Tests behavior of {@code BindToJsonPayloadWrappedWith}
  31. *
  32. * @author Adrian Cole
  33. */
  34. @Test(groups = "unit")
  35. public class BindToJsonPayloadWrappedWithTest {
  36. Injector injector = Guice.createInjector(new GsonModule(), new FactoryModuleBuilder()
  37. .build(BindToJsonPayloadWrappedWith.Factory.class));
  38. @Test
  39. public void testCorrect() throws SecurityException, NoSuchMethodException {
  40. BindToJsonPayloadWrappedWith binder = new BindToJsonPayloadWrappedWith(injector
  41. .getInstance(BindToJsonPayload.class), "envelope");
  42. HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
  43. request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
  44. assertEquals(request.getPayload().getRawContent(), "{\"envelope\":{\"imageName\":\"foo\",\"serverId\":\"2\"}}");
  45. }
  46. @Test
  47. public void testFactoryCorrect() throws SecurityException, NoSuchMethodException {
  48. BindToJsonPayloadWrappedWith binder = injector.getInstance(BindToJsonPayloadWrappedWith.Factory.class).create(
  49. "envelope");
  50. HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
  51. request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
  52. assertEquals(request.getPayload().getRawContent(), "{\"envelope\":{\"imageName\":\"foo\",\"serverId\":\"2\"}}");
  53. }
  54. @Test(expectedExceptions = NullPointerException.class)
  55. public void testNullIsBad() {
  56. BindToJsonPayloadWrappedWith binder = new BindToJsonPayloadWrappedWith(injector
  57. .getInstance(BindToJsonPayload.class), "envelope");
  58. binder.bindToRequest(HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build(), null);
  59. }
  60. }