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