/core/src/test/java/org/jclouds/rest/binders/BindToJsonPayloadWrappedWithTest.java
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 */
19package org.jclouds.rest.binders;
20
21import static org.testng.Assert.assertEquals;
22
23import java.net.URI;
24
25import org.jclouds.http.HttpRequest;
26import org.jclouds.json.config.GsonModule;
27import org.testng.annotations.Test;
28
29import com.google.common.collect.ImmutableMap;
30import com.google.inject.Guice;
31import com.google.inject.Injector;
32import com.google.inject.assistedinject.FactoryModuleBuilder;
33
34/**
35 * Tests behavior of {@code BindToJsonPayloadWrappedWith}
36 *
37 * @author Adrian Cole
38 */
39@Test(groups = "unit")
40public class BindToJsonPayloadWrappedWithTest {
41
42 Injector injector = Guice.createInjector(new GsonModule(), new FactoryModuleBuilder()
43 .build(BindToJsonPayloadWrappedWith.Factory.class));
44
45 @Test
46 public void testCorrect() throws SecurityException, NoSuchMethodException {
47 BindToJsonPayloadWrappedWith binder = new BindToJsonPayloadWrappedWith(injector
48 .getInstance(BindToJsonPayload.class), "envelope");
49
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 }
55
56 @Test
57 public void testFactoryCorrect() throws SecurityException, NoSuchMethodException {
58 BindToJsonPayloadWrappedWith binder = injector.getInstance(BindToJsonPayloadWrappedWith.Factory.class).create(
59 "envelope");
60
61 HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
62 request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
63 assertEquals(request.getPayload().getRawContent(), "{\"envelope\":{\"imageName\":\"foo\",\"serverId\":\"2\"}}");
64
65 }
66
67 @Test(expectedExceptions = NullPointerException.class)
68 public void testNullIsBad() {
69 BindToJsonPayloadWrappedWith binder = new BindToJsonPayloadWrappedWith(injector
70 .getInstance(BindToJsonPayload.class), "envelope");
71 binder.bindToRequest(HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build(), null);
72 }
73}