PageRenderTime 52ms CodeModel.GetById 39ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

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

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