Repository: incubator-ranger
Updated Branches:
  refs/heads/master bdf44e402 -> d40a02054


RANGER-864: RangerPolicy.set(List) methods append to existing value, instead of 
overwriting


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

Branch: refs/heads/master
Commit: d40a0205433c526261ad408f2ddcf640b6b81f7a
Parents: bdf44e4
Author: Madhan Neethiraj <[email protected]>
Authored: Tue Mar 1 11:14:34 2016 -0800
Committer: Madhan Neethiraj <[email protected]>
Committed: Tue Mar 1 13:48:14 2016 -0800

----------------------------------------------------------------------
 .../ranger/plugin/model/RangerPolicy.java       |  23 +++-
 .../ranger/plugin/model/TestRangerPolicy.java   | 137 +++++++++++++++++++
 2 files changed, 157 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d40a0205/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerPolicy.java
----------------------------------------------------------------------
diff --git 
a/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerPolicy.java 
b/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerPolicy.java
index 268658c..7354dfe 100644
--- 
a/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerPolicy.java
+++ 
b/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerPolicy.java
@@ -624,6 +624,8 @@ public class RangerPolicy extends RangerBaseModelObject 
implements java.io.Seria
                                return;
                        }
 
+                       this.accesses.clear();
+
                        if(accesses != null) {
                                for(RangerPolicyItemAccess access : accesses) {
                                        this.accesses.add(access);
@@ -648,6 +650,8 @@ public class RangerPolicy extends RangerBaseModelObject 
implements java.io.Seria
                                return;
                        }
 
+                       this.users.clear();
+
                        if(users != null) {
                                for(String user : users) {
                                        this.users.add(user);
@@ -672,6 +676,8 @@ public class RangerPolicy extends RangerBaseModelObject 
implements java.io.Seria
                                return;
                        }
 
+                       this.groups.clear();
+
                        if(groups != null) {
                                for(String group : groups) {
                                        this.groups.add(group);
@@ -696,6 +702,8 @@ public class RangerPolicy extends RangerBaseModelObject 
implements java.io.Seria
                                return;
                        }
 
+                       this.conditions.clear();
+
                        if(conditions != null) {
                                for(RangerPolicyItemCondition condition : 
conditions) {
                                        this.conditions.add(condition);
@@ -979,11 +987,20 @@ public class RangerPolicy extends RangerBaseModelObject 
implements java.io.Seria
                 * @param values the value to set
                 */
                public void setValues(List<String> values) {
-                       if (values == null) {
+                       if (this.values == null) {
                                this.values = new ArrayList<String>();
                        }
-                       else {
-                               this.values = new ArrayList<String>(values);
+
+                       if(this.values == values) {
+                               return;
+                       }
+
+                       this.values.clear();
+
+                       if(values != null) {
+                               for(String value : values) {
+                                       this.values.add(value);
+                               }
                        }
                }
 

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d40a0205/agents-common/src/test/java/org/apache/ranger/plugin/model/TestRangerPolicy.java
----------------------------------------------------------------------
diff --git 
a/agents-common/src/test/java/org/apache/ranger/plugin/model/TestRangerPolicy.java
 
b/agents-common/src/test/java/org/apache/ranger/plugin/model/TestRangerPolicy.java
new file mode 100644
index 0000000..cf0daef
--- /dev/null
+++ 
b/agents-common/src/test/java/org/apache/ranger/plugin/model/TestRangerPolicy.java
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.plugin.model;
+
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem;
+import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess;
+import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
+import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestRangerPolicy {
+
+    @Test
+    public void test_01_Policy_SetListMethods() {
+        RangerPolicy           policy         = new RangerPolicy();
+        List<RangerPolicyItem> policyItemList = getList(new 
RangerPolicyItem());
+
+        Assert.assertEquals("RangerPolicy.getPolicyItems()", 0, 
policy.getPolicyItems().size());
+        policy.getPolicyItems().add(new RangerPolicyItem());
+        Assert.assertEquals("RangerPolicy.getPolicyItems().add()", 1, 
policy.getPolicyItems().size());
+        policy.setPolicyItems(policyItemList);
+        Assert.assertEquals("RangerPolicy.setPolicyItems()", 
policyItemList.size(), policy.getPolicyItems().size());
+
+        Assert.assertEquals("RangerPolicy.getDenyPolicyItems()", 0, 
policy.getDenyPolicyItems().size());
+        policy.getDenyPolicyItems().add(new RangerPolicyItem());
+        Assert.assertEquals("RangerPolicy.getDenyPolicyItems().add()", 1, 
policy.getDenyPolicyItems().size());
+        policy.setDenyPolicyItems(policyItemList);
+        Assert.assertEquals("RangerPolicy.setDenyPolicyItems()", 
policyItemList.size(), policy.getDenyPolicyItems().size());
+
+        Assert.assertEquals("RangerPolicy.getAllowExceptions()", 0, 
policy.getAllowExceptions().size());
+        policy.getAllowExceptions().add(new RangerPolicyItem());
+        Assert.assertEquals("RangerPolicy.getAllowExceptions().add()", 1, 
policy.getAllowExceptions().size());
+        policy.setAllowExceptions(policyItemList);
+        Assert.assertEquals("RangerPolicy.setAllowExceptions()", 
policyItemList.size(), policy.getAllowExceptions().size());
+
+        Assert.assertEquals("RangerPolicy.getDenyExceptions()", 0, 
policy.getDenyExceptions().size());
+        policy.getDenyExceptions().add(new RangerPolicyItem());
+        Assert.assertEquals("RangerPolicy.getDenyExceptions().add()", 1, 
policy.getDenyExceptions().size());
+        policy.setDenyExceptions(policyItemList);
+        Assert.assertEquals("RangerPolicy.setDenyExceptions()", 
policyItemList.size(), policy.getDenyExceptions().size());
+    }
+
+    @Test
+    public void test_02_PolicyItem_SetListMethods() {
+        RangerPolicyItem                policyItem = new RangerPolicyItem();
+        List<RangerPolicyItemAccess>    accesses   = getList(new 
RangerPolicyItemAccess());
+        List<String>                    users      = getList("user");
+        List<String>                    groups     = getList("group");
+        List<RangerPolicyItemCondition> conditions = getList(new 
RangerPolicyItemCondition());
+
+
+        Assert.assertEquals("RangerPolicyItem.getAccesses()", 0, 
policyItem.getAccesses().size());
+        policyItem.getAccesses().add(new RangerPolicyItemAccess());
+        Assert.assertEquals("RangerPolicyItem.getAccesses().add()", 1, 
policyItem.getAccesses().size());
+        policyItem.setAccesses(accesses);
+        Assert.assertEquals("RangerPolicyItem.setAccesses()", accesses.size(), 
policyItem.getAccesses().size());
+
+        Assert.assertEquals("RangerPolicyItem.getUsers()", 0, 
policyItem.getUsers().size());
+        policyItem.getUsers().add(new String());
+        Assert.assertEquals("RangerPolicyItem.getUsers().add()", 1, 
policyItem.getUsers().size());
+        policyItem.setUsers(users);
+        Assert.assertEquals("RangerPolicyItem.setUsers()", users.size(), 
policyItem.getUsers().size());
+
+        Assert.assertEquals("RangerPolicyItem.getGroups()", 0, 
policyItem.getGroups().size());
+        policyItem.getGroups().add(new String());
+        Assert.assertEquals("RangerPolicyItem.getGroups().add()", 1, 
policyItem.getGroups().size());
+        policyItem.setGroups(groups);
+        Assert.assertEquals("RangerPolicyItem.setGroups()", groups.size(), 
policyItem.getGroups().size());
+
+        Assert.assertEquals("RangerPolicyItem.getConditions()", 0, 
policyItem.getConditions().size());
+        policyItem.getConditions().add(new RangerPolicyItemCondition());
+        Assert.assertEquals("RangerPolicyItem.getConditions().add()", 1, 
policyItem.getConditions().size());
+        policyItem.setConditions(conditions);
+        Assert.assertEquals("RangerPolicyItem.setConditions()", 
conditions.size(), policyItem.getConditions().size());
+    }
+
+    @Test
+    public void test_03_PolicyResource_SetListMethods() {
+        RangerPolicyResource policyResource = new RangerPolicyResource();
+        List<String>         values         = getList("value");
+
+        Assert.assertEquals("RangerPolicyResource.getValues()", 0, 
policyResource.getValues().size());
+        policyResource.getValues().add(new String());
+        Assert.assertEquals("RangerPolicyResource.getValues().add()", 1, 
policyResource.getValues().size());
+        policyResource.setValues(values);
+        Assert.assertEquals("RangerPolicyResource.setValues()", values.size(), 
policyResource.getValues().size());
+    }
+
+    @Test
+    public void test_04_PolicyItemCondition_SetListMethods() {
+        RangerPolicyItemCondition policyItemCondition = new 
RangerPolicyItemCondition();
+        List<String>              values              = getList("value");
+
+        Assert.assertEquals("RangerPolicyItemCondition.getValues()", 0, 
policyItemCondition.getValues().size());
+        policyItemCondition.getValues().add(new String());
+        Assert.assertEquals("RangerPolicyItemCondition.getValues().add()", 1, 
policyItemCondition.getValues().size());
+        policyItemCondition.setValues(values);
+        Assert.assertEquals("RangerPolicyItemCondition.setValues()", 
values.size(), policyItemCondition.getValues().size());
+    }
+
+    private <T> List<T> getList(T value) {
+        List<T> ret = new ArrayList<T>();
+
+        int count = getRandomNumber(10);
+        for(int i = 0; i < count; i ++) {
+            ret.add(value);
+        }
+
+        return ret;
+    }
+
+    private int getRandomNumber(int maxValue) {
+        return (int)(Math.random() * maxValue);
+    }
+}

Reply via email to