PageRenderTime 68ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/andreisavu/jclouds
Java | 71 lines | 36 code | 12 blank | 23 comment | 0 complexity | 21a2b04602d9437be2ed1fc2f04075e6 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 org.jclouds.http.HttpRequest;
  22. import org.jclouds.json.config.GsonModule;
  23. import org.testng.annotations.Test;
  24. import com.google.common.collect.ImmutableMap;
  25. import com.google.inject.Guice;
  26. import com.google.inject.Injector;
  27. import com.google.inject.assistedinject.FactoryModuleBuilder;
  28. /**
  29. * Tests behavior of {@code BindToJsonPayloadWrappedWith}
  30. *
  31. * @author Adrian Cole
  32. */
  33. @Test(groups = "unit")
  34. public class BindToJsonPayloadWrappedWithTest {
  35. Injector injector = Guice.createInjector(new GsonModule(), new FactoryModuleBuilder()
  36. .build(BindToJsonPayloadWrappedWith.Factory.class));
  37. @Test
  38. public void testCorrect() throws SecurityException, NoSuchMethodException {
  39. BindToJsonPayloadWrappedWith binder = new BindToJsonPayloadWrappedWith(injector
  40. .getInstance(BindToJsonPayload.class), "envelope");
  41. HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build();
  42. request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
  43. assertEquals(request.getPayload().getRawContent(), "{\"envelope\":{\"imageName\":\"foo\",\"serverId\":\"2\"}}");
  44. }
  45. @Test
  46. public void testFactoryCorrect() throws SecurityException, NoSuchMethodException {
  47. BindToJsonPayloadWrappedWith binder = injector.getInstance(BindToJsonPayloadWrappedWith.Factory.class).create(
  48. "envelope");
  49. HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build();
  50. request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
  51. assertEquals(request.getPayload().getRawContent(), "{\"envelope\":{\"imageName\":\"foo\",\"serverId\":\"2\"}}");
  52. }
  53. @Test(expectedExceptions = NullPointerException.class)
  54. public void testNullIsBad() {
  55. BindToJsonPayloadWrappedWith binder = new BindToJsonPayloadWrappedWith(injector
  56. .getInstance(BindToJsonPayload.class), "envelope");
  57. binder.bindToRequest(HttpRequest.builder().method("GET").endpoint("http://momma").build(), null);
  58. }
  59. }