/apis/cloudservers/src/test/java/org/jclouds/cloudservers/functions/ParseSharedIpGroupListFromJsonResponseTest.java

https://github.com/regularfry/jclouds · Java · 83 lines · 43 code · 17 blank · 23 comment · 0 complexity · a0638c4ef11048d0d106ec67a8eefad6 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.cloudservers.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.InputStream;
  22. import java.net.UnknownHostException;
  23. import java.util.List;
  24. import org.jclouds.cloudservers.domain.SharedIpGroup;
  25. import org.jclouds.http.HttpResponse;
  26. import org.jclouds.http.functions.UnwrapOnlyJsonValue;
  27. import org.jclouds.io.Payloads;
  28. import org.jclouds.json.config.GsonModule;
  29. import org.testng.annotations.Test;
  30. import com.google.common.collect.ImmutableList;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. import com.google.inject.Key;
  34. import com.google.inject.TypeLiteral;
  35. /**
  36. * Tests behavior of {@code ParseSharedIpGroupListFromJsonResponse}
  37. *
  38. * @author Adrian Cole
  39. */
  40. @Test(groups = "unit")
  41. public class ParseSharedIpGroupListFromJsonResponseTest {
  42. Injector i = Guice.createInjector(new GsonModule());
  43. public void testApplyInputStream() {
  44. InputStream is = getClass().getResourceAsStream("/test_list_sharedipgroups.json");
  45. List<SharedIpGroup> expects = ImmutableList.of(new SharedIpGroup(1234, "Shared IP Group 1"), new SharedIpGroup(
  46. 5678, "Shared IP Group 2"));
  47. UnwrapOnlyJsonValue<List<SharedIpGroup>> parser = i.getInstance(Key
  48. .get(new TypeLiteral<UnwrapOnlyJsonValue<List<SharedIpGroup>>>() {
  49. }));
  50. List<SharedIpGroup> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
  51. assertEquals(response, expects);
  52. }
  53. public void testApplyInputStreamDetails() throws UnknownHostException {
  54. InputStream is = getClass().getResourceAsStream("/test_list_sharedipgroups_detail.json");
  55. UnwrapOnlyJsonValue<List<SharedIpGroup>> parser = i.getInstance(Key
  56. .get(new TypeLiteral<UnwrapOnlyJsonValue<List<SharedIpGroup>>>() {
  57. }));
  58. List<SharedIpGroup> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
  59. assertEquals(response.get(0).getId(), 1234);
  60. assertEquals(response.get(0).getName(), "Shared IP Group 1");
  61. assertEquals(response.get(0).getServers(), ImmutableList.of(422, 3445));
  62. assertEquals(response.get(1).getId(), 5678);
  63. assertEquals(response.get(1).getName(), "Shared IP Group 2");
  64. assertEquals(response.get(1).getServers(), ImmutableList.of(23203, 2456, 9891));
  65. }
  66. }