/core/src/test/java/org/jclouds/http/functions/ParseFirstJsonValueNamedTest.java

https://github.com/richardcloudsoft/legacy-jclouds · Java · 151 lines · 99 code · 27 blank · 25 comment · 0 complexity · 621dc5223c793020e8aa9f05bd1c0789 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.http.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.IOException;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.jclouds.http.HttpResponse;
  26. import org.jclouds.io.Payloads;
  27. import org.jclouds.json.config.GsonModule;
  28. import org.jclouds.json.internal.GsonWrapper;
  29. import org.testng.annotations.Test;
  30. import com.google.common.collect.ImmutableList;
  31. import com.google.common.collect.ImmutableMap;
  32. import com.google.common.collect.ImmutableSet;
  33. import com.google.inject.Guice;
  34. import com.google.inject.TypeLiteral;
  35. /**
  36. *
  37. * @author Adrian Cole
  38. */
  39. @Test(testName = "ParseFirstJsonValueNamedTest")
  40. public class ParseFirstJsonValueNamedTest {
  41. GsonWrapper json = Guice.createInjector(new GsonModule()).getInstance(GsonWrapper.class);
  42. static class Event {
  43. private String name;
  44. private String source;
  45. private Event(String name, String source) {
  46. this.name = name;
  47. this.source = source;
  48. }
  49. @Override
  50. public String toString() {
  51. return String.format("(name=%s, source=%s)", name, source);
  52. }
  53. }
  54. public void testParseNestedElements() throws IOException {
  55. String nested = "{ \"count\":1 ,\"event\" : [ {name:'GREETINGS',source:'guest'} ] }";
  56. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie")
  57. .payload(Payloads.newPayload(nested)).build();
  58. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  59. }, "event").apply(response);
  60. assertEquals(val.toString(), "[(name=GREETINGS, source=guest)]");
  61. }
  62. /**
  63. * scenario: server renames field from event to _event
  64. */
  65. public void testParseRenamedField() throws IOException {
  66. String nested = "{ \"count\":1 ,\"_event\" : [ {name:'GREETINGS',source:'guest'} ] }";
  67. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie")
  68. .payload(Payloads.newPayload(nested)).build();
  69. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  70. }, "event", "_event").apply(response);
  71. assertEquals(val.toString(), "[(name=GREETINGS, source=guest)]");
  72. }
  73. public void testParseNestedElementsWhenNotFoundIsEmpty() throws IOException {
  74. String nested = "{ \"count\":1 ,\"evant\" : [ {name:'GREETINGS',source:'guest'} ] }";
  75. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie")
  76. .payload(Payloads.newPayload(nested)).build();
  77. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  78. }, "event").apply(response);
  79. assertEquals(val.toString(), "[]");
  80. }
  81. public void testParseNestedElementsButNothing() throws IOException {
  82. String nested = "{ \"count\":1 ,\"event\" : [ ] }";
  83. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie")
  84. .payload(Payloads.newPayload(nested)).build();
  85. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  86. }, "event").apply(response);
  87. assertEquals(val.toString(), "[]");
  88. }
  89. public void testParseNestedFurtherElements() throws IOException {
  90. String nestedFurther = "{ \"listaccountsresponse\" : { \"count\":1 ,\"event\" : [ {name:'GREETINGS',source:'guest'} ] } }";
  91. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie")
  92. .payload(Payloads.newPayload(nestedFurther)).build();
  93. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  94. }, "event").apply(response);
  95. assertEquals(val.toString(), "[(name=GREETINGS, source=guest)]");
  96. }
  97. public void testParseNestedFurtherElementsButNothing() throws IOException {
  98. String nestedFurther = "{ \"listaccountsresponse\" : { \"count\":1 ,\"event\" : [ ] } }";
  99. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie")
  100. .payload(Payloads.newPayload(nestedFurther)).build();
  101. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  102. }, "event").apply(response);
  103. assertEquals(val.toString(), "[]");
  104. }
  105. public void testParseNoPayloadEmptyList() throws IOException {
  106. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie").build();
  107. List<Event> val = new ParseFirstJsonValueNamed<List<Event>>(json, new TypeLiteral<List<Event>>() {
  108. }, "event").apply(response);
  109. assertEquals(val, ImmutableList.<Event> of());
  110. }
  111. public void testParseNoPayloadEmptyMap() throws IOException {
  112. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie").build();
  113. Map<String, String> val = new ParseFirstJsonValueNamed<Map<String, String>>(json,
  114. new TypeLiteral<Map<String, String>>() {
  115. }, "event").apply(response);
  116. assertEquals(val, ImmutableMap.<String, String> of());
  117. }
  118. public void testParseNoPayloadEmptySet() throws IOException {
  119. HttpResponse response = HttpResponse.builder().statusCode(200).message("goodie").build();
  120. Set<Event> val = new ParseFirstJsonValueNamed<Set<Event>>(json, new TypeLiteral<Set<Event>>() {
  121. }, "event").apply(response);
  122. assertEquals(val, ImmutableSet.<Event> of());
  123. }
  124. }