Repository: ambari
Updated Branches:
  refs/heads/trunk 565685d32 -> 1123ba265


AMBARI-18384. Analyze and Optimize Ambari Server Unit Tests - Group 
3.(vbrodetskyi)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/1123ba26
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/1123ba26
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/1123ba26

Branch: refs/heads/trunk
Commit: 1123ba265592958f188eaf9b1eac843bcb4d3f1c
Parents: 565685d
Author: Vitaly Brodetskyi <vbrodets...@hortonworks.com>
Authored: Thu Sep 15 14:40:43 2016 +0300
Committer: Vitaly Brodetskyi <vbrodets...@hortonworks.com>
Committed: Thu Sep 15 14:40:43 2016 +0300

----------------------------------------------------------------------
 .../server/agent/TestHeartbeatMonitor.java      | 100 +++---
 .../security/authorization/TestUsers.java       | 321 ++++++++++---------
 .../upgrades/UpgradeActionTest.java             | 119 ++++---
 .../svccomphost/ServiceComponentHostTest.java   | 142 ++++----
 .../server/upgrade/UpgradeCatalog210Test.java   |  63 ++--
 5 files changed, 419 insertions(+), 326 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1123ba26/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
index 31e3a09..b6f5ec2 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
@@ -58,6 +58,7 @@ import 
org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpSucceede
 import 
org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartedEvent;
 import org.junit.After;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 import org.slf4j.Logger;
@@ -65,7 +66,6 @@ import org.slf4j.LoggerFactory;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 
 public class TestHeartbeatMonitor {
 
@@ -76,23 +76,42 @@ public class TestHeartbeatMonitor {
   private String clusterName = "cluster1";
   private String serviceName = "HDFS";
   private int heartbeatMonitorWakeupIntervalMS = 30;
-  private AmbariMetaInfo ambariMetaInfo;
-  private OrmTestHelper helper;
+  private static AmbariMetaInfo ambariMetaInfo;
+  private static OrmTestHelper helper;
 
   private static final Logger LOG =
           LoggerFactory.getLogger(TestHeartbeatMonitor.class);
 
-  @Before
-  public void setup() throws Exception {
+  @BeforeClass
+  public static void classSetUp() {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
     helper = injector.getInstance(OrmTestHelper.class);
     ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
   }
 
+  @Before
+  public void setup() throws Exception {
+    cleanup();
+  }
+
   @After
   public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+
+  }
+
+  private void cleanup() throws AmbariException {
+    Clusters clusters = injector.getInstance(Clusters.class);
+    Map<String, Cluster> clusterMap = clusters.getClusters();
+
+
+    for (String clusterName : clusterMap.keySet()) {
+      clusters.deleteCluster(clusterName);
+    }
+
+    for (Host host : clusters.getHosts()) {
+      clusters.deleteHost(host.getHostName());
+    }
   }
 
   private void setOsFamily(Host host, String osFamily, String osVersion) {
@@ -104,6 +123,41 @@ public class TestHeartbeatMonitor {
   }
 
   @Test
+  public void testHeartbeatLoss() throws AmbariException, InterruptedException,
+          InvalidStateTransitionException {
+    Clusters fsm = injector.getInstance(Clusters.class);
+    String hostname = "host1";
+    fsm.addHost(hostname);
+    ActionQueue aq = new ActionQueue();
+    ActionManager am = mock(ActionManager.class);
+    HeartbeatMonitor hm = new HeartbeatMonitor(fsm, aq, am, 10, injector);
+    HeartBeatHandler handler = new HeartBeatHandler(fsm, aq, am, injector);
+    Register reg = new Register();
+    reg.setHostname(hostname);
+    reg.setResponseId(12);
+    reg.setTimestamp(System.currentTimeMillis() - 300);
+    reg.setAgentVersion(ambariMetaInfo.getServerVersion());
+    HostInfo hi = new HostInfo();
+    hi.setOS("Centos5");
+    reg.setHardwareProfile(hi);
+    handler.handleRegistration(reg);
+    HeartBeat hb = new HeartBeat();
+    hb.setHostname(hostname);
+    hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, "cool"));
+    hb.setTimestamp(System.currentTimeMillis());
+    hb.setResponseId(12);
+    handler.handleHeartBeat(hb);
+    hm.start();
+    aq.enqueue(hostname, new ExecutionCommand());
+    //Heartbeat will expire and action queue will be flushed
+    while (aq.size(hostname) != 0) {
+      Thread.sleep(1);
+    }
+    assertEquals(fsm.getHost(hostname).getState(), HostState.HEARTBEAT_LOST);
+    classSetUp();
+  }
+
+  @Test
   public void testStateCommandsGeneration() throws AmbariException, 
InterruptedException,
           InvalidStateTransitionException {
     StackId stackId = new StackId("HDP-0.1");
@@ -401,40 +455,6 @@ public class TestHeartbeatMonitor {
   }
 
   @Test
-  public void testHeartbeatLoss() throws AmbariException, InterruptedException,
-          InvalidStateTransitionException {
-    Clusters fsm = injector.getInstance(Clusters.class);
-    String hostname = "host1";
-    fsm.addHost(hostname);
-    ActionQueue aq = new ActionQueue();
-    ActionManager am = mock(ActionManager.class);
-    HeartbeatMonitor hm = new HeartbeatMonitor(fsm, aq, am, 10, injector);
-    HeartBeatHandler handler = new HeartBeatHandler(fsm, aq, am, injector);
-    Register reg = new Register();
-    reg.setHostname(hostname);
-    reg.setResponseId(12);
-    reg.setTimestamp(System.currentTimeMillis() - 300);
-    reg.setAgentVersion(ambariMetaInfo.getServerVersion());
-    HostInfo hi = new HostInfo();
-    hi.setOS("Centos5");
-    reg.setHardwareProfile(hi);
-    handler.handleRegistration(reg);
-    HeartBeat hb = new HeartBeat();
-    hb.setHostname(hostname);
-    hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, "cool"));
-    hb.setTimestamp(System.currentTimeMillis());
-    hb.setResponseId(12);
-    handler.handleHeartBeat(hb);
-    hm.start();
-    aq.enqueue(hostname, new ExecutionCommand());
-    //Heartbeat will expire and action queue will be flushed
-    while (aq.size(hostname) != 0) {
-      Thread.sleep(1);
-    }
-    assertEquals(fsm.getHost(hostname).getState(), HostState.HEARTBEAT_LOST);
-  }
-
-  @Test
   public void testHeartbeatLossWithComponent() throws AmbariException, 
InterruptedException,
           InvalidStateTransitionException {
     StackId stackId = new StackId("HDP-0.1");

http://git-wip-us.apache.org/repos/asf/ambari/blob/1123ba26/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 4571c86..9b9fee5 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
@@ -17,6 +17,7 @@
  */
 package org.apache.ambari.server.security.authorization;
 
+import junit.framework.Assert;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
@@ -25,13 +26,10 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.List;
 
-import javax.persistence.EntityManager;
-
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.GroupDAO;
-import org.apache.ambari.server.orm.dao.MemberDAO;
 import org.apache.ambari.server.orm.dao.PermissionDAO;
 import org.apache.ambari.server.orm.dao.PrincipalDAO;
 import org.apache.ambari.server.orm.dao.PrincipalTypeDAO;
@@ -46,6 +44,7 @@ import 
org.apache.ambari.server.orm.entities.ResourceTypeEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
 import org.junit.After;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -55,78 +54,170 @@ import 
org.springframework.security.core.context.SecurityContextHolder;
 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.Provider;
-import com.google.inject.persist.PersistService;
-
-import junit.framework.Assert;
 
 public class TestUsers {
-  private Injector injector;
-
-  @Inject
-  protected Users users;
-  @Inject
-  protected UserDAO userDAO;
-  @Inject
-  protected GroupDAO groupDAO;
-  @Inject
-  protected MemberDAO memberDAO;
-  @Inject
-  protected PermissionDAO permissionDAO;
-  @Inject
-  protected ResourceDAO resourceDAO;
-  @Inject
-  protected ResourceTypeDAO resourceTypeDAO;
-  @Inject
-  protected PrincipalTypeDAO principalTypeDAO;
-  @Inject
-  protected PrincipalDAO principalDAO;
-  @Inject
-  protected PasswordEncoder passwordEncoder;
-  @Inject
-  Provider<EntityManager> entityManagerProvider;
+  private static Injector injector;
+
+
+  protected static Users users;
+  protected static UserDAO userDAO;
+  protected static GroupDAO groupDAO;
+  protected static PermissionDAO permissionDAO;
+  protected static ResourceDAO resourceDAO;
+  protected static ResourceTypeDAO resourceTypeDAO;
+  protected static PrincipalTypeDAO principalTypeDAO;
+  protected static PrincipalDAO principalDAO;
+  protected static PasswordEncoder passwordEncoder;
+
+
+  @BeforeClass
+  public static void classSetUp() {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    users = injector.getInstance(Users.class);
+    userDAO = injector.getInstance(UserDAO.class);
+    groupDAO = injector.getInstance(GroupDAO.class);
+    permissionDAO = injector.getInstance(PermissionDAO.class);
+    resourceDAO = injector.getInstance(ResourceDAO.class);
+    resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
+    principalTypeDAO = injector.getInstance(PrincipalTypeDAO.class);
+    principalDAO = injector.getInstance(PrincipalDAO.class);
+    passwordEncoder = injector.getInstance(PasswordEncoder.class);
+  }
 
   @Before
   public void setup() throws AmbariException {
-    InMemoryDefaultTestModule module = new InMemoryDefaultTestModule();
-    injector = Guice.createInjector(module);
-    injector.getInstance(GuiceJpaInitializer.class);
-    injector.injectMembers(this);
+    //injector.injectMembers(this);
     Authentication auth = new UsernamePasswordAuthenticationToken("admin", 
null);
     SecurityContextHolder.getContext().setAuthentication(auth);
 
-    // create admin permission
-    ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity();
-    resourceTypeEntity.setId(ResourceType.AMBARI.getId());
-    resourceTypeEntity.setName(ResourceType.AMBARI.name());
-    resourceTypeDAO.create(resourceTypeEntity);
+    if (resourceTypeDAO.findByName(ResourceType.AMBARI.name()) == null) {
+      // create admin permission
+      ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity();
+      resourceTypeEntity.setId(ResourceType.AMBARI.getId());
+      resourceTypeEntity.setName(ResourceType.AMBARI.name());
+      resourceTypeDAO.create(resourceTypeEntity);
+
+      ResourceEntity resourceEntity = new ResourceEntity();
+      resourceEntity.setId(ResourceEntity.AMBARI_RESOURCE_ID);
+      resourceEntity.setResourceType(resourceTypeEntity);
+      resourceDAO.create(resourceEntity);
+
+      PrincipalTypeEntity principalTypeEntity = new PrincipalTypeEntity();
+      principalTypeEntity.setName("ROLE");
+      principalTypeEntity = principalTypeDAO.merge(principalTypeEntity);
+
+      PrincipalEntity principalEntity = new PrincipalEntity();
+      principalEntity.setPrincipalType(principalTypeEntity);
+      principalEntity = principalDAO.merge(principalEntity);
+
+      PermissionEntity adminPermissionEntity = new PermissionEntity();
+      
adminPermissionEntity.setId(PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION);
+      
adminPermissionEntity.setPermissionName(PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION_NAME);
+      adminPermissionEntity.setPrincipal(principalEntity);
+      adminPermissionEntity.setResourceType(resourceTypeEntity);
+      permissionDAO.create(adminPermissionEntity);
+
+      users.createUser("admin", "admin", UserType.LOCAL, true, true);
+    }
+    cleanup();
+  }
 
-    ResourceEntity resourceEntity = new ResourceEntity();
-    resourceEntity.setId(ResourceEntity.AMBARI_RESOURCE_ID);
-    resourceEntity.setResourceType(resourceTypeEntity);
-    resourceDAO.create(resourceEntity);
+  @After
+  public void tearDown() throws AmbariException {
 
-    PrincipalTypeEntity principalTypeEntity = new PrincipalTypeEntity();
-    principalTypeEntity.setName("ROLE");
-    principalTypeEntity = principalTypeDAO.merge(principalTypeEntity);
+  }
 
-    PrincipalEntity principalEntity = new PrincipalEntity();
-    principalEntity.setPrincipalType(principalTypeEntity);
-    principalEntity = principalDAO.merge(principalEntity);
+  private void cleanup() throws AmbariException {
+    for (User user : users.getAllUsers()) {
+      if (!user.getUserName().equals("admin")) {
+        users.removeUser(user);
+      }
+    }
 
-    PermissionEntity adminPermissionEntity = new PermissionEntity();
-    
adminPermissionEntity.setId(PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION);
-    
adminPermissionEntity.setPermissionName(PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION_NAME);
-    adminPermissionEntity.setPrincipal(principalEntity);
-    adminPermissionEntity.setResourceType(resourceTypeEntity);
-    permissionDAO.create(adminPermissionEntity);
+    for (Group group : users.getAllGroups()) {
+      users.removeGroup(group);
+    }
   }
 
-  @After
-  public void tearDown() throws AmbariException {
-    injector.getInstance(PersistService.class).stop();
+  @Test
+  public void testIsUserCanBeRemoved() throws Exception {
+    //users.createUser("admin", "admin", UserType.LOCAL, true, true);
+    users.createUser("admin222", "admin222", UserType.LOCAL, true, true);
+
+    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin")));
+    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin222")));
+
+    users.removeUser(users.getAnyUser("admin222"));
+
+    
Assert.assertFalse(users.isUserCanBeRemoved(userDAO.findUserByName("admin")));
+    users.createUser("user", "user");
+    
Assert.assertFalse(users.isUserCanBeRemoved(userDAO.findUserByName("admin")));
+
+    users.createUser("admin333", "admin333", UserType.LOCAL, true, true);
+    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin")));
+    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin333")));
+  }
+
+  @Test
+  public void testGetUserIfUnique() throws Exception {
+    users.createUser("user333", "user333", UserType.LOCAL, true, false);
+
+    Assert.assertNotNull(users.getUserIfUnique("user333"));
+
+    users.createUser("user333", "user333", UserType.LDAP, true, false);
+
+    Assert.assertNull(users.getUserIfUnique("user333"));
+  }
+
+  @Test
+  public void testModifyPassword_UserByAdmin() throws Exception {
+    users.createUser("new_admin", "new_admin", UserType.LOCAL, true, true);
+    users.createUser("user", "user");
+
+    UserEntity userEntity = userDAO.findUserByName("user");
+
+    assertNotSame("user", userEntity.getUserPassword());
+    assertTrue(passwordEncoder.matches("user", userEntity.getUserPassword()));
+
+    users.modifyPassword("user", "admin", "user_new_password");
+    assertTrue(passwordEncoder.matches("user_new_password", 
userDAO.findUserByName("user").getUserPassword()));
+  }
+
+  @Test
+  public void testRevokeAdminPrivilege() throws Exception {
+    users.createUser("old_admin", "old_admin", UserType.LOCAL, true, true);
+
+    final User admin = users.getAnyUser("old_admin");
+    users.revokeAdminPrivilege(admin.getUserId());
+
+    Assert.assertFalse(users.getAnyUser("old_admin").isAdmin());
+  }
+
+  @Test
+  public void testGrantAdminPrivilege() throws Exception {
+    users.createUser("user", "user");
+
+    final User user = users.getAnyUser("user");
+    users.grantAdminPrivilege(user.getUserId());
+
+    Assert.assertTrue(users.getAnyUser("user").isAdmin());
+  }
+
+  @Test
+  public void testGetGroupMembers() throws Exception {
+    final String groupNameTwoMembers = "engineering";
+    final String groupNameZeroMembers = "management";
+    users.createGroup(groupNameTwoMembers);
+    users.createGroup(groupNameZeroMembers);
+    users.createUser("user1", "user1");
+    users.createUser("user2", "user2");
+    users.addMemberToGroup(groupNameTwoMembers, "user1");
+    users.addMemberToGroup(groupNameTwoMembers, "user2");
+
+    assertEquals(users.getGroupMembers(groupNameTwoMembers).size(), 2);
+    assertEquals(users.getGroupMembers(groupNameZeroMembers).size(), 0);
   }
 
   @Test
@@ -135,7 +226,6 @@ public class TestUsers {
     SecurityContextHolder.getContext().setAuthentication(auth);
 
     users.createUser("user", "user");
-    users.createUser("admin", "admin");
 
     List<User> userList = users.getAllUsers();
 
@@ -317,24 +407,10 @@ public class TestUsers {
     assertEquals(0, 
groupDAO.findGroupByName(groupName).getMemberEntities().size());
   }
 
-  @Test
-  public void testGetGroupMembers() throws Exception {
-    final String groupNameTwoMembers = "engineering";
-    final String groupNameZeroMembers = "management";
-    users.createGroup(groupNameTwoMembers);
-    users.createGroup(groupNameZeroMembers);
-    users.createUser("user", "user");
-    users.createUser("admin", "admin");
-    users.addMemberToGroup(groupNameTwoMembers, "user");
-    users.addMemberToGroup(groupNameTwoMembers, "admin");
-
-    assertEquals(users.getGroupMembers(groupNameTwoMembers).size(), 2);
-    assertEquals(users.getGroupMembers(groupNameZeroMembers).size(), 0);
-  }
 
   @Test
   public void testGetGroupMembersUnexistingGroup() throws Exception {
-    assertEquals(users.getGroupMembers("unexisting"), null);
+   assertEquals(users.getGroupMembers("unexisting"), null);
   }
 
   @Test
@@ -394,20 +470,6 @@ public class TestUsers {
   }
 
   @Test
-  public void testModifyPassword_UserByAdmin() throws Exception {
-    users.createUser("admin", "admin", UserType.LOCAL, true, true);
-    users.createUser("user", "user");
-
-    UserEntity userEntity = userDAO.findUserByName("user");
-
-    assertNotSame("user", userEntity.getUserPassword());
-    assertTrue(passwordEncoder.matches("user", userEntity.getUserPassword()));
-
-    users.modifyPassword("user", "admin", "user_new_password");
-    assertTrue(passwordEncoder.matches("user_new_password", 
userDAO.findUserByName("user").getUserPassword()));
-  }
-
-  @Test
   public void testCreateUserTwoParams() throws Exception {
     users.createUser("user", "user");
 
@@ -426,23 +488,6 @@ public class TestUsers {
     Mockito.verify(spy).createUser("user", "user", UserType.LOCAL, true, 
false);
   }
 
-  @Test
-  public void testCreateUserFiveParams() throws Exception {
-    users.createUser("user", "user", UserType.LOCAL, false, false);
-
-    final User createdUser = users.getAnyUser("user");
-    Assert.assertEquals("user", createdUser.getUserName());
-    Assert.assertEquals(false, createdUser.isActive());
-    Assert.assertEquals(false, createdUser.isLdapUser());
-    Assert.assertEquals(false, createdUser.isAdmin());
-
-    users.createUser("user2", "user2", UserType.LDAP, true, true);
-    final User createdUser2 = users.getAnyUser("user2");
-    Assert.assertEquals("user2", createdUser2.getUserName());
-    Assert.assertEquals(true, createdUser2.isActive());
-    Assert.assertEquals(true, createdUser2.isLdapUser());
-    Assert.assertEquals(true, createdUser2.isAdmin());
-  }
 
   @Test(expected = AmbariException.class)
   public void testCreateUserDuplicate() throws Exception {
@@ -461,62 +506,30 @@ public class TestUsers {
     users.createUser("user1", "user1");
     users.createUser("user2", "user2");
     users.createUser("user3", "user3");
-    Assert.assertEquals(3, users.getAllUsers().size());
+    Assert.assertEquals(4, users.getAllUsers().size());
 
     users.removeUser(users.getAnyUser("user1"));
 
     Assert.assertNull(users.getAnyUser("user1"));
-    Assert.assertEquals(2, users.getAllUsers().size());
-  }
-
-  @Test
-  public void testGrantAdminPrivilege() throws Exception {
-    users.createUser("user", "user");
-
-    final User user = users.getAnyUser("user");
-    users.grantAdminPrivilege(user.getUserId());
-
-    Assert.assertTrue(users.getAnyUser("user").isAdmin());
-  }
-
-  @Test
-  public void testRevokeAdminPrivilege() throws Exception {
-    users.createUser("admin", "admin", UserType.LOCAL, true, true);
-
-    final User admin = users.getAnyUser("admin");
-    users.revokeAdminPrivilege(admin.getUserId());
-
-    Assert.assertFalse(users.getAnyUser("admin").isAdmin());
-  }
-
-  @Test
-  public void testIsUserCanBeRemoved() throws Exception {
-    users.createUser("admin", "admin", UserType.LOCAL, true, true);
-    users.createUser("admin2", "admin2", UserType.LOCAL, true, true);
-
-    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin")));
-    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin2")));
-
-    users.removeUser(users.getAnyUser("admin"));
-
-    
Assert.assertFalse(users.isUserCanBeRemoved(userDAO.findUserByName("admin2")));
-    users.createUser("user", "user");
-    
Assert.assertFalse(users.isUserCanBeRemoved(userDAO.findUserByName("admin2")));
-
-    users.createUser("admin3", "admin3", UserType.LOCAL, true, true);
-    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin2")));
-    
Assert.assertTrue(users.isUserCanBeRemoved(userDAO.findUserByName("admin3")));
+    Assert.assertEquals(3, users.getAllUsers().size());
   }
 
   @Test
-  public void testGetUserIfUnique() throws Exception {
-    users.createUser("admin", "admin", UserType.LOCAL, true, false);
-
-    Assert.assertNotNull(users.getUserIfUnique("admin"));
+  public void testCreateUserFiveParams() throws Exception {
+    users.createUser("user", "user", UserType.LOCAL, false, false);
 
-    users.createUser("admin", "admin", UserType.LDAP, true, false);
+    final User createdUser = users.getAnyUser("user");
+    Assert.assertEquals("user", createdUser.getUserName());
+    Assert.assertEquals(false, createdUser.isActive());
+    Assert.assertEquals(false, createdUser.isLdapUser());
+    Assert.assertEquals(false, createdUser.isAdmin());
 
-    Assert.assertNull(users.getUserIfUnique("admin"));
+    users.createUser("user2", "user2", UserType.LDAP, true, true);
+    final User createdUser2 = users.getAnyUser("user2");
+    Assert.assertEquals("user2", createdUser2.getUserName());
+    Assert.assertEquals(true, createdUser2.isActive());
+    Assert.assertEquals(true, createdUser2.isLdapUser());
+    Assert.assertEquals(true, createdUser2.isAdmin());
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/1123ba26/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 8f4e445..cb63b0f 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
@@ -83,6 +83,8 @@ import 
org.apache.ambari.server.state.stack.upgrade.UpgradeType;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import com.google.gson.Gson;
@@ -112,54 +114,42 @@ public class UpgradeActionTest {
 
   private static final String HDP_211_CENTOS6_REPO_URL = 
"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos6/2.x/BUILDS/2.1.1.0-118";;
 
-  private Injector m_injector;
-
-  private AmbariManagementController amc;
-
-  @Inject
-  private OrmTestHelper m_helper;
-
-  @Inject
-  private RepositoryVersionDAO repoVersionDAO;
-
-  @Inject
-  private Clusters clusters;
-
-  @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
-  private HostVersionDAO hostVersionDAO;
-
-  @Inject
-  private HostDAO hostDAO;
-
-  @Inject
-  private HostRoleCommandFactory hostRoleCommandFactory;
-
-  @Inject
-  private ServiceFactory serviceFactory;
-
-  @Inject
-  private ServiceComponentFactory serviceComponentFactory;
-
-  @Inject
-  private ServiceComponentHostFactory serviceComponentHostFactory;
-
-  @Inject
-  private RequestDAO requestDAO;
-
-  @Inject
-  private UpgradeDAO upgradeDAO;
-
-  @Inject
-  private ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO;
-
-  @Before
-  public void setup() throws Exception {
+  private static Injector m_injector;
+  private static AmbariManagementController amc;
+  private static OrmTestHelper m_helper;
+  private static RepositoryVersionDAO repoVersionDAO;
+  private static Clusters clusters;
+  private static ClusterVersionDAO clusterVersionDAO;
+  private static HostVersionDAO hostVersionDAO;
+  private static HostDAO hostDAO;
+  private static HostRoleCommandFactory hostRoleCommandFactory;
+  private static ServiceFactory serviceFactory;
+  private static ServiceComponentFactory serviceComponentFactory;
+  private static ServiceComponentHostFactory serviceComponentHostFactory;
+  private static RequestDAO requestDAO;
+  private static UpgradeDAO upgradeDAO;
+  private static ServiceComponentDesiredStateDAO 
serviceComponentDesiredStateDAO;
+
+  @BeforeClass
+  public static void classSetUp() throws NoSuchFieldException, 
IllegalAccessException {
     m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
     m_injector.getInstance(GuiceJpaInitializer.class);
-    m_injector.injectMembers(this);
+
+    amc = m_injector.getInstance(AmbariManagementController.class);
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+    repoVersionDAO = m_injector.getInstance(RepositoryVersionDAO.class);
+    clusters = m_injector.getInstance(Clusters.class);
+    clusterVersionDAO = m_injector.getInstance(ClusterVersionDAO.class);
+    hostVersionDAO = m_injector.getInstance(HostVersionDAO.class);
+    hostDAO = m_injector.getInstance(HostDAO.class);
+    hostRoleCommandFactory = 
m_injector.getInstance(HostRoleCommandFactory.class);
+    serviceFactory = m_injector.getInstance(ServiceFactory.class);
+    serviceComponentFactory = 
m_injector.getInstance(ServiceComponentFactory.class);
+    serviceComponentHostFactory = 
m_injector.getInstance(ServiceComponentHostFactory.class);
+    requestDAO = m_injector.getInstance(RequestDAO.class);
+    upgradeDAO = m_injector.getInstance(UpgradeDAO.class);
+    serviceComponentDesiredStateDAO = 
m_injector.getInstance(ServiceComponentDesiredStateDAO.class);
+
     m_injector.getInstance(UnitOfWork.class).begin();
 
     // Initialize AmbariManagementController
@@ -172,8 +162,41 @@ public class UpgradeActionTest {
 
   @After
   public void teardown() throws Exception {
-    m_injector.getInstance(UnitOfWork.class).end();
-    m_injector.getInstance(PersistService.class).stop();
+    cleanup();
+  }
+
+  private void cleanup() throws AmbariException {
+    Map<String, Cluster> clusterMap = clusters.getClusters();
+
+    List<ClusterVersionEntity> clusterVersionEntities = 
clusterVersionDAO.findAll();
+    if (clusterVersionEntities != null) {
+      for (ClusterVersionEntity cve : clusterVersionEntities) {
+        clusterVersionDAO.remove(cve);
+      }
+    }
+
+    List<RepositoryVersionEntity> repositoryVersionEntities = 
repoVersionDAO.findAll();
+    if (repositoryVersionEntities != null) {
+      for (RepositoryVersionEntity rve : repositoryVersionEntities) {
+        repoVersionDAO.remove(rve);
+      }
+    }
+
+    List<HostVersionEntity> hostVersionEntities = hostVersionDAO.findAll();
+    if (clusterVersionEntities != null) {
+      for (HostVersionEntity hve : hostVersionEntities) {
+        hostVersionDAO.remove(hve);
+      }
+    }
+
+    for (String clusterName : clusterMap.keySet()) {
+      clusters.deleteCluster(clusterName);
+    }
+
+    for (Host host : clusters.getHosts()) {
+      clusters.deleteHost(host.getHostName());
+    }
+
   }
 
   private void makeDowngradeCluster(StackId sourceStack, String sourceRepo, 
StackId targetStack, String targetRepo) throws Exception {

http://git-wip-us.apache.org/repos/asf/ambari/blob/1123ba26/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
index 0383e4c..afcc90e 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
@@ -26,12 +26,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javax.persistence.EntityManager;
-
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceComponentHostResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -49,7 +46,6 @@ import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigFactory;
-import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.HostConfig;
 import org.apache.ambari.server.state.MaintenanceState;
@@ -71,74 +67,103 @@ import 
org.apache.ambari.server.state.fsm.InvalidStateTransitionException;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.inject.Guice;
-import com.google.inject.Inject;
 import com.google.inject.Injector;
-import com.google.inject.Provider;
 import com.google.inject.persist.PersistService;
 
 public class ServiceComponentHostTest {
   private static Logger LOG = 
LoggerFactory.getLogger(ServiceComponentHostTest.class);
-  @Inject
-  private Injector injector;
-  @Inject
-  private Clusters clusters;
-  @Inject
-  private ServiceFactory serviceFactory;
-  @Inject
-  private ServiceComponentFactory serviceComponentFactory;
-  @Inject
-  private ServiceComponentHostFactory serviceComponentHostFactory;
-  @Inject
-  private AmbariMetaInfo metaInfo;
-  @Inject
-  Provider<EntityManager> entityManagerProvider;
-  @Inject
-  private ConfigFactory configFactory;
-  @Inject
-  private ConfigGroupFactory configGroupFactory;
-  @Inject
-  private ConfigHelper configHelper;
-  @Inject
-  private OrmTestHelper helper;
-  @Inject
-  private ClusterDAO clusterDAO;
-  @Inject
-  private HostDAO hostDAO;
+
+  private static Injector injector;
+  private static Clusters clusters;
+  private static ServiceFactory serviceFactory;
+  private static ServiceComponentFactory serviceComponentFactory;
+  private static ServiceComponentHostFactory serviceComponentHostFactory;
+  private static ConfigFactory configFactory;
+  private static ConfigGroupFactory configGroupFactory;
+  private static OrmTestHelper helper;
+  private static ClusterDAO clusterDAO;
+  private static HostDAO hostDAO;
 
   private String clusterName = "c1";
   private String hostName1 = "h1";
   private Map<String, String> hostAttributes = new HashMap<String, String>();
 
+  @BeforeClass
+  public static void classSetUp() {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    clusters = injector.getInstance(Clusters.class);
+    serviceFactory = injector.getInstance(ServiceFactory.class);
+    serviceComponentFactory = 
injector.getInstance(ServiceComponentFactory.class);
+    serviceComponentHostFactory = 
injector.getInstance(ServiceComponentHostFactory.class);
+    configFactory = injector.getInstance(ConfigFactory.class);
+    configGroupFactory = injector.getInstance(ConfigGroupFactory.class);
+    helper = injector.getInstance(OrmTestHelper.class);
+    clusterDAO = injector.getInstance(ClusterDAO.class);
+    hostDAO = injector.getInstance(HostDAO.class);
+  }
 
   @Before
   public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-    injector.injectMembers(this);
 
-    StackId stackId = new StackId("HDP-0.1");
-    createCluster(stackId, clusterName);
-    hostAttributes.put("os_family", "redhat");
-    hostAttributes.put("os_release_version", "5.9");
 
-    Set<String> hostNames = new HashSet<String>();
-    hostNames.add(hostName1);
-    addHostsToCluster(clusterName, hostAttributes, hostNames);
+    if (clusters.getClusters().size() == 0) {
+      StackId stackId = new StackId("HDP-0.1");
+      createCluster(stackId, clusterName);
+      hostAttributes.put("os_family", "redhat");
+      hostAttributes.put("os_release_version", "5.9");
 
-    Cluster c1 = clusters.getCluster(clusterName);
-    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
-    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+      Set<String> hostNames = new HashSet<String>();
+      hostNames.add(hostName1);
+      addHostsToCluster(clusterName, hostAttributes, hostNames);
+
+      Cluster c1 = clusters.getCluster(clusterName);
+      helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+      c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+          RepositoryVersionState.INSTALLING);
+    }
   }
 
   @After
-  public void teardown() {
-    injector.getInstance(PersistService.class).stop();
+  public void teardown() throws AmbariException {
+    cleanup();
+  }
+
+  private void cleanup() throws AmbariException {
+    try {
+      Map<String, Cluster> clusterMap = clusters.getClusters();
+
+      HostComponentDesiredStateDAO hostComponentDesiredStateDAO = 
injector.getInstance(HostComponentDesiredStateDAO.class);
+      List<HostComponentDesiredStateEntity> hostComponentDesiredStateEntities 
= hostComponentDesiredStateDAO.findAll();
+      if (hostComponentDesiredStateEntities != null) {
+        for (HostComponentDesiredStateEntity hcdse : 
hostComponentDesiredStateEntities) {
+          hostComponentDesiredStateDAO.remove(hcdse);
+        }
+      }
+
+      HostComponentStateDAO hostComponentStateDAO = 
injector.getInstance(HostComponentStateDAO.class);
+      List<HostComponentStateEntity> hostComponentStateEntities = 
hostComponentStateDAO.findAll();
+      if (hostComponentStateEntities != null) {
+        for (HostComponentStateEntity hcse : hostComponentStateEntities) {
+          hostComponentStateDAO.remove(hcse);
+        }
+      }
+
+      for (String clusterName : clusterMap.keySet()) {
+        clusters.deleteCluster(clusterName);
+      }
+
+      for (Host host : clusters.getHosts()) {
+        clusters.deleteHost(host.getHostName());
+      }
+    } catch (IllegalStateException ise) {}
   }
 
   private ClusterEntity createCluster(StackId stackId, String clusterName) 
throws AmbariException {
@@ -516,18 +541,19 @@ public class ServiceComponentHostTest {
         State.UNINSTALLED);
 
     runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_WIPEOUT,
-        State.UNINSTALLED,
-        State.WIPING_OUT,
-        State.WIPING_OUT,
-        State.INIT);
+            State.UNINSTALLED,
+            State.WIPING_OUT,
+            State.WIPING_OUT,
+            State.INIT);
   }
 
+  @Ignore
   @Test
   public void testJobHandling() {
     // TODO fix once jobs are handled
   }
 
-
+  @Ignore
   @Test
   public void testGetAndSetConfigs() {
     // FIXME config handling
@@ -552,7 +578,7 @@ public class ServiceComponentHostTest {
     Assert.assertEquals("HDP-1.2.0",
         sch.getStackVersion().getStackId());
     Assert.assertEquals("HDP-1.2.0",
-        sch.getDesiredStackVersion().getStackId());
+            sch.getDesiredStackVersion().getStackId());
   }
 
   @Test
@@ -657,7 +683,7 @@ public class ServiceComponentHostTest {
     Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
     Assert.assertEquals(-1, impl.getLastOpEndTime());
     Assert.assertEquals(State.STOPPING,
-        impl.getState());
+            impl.getState());
   }
 
   @Test
@@ -929,6 +955,9 @@ public class ServiceComponentHostTest {
     tags.remove(id.toString());
     sch3.updateActualConfigs(actual);
     Assert.assertFalse(sch3.convertToResponse(null).isStaleConfig());
+
+    injector.getInstance(PersistService.class).stop();
+    classSetUp();
   }
 
   @Test
@@ -1046,6 +1075,9 @@ public class ServiceComponentHostTest {
     Assert.assertTrue(sch1.convertToResponse(null).isStaleConfig());
     Assert.assertTrue(sch2.convertToResponse(null).isStaleConfig());
     Assert.assertFalse(sch3.convertToResponse(null).isStaleConfig());
+
+    injector.getInstance(PersistService.class).stop();
+    classSetUp();
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/1123ba26/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
index 08374da..50ba3ef 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
@@ -18,6 +18,24 @@
 
 package org.apache.ambari.server.upgrade;
 
+import javax.persistence.EntityManager;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.createStrictMock;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
+
 import java.io.File;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -33,8 +51,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javax.persistence.EntityManager;
-
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
@@ -75,6 +91,7 @@ import org.easymock.EasyMockSupport;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.google.inject.AbstractModule;
@@ -83,40 +100,19 @@ import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
 import com.google.inject.Provider;
-import com.google.inject.persist.PersistService;
-
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertTrue;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
 
 /**
  * {@link org.apache.ambari.server.upgrade.UpgradeCatalog210} unit tests.
  */
 public class UpgradeCatalog210Test {
-  private Injector injector;
+  private static Injector injector;
   private Provider<EntityManager> entityManagerProvider = 
createStrictMock(Provider.class);
   private EntityManager entityManager = createNiceMock(EntityManager.class);
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-  private StackEntity desiredStackEntity;
+  private static UpgradeCatalogHelper upgradeCatalogHelper;
+  private static StackEntity desiredStackEntity;
 
-  @Before
-  public void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
+  @BeforeClass
+  public static void classSetUp() {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
 
@@ -128,9 +124,18 @@ public class UpgradeCatalog210Test {
     desiredStackEntity = stackDAO.find("HDP", "2.2.0");
   }
 
+  @Before
+  public void init() {
+    reset(entityManagerProvider);
+    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
+    replay(entityManagerProvider);
+
+
+  }
+
   @After
   public void tearDown() {
-    injector.getInstance(PersistService.class).stop();
+
   }
 
   @Test

Reply via email to