PageRenderTime 134ms CodeModel.GetById 41ms app.highlight 57ms RepoModel.GetById 14ms app.codeStats 0ms

/labs/hostedchef/src/test/java/org/jclouds/hostedchef/binders/BindGroupToUpdateRequestJsonPayloadTest.java

https://github.com/jclouds/legacy-jclouds-chef
Java | 90 lines | 53 code | 14 blank | 23 comment | 0 complexity | 6591c3bc160dd2818897e12e78639e98 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.hostedchef.binders;
20
21import static org.testng.Assert.assertEquals;
22
23import java.io.IOException;
24import java.net.URI;
25
26import org.jclouds.chef.ChefApi;
27import org.jclouds.chef.config.ChefParserModule;
28import org.jclouds.hostedchef.domain.Group;
29import org.jclouds.http.HttpRequest;
30import org.jclouds.json.config.GsonModule;
31import org.jclouds.rest.annotations.ApiVersion;
32import org.jclouds.util.Strings2;
33import org.testng.annotations.Test;
34
35import com.google.common.collect.ImmutableSet;
36import com.google.inject.AbstractModule;
37import com.google.inject.Guice;
38import com.google.inject.Injector;
39
40/**
41 * Unit tests for the {@link BindGroupToUpdateRequestJsonPayload} class.
42 * 
43 * @author Ignasi Barrera
44 */
45@Test(groups = "unit", testName = "BindGroupToUpdateRequestJsonPayloadTest")
46public class BindGroupToUpdateRequestJsonPayloadTest {
47
48   private Injector injector = Guice.createInjector(new AbstractModule() {
49      @Override
50      protected void configure() {
51         bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApi.VERSION);
52      }
53   }, new ChefParserModule(), new GsonModule());
54
55   private BindGroupToUpdateRequestJsonPayload binder = injector.getInstance(BindGroupToUpdateRequestJsonPayload.class);
56
57   @Test(expectedExceptions = NullPointerException.class)
58   public void testInvalidNullInput() {
59      HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
60      binder.bindToRequest(request, null);
61   }
62
63   @Test(expectedExceptions = IllegalArgumentException.class)
64   public void testInvalidTypeInput() {
65      HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
66      binder.bindToRequest(request, new Object());
67   }
68
69   public void testBindOnlyName() throws IOException {
70      HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
71      HttpRequest newRequest = binder.bindToRequest(request, new Group("foo"));
72
73      String payload = Strings2.toStringAndClose(newRequest.getPayload().getInput());
74      assertEquals(payload, "{\"groupname\":\"foo\",\"actors\":{\"clients\":[],\"groups\":[],\"users\":[]}}");
75   }
76
77   public void testBindNameAndLists() throws IOException {
78      Group group = new Group("foo");
79      group.setClients(ImmutableSet.of("nacx-validator"));
80      group.setGroups(ImmutableSet.of("admins"));
81      group.setUsers(ImmutableSet.of("nacx"));
82
83      HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
84      HttpRequest newRequest = binder.bindToRequest(request, group);
85
86      String payload = Strings2.toStringAndClose(newRequest.getPayload().getInput());
87      assertEquals(payload,
88            "{\"groupname\":\"foo\",\"actors\":{\"clients\":[\"nacx-validator\"],\"groups\":[\"admins\"],\"users\":[\"nacx\"]}}");
89   }
90}