PageRenderTime 226ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/smart-hbase/smart-hbase-dao/src/test/java/com/smartitengineering/dao/impl/hbase/CommonDaoWriteTest.java

https://github.com/saumitra123/smart-dao
Java | 290 lines | 239 code | 21 blank | 30 comment | 0 complexity | fb4d2590e4d50ccbe8699652913f0387 MD5 | raw file
  1. /*
  2. * This is a common dao with basic CRUD operations and is not limited to any
  3. * persistent layer implementation
  4. *
  5. * Copyright (C) 2010 Imran M Yousuf (imyousuf@smartitengineering.com)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. package com.smartitengineering.dao.impl.hbase;
  20. import com.google.inject.AbstractModule;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Inject;
  23. import com.google.inject.Injector;
  24. import com.google.inject.PrivateModule;
  25. import com.google.inject.Scopes;
  26. import com.google.inject.Singleton;
  27. import com.google.inject.TypeLiteral;
  28. import com.google.inject.name.Named;
  29. import com.google.inject.name.Names;
  30. import com.google.inject.util.Providers;
  31. import com.smartitengineering.dao.hbase.ddl.HBaseTableConfiguration;
  32. import com.smartitengineering.dao.hbase.ddl.HBaseTableGenerator;
  33. import com.smartitengineering.dao.hbase.ddl.config.json.ConfigurationJsonParser;
  34. import com.smartitengineering.dao.impl.hbase.data.SampleDomain;
  35. import com.smartitengineering.dao.impl.hbase.data.SampleDomainObjectCoverter;
  36. import com.smartitengineering.dao.impl.hbase.spi.AsyncExecutorService;
  37. import com.smartitengineering.dao.impl.hbase.spi.DomainIdInstanceProvider;
  38. import com.smartitengineering.dao.impl.hbase.spi.FilterConfigs;
  39. import com.smartitengineering.dao.impl.hbase.spi.LockAttainer;
  40. import com.smartitengineering.dao.impl.hbase.spi.LockType;
  41. import com.smartitengineering.dao.impl.hbase.spi.MergeService;
  42. import com.smartitengineering.dao.impl.hbase.spi.ObjectRowConverter;
  43. import com.smartitengineering.dao.impl.hbase.spi.SchemaInfoProvider;
  44. import com.smartitengineering.dao.impl.hbase.spi.impl.LockAttainerImpl;
  45. import com.smartitengineering.dao.impl.hbase.spi.impl.MixedExecutorServiceImpl;
  46. import com.smartitengineering.dao.impl.hbase.spi.impl.SchemaInfoProviderBaseConfig;
  47. import com.smartitengineering.dao.impl.hbase.spi.impl.SchemaInfoProviderImpl;
  48. import com.smartitengineering.dao.impl.hbase.spi.impl.guice.GenericBaseConfigProvider;
  49. import com.smartitengineering.dao.impl.hbase.spi.impl.guice.GenericFilterConfigsProvider;
  50. import java.io.InputStream;
  51. import java.util.Collection;
  52. import java.util.concurrent.ExecutorService;
  53. import java.util.concurrent.Executors;
  54. import java.util.concurrent.TimeUnit;
  55. import org.apache.hadoop.hbase.HBaseTestingUtility;
  56. import org.junit.AfterClass;
  57. import org.junit.Assert;
  58. import org.junit.BeforeClass;
  59. import org.junit.Test;
  60. import org.slf4j.Logger;
  61. import org.slf4j.LoggerFactory;
  62. /**
  63. *
  64. * @author imyousuf
  65. */
  66. public class CommonDaoWriteTest {
  67. private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
  68. private static final Logger LOGGER = LoggerFactory.getLogger(CommonDaoWriteTest.class);
  69. private static Injector injector;
  70. private static CommonDaos commonDaos;
  71. @BeforeClass
  72. public static void globalSetup() throws Exception {
  73. /*
  74. * Start HBase and initialize tables
  75. */
  76. //-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
  77. System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
  78. "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
  79. try {
  80. TEST_UTIL.startMiniCluster();
  81. }
  82. catch (Exception ex) {
  83. LOGGER.error(ex.getMessage(), ex);
  84. }
  85. //Create table for testing
  86. InputStream classpathResource = CommonDaoWriteTest.class.getClassLoader().getResourceAsStream(
  87. "com/smartitengineering/dao/impl/hbase/domain/ddl-config-sample.json");
  88. Collection<HBaseTableConfiguration> configs = ConfigurationJsonParser.getConfigurations(classpathResource);
  89. try {
  90. new HBaseTableGenerator(configs, TEST_UTIL.getConfiguration(), true).generateTables();
  91. }
  92. catch (Exception ex) {
  93. LOGGER.error("Could not create table!", ex);
  94. Assert.fail(ex.getMessage());
  95. }
  96. /*
  97. * Perform injection for different types of common dao
  98. */
  99. injector = Guice.createInjector(new TestModule(), new TestNoModule(), new TestOpModule(), new TestPesModule());
  100. commonDaos = injector.getInstance(CommonDaos.class);
  101. Assert.assertNotNull("Common Daos not initialized properly!", commonDaos);
  102. }
  103. @AfterClass
  104. public static void globalTearDown() {
  105. try {
  106. TEST_UTIL.shutdownMiniCluster();
  107. }
  108. catch (Exception ex) {
  109. LOGGER.warn("Error shutting down!", ex);
  110. }
  111. }
  112. @Test
  113. public void testOptimisticPersist() {
  114. com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> dao = commonDaos.optimisticDao;
  115. SampleDomain domain = new SampleDomain();
  116. domain.setName("Name 1");
  117. domain.setId(1l);
  118. dao.save(domain);
  119. domain.setName("Name 1 `2");
  120. try {
  121. dao.update(domain);
  122. Assert.fail("Should have failed!");
  123. }
  124. catch (Exception ex) {
  125. LOGGER.info(ex.getMessage(), ex);
  126. }
  127. domain.setVersion(1l);
  128. dao.update(domain);
  129. domain.setVersion(3l);
  130. try {
  131. dao.delete(domain);
  132. Assert.fail("Should have failed!");
  133. }
  134. catch (Exception ex) {
  135. LOGGER.info(ex.getMessage(), ex);
  136. }
  137. domain.setVersion(0l);
  138. try {
  139. dao.delete(domain);
  140. Assert.fail("Should have failed!");
  141. }
  142. catch (Exception ex) {
  143. LOGGER.info(ex.getMessage(), ex);
  144. }
  145. domain.setVersion(2l);
  146. dao.delete(domain);
  147. }
  148. @Test
  149. public void testPessimisticPersist() {
  150. com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> dao = commonDaos.pessimisticDao;
  151. SampleDomain domain = new SampleDomain();
  152. domain.setName("Name 2");
  153. domain.setId(2l);
  154. dao.save(domain);
  155. domain.setName("Name 2 `2");
  156. try {
  157. dao.update(domain);
  158. }
  159. catch (Exception ex) {
  160. LOGGER.info(ex.getMessage(), ex);
  161. Assert.fail("Should not have failed!");
  162. }
  163. domain.setVersion(1l);
  164. dao.update(domain);
  165. dao.delete(domain);
  166. }
  167. private static class CommonDaos {
  168. public static final String NO_LOCK = "noLockDao";
  169. public static final String PESSIMISTIC_LOCK = "pessimisticDao";
  170. public static final String OPTIMISTIC_LOCK = "optimistic";
  171. @Inject
  172. @Named(OPTIMISTIC_LOCK)
  173. private com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> optimisticDao;
  174. @Inject
  175. @Named(NO_LOCK)
  176. private com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> noLockDao;
  177. @Inject
  178. @Named(PESSIMISTIC_LOCK)
  179. private com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> pessimisticDao;
  180. public com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> getNoLockDao() {
  181. return noLockDao;
  182. }
  183. public com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> getOptimisticDao() {
  184. return optimisticDao;
  185. }
  186. public com.smartitengineering.dao.common.CommonDao<SampleDomain, Long> getPessimisticDao() {
  187. return pessimisticDao;
  188. }
  189. }
  190. private static final TypeLiteral<SchemaInfoProviderImpl<SampleDomain, Long>> vTypeLiteral = new TypeLiteral<SchemaInfoProviderImpl<SampleDomain, Long>>() {
  191. };
  192. private static final TypeLiteral<com.smartitengineering.dao.common.CommonDao<SampleDomain, Long>> l = new TypeLiteral<com.smartitengineering.dao.common.CommonDao<SampleDomain, Long>>() {
  193. };
  194. private static final TypeLiteral<CommonDao<SampleDomain, Long>> p = new TypeLiteral<CommonDao<SampleDomain, Long>>() {
  195. };
  196. private static class TestModule extends AbstractModule {
  197. @Override
  198. protected void configure() {
  199. bind(new TypeLiteral<FilterConfigs<SampleDomain>>() {
  200. }).toProvider(new GenericFilterConfigsProvider<SampleDomain>(
  201. "com/smartitengineering/dao/impl/hbase/domain/Configs.json")).in(Scopes.SINGLETON);
  202. bind(AsyncExecutorService.class).to(MixedExecutorServiceImpl.class).in(Singleton.class);
  203. bind(ExecutorService.class).toInstance(Executors.newCachedThreadPool());
  204. bind(Integer.class).annotatedWith(Names.named("maxRows")).toInstance(new Integer(100));
  205. bind(Long.class).annotatedWith(Names.named("waitTime")).toInstance(new Long(10));
  206. bind(TimeUnit.class).annotatedWith(Names.named("unit")).toInstance(TimeUnit.SECONDS);
  207. bind(Boolean.class).annotatedWith(Names.named("mergeEnabled")).toInstance(Boolean.FALSE);
  208. bind(DomainIdInstanceProvider.class).toProvider(Providers.<DomainIdInstanceProvider>of(null));
  209. bind(new TypeLiteral<MergeService<SampleDomain, Long>>() {
  210. }).toProvider(Providers.<MergeService<SampleDomain, Long>>of(null));
  211. bind(new TypeLiteral<Class<Long>>() {
  212. }).toInstance(Long.class);
  213. }
  214. }
  215. private static class TestOpModule extends PrivateModule {
  216. @Override
  217. protected void configure() {
  218. bind(new TypeLiteral<ObjectRowConverter<SampleDomain>>() {
  219. }).to(SampleDomainObjectCoverter.class).in(Singleton.class);
  220. bind(new TypeLiteral<LockAttainer<SampleDomain, Long>>() {
  221. }).to(new TypeLiteral<LockAttainerImpl<SampleDomain, Long>>() {
  222. }).in(Scopes.SINGLETON);
  223. bind(new TypeLiteral<SchemaInfoProviderBaseConfig<SampleDomain>>() {
  224. }).toProvider(new GenericBaseConfigProvider<SampleDomain>(
  225. "com/smartitengineering/dao/impl/hbase/domain/BaseConfigOptimistic.json")).in(Scopes.SINGLETON);
  226. bind(LockType.class).toInstance(LockType.OPTIMISTIC);
  227. bind(new TypeLiteral<SchemaInfoProvider<SampleDomain, Long>>() {
  228. }).to(vTypeLiteral).in(Singleton.class);
  229. bind(l).annotatedWith(Names.named(CommonDaos.OPTIMISTIC_LOCK)).to(p).in(Scopes.SINGLETON);
  230. binder().expose(l).annotatedWith(Names.named(CommonDaos.OPTIMISTIC_LOCK));
  231. }
  232. }
  233. private static class TestNoModule extends PrivateModule {
  234. @Override
  235. protected void configure() {
  236. bind(new TypeLiteral<ObjectRowConverter<SampleDomain>>() {
  237. }).to(SampleDomainObjectCoverter.class).in(Singleton.class);
  238. bind(new TypeLiteral<LockAttainer<SampleDomain, Long>>() {
  239. }).to(new TypeLiteral<LockAttainerImpl<SampleDomain, Long>>() {
  240. }).in(Scopes.SINGLETON);
  241. bind(new TypeLiteral<SchemaInfoProvider<SampleDomain, Long>>() {
  242. }).to(vTypeLiteral).in(Singleton.class);
  243. bind(new TypeLiteral<SchemaInfoProviderBaseConfig<SampleDomain>>() {
  244. }).toProvider(new GenericBaseConfigProvider<SampleDomain>(
  245. "com/smartitengineering/dao/impl/hbase/domain/BaseConfigNonOptimistic.json")).in(Scopes.SINGLETON);
  246. bind(LockType.class).toInstance(LockType.NONE);
  247. bind(l).annotatedWith(Names.named(CommonDaos.NO_LOCK)).to(p).in(Scopes.SINGLETON);
  248. binder().expose(l).annotatedWith(Names.named(CommonDaos.NO_LOCK));
  249. }
  250. }
  251. private static class TestPesModule extends PrivateModule {
  252. @Override
  253. protected void configure() {
  254. bind(new TypeLiteral<ObjectRowConverter<SampleDomain>>() {
  255. }).to(SampleDomainObjectCoverter.class).in(Singleton.class);
  256. bind(new TypeLiteral<LockAttainer<SampleDomain, Long>>() {
  257. }).to(new TypeLiteral<LockAttainerImpl<SampleDomain, Long>>() {
  258. }).in(Scopes.SINGLETON);
  259. bind(new TypeLiteral<SchemaInfoProvider<SampleDomain, Long>>() {
  260. }).to(vTypeLiteral).in(Singleton.class);
  261. bind(new TypeLiteral<SchemaInfoProviderBaseConfig<SampleDomain>>() {
  262. }).toProvider(new GenericBaseConfigProvider<SampleDomain>(
  263. "com/smartitengineering/dao/impl/hbase/domain/BaseConfigNonOptimistic.json")).in(Scopes.SINGLETON);
  264. bind(LockType.class).toInstance(LockType.PESSIMISTIC);
  265. bind(l).annotatedWith(Names.named(CommonDaos.PESSIMISTIC_LOCK)).to(p).in(Scopes.SINGLETON);
  266. binder().expose(l).annotatedWith(Names.named(CommonDaos.PESSIMISTIC_LOCK));
  267. }
  268. }
  269. }