PageRenderTime 76ms CodeModel.GetById 46ms app.highlight 5ms RepoModel.GetById 23ms app.codeStats 0ms

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

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