PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/ohai/functions/NestSlashKeysTest.java

https://github.com/jclouds/legacy-jclouds-chef
Java | 121 lines | 84 code | 14 blank | 23 comment | 0 complexity | 467ebb6fa1e9c41d28967d7db9826cfb 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.ohai.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.IOException;
  22. import org.jclouds.chef.ChefApi;
  23. import org.jclouds.chef.config.ChefParserModule;
  24. import org.jclouds.domain.JsonBall;
  25. import org.jclouds.json.Json;
  26. import org.jclouds.json.config.GsonModule;
  27. import org.jclouds.rest.annotations.ApiVersion;
  28. import org.testng.annotations.BeforeTest;
  29. import org.testng.annotations.Test;
  30. import com.google.common.base.Supplier;
  31. import com.google.common.base.Suppliers;
  32. import com.google.common.collect.ImmutableMultimap;
  33. import com.google.inject.AbstractModule;
  34. import com.google.inject.Guice;
  35. import com.google.inject.Injector;
  36. /**
  37. * Tests behavior of {@code NestSlashKeys}
  38. *
  39. * @author Adrian Cole
  40. */
  41. @Test(groups = { "unit" }, sequential = true)
  42. public class NestSlashKeysTest {
  43. private NestSlashKeys converter;
  44. private Json json;
  45. @BeforeTest
  46. protected void setUpInjector() throws IOException {
  47. Injector injector = Guice.createInjector(new AbstractModule() {
  48. @Override
  49. protected void configure() {
  50. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApi.VERSION);
  51. }
  52. }, new ChefParserModule(), new GsonModule());
  53. converter = injector.getInstance(NestSlashKeys.class);
  54. json = injector.getInstance(Json.class);
  55. }
  56. @Test
  57. public void testBase() {
  58. assertEquals(
  59. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  60. Suppliers.ofInstance(new JsonBall("java"))))), "{\"java\":\"java\"}");
  61. }
  62. @Test(expectedExceptions = IllegalArgumentException.class)
  63. public void testIllegal() {
  64. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  65. Suppliers.ofInstance(new JsonBall("java")), "java/system", Suppliers.ofInstance(new JsonBall("system")))));
  66. }
  67. @Test
  68. public void testOne() {
  69. assertEquals(
  70. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  71. Suppliers.ofInstance(new JsonBall("{\"time\":\"time\"}")), "java/system",
  72. Suppliers.ofInstance(new JsonBall("system"))))),
  73. "{\"java\":{\"system\":\"system\",\"time\":\"time\"}}");
  74. }
  75. @Test
  76. public void testOneDuplicate() {
  77. assertEquals(
  78. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  79. Suppliers.ofInstance(new JsonBall("{\"time\":\"time\"}")), "java",
  80. Suppliers.ofInstance(new JsonBall("{\"system\":\"system\"}"))))),
  81. "{\"java\":{\"system\":\"system\",\"time\":\"time\"}}");
  82. }
  83. @Test
  84. public void testMerge() {
  85. assertEquals(
  86. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  87. Suppliers.ofInstance(new JsonBall("{\"time\":{\"1\":\"hello\"}}")), "java/time",
  88. Suppliers.ofInstance(new JsonBall("{\"2\":\"goodbye\"}"))))),
  89. "{\"java\":{\"time\":{\"2\":\"goodbye\",\"1\":\"hello\"}}}");
  90. }
  91. @Test
  92. public void testMergeNestedTwice() {
  93. assertEquals(
  94. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  95. Suppliers.ofInstance(new JsonBall("{\"time\":{\"1\":\"hello\"}}")), "java",
  96. Suppliers.ofInstance(new JsonBall("{\"time\":{\"2\":\"goodbye\"}}"))))),
  97. "{\"java\":{\"time\":{\"2\":\"goodbye\",\"1\":\"hello\"}}}");
  98. }
  99. @Test
  100. public void testReplaceList() {
  101. assertEquals(
  102. json.toJson(converter.apply(ImmutableMultimap.<String, Supplier<JsonBall>> of("java",
  103. Suppliers.ofInstance(new JsonBall("{\"time\":{\"1\":[\"hello\"]}}")), "java/time",
  104. Suppliers.ofInstance(new JsonBall("{\"1\":[\"goodbye\"]}"))))),
  105. "{\"java\":{\"time\":{\"1\":[\"goodbye\"]}}}");
  106. }
  107. }