RANGER-203: fix service-store, policy-refresher unit tests to start from
clean state

Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/ea89bb5f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/ea89bb5f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/ea89bb5f

Branch: refs/heads/stack
Commit: ea89bb5f4aa5e8a92c7e2bfb04d694195c78f42d
Parents: 50c639a
Author: Madhan Neethiraj <[email protected]>
Authored: Tue Jan 13 16:21:30 2015 -0800
Committer: Madhan Neethiraj <[email protected]>
Committed: Tue Jan 13 16:21:30 2015 -0800

----------------------------------------------------------------------
 .../ranger/plugin/store/TestServiceStore.java   | 22 ++++--
 .../ranger/plugin/util/TestPolicyRefresher.java | 74 ++++++++------------
 2 files changed, 48 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/ea89bb5f/plugin-common/src/test/java/org/apache/ranger/plugin/store/TestServiceStore.java
----------------------------------------------------------------------
diff --git 
a/plugin-common/src/test/java/org/apache/ranger/plugin/store/TestServiceStore.java
 
b/plugin-common/src/test/java/org/apache/ranger/plugin/store/TestServiceStore.java
index 6bf5811..16e4fc5 100644
--- 
a/plugin-common/src/test/java/org/apache/ranger/plugin/store/TestServiceStore.java
+++ 
b/plugin-common/src/test/java/org/apache/ranger/plugin/store/TestServiceStore.java
@@ -38,17 +38,28 @@ import org.junit.Test;
 public class TestServiceStore {
        static ServiceStore svcStore    = null;
 
-       static String sdName      = "HdfsTest";
-       static String serviceName = "HdfsTest-dev";
+       static String sdName      = "svcDef-unit-test-TestServiceStore";
+       static String serviceName = "svc-unit-test-TestServiceStore";
        static String policyName  = "testPolicy-1";
 
        @BeforeClass
-       public static void setupTest() {
+       public static void setupTest() throws Exception {
                svcStore = ServiceStoreFactory.instance().getServiceStore();
+
+               // cleanup if the test service and service-def if they already 
exist
+               RangerService svc = svcStore.getServiceByName(serviceName);
+               if(svc != null) {
+                       svcStore.deleteService(svc.getId());
+               }
+
+               RangerServiceDef svcDef = svcStore.getServiceDefByName(sdName);
+               if(svcDef != null) {
+                       svcStore.deleteServiceDef(svcDef.getId());
+               }
        }
 
        @Test
-       public void testServiceManager() throws Exception {
+       public void testServiceStore() throws Exception {
                List<RangerServiceDef> sds = svcStore.getAllServiceDefs();
 
                int initSdCount = sds == null ? 0 : sds.size();
@@ -199,7 +210,8 @@ public class TestServiceStore {
                assertEquals("getServicePolicies(" + updatedSvc.getName() + ") 
failed", svcPolicies.getPolicies().get(0).getName(), updatedPolicy.getName());
 
                ServicePolicies updatedPolicies = 
svcStore.getServicePoliciesIfUpdated(updatedSvc.getName(), 
svcPolicies.getPolicyVersion());
-               assertNull(updatedPolicies);
+               assertNotNull(updatedPolicies);
+               assertEquals(0, updatedPolicies.getPolicies().size());
 
                svcStore.deletePolicy(policy.getId());
                policies = svcStore.getAllPolicies();

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/ea89bb5f/plugin-common/src/test/java/org/apache/ranger/plugin/util/TestPolicyRefresher.java
----------------------------------------------------------------------
diff --git 
a/plugin-common/src/test/java/org/apache/ranger/plugin/util/TestPolicyRefresher.java
 
b/plugin-common/src/test/java/org/apache/ranger/plugin/util/TestPolicyRefresher.java
index 6baa613..030c1f1 100644
--- 
a/plugin-common/src/test/java/org/apache/ranger/plugin/util/TestPolicyRefresher.java
+++ 
b/plugin-common/src/test/java/org/apache/ranger/plugin/util/TestPolicyRefresher.java
@@ -38,16 +38,16 @@ import org.junit.Test;
 
 
 public class TestPolicyRefresher {
-       static RangerPolicyEngineImpl policyEngine      = null;
-       static ServiceStore           svcStore          = null;
-       static PolicyRefresher        refresher         = null;
+       static RangerPolicyEngineImpl policyEngine = null;
+       static ServiceStore           svcStore     = null;
+       static PolicyRefresher        refresher    = null;
 
-       static long                   sleepTimeInMs     = 45 * 1000;
-       static String                 sdName            = "hbase";
-       static String                 svcName           = 
"unit-test-TestPolicyRefresher";
-       static RangerService          svc               = null;
-       static RangerPolicy           policy1           = null;
-       static RangerPolicy           policy2           = null;
+       static long                   sleepTimeInMs = 35 * 1000;
+       static String                 sdName        = "hbase";
+       static String                 svcName       = 
"svc-unit-test-TestPolicyRefresher";
+       static RangerService          svc           = null;
+       static RangerPolicy           policy1       = null;
+       static RangerPolicy           policy2       = null;
 
        static boolean                isPolicyRefreshed = false;
        static long                   policyCount       = 0;
@@ -58,6 +58,14 @@ public class TestPolicyRefresher {
         */
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
+               svcStore = ServiceStoreFactory.instance().getServiceStore();
+               
+               // cleanup if the test service already exists
+               svc = svcStore.getServiceByName(svcName);
+               if(svc != null) {
+                       svcStore.deleteService(svc.getId());
+               }
+
                policyEngine = new RangerPolicyEngineImpl() {
                        @Override
                        public void setPolicies(String serviceName, 
RangerServiceDef serviceDef, List<RangerPolicy> policies) {
@@ -68,17 +76,9 @@ public class TestPolicyRefresher {
                        }
                };
 
-               svcStore = ServiceStoreFactory.instance().getServiceStore();
-               
                refresher = new PolicyRefresher(policyEngine, svcName, 
svcStore);
                refresher.start();
 
-               // cleanup if the test service already exists
-               svc = svcStore.getServiceByName(svcName);
-               if(svc != null) {
-                       svcStore.deleteService(svc.getId());
-               }
-
                // create a service
                svc = new RangerService(sdName, svcName, "test service 
description", Boolean.TRUE, null);
 
@@ -91,25 +91,23 @@ public class TestPolicyRefresher {
         */
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
-               if(svcStore == null) {
-                       return;
-               }
-
-               if(policy1 != null) {
-                       svcStore.deletePolicy(policy1.getId());
-               }
-
-               if(policy2 != null) {
-                       svcStore.deletePolicy(policy2.getId());
-               }
-
-               if(svc != null) {
-                       svcStore.deleteService(svc.getId());
-               }
-               
                if(refresher != null) {
                        refresher.stopRefresher();
                }
+
+               if(svcStore != null) {
+                       if(policy1 != null) {
+                               svcStore.deletePolicy(policy1.getId());
+                       }
+       
+                       if(policy2 != null) {
+                               svcStore.deletePolicy(policy2.getId());
+                       }
+       
+                       if(svc != null) {
+                               svcStore.deleteService(svc.getId());
+                       }
+               }
        }
 
        @Test
@@ -135,12 +133,6 @@ public class TestPolicyRefresher {
 
                policy1 = svcStore.createPolicy(policy);
 
-               Thread.sleep(sleepTimeInMs);
-
-               assertTrue("policy refresh - after one new policy", 
isPolicyRefreshed);
-               assertEquals("policy count - after one new policy", 1, 
policyCount);
-               isPolicyRefreshed = false;
-
                policy = new RangerPolicy(svc.getName(), "policy2", "test 
policy description", Boolean.TRUE, null, null);
                policy.getResources().put("table", new 
RangerPolicyResource("employee", Boolean.FALSE, Boolean.TRUE));
                policy.getResources().put("column-family", new 
RangerPolicyResource("finance", Boolean.FALSE, Boolean.TRUE));
@@ -156,13 +148,11 @@ public class TestPolicyRefresher {
                policy2 = svcStore.createPolicy(policy);
 
                Thread.sleep(sleepTimeInMs);
-
                assertTrue("policy refresh - after two new policies", 
isPolicyRefreshed);
                assertEquals("policy count - after two new policies", 2, 
policyCount);
                isPolicyRefreshed = false;
 
                Thread.sleep(sleepTimeInMs);
-
                assertFalse("policy refresh - after no new policies", 
isPolicyRefreshed);
                assertEquals("policy count - after no new policies", 2, 
policyCount);
                isPolicyRefreshed = false;
@@ -175,7 +165,6 @@ public class TestPolicyRefresher {
                policy2 = svcStore.updatePolicy(policy2);
 
                Thread.sleep(sleepTimeInMs);
-
                assertTrue("policy refresh - after update policy", 
isPolicyRefreshed);
                assertEquals("policy count - after update policy", 2, 
policyCount);
                isPolicyRefreshed = false;
@@ -183,7 +172,6 @@ public class TestPolicyRefresher {
                svcStore.deletePolicy(policy2.getId());
 
                Thread.sleep(sleepTimeInMs);
-
                assertTrue("policy refresh - after delete policy", 
isPolicyRefreshed);
                assertEquals("policy count - after delete policy", 1, 
policyCount);
                isPolicyRefreshed = false;

Reply via email to