ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [40/46] git commit: AMBARI-6974 - Views : Support ApiAuthentication==false
Date Fri, 22 Aug 2014 12:48:38 GMT
AMBARI-6974 - Views : Support ApiAuthentication==false


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

Branch: refs/heads/branch-alerts-dev
Commit: 1fc24756668b2dbb467b5652b1291659958c763a
Parents: f18937a
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Thu Aug 21 14:02:36 2014 -0400
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Thu Aug 21 14:58:18 2014 -0400

----------------------------------------------------------------------
 .../ambari/server/controller/AmbariServer.java  |  16 +-
 .../apache/ambari/server/view/ViewRegistry.java | 172 ++++---------
 .../server/api/handlers/CreateHandlerTest.java  |   7 +
 .../server/api/handlers/DeleteHandlerTest.java  |   7 +
 .../server/api/handlers/UpdateHandlerTest.java  |   7 +
 .../resources/BaseResourceDefinitionTest.java   |   7 +
 .../AmbariPrivilegeResourceProviderTest.java    |  20 +-
 .../ViewPrivilegeResourceProviderTest.java      |  21 +-
 .../ambari/server/view/ViewRegistryTest.java    | 241 ++++++++-----------
 9 files changed, 198 insertions(+), 300 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
index 7b93836..e0049ec 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
@@ -63,16 +63,12 @@ import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.BlueprintDAO;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.GroupDAO;
-import org.apache.ambari.server.orm.dao.MemberDAO;
 import org.apache.ambari.server.orm.dao.MetainfoDAO;
 import org.apache.ambari.server.orm.dao.PermissionDAO;
 import org.apache.ambari.server.orm.dao.PrincipalDAO;
 import org.apache.ambari.server.orm.dao.PrivilegeDAO;
 import org.apache.ambari.server.orm.dao.ResourceDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.dao.UserDAO;
-import org.apache.ambari.server.orm.dao.ViewDAO;
-import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
 import org.apache.ambari.server.orm.entities.MetainfoEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.server.resources.ResourceManager;
@@ -80,7 +76,6 @@ import org.apache.ambari.server.resources.api.rest.GetResource;
 import org.apache.ambari.server.scheduler.ExecutionScheduleManager;
 import org.apache.ambari.server.security.CertificateManager;
 import org.apache.ambari.server.security.SecurityFilter;
-import org.apache.ambari.server.security.SecurityHelper;
 import org.apache.ambari.server.security.authorization.AmbariLdapAuthenticationProvider;
 import org.apache.ambari.server.security.authorization.AmbariLdapDataPopulator;
 import org.apache.ambari.server.security.authorization.AmbariLocalUserDetailsService;
@@ -149,6 +144,11 @@ public class AmbariServer {
   @Inject
   @Named("dbInitNeeded")
   boolean dbInitNeeded;
+  /**
+   * The singleton view registry.
+   */
+  @Inject
+  ViewRegistry viewRegistry;
 
   public String getServerOsType() {
     return configs.getServerOsType();
@@ -304,7 +304,6 @@ public class AmbariServer {
       FailsafeHandlerList handlerList = new FailsafeHandlerList();
 
       try {
-        ViewRegistry viewRegistry = ViewRegistry.getInstance();
         for (ViewInstanceEntity entity : viewRegistry.readViewArchives(configs)){
           handlerList.addFailsafeHandler(viewRegistry.getWebAppContext(entity));
         }
@@ -541,10 +540,6 @@ public class AmbariServer {
         injector.getInstance(PermissionDAO.class), injector.getInstance(ResourceDAO.class));
     ClusterPrivilegeResourceProvider.init(injector.getInstance(ClusterDAO.class));
     AmbariPrivilegeResourceProvider.init(injector.getInstance(ClusterDAO.class));
-    ViewRegistry.init(injector.getInstance(ViewDAO.class), injector.getInstance(ViewInstanceDAO.class),
-        injector.getInstance(UserDAO.class), injector.getInstance(MemberDAO.class),
-        injector.getInstance(PrivilegeDAO.class), injector.getInstance(SecurityHelper.class),
-        injector.getInstance(ResourceDAO.class), injector.getInstance(ResourceTypeDAO.class));
   }
 
   /**
@@ -586,6 +581,7 @@ public class AmbariServer {
       server = injector.getInstance(AmbariServer.class);
       CertificateManager certMan = injector.getInstance(CertificateManager.class);
       certMan.initRootCert();
+      ViewRegistry.initInstance(server.viewRegistry);
       ComponentSSLConfiguration.instance().init(server.configs);
       server.run();
     } catch (Throwable t) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
index d40eb0e..56a3168 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
@@ -40,6 +40,8 @@ import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
@@ -103,6 +105,7 @@ import com.google.inject.Injector;
 /**
  * Registry for view and view instance definitions.
  */
+@Singleton
 public class ViewRegistry {
 
   /**
@@ -144,7 +147,7 @@ public class ViewRegistry {
   /**
    * The singleton view registry instance.
    */
-  private static final ViewRegistry singleton = new ViewRegistry();
+  private static ViewRegistry singleton;
 
   /**
    * The logger.
@@ -154,50 +157,56 @@ public class ViewRegistry {
   /**
    * View data access object.
    */
-  private static ViewDAO viewDAO;
+  @Inject
+  ViewDAO viewDAO;
 
   /**
    * View instance data access object.
    */
-  private static ViewInstanceDAO instanceDAO;
+  @Inject
+  ViewInstanceDAO instanceDAO;
 
   /**
    * User data access object.
    */
-  private static UserDAO userDAO;
+  @Inject
+  UserDAO userDAO;
 
   /**
    * Group member data access object.
    */
-  private static MemberDAO memberDAO;
+  @Inject
+  MemberDAO memberDAO;
 
   /**
    * Privilege data access object.
    */
-  private static PrivilegeDAO privilegeDAO;
+  @Inject
+  PrivilegeDAO privilegeDAO;
 
   /**
    * Helper with security related utilities.
    */
-  private static SecurityHelper securityHelper;
+  @Inject
+  SecurityHelper securityHelper;
 
   /**
    * Resource data access object.
    */
-  private static ResourceDAO resourceDAO;
+  @Inject
+  ResourceDAO resourceDAO;
 
   /**
    * Resource type data access object.
    */
-  private static ResourceTypeDAO resourceTypeDAO;
-
-  // ----- Constructors ------------------------------------------------------
+  @Inject
+  ResourceTypeDAO resourceTypeDAO;
 
   /**
-   * Hide the constructor for this singleton.
+   * Ambari configuration.
    */
-  private ViewRegistry() {
-  }
+  @Inject
+  Configuration configuration;
 
 
   // ----- ViewRegistry ------------------------------------------------------
@@ -328,6 +337,15 @@ public class ViewRegistry {
   }
 
   /**
+   * Init the singleton instance.
+   *
+   * @param singleton  the view registry
+   */
+  public static void initInstance(ViewRegistry singleton) {
+    ViewRegistry.singleton = singleton;
+  }
+
+  /**
    * Get the view registry singleton.
    *
    * @return  the view registry
@@ -692,7 +710,8 @@ public class ViewRegistry {
 
     ResourceEntity resourceEntity = instanceEntity == null ? null : instanceEntity.getResource();
 
-    return (resourceEntity == null && readOnly) || checkAuthorization(resourceEntity);
+    return !configuration.getApiAuthentication() ||
+        (resourceEntity == null && readOnly) || checkAuthorization(resourceEntity);
   }
 
   /**
@@ -705,29 +724,16 @@ public class ViewRegistry {
    */
   public boolean includeDefinition(ViewEntity definitionEntity) {
 
-    ViewRegistry viewRegistry = ViewRegistry.getInstance();
-
-    for (GrantedAuthority grantedAuthority : securityHelper.getCurrentAuthorities()) {
-      if (grantedAuthority instanceof AmbariGrantedAuthority) {
-
-        AmbariGrantedAuthority authority = (AmbariGrantedAuthority) grantedAuthority;
-        PrivilegeEntity privilegeEntity = authority.getPrivilegeEntity();
-        Integer permissionId = privilegeEntity.getPermission().getId();
-
-        // admin has full access
-        if (permissionId.equals(PermissionEntity.AMBARI_ADMIN_PERMISSION)) {
-          return true;
-        }
-      }
+    if (checkPermission(null, false)) {
+      return true;
     }
 
-    boolean allowed = false;
-
     for (ViewInstanceEntity instanceEntity: definitionEntity.getInstances()) {
-      allowed |= viewRegistry.checkPermission(instanceEntity, true);
+      if (checkPermission(instanceEntity, true) ) {
+        return true;
+      }
     }
-
-    return allowed;
+    return false;
   }
 
 
@@ -1218,7 +1224,7 @@ public class ViewRegistry {
       if (grantedAuthority instanceof AmbariGrantedAuthority) {
 
         AmbariGrantedAuthority authority       = (AmbariGrantedAuthority) grantedAuthority;
-        PrivilegeEntity privilegeEntity = authority.getPrivilegeEntity();
+        PrivilegeEntity        privilegeEntity = authority.getPrivilegeEntity();
         Integer                permissionId    = privilegeEntity.getPermission().getId();
 
         // admin has full access
@@ -1239,102 +1245,6 @@ public class ViewRegistry {
     return false;
   }
 
-  /**
-   * Static initialization of DAO.
-   *
-   * @param viewDAO         view data access object
-   * @param instanceDAO     view instance data access object
-   * @param userDAO         user data access object
-   * @param memberDAO       group member data access object
-   * @param privilegeDAO    the privilege data access object
-   * @param securityHelper  the security helper
-   */
-  public static void init(ViewDAO viewDAO, ViewInstanceDAO instanceDAO,
-                          UserDAO userDAO, MemberDAO memberDAO, PrivilegeDAO privilegeDAO,
-                          SecurityHelper securityHelper, ResourceDAO resourceDAO,
-                          ResourceTypeDAO resourceTypeDAO) {
-    setViewDAO(viewDAO);
-    setInstanceDAO(instanceDAO);
-    setUserDAO(userDAO);
-    setMemberDAO(memberDAO);
-    setPrivilegeDAO(privilegeDAO);
-    setSecurityHelper(securityHelper);
-    setResourceDAO(resourceDAO);
-    setResourceTypeDAO(resourceTypeDAO);
-  }
-
-  /**
-   * Set the view DAO.
-   *
-   * @param viewDAO  the view DAO
-   */
-  protected static void setViewDAO(ViewDAO viewDAO) {
-    ViewRegistry.viewDAO = viewDAO;
-  }
-
-  /**
-   * Set the instance DAO.
-   *
-   * @param instanceDAO  the instance DAO
-   */
-  protected static void setInstanceDAO(ViewInstanceDAO instanceDAO) {
-    ViewRegistry.instanceDAO = instanceDAO;
-  }
-
-  /**
-   * Set the user DAO.
-   *
-   * @param userDAO  the user DAO
-   */
-  protected static void setUserDAO(UserDAO userDAO) {
-    ViewRegistry.userDAO = userDAO;
-  }
-
-  /**
-   * Set the group member DAO.
-   *
-   * @param memberDAO  the group member DAO
-   */
-  protected static void setMemberDAO(MemberDAO memberDAO) {
-    ViewRegistry.memberDAO = memberDAO;
-  }
-
-  /**
-   * Set the privilege DAO.
-   *
-   * @param privilegeDAO  the privilege DAO
-   */
-  protected static void setPrivilegeDAO(PrivilegeDAO privilegeDAO) {
-    ViewRegistry.privilegeDAO = privilegeDAO;
-  }
-
-  /**
-   * Set the security helper.
-   *
-   * @param securityHelper  the security helper
-   */
-  protected static void setSecurityHelper(SecurityHelper securityHelper) {
-    ViewRegistry.securityHelper = securityHelper;
-  }
-
-  /**
-   * Set the resource DAO.
-   *
-   * @param resourceDAO the resource DAO
-   */
-  protected static void setResourceDAO(ResourceDAO resourceDAO) {
-    ViewRegistry.resourceDAO = resourceDAO;
-  }
-
-  /**
-   * Set the resource type DAO.
-   *
-   * @param resourceTypeDAO the resource type DAO.
-   */
-  protected static void setResourceTypeDAO(ResourceTypeDAO resourceTypeDAO) {
-    ViewRegistry.resourceTypeDAO = resourceTypeDAO;
-  }
-
 
   // ----- inner class : ViewRegistryHelper ----------------------------------
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java
index c2562bd..87c07c8 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java
@@ -28,6 +28,8 @@ import org.apache.ambari.server.api.services.persistence.PersistenceManager;
 import org.apache.ambari.server.api.util.TreeNode;
 import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.view.ViewRegistry;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.util.*;
@@ -40,6 +42,11 @@ import static org.junit.Assert.*;
  */
 public class CreateHandlerTest {
 
+  @Before
+  public void before() {
+    ViewRegistry.initInstance(new ViewRegistry());
+  }
+
   @Test
   public void testHandleRequest__Synchronous_NoPropsInBody() throws Exception {
     Request request = createNiceMock(Request.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java
index 3876058..33bae13 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java
@@ -28,6 +28,8 @@ import org.apache.ambari.server.api.util.TreeNode;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.view.ViewRegistry;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.util.*;
@@ -41,6 +43,11 @@ import static org.junit.Assert.assertEquals;
  */
 public class DeleteHandlerTest {
 
+  @Before
+  public void before() {
+    ViewRegistry.initInstance(new ViewRegistry());
+  }
+
   @Test
   public void testHandleRequest__Synchronous_NoPropsInBody() throws Exception {
     Request request = createMock(Request.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java
index 4140ce2..5ef3e53 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java
@@ -28,6 +28,8 @@ import org.apache.ambari.server.api.util.TreeNode;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.view.ViewRegistry;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.util.*;
@@ -40,6 +42,11 @@ import static org.junit.Assert.*;
  */
 public class UpdateHandlerTest {
 
+  @Before
+  public void before() {
+    ViewRegistry.initInstance(new ViewRegistry());
+  }
+
   @Test
   public void testHandleRequest__Synchronous_NoPropsInBody() throws Exception {
     Request request = createMock(Request.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/api/resources/BaseResourceDefinitionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/resources/BaseResourceDefinitionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/resources/BaseResourceDefinitionTest.java
index aafe8ec..73aa828 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/resources/BaseResourceDefinitionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/resources/BaseResourceDefinitionTest.java
@@ -42,7 +42,9 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.view.ViewRegistry;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.util.List;
@@ -54,6 +56,11 @@ import java.util.Set;
  */
 public class BaseResourceDefinitionTest {
 
+  @Before
+  public void before() {
+    ViewRegistry.initInstance(new ViewRegistry());
+  }
+
   @Test
   public void testGetPostProcessors() throws AmbariException {
     BaseResourceDefinition resourceDefinition = getResourceDefinition();

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AmbariPrivilegeResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AmbariPrivilegeResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AmbariPrivilegeResourceProviderTest.java
index 46e864e..243c98c 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AmbariPrivilegeResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AmbariPrivilegeResourceProviderTest.java
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.controller.internal;
 
 import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.createStrictMock;
 import static org.easymock.EasyMock.expect;
@@ -35,17 +36,20 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.ambari.server.controller.ivory.Cluster;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 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.PrivilegeDAO;
 import org.apache.ambari.server.orm.dao.ResourceDAO;
+import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.dao.UserDAO;
+import org.apache.ambari.server.orm.dao.ViewDAO;
+import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.GroupEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
@@ -57,7 +61,9 @@ import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
 import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
+import org.apache.ambari.server.security.SecurityHelper;
 import org.apache.ambari.server.view.ViewRegistry;
+import org.apache.ambari.server.view.ViewRegistryTest;
 import org.easymock.EasyMock;
 import org.junit.Assert;
 import org.junit.Before;
@@ -75,6 +81,11 @@ public class AmbariPrivilegeResourceProviderTest {
   private final static PrincipalDAO principalDAO = createStrictMock(PrincipalDAO.class);
   private final static PermissionDAO permissionDAO = createStrictMock(PermissionDAO.class);
   private final static ResourceDAO resourceDAO = createStrictMock(ResourceDAO.class);
+  private static final ViewDAO viewDAO = createMock(ViewDAO.class);
+  private static final ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
+  private static final MemberDAO memberDAO = createNiceMock(MemberDAO.class);
+  private static final ResourceTypeDAO resourceTypeDAO = createNiceMock(ResourceTypeDAO.class);
+  private static final SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
 
   @BeforeClass
   public static void initClass() {
@@ -84,6 +95,8 @@ public class AmbariPrivilegeResourceProviderTest {
 
   @Before
   public void resetGlobalMocks() {
+    ViewRegistry.initInstance(ViewRegistryTest.getRegistry(viewDAO, viewInstanceDAO, userDAO,
+        memberDAO, privilegeDAO, resourceDAO, resourceTypeDAO, securityHelper));
     reset(privilegeDAO, userDAO, groupDAO, principalDAO, permissionDAO, resourceDAO, clusterDAO);
   }
 
@@ -220,11 +233,6 @@ public class AmbariPrivilegeResourceProviderTest {
     expect(clusterEntity.getResource()).andReturn(clusterResourceEntity).anyTimes();
     expect(clusterEntity.getClusterName()).andReturn("cluster1").anyTimes();
 
-    List<PrincipalEntity> principalEntities = new LinkedList<PrincipalEntity>();
-    principalEntities.add(ambariPrincipalEntity);
-    principalEntities.add(viewPrincipalEntity);
-    principalEntities.add(clusterPrincipalEntity);
-
     List<UserEntity> userEntities = new LinkedList<UserEntity>();
     userEntities.add(ambariUserEntity);
     userEntities.add(viewUserEntity);

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewPrivilegeResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewPrivilegeResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewPrivilegeResourceProviderTest.java
index 695ceea..a092735 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewPrivilegeResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewPrivilegeResourceProviderTest.java
@@ -21,11 +21,15 @@ package org.apache.ambari.server.controller.internal;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 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.PrivilegeDAO;
 import org.apache.ambari.server.orm.dao.ResourceDAO;
+import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.dao.UserDAO;
+import org.apache.ambari.server.orm.dao.ViewDAO;
+import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
 import org.apache.ambari.server.orm.entities.GroupEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
 import org.apache.ambari.server.orm.entities.PrincipalEntity;
@@ -37,9 +41,9 @@ import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.orm.entities.ViewEntityTest;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntityTest;
+import org.apache.ambari.server.security.SecurityHelper;
 import org.apache.ambari.server.view.ViewRegistry;
 import org.apache.ambari.server.view.ViewRegistryTest;
-import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -50,6 +54,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 
+import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.createStrictMock;
 import static org.easymock.EasyMock.expect;
@@ -67,6 +72,11 @@ public class ViewPrivilegeResourceProviderTest {
   private final static PrincipalDAO principalDAO = createStrictMock(PrincipalDAO.class);
   private final static PermissionDAO permissionDAO = createStrictMock(PermissionDAO.class);
   private final static ResourceDAO resourceDAO = createStrictMock(ResourceDAO.class);
+  private static final ViewDAO viewDAO = createMock(ViewDAO.class);
+  private static final ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
+  private static final MemberDAO memberDAO = createNiceMock(MemberDAO.class);
+  private static final ResourceTypeDAO resourceTypeDAO = createNiceMock(ResourceTypeDAO.class);
+  private static final SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
 
   @BeforeClass
   public static void initClass() {
@@ -75,13 +85,10 @@ public class ViewPrivilegeResourceProviderTest {
 
   @Before
   public void resetGlobalMocks() {
-    ViewRegistryTest.clear();
-    reset(privilegeDAO, userDAO, groupDAO, principalDAO, permissionDAO, resourceDAO);
-  }
 
-  @AfterClass
-  public static void afterClass() {
-    ViewRegistryTest.clear();
+    ViewRegistry.initInstance(ViewRegistryTest.getRegistry(viewDAO, viewInstanceDAO, userDAO,
+        memberDAO, privilegeDAO, resourceDAO, resourceTypeDAO, securityHelper));
+    reset(privilegeDAO, userDAO, groupDAO, principalDAO, permissionDAO, resourceDAO);
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/1fc24756/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
index 277c739..ed3a6a9 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
@@ -18,11 +18,11 @@
 
 package org.apache.ambari.server.view;
 
-import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
 import static org.easymock.EasyMock.verify;
 
 import java.io.File;
@@ -78,11 +78,8 @@ import org.apache.ambari.server.view.events.EventImpl;
 import org.apache.ambari.server.view.events.EventImplTest;
 import org.apache.ambari.view.events.Event;
 import org.apache.ambari.view.events.Listener;
-import org.easymock.Capture;
 import org.easymock.EasyMock;
-import org.junit.AfterClass;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 import org.springframework.security.core.GrantedAuthority;
 
@@ -151,8 +148,21 @@ public class ViewRegistryTest {
       "    </instance>\n" +
       "</view>";
 
+  // registry mocks
+  private static final ViewDAO viewDAO = createMock(ViewDAO.class);
+  private static final ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
+  private static final UserDAO userDAO = createNiceMock(UserDAO.class);
+  private static final MemberDAO memberDAO = createNiceMock(MemberDAO.class);
+  private static final PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
+  private static final ResourceDAO resourceDAO = createNiceMock(ResourceDAO.class);
+  private static final ResourceTypeDAO resourceTypeDAO = createNiceMock(ResourceTypeDAO.class);
+  private static final SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
+  private static final Configuration configuration = createNiceMock(Configuration.class);
+
   @Test
   public void testReadViewArchives() throws Exception {
+    ViewRegistry registry = getRegistry();
+
     Configuration configuration = createNiceMock(Configuration.class);
     File viewDir = createNiceMock(File.class);
     File extractedArchiveDir = createNiceMock(File.class);
@@ -173,14 +183,6 @@ public class ViewRegistryTest {
     resourceTypeEntity.setId(10);
     resourceTypeEntity.setName("MY_VIEW{1.0.0}");
 
-    ViewDAO vDAO = createMock(ViewDAO.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ViewInstanceDAO viDAO = createNiceMock(ViewInstanceDAO.class);
-
-    ViewRegistry.setViewDAO(vDAO);
-    ViewRegistry.setResourceDAO(rDAO);
-    ViewRegistry.setInstanceDAO(viDAO);
-
     ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
     viewDefinition.setResourceType(resourceTypeEntity);
 
@@ -257,15 +259,14 @@ public class ViewRegistryTest {
     expect(libDir.listFiles()).andReturn(new File[]{fileEntry});
     expect(fileEntry.toURI()).andReturn(new URI("file:./"));
 
-    expect(vDAO.findByName("MY_VIEW{1.0.0}")).andReturn(viewDefinition);
+    expect(viewDAO.findByName("MY_VIEW{1.0.0}")).andReturn(viewDefinition);
 
-    expect(vDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList());
+    expect(viewDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList());
 
     // replay mocks
     replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, rDAO, vDAO, viDAO);
+        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, resourceDAO, viewDAO, viewInstanceDAO);
 
-    ViewRegistry registry = ViewRegistry.getInstance();
     registry.setHelper(new TestViewRegistryHelper(viewConfigs, files, outputStreams, jarFiles));
 
     Set<ViewInstanceEntity> instanceEntities = registry.readViewArchives(configuration);
@@ -274,11 +275,13 @@ public class ViewRegistryTest {
 
     // verify mocks
     verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, rDAO, vDAO, viDAO);
+        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, resourceDAO, viewDAO, viewInstanceDAO);
   }
 
   @Test
   public void testReadViewArchives_exception() throws Exception {
+    ViewRegistry registry = getRegistry();
+
     Configuration configuration = createNiceMock(Configuration.class);
     File viewDir = createNiceMock(File.class);
     File extractedArchiveDir = createNiceMock(File.class);
@@ -299,10 +302,6 @@ public class ViewRegistryTest {
     resourceTypeEntity.setId(10);
     resourceTypeEntity.setName("MY_VIEW{1.0.0}");
 
-    ViewDAO vDAO = createMock(ViewDAO.class);
-
-    ViewRegistry.setViewDAO(vDAO);
-
     ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
     viewDefinition.setResourceType(resourceTypeEntity);
 
@@ -379,14 +378,13 @@ public class ViewRegistryTest {
     expect(libDir.listFiles()).andReturn(new File[]{fileEntry});
     expect(fileEntry.toURI()).andReturn(new URI("file:./"));
 
-    expect(vDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList());
-    expect(vDAO.findByName("MY_VIEW{1.0.0}")).andThrow(new IllegalArgumentException("Expected exception."));
+    expect(viewDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList());
+    expect(viewDAO.findByName("MY_VIEW{1.0.0}")).andThrow(new IllegalArgumentException("Expected exception."));
 
     // replay mocks
     replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, vDAO);
+        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, viewDAO);
 
-    ViewRegistry registry = ViewRegistry.getInstance();
     registry.setHelper(new TestViewRegistryHelper(viewConfigs, files, outputStreams, jarFiles));
 
     Set<ViewInstanceEntity> instanceEntities = registry.readViewArchives(configuration);
@@ -395,12 +393,12 @@ public class ViewRegistryTest {
 
     // verify mocks
     verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, vDAO);
+        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, viewDAO);
   }
 
   @Test
   public void testListener() throws Exception {
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     TestListener listener = new TestListener();
     registry.registerListener(listener, "MY_VIEW", "1.0.0");
@@ -424,7 +422,7 @@ public class ViewRegistryTest {
   public void testAddGetDefinitions() throws Exception {
     ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
 
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     registry.addDefinition(viewDefinition);
 
@@ -442,7 +440,7 @@ public class ViewRegistryTest {
     ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
     ViewInstanceEntity viewInstanceDefinition = ViewInstanceEntityTest.getViewInstanceEntity();
 
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     registry.addDefinition(viewDefinition);
 
@@ -460,7 +458,7 @@ public class ViewRegistryTest {
   @Test
   public void testGetSubResourceDefinitions() throws Exception {
     ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     ResourceConfig config = ResourceConfigTest.getResourceConfigs().get(0);
     Resource.Type type1 = new Resource.Type("myType");
@@ -478,7 +476,7 @@ public class ViewRegistryTest {
 
   @Test
   public void testAddInstanceDefinition() throws Exception {
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     ViewEntity viewEntity = ViewEntityTest.getViewEntity();
     InstanceConfig instanceConfig = InstanceConfigTest.getInstanceConfigs().get(0);
@@ -509,18 +507,7 @@ public class ViewRegistryTest {
   @Test
   public void testInstallViewInstance() throws Exception {
 
-    ViewDAO viewDAO = createNiceMock(ViewDAO.class);
-    ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
-    UserDAO userDAO = createNiceMock(UserDAO.class);
-    MemberDAO memberDAO = createNiceMock(MemberDAO.class);
-    PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ResourceTypeDAO rtDAO = createNiceMock(ResourceTypeDAO.class);
-
-    ViewRegistry.init(viewDAO, viewInstanceDAO, userDAO, memberDAO, privilegeDAO, securityHelper, rDAO, rtDAO);
-
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     Properties properties = new Properties();
     properties.put("p1", "v1");
@@ -551,18 +538,7 @@ public class ViewRegistryTest {
   @Test
   public void testInstallViewInstance_invalid() throws Exception {
 
-    ViewDAO viewDAO = createNiceMock(ViewDAO.class);
-    ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
-    UserDAO userDAO = createNiceMock(UserDAO.class);
-    MemberDAO memberDAO = createNiceMock(MemberDAO.class);
-    PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ResourceTypeDAO rtDAO = createNiceMock(ResourceTypeDAO.class);
-
-    ViewRegistry.init(viewDAO, viewInstanceDAO, userDAO, memberDAO, privilegeDAO, securityHelper, rDAO, rtDAO);
-
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     Properties properties = new Properties();
     properties.put("p1", "v1");
@@ -573,7 +549,7 @@ public class ViewRegistryTest {
     ViewEntity viewEntity = getViewEntity(config, ambariConfig, getClass().getClassLoader(), "");
     ViewInstanceEntity viewInstanceEntity = getViewInstanceEntity(viewEntity, config.getInstances().get(0));
 
-    replay(viewDAO, viewInstanceDAO, securityHelper, rtDAO);
+    replay(viewDAO, viewInstanceDAO, securityHelper, resourceTypeDAO);
 
     registry.addDefinition(viewEntity);
     try {
@@ -582,24 +558,13 @@ public class ViewRegistryTest {
     } catch (IllegalStateException e) {
       // expected
     }
-    verify(viewDAO, viewInstanceDAO, securityHelper);
+    verify(viewDAO, viewInstanceDAO, securityHelper, resourceTypeDAO);
   }
 
   @Test
   public void testInstallViewInstance_unknownView() throws Exception {
 
-    ViewDAO viewDAO = createNiceMock(ViewDAO.class);
-    ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
-    UserDAO userDAO = createNiceMock(UserDAO.class);
-    MemberDAO memberDAO = createNiceMock(MemberDAO.class);
-    PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ResourceTypeDAO rtDAO = createNiceMock(ResourceTypeDAO.class);
-
-    ViewRegistry.init(viewDAO, viewInstanceDAO, userDAO, memberDAO, privilegeDAO, securityHelper, rDAO, rtDAO);
-
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     Properties properties = new Properties();
     properties.put("p1", "v1");
@@ -611,7 +576,7 @@ public class ViewRegistryTest {
     ViewInstanceEntity viewInstanceEntity = getViewInstanceEntity(viewEntity, config.getInstances().get(0));
     viewInstanceEntity.setViewName("BOGUS_VIEW");
 
-    replay(viewDAO, viewInstanceDAO, securityHelper, rtDAO);
+    replay(viewDAO, viewInstanceDAO, securityHelper, resourceTypeDAO);
 
     registry.addDefinition(viewEntity);
     try {
@@ -620,24 +585,13 @@ public class ViewRegistryTest {
     } catch (IllegalArgumentException e) {
       // expected
     }
-    verify(viewDAO, viewInstanceDAO, securityHelper);
+    verify(viewDAO, viewInstanceDAO, securityHelper, resourceTypeDAO);
   }
 
   @Test
   public void testUpdateViewInstance() throws Exception {
 
-    ViewDAO viewDAO = createNiceMock(ViewDAO.class);
-    ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
-    UserDAO userDAO = createNiceMock(UserDAO.class);
-    MemberDAO memberDAO = createNiceMock(MemberDAO.class);
-    PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ResourceTypeDAO rtDAO = createNiceMock(ResourceTypeDAO.class);
-
-    ViewRegistry.init(viewDAO, viewInstanceDAO, userDAO, memberDAO, privilegeDAO, securityHelper, rDAO, rtDAO);
-
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     Properties properties = new Properties();
     properties.put("p1", "v1");
@@ -671,18 +625,7 @@ public class ViewRegistryTest {
   @Test
   public void testUpdateViewInstance_invalid() throws Exception {
 
-    ViewDAO viewDAO = createNiceMock(ViewDAO.class);
-    ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
-    UserDAO userDAO = createNiceMock(UserDAO.class);
-    MemberDAO memberDAO = createNiceMock(MemberDAO.class);
-    PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ResourceTypeDAO rtDAO = createNiceMock(ResourceTypeDAO.class);
-
-    ViewRegistry.init(viewDAO, viewInstanceDAO, userDAO, memberDAO, privilegeDAO, securityHelper, rDAO, rtDAO);
-
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     Properties properties = new Properties();
     properties.put("p1", "v1");
@@ -715,18 +658,7 @@ public class ViewRegistryTest {
   @Test
   public void testRemoveInstanceData() throws Exception {
 
-    ViewDAO viewDAO = createNiceMock(ViewDAO.class);
-    ViewInstanceDAO viewInstanceDAO = createNiceMock(ViewInstanceDAO.class);
-    UserDAO userDAO = createNiceMock(UserDAO.class);
-    MemberDAO memberDAO = createNiceMock(MemberDAO.class);
-    PrivilegeDAO privilegeDAO = createNiceMock(PrivilegeDAO.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-    ResourceDAO rDAO = createNiceMock(ResourceDAO.class);
-    ResourceTypeDAO rtDAO = createNiceMock(ResourceTypeDAO.class);
-
-    ViewRegistry.init(viewDAO, viewInstanceDAO, userDAO, memberDAO, privilegeDAO, securityHelper, rDAO, rtDAO);
-
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     ViewInstanceEntity viewInstanceEntity = ViewInstanceEntityTest.getViewInstanceEntity();
 
@@ -746,15 +678,12 @@ public class ViewRegistryTest {
 
   @Test
   public void testIncludeDefinitionForAdmin() {
-    ViewRegistry viewRegistry = ViewRegistry.getInstance();
+    ViewRegistry viewRegistry = getRegistry();
     ViewEntity viewEntity = createNiceMock(ViewEntity.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
     AmbariGrantedAuthority adminAuthority = createNiceMock(AmbariGrantedAuthority.class);
     PrivilegeEntity privilegeEntity = createNiceMock(PrivilegeEntity.class);
     PermissionEntity permissionEntity = createNiceMock(PermissionEntity.class);
 
-    viewRegistry.setSecurityHelper(securityHelper);
-
     Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
     authorities.add(adminAuthority);
 
@@ -763,20 +692,19 @@ public class ViewRegistryTest {
     expect(adminAuthority.getPrivilegeEntity()).andReturn(privilegeEntity);
     expect(privilegeEntity.getPermission()).andReturn(permissionEntity);
     expect(permissionEntity.getId()).andReturn(PermissionEntity.AMBARI_ADMIN_PERMISSION);
-    replay(securityHelper, adminAuthority, privilegeEntity, permissionEntity);
+
+    expect(configuration.getApiAuthentication()).andReturn(true);
+    replay(securityHelper, adminAuthority, privilegeEntity, permissionEntity, configuration);
 
     Assert.assertTrue(viewRegistry.includeDefinition(viewEntity));
 
-    verify(securityHelper, adminAuthority, privilegeEntity, permissionEntity);
+    verify(securityHelper, adminAuthority, privilegeEntity, permissionEntity, configuration);
   }
 
   @Test
   public void testIncludeDefinitionForUserNoInstances() {
-    ViewRegistry viewRegistry = ViewRegistry.getInstance();
+    ViewRegistry viewRegistry = getRegistry();
     ViewEntity viewEntity = createNiceMock(ViewEntity.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
-
-    viewRegistry.setSecurityHelper(securityHelper);
 
     Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
 
@@ -785,26 +713,25 @@ public class ViewRegistryTest {
     securityHelper.getCurrentAuthorities();
     EasyMock.expectLastCall().andReturn(authorities);
     expect(viewEntity.getInstances()).andReturn(instances);
-    replay(securityHelper, viewEntity);
+
+    expect(configuration.getApiAuthentication()).andReturn(true);
+    replay(securityHelper, viewEntity, configuration);
 
     Assert.assertFalse(viewRegistry.includeDefinition(viewEntity));
 
-    verify(securityHelper, viewEntity);
+    verify(securityHelper, viewEntity, configuration);
   }
 
   @Test
   public void testIncludeDefinitionForUserHasAccess() {
-    ViewRegistry viewRegistry = ViewRegistry.getInstance();
+    ViewRegistry viewRegistry = getRegistry();
     ViewEntity viewEntity = createNiceMock(ViewEntity.class);
-    SecurityHelper securityHelper = createNiceMock(SecurityHelper.class);
     ViewInstanceEntity instanceEntity = createNiceMock(ViewInstanceEntity.class);
     ResourceEntity resourceEntity = createNiceMock(ResourceEntity.class);
     AmbariGrantedAuthority viewUseAuthority = createNiceMock(AmbariGrantedAuthority.class);
     PrivilegeEntity privilegeEntity = createNiceMock(PrivilegeEntity.class);
     PermissionEntity permissionEntity = createNiceMock(PermissionEntity.class);
 
-    viewRegistry.setSecurityHelper(securityHelper);
-
     Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
     authorities.add(viewUseAuthority);
 
@@ -819,34 +746,25 @@ public class ViewRegistryTest {
     expect(permissionEntity.getId()).andReturn(PermissionEntity.VIEW_USE_PERMISSION).anyTimes();
     securityHelper.getCurrentAuthorities();
     EasyMock.expectLastCall().andReturn(authorities).anyTimes();
-    replay(securityHelper, viewEntity, instanceEntity, viewUseAuthority, privilegeEntity, permissionEntity);
+    expect(configuration.getApiAuthentication()).andReturn(true);
+    replay(securityHelper, viewEntity, instanceEntity, viewUseAuthority, privilegeEntity, permissionEntity, configuration);
 
     Assert.assertTrue(viewRegistry.includeDefinition(viewEntity));
 
-    verify(securityHelper, viewEntity, instanceEntity, viewUseAuthority, privilegeEntity, permissionEntity);
+    verify(securityHelper, viewEntity, instanceEntity, viewUseAuthority, privilegeEntity, permissionEntity, configuration);
   }
 
-  @Before
-  public void before() throws Exception {
-    clear();
-  }
+  @Test
+  public void testIncludeDefinitionForNoApiAuthentication() {
+    ViewRegistry viewRegistry = getRegistry();
+    ViewEntity viewEntity = createNiceMock(ViewEntity.class);
 
-  @AfterClass
-  public static void afterClass() {
-    clear();
-  }
+    expect(configuration.getApiAuthentication()).andReturn(false);
+    replay(securityHelper, viewEntity, configuration);
+
+    Assert.assertTrue(viewRegistry.includeDefinition(viewEntity));
 
-  public static void clear() {
-    ViewRegistry.getInstance().clear();
-
-    ViewRegistry.setInstanceDAO(null);
-    ViewRegistry.setMemberDAO(null);
-    ViewRegistry.setPrivilegeDAO(null);
-    ViewRegistry.setResourceDAO(null);
-    ViewRegistry.setResourceTypeDAO(null);
-    ViewRegistry.setSecurityHelper(null);
-    ViewRegistry.setUserDAO(null);
-    ViewRegistry.setViewDAO(null);
+    verify(securityHelper, viewEntity, configuration);
   }
 
   public class TestViewRegistryHelper extends ViewRegistry.ViewRegistryHelper {
@@ -911,15 +829,46 @@ public class ViewRegistryTest {
     }
   }
 
+  private static ViewRegistry getRegistry() {
+    ViewRegistry instance = getRegistry(viewDAO, viewInstanceDAO,
+        userDAO, memberDAO, privilegeDAO,
+        resourceDAO, resourceTypeDAO, securityHelper);
+
+    reset(viewDAO, resourceDAO, viewInstanceDAO, userDAO, memberDAO,
+        privilegeDAO, resourceTypeDAO, securityHelper, configuration);
+
+    return instance;
+  }
+
+  public static ViewRegistry getRegistry(ViewDAO viewDAO, ViewInstanceDAO viewInstanceDAO,
+                                  UserDAO userDAO, MemberDAO memberDAO,
+                                  PrivilegeDAO privilegeDAO, ResourceDAO resourceDAO,
+                                  ResourceTypeDAO resourceTypeDAO, SecurityHelper securityHelper ) {
+
+    ViewRegistry instance = new ViewRegistry();
+
+    instance.viewDAO = viewDAO;
+    instance.resourceDAO = resourceDAO;
+    instance.instanceDAO = viewInstanceDAO;
+    instance.userDAO = userDAO;
+    instance.memberDAO = memberDAO;
+    instance.privilegeDAO = privilegeDAO;
+    instance.resourceTypeDAO = resourceTypeDAO;
+    instance.securityHelper = securityHelper;
+    instance.configuration = configuration;
+
+    return instance;
+  }
+
   public static ViewEntity getViewEntity(ViewConfig viewConfig, Configuration ambariConfig,
                                      ClassLoader cl, String archivePath) throws Exception{
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     return registry.createViewDefinition(viewConfig, ambariConfig, cl, archivePath);
   }
 
   public static ViewInstanceEntity getViewInstanceEntity(ViewEntity viewDefinition, InstanceConfig instanceConfig) throws Exception {
-    ViewRegistry registry = ViewRegistry.getInstance();
+    ViewRegistry registry = getRegistry();
 
     ViewInstanceEntity viewInstanceDefinition =
         new ViewInstanceEntity(viewDefinition, instanceConfig);


Mime
View raw message