ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [10/50] [abbrv] ambari git commit: AMBARI-19799. Optimize DB initialization for Ambari Server Unit Tests. (mpapirkovskyy)
Date Fri, 03 Feb 2017 14:53:14 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
index 5890c35..e29c276 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
@@ -17,12 +17,14 @@
  */
 package org.apache.ambari.server.orm.dao;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -47,7 +49,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 public class ServiceConfigDAOTest {
   private static final StackId HDP_01 = new StackId("HDP", "0.1");
@@ -78,8 +79,8 @@ public class ServiceConfigDAOTest {
   }
 
   @After
-  public void teardown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   private ServiceConfigEntity createServiceConfig(String serviceName,
@@ -156,9 +157,11 @@ public class ServiceConfigDAOTest {
     ServiceConfigEntity serviceConfigEntity =
       createServiceConfig("HDFS", "admin", 1L, 1L, 1111L, null);
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     Assert.assertNotNull(serviceConfigEntity);
     Assert.assertEquals("c1", serviceConfigEntity.getClusterEntity().getClusterName());
-    Assert.assertEquals(Long.valueOf(1), serviceConfigEntity.getClusterEntity()
+    Assert.assertEquals(clusterId, serviceConfigEntity.getClusterEntity()
       .getClusterId());
     Assert.assertEquals("HDFS", serviceConfigEntity.getServiceName());
     Assert.assertEquals(Long.valueOf(1111L), serviceConfigEntity.getCreateTimestamp());
@@ -175,9 +178,11 @@ public class ServiceConfigDAOTest {
 
     ServiceConfigEntity serviceConfigEntity = serviceConfigDAO.find(sce.getServiceConfigId());
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     Assert.assertNotNull(serviceConfigEntity);
     Assert.assertEquals("c1", serviceConfigEntity.getClusterEntity().getClusterName());
-    Assert.assertEquals(Long.valueOf(1), serviceConfigEntity.getClusterEntity()
+    Assert.assertEquals(clusterId, serviceConfigEntity.getClusterEntity()
       .getClusterId());
     Assert.assertEquals("HDFS", serviceConfigEntity.getServiceName());
     Assert.assertEquals(Long.valueOf(1111L), serviceConfigEntity.getCreateTimestamp());
@@ -195,9 +200,11 @@ public class ServiceConfigDAOTest {
     ServiceConfigEntity serviceConfigEntity =
       serviceConfigDAO.findByServiceAndVersion("HDFS", 1L);
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     Assert.assertNotNull(serviceConfigEntity);
     Assert.assertEquals("c1", serviceConfigEntity.getClusterEntity().getClusterName());
-    Assert.assertEquals(Long.valueOf(1), serviceConfigEntity.getClusterEntity()
+    Assert.assertEquals(clusterId, serviceConfigEntity.getClusterEntity()
       .getClusterId());
     Assert.assertEquals("HDFS", serviceConfigEntity.getServiceName());
     Assert.assertEquals(Long.valueOf(1111L), serviceConfigEntity.getCreateTimestamp());
@@ -235,10 +242,12 @@ public class ServiceConfigDAOTest {
     Assert.assertNotNull(serviceConfigEntities);
     Assert.assertEquals(2, serviceConfigEntities.size());
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     for (ServiceConfigEntity sce: serviceConfigEntities) {
       if ("HDFS".equals(sce.getServiceName())) {
         Assert.assertEquals("c1", sce.getClusterEntity().getClusterName());
-        Assert.assertEquals(Long.valueOf(1), sce.getClusterEntity()
+        Assert.assertEquals(clusterId, sce.getClusterEntity()
           .getClusterId());
         Assert.assertEquals(Long.valueOf(2222L), sce.getCreateTimestamp());
         Assert.assertEquals(Long.valueOf(2), sce.getVersion());
@@ -247,7 +256,7 @@ public class ServiceConfigDAOTest {
       }
       if ("YARN".equals(sce.getServiceName())) {
         Assert.assertEquals("c1", sce.getClusterEntity().getClusterName());
-        Assert.assertEquals(Long.valueOf(1), sce.getClusterEntity()
+        Assert.assertEquals(clusterId, sce.getClusterEntity()
           .getClusterId());
         Assert.assertEquals(Long.valueOf(3333L), sce.getCreateTimestamp());
         Assert.assertEquals(Long.valueOf(1), sce.getVersion());
@@ -265,7 +274,7 @@ public class ServiceConfigDAOTest {
     Clusters clusters = injector.getInstance(Clusters.class);
     clusters.addCluster("c1", HDP_01);
     ConfigGroupEntity configGroupEntity1 = new ConfigGroupEntity();
-    ClusterEntity clusterEntity = clusterDAO.findById(1L);
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
     configGroupEntity1.setClusterEntity(clusterEntity);
     configGroupEntity1.setClusterId(clusterEntity.getClusterId());
     configGroupEntity1.setGroupName("group1");
@@ -309,12 +318,14 @@ public class ServiceConfigDAOTest {
     createServiceConfig("HDFS", "admin", 2L, 2L, 2222L, null);
     createServiceConfig("YARN", "admin", 1L, 3L, 3333L, null);
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     ServiceConfigEntity serviceConfigEntity =
-      serviceConfigDAO.getLastServiceConfig(1L, "HDFS");
+      serviceConfigDAO.getLastServiceConfig(clusterId, "HDFS");
 
     Assert.assertNotNull(serviceConfigEntity);
     Assert.assertEquals("c1", serviceConfigEntity.getClusterEntity().getClusterName());
-    Assert.assertEquals(Long.valueOf(1), serviceConfigEntity.getClusterEntity()
+    Assert.assertEquals(clusterId, serviceConfigEntity.getClusterEntity()
       .getClusterId());
     Assert.assertEquals("HDFS", serviceConfigEntity.getServiceName());
     Assert.assertEquals(Long.valueOf(2222L), serviceConfigEntity.getCreateTimestamp());
@@ -330,8 +341,10 @@ public class ServiceConfigDAOTest {
     createServiceConfig("HDFS", "admin", 2L, 2L, 2222L, null);
     createServiceConfig("YARN", "admin", 1L, 3L, 3333L, null);
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     List<ServiceConfigEntity> serviceConfigEntities =
-      serviceConfigDAO.getServiceConfigs(clusterDAO.findByName("c1").getClusterId());
+      serviceConfigDAO.getServiceConfigs(clusterId);
 
     Assert.assertNotNull(serviceConfigEntities);
     Assert.assertEquals(3, serviceConfigEntities.size());
@@ -339,21 +352,21 @@ public class ServiceConfigDAOTest {
     for (ServiceConfigEntity sce: serviceConfigEntities) {
       if ("HDFS".equals(sce.getServiceName()) && (sce.getVersion() == 1)) {
         Assert.assertEquals("c1", sce.getClusterEntity().getClusterName());
-        Assert.assertEquals(Long.valueOf(1), sce.getClusterEntity()
+        Assert.assertEquals(clusterId, sce.getClusterEntity()
           .getClusterId());
         Assert.assertEquals(Long.valueOf(1111L), sce.getCreateTimestamp());
         Assert.assertTrue(sce.getClusterConfigEntities().isEmpty());
         Assert.assertNotNull(sce.getServiceConfigId());
       } else if ("HDFS".equals(sce.getServiceName()) && (sce.getVersion() == 2)) {
         Assert.assertEquals("c1", sce.getClusterEntity().getClusterName());
-        Assert.assertEquals(Long.valueOf(1), sce.getClusterEntity()
+        Assert.assertEquals(clusterId, sce.getClusterEntity()
           .getClusterId());
         Assert.assertEquals(Long.valueOf(2222L), sce.getCreateTimestamp());
         Assert.assertTrue(sce.getClusterConfigEntities().isEmpty());
         Assert.assertNotNull(sce.getServiceConfigId());
       } else if ("YARN".equals(sce.getServiceName())) {
         Assert.assertEquals("c1", sce.getClusterEntity().getClusterName());
-        Assert.assertEquals(Long.valueOf(1), sce.getClusterEntity()
+        Assert.assertEquals(clusterId, sce.getClusterEntity()
           .getClusterId());
         Assert.assertEquals(Long.valueOf(3333L), sce.getCreateTimestamp());
         Assert.assertEquals(Long.valueOf(1), sce.getVersion());
@@ -457,11 +470,13 @@ public class ServiceConfigDAOTest {
 
     List<ConfigGroupEntity> configGroupEntities = configGroupDAO.findAllByTag("OOZIE");
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     Assert.assertNotNull(configGroupEntities);
     ConfigGroupEntity configGroupEntity = configGroupEntities.get(0);
     Assert.assertNotNull(configGroupEntity);
     Assert.assertEquals("c1", configGroupEntity.getClusterEntity().getClusterName());
-    Assert.assertEquals(Long.valueOf(1), configGroupEntity.getClusterEntity()
+    Assert.assertEquals(clusterId, configGroupEntity.getClusterEntity()
       .getClusterId());
     Assert.assertEquals("oozie_server", configGroupEntity.getGroupName());
     Assert.assertEquals("OOZIE", configGroupEntity.getTag());
@@ -551,7 +566,8 @@ public class ServiceConfigDAOTest {
     clusters.addCluster("c1", HDP_01);
     initClusterEntitiesWithConfigGroups();
     ConfigGroupEntity configGroupEntity1 = new ConfigGroupEntity();
-    ClusterEntity clusterEntity = clusterDAO.findById(1L);
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    Long clusterId = clusterEntity.getClusterId();
     configGroupEntity1.setClusterEntity(clusterEntity);
     configGroupEntity1.setClusterId(clusterEntity.getClusterId());
     configGroupEntity1.setGroupName("toTestDeleteGroup_OOZIE");
@@ -562,11 +578,11 @@ public class ServiceConfigDAOTest {
     ConfigGroupEntity testDeleteGroup_OOZIE = configGroupDAO.findByName("toTestDeleteGroup_OOZIE");
     createServiceConfigWithGroup("OOZIE", "", 2L, 2L, System.currentTimeMillis(), null,
         testDeleteGroup_OOZIE.getGroupId());
-    Collection<ServiceConfigEntity> serviceConfigEntityList = serviceConfigDAO.getLastServiceConfigsForService(1L,
+    Collection<ServiceConfigEntity> serviceConfigEntityList = serviceConfigDAO.getLastServiceConfigsForService(clusterId,
         "OOZIE");
     Assert.assertEquals(2, serviceConfigEntityList.size());
     configGroupDAO.remove(configGroupEntity1);
-    serviceConfigEntityList = serviceConfigDAO.getLastServiceConfigsForService(1L, "OOZIE");
+    serviceConfigEntityList = serviceConfigDAO.getLastServiceConfigsForService(clusterId, "OOZIE");
     Assert.assertEquals(1, serviceConfigEntityList.size());
   }
   private void initClusterEntities() throws Exception{
@@ -589,6 +605,7 @@ public class ServiceConfigDAOTest {
     String oozieSite = "oozie-site";
 
     for (int i = 1; i < 6; i++){
+      Thread.sleep(1);
       ClusterConfigEntity entity = new ClusterConfigEntity();
       entity.setClusterEntity(clusterEntity);
       entity.setClusterId(clusterEntity.getClusterId());
@@ -613,6 +630,7 @@ public class ServiceConfigDAOTest {
       clusterEntity.setConfigMappingEntities(entities);
     }
 
+    Thread.sleep(1);
     ClusterConfigMappingEntity e1 = new ClusterConfigMappingEntity();
     e1.setClusterEntity(clusterEntity);
     e1.setClusterId(clusterEntity.getClusterId());
@@ -624,6 +642,7 @@ public class ServiceConfigDAOTest {
     entities.add(e1);
     clusterDAO.merge(clusterEntity);
 
+    Thread.sleep(1);
     ClusterConfigMappingEntity e2 = new ClusterConfigMappingEntity();
     e2.setClusterEntity(clusterEntity);
     e2.setClusterId(clusterEntity.getClusterId());
@@ -635,6 +654,7 @@ public class ServiceConfigDAOTest {
     entities.add(e2);
     clusterDAO.merge(clusterEntity);
 
+    Thread.sleep(1);
     ClusterConfigMappingEntity e3 = new ClusterConfigMappingEntity();
     e3.setClusterEntity(clusterEntity);
     e3.setClusterId(clusterEntity.getClusterId());
@@ -666,6 +686,7 @@ public class ServiceConfigDAOTest {
 
     int count = 3;
     for (int i = 1; i < count; i++){
+      Thread.sleep(1);
       ClusterConfigEntity entity = new ClusterConfigEntity();
       entity.setClusterEntity(clusterEntity);
       entity.setClusterId(clusterEntity.getClusterId());
@@ -686,6 +707,7 @@ public class ServiceConfigDAOTest {
       clusterEntity.setConfigMappingEntities(entities);
     }
 
+    Thread.sleep(1);
     ClusterConfigMappingEntity e1 = new ClusterConfigMappingEntity();
     e1.setClusterEntity(clusterEntity);
     e1.setClusterId(clusterEntity.getClusterId());
@@ -697,6 +719,7 @@ public class ServiceConfigDAOTest {
     entities.add(e1);
     clusterDAO.merge(clusterEntity);
 
+    Thread.sleep(1);
     ClusterConfigMappingEntity e2 = new ClusterConfigMappingEntity();
     e2.setClusterEntity(clusterEntity);
     e2.setClusterId(clusterEntity.getClusterId());
@@ -749,6 +772,7 @@ public class ServiceConfigDAOTest {
         config.setClusterId(clusterEntity.getClusterId());
         clusterDAO.createConfig(config);
 
+        Thread.sleep(1);
         ConfigGroupConfigMappingEntity configMappingEntity = new
           ConfigGroupConfigMappingEntity();
         configMappingEntity.setClusterId(clusterEntity.getClusterId());
@@ -766,4 +790,4 @@ public class ServiceConfigDAOTest {
       configGroupDAO.merge(configGroupEntity);
     }
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/SettingDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/SettingDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/SettingDAOTest.java
index 4029bae..b96a7b2 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/SettingDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/SettingDAOTest.java
@@ -24,10 +24,12 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.entities.SettingEntity;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -46,6 +48,11 @@ public class SettingDAOTest {
     injector.getInstance(OrmTestHelper.class).createCluster();
   }
 
+  @After
+  public void teardown() throws Exception {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
+  }
+
   @Test
   public void testCRUD() {
     Map<String, SettingEntity> entities = new HashMap<>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/StageDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/StageDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/StageDAOTest.java
index 0215a0e..1f71af2 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/StageDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/StageDAOTest.java
@@ -21,11 +21,14 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.controller.internal.SortRequestImpl;
 import org.apache.ambari.server.controller.internal.StageResourceProvider;
 import org.apache.ambari.server.controller.spi.Predicate;
@@ -45,7 +48,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 /**
  * StageDAO tests.
@@ -90,8 +92,8 @@ public class StageDAOTest {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
     injector = null;
   }
 
@@ -174,4 +176,4 @@ public class StageDAOTest {
       lastInfo = currentInfo;
     }
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyLogicalRequestDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyLogicalRequestDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyLogicalRequestDAOTest.java
index 3e7ba68..fef76c8 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyLogicalRequestDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyLogicalRequestDAOTest.java
@@ -17,11 +17,13 @@
  */
 package org.apache.ambari.server.orm.dao;
 
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -39,7 +41,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -63,8 +64,8 @@ public class TopologyLogicalRequestDAOTest {
   }
 
   @After
-  public void teardown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   private void create() {

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyRequestDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyRequestDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyRequestDAOTest.java
index c85dceb..5f1490c 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyRequestDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/TopologyRequestDAOTest.java
@@ -17,11 +17,13 @@
  */
 package org.apache.ambari.server.orm.dao;
 
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -34,7 +36,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -54,8 +55,8 @@ public class TopologyRequestDAOTest {
   }
 
   @After
-  public void teardown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   private void create() {

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
index ae85241..3c5970d 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java
@@ -22,10 +22,13 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -46,7 +49,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 /**
  * Tests {@link AlertDefinitionDAO} for interacting with
@@ -68,6 +70,7 @@ public class UpgradeDAOTest {
   @Before
   public void setup() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    H2DatabaseCleaner.resetSequences(injector);
     injector.getInstance(GuiceJpaInitializer.class);
 
     dao = injector.getInstance(UpgradeDAO.class);
@@ -107,8 +110,8 @@ public class UpgradeDAOTest {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
     injector = null;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java
index 8be805a..1f03f8f 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java
@@ -18,9 +18,12 @@
 
 package org.apache.ambari.server.orm.dao;
 
+import java.sql.SQLException;
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -34,7 +37,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 /**
  * WidgetDAO unit tests.
@@ -121,8 +123,8 @@ public class WidgetDAOTest {
   }
 
   @After
-  public void after() {
-    injector.getInstance(PersistService.class).stop();
+  public void after() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
     injector = null;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java
index f50ae44..dc79b6f 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java
@@ -18,9 +18,12 @@
 
 package org.apache.ambari.server.orm.dao;
 
+import java.sql.SQLException;
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -34,7 +37,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 /**
  * WidgetLayoutDAO unit tests.
@@ -130,8 +132,8 @@ public class WidgetLayoutDAOTest {
   }
 
   @After
-  public void after() {
-    injector.getInstance(PersistService.class).stop();
+  public void after() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
     injector = null;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/scheduler/ExecutionScheduleManagerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/scheduler/ExecutionScheduleManagerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/scheduler/ExecutionScheduleManagerTest.java
index aa6ab1c..726972b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/scheduler/ExecutionScheduleManagerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/scheduler/ExecutionScheduleManagerTest.java
@@ -38,6 +38,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.actionmanager.ActionDBAccessor;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
@@ -82,7 +83,6 @@ import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.Module;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.Transactional;
 import com.google.inject.util.Modules;
 
@@ -131,7 +131,7 @@ public class ExecutionScheduleManagerTest {
   @AfterClass
   public static void teardown() throws Exception {
     executionScheduleManager.stop();
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   public static class TestExecutionScheduler extends ExecutionSchedulerImpl {

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/AmbariLdapUtilsTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/AmbariLdapUtilsTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/AmbariLdapUtilsTest.java
index 46f9689..1f2010d 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/AmbariLdapUtilsTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/AmbariLdapUtilsTest.java
@@ -29,7 +29,6 @@ import static org.easymock.EasyMock.verify;
 import javax.naming.Name;
 import javax.naming.NamingException;
 
-
 import org.apache.ambari.server.security.authorization.AmbariLdapUtils;
 import org.junit.Assert;
 import org.junit.Test;

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java
index 1e1c72f..18c4cce 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java
@@ -41,6 +41,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.crypto.codec.Base64;
 
 public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport {
@@ -63,13 +64,14 @@ public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport {
 
   @Test
   public void testDoFilter() throws IOException, ServletException {
+    SecurityContextHolder.getContext().setAuthentication(null);
     // GIVEN
     HttpServletRequest request = createMock(HttpServletRequest.class);
     HttpServletResponse response = createMock(HttpServletResponse.class);
     FilterChain filterChain = createMock(FilterChain.class);
     expect(request.getHeader("Authorization")).andReturn("Basic ").andReturn(null);
-    expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4");
-    expect(mockedAuditLogger.isEnabled()).andReturn(true);
+    expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4").anyTimes();
+    expect(mockedAuditLogger.isEnabled()).andReturn(true).anyTimes();
     mockedAuditLogger.log(anyObject(AuditEvent.class));
     expectLastCall().times(1);
     filterChain.doFilter(request, response);

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java
index c18eef5..819668a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.Properties;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.audit.AuditLoggerModule;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.ControllerModule;
@@ -45,7 +46,6 @@ import org.springframework.security.core.Authentication;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 @RunWith(FrameworkRunner.class)
 @CreateDS(allowAnonAccess = true,
@@ -93,7 +93,7 @@ public class AmbariLdapAuthenticationProviderForDNWithSpaceTest extends AmbariLd
 
   @After
   public void tearDown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test(expected = InvalidUsernamePasswordCombinationException.class)

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java
index 2a2e2a3..37c6cb3 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java
@@ -26,6 +26,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.audit.AuditLoggerModule;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -51,7 +52,6 @@ import org.springframework.security.core.Authentication;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -101,7 +101,7 @@ public class AmbariLdapAuthenticationProviderTest extends AmbariLdapAuthenticati
 
   @After
   public void tearDown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test(expected = InvalidUsernamePasswordCombinationException.class)

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java
index 4ab7a5a..9ff381f 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java
@@ -25,12 +25,14 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.audit.AuditLoggerModule;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.UserDAO;
 import org.apache.ambari.server.orm.entities.PrincipalEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
+import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -65,6 +67,11 @@ public class AmbariLocalUserProviderTest {
     SecurityContextHolder.getContext().setAuthentication(null);
   }
 
+  @AfterClass
+  public static void tearDown() throws Exception {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
+  }
+
   @Test
   public void testSuccessfulAuth() {
     Users users = createMock(Users.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariPamAuthenticationProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariPamAuthenticationProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariPamAuthenticationProviderTest.java
index 2a6c754..99d0055 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariPamAuthenticationProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariPamAuthenticationProviderTest.java
@@ -24,6 +24,7 @@ import static org.easymock.EasyMock.expect;
 import java.util.Arrays;
 import java.util.HashSet;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.audit.AuditLoggerModule;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -41,7 +42,6 @@ import org.springframework.security.core.AuthenticationException;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -65,7 +65,7 @@ public class AmbariPamAuthenticationProviderTest {
 
   @After
   public void tearDown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test(expected = AuthenticationException.class)

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/TestUsers.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/TestUsers.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/TestUsers.java
index e15c648..2ca5396 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/TestUsers.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/TestUsers.java
@@ -23,9 +23,11 @@ import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.sql.SQLException;
 import java.util.List;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.GroupDAO;
@@ -54,7 +56,6 @@ import org.springframework.security.crypto.password.PasswordEncoder;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -117,8 +118,8 @@ public class TestUsers {
   }
 
   @After
-  public void tearDown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void tearDown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
index f770f4c..02e67f5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
@@ -18,15 +18,18 @@
 
 package org.apache.ambari.server.security.ldap;
 
+import java.sql.SQLException;
 import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.security.ClientSecurityType;
 import org.apache.ambari.server.security.authorization.AuthorizationTestModule;
 import org.apache.ambari.server.security.authorization.Users;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -74,6 +77,11 @@ public class LdapPerformanceTest {
     configuration.setProperty(Configuration.LDAP_MANAGER_PASSWORD.getKey(), "hdfs");
   }
 
+  @After
+  public void tearDown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
+  }
+
   @Test
   public void testLdapSync() throws AmbariException, InterruptedException {
     long time = System.currentTimeMillis();

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/serveraction/ServerActionExecutorTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/ServerActionExecutorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/ServerActionExecutorTest.java
index c3ada2b..44d5b63 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/ServerActionExecutorTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/ServerActionExecutorTest.java
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -38,6 +39,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.actionmanager.ActionDBAccessor;
@@ -53,6 +56,7 @@ import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostServerActionEvent;
 import org.apache.ambari.server.utils.StageUtils;
 import org.easymock.IAnswer;
+import org.junit.After;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -83,6 +87,11 @@ public class ServerActionExecutorTest {
     injector = Guice.createInjector(new MockModule());
   }
 
+  @After
+  public void tearDown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
+  }
+
     /**
      * Test a normal server action
      */
@@ -339,4 +348,4 @@ public class ServerActionExecutorTest {
       bind(Clusters.class).toInstance(mock(Clusters.class));
     }
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java
index 140277e..863e4f5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java
@@ -227,4 +227,4 @@ public class KerberosServerActionTest {
 
     verify(kerberosHelper);
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/AutoSkipFailedSummaryActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/AutoSkipFailedSummaryActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/AutoSkipFailedSummaryActionTest.java
index 8dc4c1b..9f27257 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/AutoSkipFailedSummaryActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/AutoSkipFailedSummaryActionTest.java
@@ -32,6 +32,9 @@ import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
+import javax.persistence.EntityManager;
+
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
@@ -66,7 +69,6 @@ import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.UnitOfWork;
 import com.google.inject.util.Modules;
 
@@ -120,7 +122,7 @@ public class AutoSkipFailedSummaryActionTest {
   @After
   public void teardown() throws Exception {
     m_injector.getInstance(UnitOfWork.class).end();
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
   }
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
index 80a3bc5..ffbb6f7 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
@@ -26,7 +26,10 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.persistence.EntityManager;
+
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
@@ -69,7 +72,6 @@ import org.junit.Test;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.UnitOfWork;
 
 /**
@@ -126,7 +128,7 @@ public class ComponentVersionCheckActionTest {
   @After
   public void teardown() throws Exception {
     m_injector.getInstance(UnitOfWork.class).end();
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
   }
 
   private void makeUpgradeCluster(StackId sourceStack, String sourceRepo, StackId targetStack, String targetRepo) throws Exception {

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
index cd8327b..976dea4 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
@@ -28,7 +28,10 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.persistence.EntityManager;
+
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
 import org.apache.ambari.server.actionmanager.HostRoleCommand;
@@ -72,7 +75,6 @@ import com.google.gson.Gson;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 /**
  * Tests upgrade-related server side actions
@@ -118,7 +120,7 @@ public class ConfigureActionTest {
 
   @After
   public void teardown() throws Exception {
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
   }
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
index 72d0f13..42a4ac5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
@@ -29,7 +29,10 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.persistence.EntityManager;
+
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
@@ -94,7 +97,6 @@ import com.google.gson.JsonObject;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.UnitOfWork;
 
 /**
@@ -174,7 +176,7 @@ public class UpgradeActionTest {
   @After
   public void teardown() throws Exception {
     m_injector.getInstance(UnitOfWork.class).end();
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
   }
 
   private void makeDowngradeCluster(StackId sourceStack, String sourceRepo, StackId targetStack, String targetRepo) throws Exception {

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java b/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java
index 7a37224..7063147 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java
@@ -21,9 +21,11 @@ import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.sql.SQLException;
 import java.util.List;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.actionmanager.CommandExecutionType;
@@ -45,7 +47,6 @@ import org.junit.Test;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 public class TestStagePlanner {
 
@@ -68,8 +69,8 @@ public class TestStagePlanner {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java
index f55bf62..c70dcc6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java
@@ -17,11 +17,13 @@
  */
 package org.apache.ambari.server.state;
 
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.ConfigGroupDAO;
@@ -37,7 +39,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.Transactional;
 
 import junit.framework.Assert;
@@ -75,8 +76,8 @@ public class ConfigGroupTest {
   }
 
   @After
-  public void teardown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Transactional
@@ -97,7 +98,7 @@ public class ConfigGroupTest {
     Map<Long, Host> hosts = new HashMap<Long, Host>();
 
     configs.put(config.getType(), config);
-    hosts.put(1L, host);
+    hosts.put(host.getHostId(), host);
 
     ConfigGroup configGroup = configGroupFactory.createNew(cluster, "cg-test",
       "HDFS", "New HDFS configs for h1", configs, hosts);
@@ -213,13 +214,15 @@ public class ConfigGroupTest {
     configGroup = cluster.getConfigGroups().get(id);
     Assert.assertNotNull(configGroup);
 
+    long hostId = clusters.getHost("h1").getHostId();
+
     clusters.unmapHostFromCluster("h1", clusterName);
 
     Assert.assertNull(clusters.getHostsForCluster(clusterName).get("h1"));
     // Assumes that 1L is the id of host h1, as specified in createConfigGroup
-    Assert.assertNotNull(configGroupHostMappingDAO.findByHostId(1L));
-    Assert.assertTrue(configGroupHostMappingDAO.findByHostId(1L).isEmpty());
-    Assert.assertFalse(configGroup.getHosts().containsKey(1L));
+    Assert.assertNotNull(configGroupHostMappingDAO.findByHostId(hostId));
+    Assert.assertTrue(configGroupHostMappingDAO.findByHostId(hostId).isEmpty());
+    Assert.assertFalse(configGroup.getHosts().containsKey(hostId));
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java
index 1e65fc2..5d849c9 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java
@@ -23,6 +23,7 @@ import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -36,6 +37,7 @@ import java.util.Set;
 import javax.persistence.EntityManager;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.actionmanager.RequestFactory;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.AmbariCustomCommandExecutionHelper;
@@ -69,7 +71,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
 import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.Transactional;
 
 import junit.framework.Assert;
@@ -222,8 +223,8 @@ public class ConfigHelperTest {
     }
 
     @AfterClass
-    public static void tearDown() {
-      injector.getInstance(PersistService.class).stop();
+    public static void tearDown() throws AmbariException, SQLException {
+      H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
 
       // Clear the authenticated user
       SecurityContextHolder.getContext().setAuthentication(null);

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigMergeHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigMergeHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigMergeHelperTest.java
index 112d3c5..b821976 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigMergeHelperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigMergeHelperTest.java
@@ -26,11 +26,14 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -42,7 +45,6 @@ import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 /**
@@ -76,8 +78,8 @@ public class ConfigMergeHelperTest {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/RequestExecutionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/RequestExecutionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/RequestExecutionTest.java
index 31d3028..55aefb4 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/RequestExecutionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/RequestExecutionTest.java
@@ -21,6 +21,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.RequestScheduleResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -41,7 +42,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.Transactional;
 
 import junit.framework.Assert;
@@ -78,7 +78,7 @@ public class RequestExecutionTest {
 
   @After
   public void teardown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Transactional

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
index 0f615ee..e7572f4 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
@@ -22,11 +22,13 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceComponentResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -54,7 +56,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -107,8 +108,8 @@ public class ServiceComponentTest {
   }
 
   @After
-  public void teardown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
index ff6dfc9..7cb9a74 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
@@ -21,10 +21,12 @@ package org.apache.ambari.server.state;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -37,7 +39,6 @@ import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -70,8 +71,8 @@ public class ServiceTest {
   }
 
   @After
-  public void teardown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
index 7be9419..8e5ad0a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
+import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -38,6 +39,7 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.ClusterRequest;
@@ -86,7 +88,6 @@ import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
 import com.google.inject.assistedinject.FactoryModuleBuilder;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 /**
@@ -140,7 +141,9 @@ public class UpgradeHelperTest {
 
     // create an injector which will inject the mocks
     injector = Guice.createInjector(Modules.override(injectorModule).with(mockModule));
+    H2DatabaseCleaner.resetSequences(injector);
     injector.getInstance(GuiceJpaInitializer.class);
+
     EventBusSynchronizer.synchronizeAmbariEventPublisher(injector);
     EventBusSynchronizer.synchronizeAlertEventPublisher(injector);
 
@@ -158,8 +161,8 @@ public class UpgradeHelperTest {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
 
     // Clear the authenticated user
     SecurityContextHolder.getContext().setAuthentication(null);

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AggregateAlertListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AggregateAlertListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AggregateAlertListenerTest.java
index 4c49ab3..c4c97d1 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AggregateAlertListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AggregateAlertListenerTest.java
@@ -17,6 +17,9 @@
  */
 package org.apache.ambari.server.state.alerts;
 
+import javax.persistence.EntityManager;
+
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.events.AlertReceivedEvent;
 import org.apache.ambari.server.events.AlertStateChangeEvent;
 import org.apache.ambari.server.events.MockEventListener;
@@ -45,7 +48,6 @@ import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 import junit.framework.Assert;
@@ -84,7 +86,7 @@ public class AggregateAlertListenerTest {
    */
   @After
   public void teardown() throws Exception {
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
     m_injector = null;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java
index 015660c..e50dba5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java
@@ -19,6 +19,7 @@ package org.apache.ambari.server.state.alerts;
 
 import java.util.UUID;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.events.AlertDefinitionChangedEvent;
 import org.apache.ambari.server.events.AlertDefinitionDeleteEvent;
 import org.apache.ambari.server.events.AmbariEvent;
@@ -54,7 +55,6 @@ import org.junit.experimental.categories.Category;
 import com.google.gson.Gson;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 import junit.framework.Assert;
 
@@ -105,7 +105,7 @@ public class AlertEventPublisherTest {
    */
   @After
   public void teardown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
     injector = null;
   }
 
@@ -161,7 +161,7 @@ public class AlertEventPublisherTest {
     installHdfsService();
 
     int definitionCount = definitionDao.findAll().size();
-    AlertDefinitionEntity definition = ormHelper.createAlertDefinition(1L);
+    AlertDefinitionEntity definition = ormHelper.createAlertDefinition(cluster.getClusterId());
     Assert.assertEquals(definitionCount + 1, definitionDao.findAll().size());
 
     AggregateSource source = new AggregateSource();
@@ -175,7 +175,7 @@ public class AlertEventPublisherTest {
     source.setType(SourceType.AGGREGATE);
 
     AlertDefinitionEntity aggregateEntity = new AlertDefinitionEntity();
-    aggregateEntity.setClusterId(1L);
+    aggregateEntity.setClusterId(cluster.getClusterId());
     aggregateEntity.setComponentName("DATANODE");
     aggregateEntity.setEnabled(true);
     aggregateEntity.setDefinitionName("datanode_aggregate");
@@ -190,7 +190,7 @@ public class AlertEventPublisherTest {
     definitionDao.create(aggregateEntity);
 
     // pull it out of the mapping and compare fields
-    AlertDefinition aggregate = aggregateMapping.getAggregateDefinition(1L,
+    AlertDefinition aggregate = aggregateMapping.getAggregateDefinition(cluster.getClusterId(),
         source.getAlertName());
 
     Assert.assertNotNull(aggregate);
@@ -207,7 +207,7 @@ public class AlertEventPublisherTest {
     definitionDao.merge(aggregateEntity);
 
     // check the aggregate mapping for the new value
-    aggregate = aggregateMapping.getAggregateDefinition(1L,
+    aggregate = aggregateMapping.getAggregateDefinition(cluster.getClusterId(),
         source.getAlertName());
 
     Assert.assertNotNull(aggregate);
@@ -272,14 +272,14 @@ public class AlertEventPublisherTest {
   @Test
   public void testAlertDefinitionRemoval() throws Exception {
     Assert.assertEquals(0, definitionDao.findAll().size());
-    AlertDefinitionEntity definition = ormHelper.createAlertDefinition(1L);
+    AlertDefinitionEntity definition = ormHelper.createAlertDefinition(cluster.getClusterId());
     Assert.assertEquals(1, definitionDao.findAll().size());
 
     AggregateSource source = new AggregateSource();
     source.setAlertName(definition.getDefinitionName());
 
     AlertDefinition aggregate = new AlertDefinition();
-    aggregate.setClusterId(1L);
+    aggregate.setClusterId(cluster.getClusterId());
     aggregate.setComponentName("DATANODE");
     aggregate.setEnabled(true);
     aggregate.setInterval(1);
@@ -290,13 +290,13 @@ public class AlertEventPublisherTest {
     aggregate.setSource(source);
     aggregate.setUuid("uuid");
 
-    aggregateMapping.registerAggregate(1L, aggregate);
-    Assert.assertNotNull(aggregateMapping.getAggregateDefinition(1L,
+    aggregateMapping.registerAggregate(cluster.getClusterId(), aggregate);
+    Assert.assertNotNull(aggregateMapping.getAggregateDefinition(cluster.getClusterId(),
         source.getAlertName()));
 
     definitionDao.remove(definition);
 
-    Assert.assertNull(aggregateMapping.getAggregateDefinition(1L,
+    Assert.assertNull(aggregateMapping.getAggregateDefinition(cluster.getClusterId(),
         source.getAlertName()));
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
index ede94dc..68395b5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
@@ -19,6 +19,7 @@ package org.apache.ambari.server.state.alerts;
 
 import static org.junit.Assert.assertEquals;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -26,6 +27,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.persistence.EntityManager;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.controller.RootServiceResponseFactory.Components;
 import org.apache.ambari.server.controller.RootServiceResponseFactory.Services;
 import org.apache.ambari.server.events.AlertReceivedEvent;
@@ -60,7 +65,6 @@ import org.junit.experimental.categories.Category;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.UnitOfWork;
 
 /**
@@ -131,9 +135,9 @@ public class AlertReceivedListenerTest {
   }
 
   @After
-  public void teardown() {
+  public void teardown() throws AmbariException, SQLException {
     m_injector.getInstance(UnitOfWork.class).end();
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
     m_injector = null;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
index 5dd04d7..c21ed6b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertStateChangedEventTest.java
@@ -23,6 +23,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.controller.RootServiceResponseFactory.Services;
 import org.apache.ambari.server.events.AggregateAlertRecalculateEvent;
 import org.apache.ambari.server.events.AlertEvent;
@@ -58,7 +59,6 @@ import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 import junit.framework.Assert;
@@ -101,7 +101,7 @@ public class AlertStateChangedEventTest extends EasyMockSupport {
    */
   @After
   public void teardown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
     injector = null;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java
index 85d13a6..483aac5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java
@@ -19,6 +19,9 @@ package org.apache.ambari.server.state.alerts;
 
 import java.util.List;
 
+import javax.persistence.EntityManager;
+
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.events.AlertReceivedEvent;
 import org.apache.ambari.server.events.InitialAlertEvent;
 import org.apache.ambari.server.events.MockEventListener;
@@ -48,7 +51,6 @@ import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 import junit.framework.Assert;
@@ -112,7 +114,7 @@ public class InitialAlertEventTest {
    */
   @After
   public void teardown() throws Exception {
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
     m_injector = null;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
index 4f56d77..05aeddb 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
@@ -23,12 +23,17 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
+import java.sql.SQLException;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicReference;
 
+import javax.persistence.EntityManager;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.events.AlertEvent;
 import org.apache.ambari.server.events.AlertReceivedEvent;
 import org.apache.ambari.server.events.AlertStateChangeEvent;
@@ -75,7 +80,6 @@ import com.google.common.eventbus.Subscribe;
 import com.google.gson.Gson;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.UnitOfWork;
 
 import junit.framework.Assert;
@@ -146,9 +150,9 @@ public class AlertDataManagerTest {
   }
 
   @After
-  public void teardown() {
+  public void teardown() throws AmbariException, SQLException {
     m_injector.getInstance(UnitOfWork.class).end();
-    m_injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabase(m_injector.getProvider(EntityManager.class).get());
     m_injector = null;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java
index 9dc405e..76146b5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.state.cluster;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -27,6 +28,7 @@ import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.events.listeners.upgrade.HostVersionOutOfSyncListener;
@@ -60,7 +62,6 @@ import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.Module;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 /**
@@ -147,8 +148,8 @@ public class ClusterDeadlockTest {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
index 401527a..3172571 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
@@ -29,10 +29,13 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.sql.SQLException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.HostNotFoundException;
 import org.apache.ambari.server.controller.AmbariSessionManager;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
@@ -44,6 +47,7 @@ import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.StackId;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -59,11 +63,16 @@ public class ClusterImplTest {
 
   @BeforeClass
   public static void setUpClass() throws Exception {
-    Injector injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
     clusters = injector.getInstance(Clusters.class);
   }
 
+  @AfterClass
+  public static void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
+  }
+
   @Test
   public void testAddSessionAttributes() throws Exception {
     Map<String, Object> attributes = new HashMap<String, Object>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
index 44c5f29..6cdfbad 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.lang.reflect.Field;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -48,6 +49,7 @@ import javax.persistence.EntityManager;
 import javax.persistence.RollbackException;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.agent.AgentEnv;
 import org.apache.ambari.server.agent.AgentEnv.Directory;
 import org.apache.ambari.server.agent.DiskInfo;
@@ -123,7 +125,6 @@ import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Singleton;
-import com.google.inject.persist.PersistService;
 import com.google.inject.persist.Transactional;
 import com.google.inject.persist.UnitOfWork;
 import com.google.inject.util.Modules;
@@ -214,9 +215,9 @@ public class ClusterTest {
   }
 
   @After
-  public void teardown() {
+  public void teardown() throws AmbariException, SQLException {
     injector.getInstance(UnitOfWork.class).end();
-    injector.getInstance(PersistService.class).stop();
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   private void createDefaultCluster() throws Exception {
@@ -354,7 +355,7 @@ public class ClusterTest {
     clusters.addCluster(clusterName, stackId);
     Cluster cluster = clusters.getCluster(clusterName);
     Assert.assertEquals(clusterName, cluster.getClusterName());
-    Assert.assertEquals(1, cluster.getClusterId());
+    //Assert.assertEquals(1, cluster.getClusterId());
 
     // Add Hosts
     List<String> hostNames = new ArrayList<String>() {{ add("h-1"); add("h-2"); add("h-3"); }};
@@ -981,6 +982,7 @@ public class ClusterTest {
     res = c1.getDesiredConfigByType("core-site");
     Assert.assertNull("Expected null config", res);
 
+    Thread.sleep(1);
     c1.addDesiredConfig("_test", Collections.singleton(config2));
     res = c1.getDesiredConfigByType("global");
     Assert.assertEquals("Expected version tag to be 'version2'", "version2", res.getTag());
@@ -1009,6 +1011,7 @@ public class ClusterTest {
     }
 
     c1.addDesiredConfig("_test1", Collections.singleton(config1));
+    Thread.sleep(1);
     c1.addDesiredConfig("_test3", Collections.singleton(config3));
 
     Map<String, DesiredConfig> desiredConfigs = c1.getDesiredConfigs();
@@ -1023,9 +1026,11 @@ public class ClusterTest {
     Assert.assertTrue("Expect no host-level overrides",
       (null == dc.getHostOverrides() || dc.getHostOverrides().size() == 0));
 
+    Thread.sleep(1);
     c1.addDesiredConfig("_test2", Collections.singleton(config2));
     Assert.assertEquals("_test2", c1.getDesiredConfigs().get(config2.getType()).getUser());
 
+    Thread.sleep(1);
     c1.addDesiredConfig("_test1", Collections.singleton(config1));
 
     // setup a host that also has a config override
@@ -2165,7 +2170,6 @@ public class ClusterTest {
     clusters.addCluster(clusterName, stackId);
     Cluster c1 = clusters.getCluster(clusterName);
     Assert.assertEquals(clusterName, c1.getClusterName());
-    Assert.assertEquals(1, c1.getClusterId());
 
     clusters.addHost("h-1");
     clusters.addHost("h-2");
@@ -2234,7 +2238,6 @@ public class ClusterTest {
     clusters.addCluster(clusterName, stackId);
     final Cluster c1 = clusters.getCluster(clusterName);
     Assert.assertEquals(clusterName, c1.getClusterName());
-    Assert.assertEquals(1, c1.getClusterId());
 
     clusters.addHost("h-1");
     clusters.addHost("h-2");

http://git-wip-us.apache.org/repos/asf/ambari/blob/3a7906c8/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
index 27e6e13..2f289bb 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersDeadlockTest.java
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.state.cluster;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -26,6 +27,7 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.events.listeners.upgrade.HostVersionOutOfSyncListener;
@@ -55,7 +57,6 @@ import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.Module;
 import com.google.inject.Provider;
-import com.google.inject.persist.PersistService;
 import com.google.inject.util.Modules;
 
 import junit.framework.Assert;
@@ -120,8 +121,8 @@ public class ClustersDeadlockTest {
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException, SQLException {
+    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
   }
 
   /**


Mime
View raw message