PageRenderTime 58ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/byon/src/test/java/org/jclouds/byon/config/YamlNodeStoreModuleTest.java

https://github.com/vkris/jclouds
Java | 196 lines | 137 code | 35 blank | 24 comment | 3 complexity | 218447abf33e28ebc43133a66e8198ba 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.byon.config;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.Map;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import org.jclouds.byon.Node;
  27. import org.jclouds.io.CopyInputStreamInputSupplierMap;
  28. import org.jclouds.location.Provider;
  29. import org.jclouds.util.Strings2;
  30. import org.testng.annotations.DataProvider;
  31. import org.testng.annotations.Test;
  32. import org.yaml.snakeyaml.Yaml;
  33. import com.google.common.base.Supplier;
  34. import com.google.common.base.Suppliers;
  35. import com.google.common.cache.Cache;
  36. import com.google.common.io.InputSupplier;
  37. import com.google.common.util.concurrent.UncheckedExecutionException;
  38. import com.google.inject.AbstractModule;
  39. import com.google.inject.Guice;
  40. import com.google.inject.Injector;
  41. import com.google.inject.Key;
  42. import com.google.inject.TypeLiteral;
  43. import com.google.inject.name.Names;
  44. /**
  45. *
  46. * @author Adrian Cole
  47. */
  48. @Test(groups = "unit", singleThreaded = true)
  49. public class YamlNodeStoreModuleTest {
  50. Yaml yaml = createInjector().getInstance(Yaml.class);
  51. @DataProvider(name = "names")
  52. public Object[][] createData() {
  53. return new Object[][] { { "instance1", "bear" }, { "instance2", "apple" }, { "instance2", "francis" },
  54. { "instance4", "robot" } };
  55. }
  56. @Test(dataProvider = "names")
  57. public void deleteObject(String id, String name) throws InterruptedException, IOException {
  58. Injector injector = createInjector();
  59. Map<String, InputStream> map = getMap(injector);
  60. check(map, getStore(injector), "i-20312", id, name);
  61. }
  62. public void testProvidedMapWithValue() throws IOException {
  63. Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
  64. new ConcurrentHashMap<String, InputSupplier<InputStream>>());
  65. map.put("test", new ByteArrayInputStream("id: instance1\nname: instancename\n".getBytes()));
  66. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
  67. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
  68. remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
  69. }
  70. public void testProvidedConsistentAcrossRepeatedWrites() throws IOException {
  71. Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
  72. new ConcurrentHashMap<String, InputSupplier<InputStream>>());
  73. Injector injector = createInjectorWithProvidedMap(map);
  74. assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
  75. }, Names.named("yaml"))), map);
  76. Cache<String, Node> store = getStore(injector);
  77. for (int i = 0; i < 10; i++)
  78. check(map, store, "test" + i, "instance1" + i, "instancename" + i);
  79. }
  80. public void testProvidedConsistentAcrossMultipleInjectors() throws IOException {
  81. Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
  82. new ConcurrentHashMap<String, InputSupplier<InputStream>>());
  83. put(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
  84. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
  85. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
  86. remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
  87. }
  88. public void testDefaultConsistentAcrossMultipleInjectors() throws IOException {
  89. Map<String, InputStream> map = getMap(createInjector());
  90. put(map, getStore(createInjector()), "test", "instance1", "instancename");
  91. checkConsistent(map, getStore(createInjector()), "test", "instance1", "instancename");
  92. checkConsistent(map, getStore(createInjector()), "test", "instance1", "instancename");
  93. remove(map, getStore(createInjector()), "test");
  94. }
  95. protected Cache<String, Node> getStore(Injector injector) {
  96. return injector.getInstance(Key.get(new TypeLiteral<Cache<String, Node>>() {
  97. }));
  98. }
  99. protected Map<String, InputStream> getMap(Injector injector) {
  100. return injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
  101. }, Names.named("yaml")));
  102. }
  103. protected Injector createInjectorWithProvidedMap(Map<String, InputStream> map) {
  104. return Guice.createInjector(new YamlNodeStoreModule(map), new AbstractModule() {
  105. @Override
  106. protected void configure() {
  107. bind(new TypeLiteral<Supplier<InputStream>>() {
  108. }).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
  109. }
  110. });
  111. }
  112. protected Injector createInjector() {
  113. return Guice.createInjector(new YamlNodeStoreModule(), new AbstractModule() {
  114. @Override
  115. protected void configure() {
  116. bind(new TypeLiteral<Supplier<InputStream>>() {
  117. }).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
  118. }
  119. });
  120. }
  121. protected void check(Map<String, InputStream> map, Cache<String, Node> store, String key, String id, String name)
  122. throws IOException {
  123. put(map, store, key, id, name);
  124. checkConsistent(map, store, key, id, name);
  125. remove(map, store, key);
  126. }
  127. protected void remove(Map<String, InputStream> map, Cache<String, Node> store, String key) {
  128. store.invalidate(key);
  129. assertEquals(store.size(), 0);
  130. map.remove(key);
  131. assertEquals(map.size(), 0);
  132. try {
  133. assertEquals(store.getUnchecked(key), null);
  134. assert false : "should not work as null is invalid";
  135. } catch (UncheckedExecutionException e) {
  136. }
  137. assertEquals(map.get(key), null);
  138. }
  139. protected void checkConsistent(Map<String, InputStream> map, Cache<String, Node> store, String key, String id,
  140. String name) throws IOException {
  141. assertEquals(map.size(), 1);
  142. if (store.size() == 0)
  143. store.getUnchecked(key);
  144. assertEquals(store.size(), 1);
  145. // checkRepeatedRead
  146. assertEquals(store.getUnchecked(key), Node.builder().id(id).name(name).build());
  147. assertEquals(store.getUnchecked(key), Node.builder().id(id).name(name).build());
  148. // checkRepeatedRead
  149. checkToYaml(map, key, id, name);
  150. checkToYaml(map, key, id, name);
  151. }
  152. protected void checkToYaml(Map<String, InputStream> map, String key, String id, String name) throws IOException {
  153. assertEquals(Strings2.toStringAndClose(map.get(key)), String.format("id: %s\nname: %s\n", id, name));
  154. }
  155. protected void put(Map<String, InputStream> map, Cache<String, Node> store, String key, String id, String name) {
  156. assertEquals(store.size(), 0);
  157. assertEquals(map.size(), 0);
  158. map.put(key, new ByteArrayInputStream(String.format("id: %s\nname: %s\n", id, name).getBytes()));
  159. store.getUnchecked(key);
  160. }
  161. }