/core/src/test/java/org/jclouds/domain/JsonObjectTest.java

https://github.com/andreisavu/jclouds · Java · 98 lines · 52 code · 23 blank · 23 comment · 0 complexity · 7648b58be9a9e1b9700ddaafb495c9e0 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.domain;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import org.jclouds.http.HttpResponse;
  24. import org.jclouds.http.functions.ParseJson;
  25. import org.jclouds.json.Json;
  26. import org.jclouds.json.config.GsonModule;
  27. import org.testng.annotations.BeforeTest;
  28. import org.testng.annotations.Test;
  29. import com.google.common.collect.ImmutableList;
  30. import com.google.common.collect.ImmutableMap;
  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. * @author Adrian Cole
  37. */
  38. @Test(groups = "unit")
  39. public class JsonObjectTest {
  40. private ParseJson<Map<String, Object>> handler;
  41. private Json mapper;
  42. @BeforeTest
  43. protected void setUpInjector() throws IOException {
  44. Injector injector = Guice.createInjector(new GsonModule());
  45. handler = injector.getInstance(Key.get(new TypeLiteral<ParseJson<Map<String, Object>>>() {
  46. }));
  47. mapper = injector.getInstance(Json.class);
  48. }
  49. public void testHash() {
  50. String json = "{\"tomcat6\":{\"ssl_port\":8433}}";
  51. // gson deserialized numbers to double, so integers end up changed to fractions
  52. assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()),
  53. ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433d)));
  54. assertEquals(mapper.toJson(ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433))), json);
  55. }
  56. public void testList() {
  57. String json = "{\"list\":[8431,8433]}";
  58. // gson deserialized numbers to double, so integers end up changed to fractions
  59. assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()),
  60. ImmutableMap.<String, Object> of("list", ImmutableList.of(8431d, 8433d)));
  61. assertEquals(mapper.toJson(ImmutableMap.<String, Object> of("list", ImmutableList.of(8431, 8433))), json);
  62. }
  63. public void testString() {
  64. String json = "{\"name\":\"fooy\"}";
  65. Map<String, Object> map = ImmutableMap.<String, Object> of("name", "fooy");
  66. assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()), map);
  67. assertEquals(mapper.toJson(map), json);
  68. }
  69. public void testNumber() {
  70. String json = "{\"number\":1.0}";
  71. Map<String, Object> map = ImmutableMap.<String, Object> of("number", 1d);
  72. assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()), map);
  73. assertEquals(mapper.toJson(map), json);
  74. }
  75. }