Repository: incubator-ranger Updated Branches: refs/heads/stack 5c4b59af7 -> 5d6881bab
RANGER-230: Added new tests for a few Coprocessor related classes Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/5d6881ba Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/5d6881ba Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/5d6881ba Branch: refs/heads/stack Commit: 5d6881babb239108096542be1b051e2fc3266512 Parents: 5c4b59a Author: Alok Lal <[email protected]> Authored: Thu Feb 5 22:38:39 2015 -0800 Committer: Madhan Neethiraj <[email protected]> Committed: Thu Feb 5 22:38:39 2015 -0800 ---------------------------------------------------------------------- .../authorization/hbase/ColumnIterator.java | 8 +- .../hbase/RangerAuthorizationCoprocessor.java | 3 +- .../authorization/hbase/ColumnIteratorTest.java | 143 +++++++++++++++++++ .../RangerAuthorizationCoprocessorTest.java | 53 +++++++ .../hbase/RangerAuthorizationFilterTest.java | 1 - .../hbase/RangerCoprocessorTest.java | 33 ----- .../authorization/hbase/TestPolicyEngine.java | 10 +- 7 files changed, 205 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/ColumnIterator.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/ColumnIterator.java b/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/ColumnIterator.java index 2c6f805..533a1c1 100644 --- a/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/ColumnIterator.java +++ b/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/ColumnIterator.java @@ -54,11 +54,11 @@ public class ColumnIterator implements Iterator<String> { public boolean hasNext() { if (_setIterator != null) { return _setIterator.hasNext(); + } else if (_listIterator != null) { + return _listIterator.hasNext(); + } else { + return false; } - if (_listIterator != null) { - _listIterator.hasNext(); - } - return false; } /** http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessor.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessor.java b/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessor.java index 2e128ec..08c40e2 100644 --- a/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessor.java +++ b/hbase-agent/src/main/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessor.java @@ -899,7 +899,8 @@ public class RangerAuthorizationCoprocessor extends RangerAuthorizationCoprocess } } // create and initialize the plugin class - new RangerBasePlugin("hbase", appType) {}.init(_authorizer); + new RangerBasePlugin("hbase", appType) {} + .init(_authorizer); if (LOG.isDebugEnabled()) { LOG.debug("Start of Coprocessor: [" + coprocessorType + "] with superUserList [" + superUserList + "]"); } http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/ColumnIteratorTest.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/ColumnIteratorTest.java b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/ColumnIteratorTest.java new file mode 100644 index 0000000..a94118b --- /dev/null +++ b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/ColumnIteratorTest.java @@ -0,0 +1,143 @@ +/* + * 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.authorization.hbase; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.hadoop.hbase.KeyValue; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ColumnIteratorTest { + + @Test + public void test_firewalling() { + // passing null collection + ColumnIterator iterator = new ColumnIterator(null); + assertFalse(iterator.hasNext()); + } + + @SuppressWarnings("unchecked") + @Test + public void test_setOfBytes() { + /* + * It is pointless to test the functionality of base iterator! What we want to assert is that ColumnIterator delegates to the real iterators appropriately. + */ + Iterator<byte[]> iterator = mock(Iterator.class); + // We want to make sure ColumnIterator will return exactly what the real iterator gives it. Let's us doctor mock iteracor to return items in a particular order. + final String[] values = new String[] {"a", "b", "c"}; + when(iterator.next()).thenAnswer(new Answer<byte[]>() { + // return all the items of the values array in order as byte[]. After which return null. + int index = 0; + @Override + public byte[] answer(InvocationOnMock invocation) throws Throwable { + if (index < values.length) { + return values[index++].getBytes(); // we need post increment + } else { + return null; + } + } + }); + + // We want hasNext() to return false after as many times as values were stuffed into it. + when(iterator.hasNext()).thenAnswer(new Answer<Boolean>() { + int i = 0; + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + return i++ < values.length; // we want post increment + } + }); + + // let's stuff this iterator into the collection that we would pass to the ColumnIterator + Set<byte[]> collection = mock(Set.class); + when(collection.iterator()).thenReturn(iterator); + ColumnIterator columnIterator = new ColumnIterator(collection); + int i = 0; + while (columnIterator.hasNext()) { + String value = columnIterator.next(); + assertEquals(values[i++], value); + } + // We should get back exactly as many items as were in the real iterator, no more no less + assertEquals(3, i); + + // this should be called only once! + verify(collection, times(1)).iterator(); + // verify next() was called on the iterator exactly 3 times + verify(iterator, times(3)).next(); + + } + + @SuppressWarnings("unchecked") + @Test + public void test_ListOfKeyValue() { + /* + * We are not interested in validating the behavior of the real iterator. Instead just the behavior specific to the column iterator. + */ + final String[] qualifiers = new String[] {"a", "b", "c"}; + Iterator<KeyValue> iterator = mock(Iterator.class); + // Have the iterator return true as many times as the size of keys array + when(iterator.hasNext()).thenAnswer(new Answer<Boolean>() { + int i = 0; + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + return i++ < qualifiers.length; + } + }); + // have the iterator return a KeyValue composed of the key and value arrays + when(iterator.next()).thenAnswer(new Answer<KeyValue>() { + int i = 0; + @Override + public KeyValue answer(InvocationOnMock invocation) + throws Throwable { + KeyValue kv = mock(KeyValue.class); + when(kv.getQualifier()).thenReturn(qualifiers[i++].getBytes()); + return kv; + } + }); + // stuff it into the collection + List<KeyValue> list = mock(List.class); + when(list.iterator()).thenReturn(iterator); + // now let's check the behavior + ColumnIterator columnIterator = new ColumnIterator(list); + int i = 0; + while (columnIterator.hasNext()) { + String value = columnIterator.next(); + assertEquals(qualifiers[i++], value); + } + // We should get back exactly as many items as were in the real iterator, no more no less + assertEquals(3, i); + + // this should be called only once! + verify(list, times(1)).iterator(); + // verify next() was called on the iterator exactly 3 times + verify(iterator, times(3)).next(); + + } +} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessorTest.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessorTest.java b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessorTest.java new file mode 100644 index 0000000..72b86d1 --- /dev/null +++ b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationCoprocessorTest.java @@ -0,0 +1,53 @@ +/* + * 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.authorization.hbase; + +import static org.junit.Assert.*; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +public class RangerAuthorizationCoprocessorTest { + + @Test + public void test_canBeNewed() { + RangerAuthorizationCoprocessor _coprocessor = new RangerAuthorizationCoprocessor(); + assertNotNull(_coprocessor); + } + + @Test + public void test_getColumnFamilies_happypath() { + + } + + @Test + public void test_getColumnFamilies_firewalling() { + // passing null collection should return back an empty map + RangerAuthorizationCoprocessor _coprocessor = new RangerAuthorizationCoprocessor(); + Map<String, Set<String>> result = _coprocessor.getColumnFamilies(null); + assertNotNull(result); + assertTrue(result.isEmpty()); + // same for passing in an empty collection +// result = _coprocessor.getColumnFamilies(new HashMap<byte[], ? extends Collection<?>>()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationFilterTest.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationFilterTest.java b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationFilterTest.java index 5575ea7..4b49721 100644 --- a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationFilterTest.java +++ b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerAuthorizationFilterTest.java @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.ranger.authorization.hbase; import static org.junit.Assert.assertEquals; http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerCoprocessorTest.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerCoprocessorTest.java b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerCoprocessorTest.java deleted file mode 100644 index 15d4f93..0000000 --- a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/RangerCoprocessorTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.authorization.hbase; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; - -public class RangerCoprocessorTest { - - @Test - public void test_canBeNewed() { - RangerAuthorizationCoprocessor _coprocessor = new RangerAuthorizationCoprocessor(); - assertNotNull(_coprocessor); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/5d6881ba/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/TestPolicyEngine.java ---------------------------------------------------------------------- diff --git a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/TestPolicyEngine.java b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/TestPolicyEngine.java index ed6bcf0..b2eaaef 100644 --- a/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/TestPolicyEngine.java +++ b/hbase-agent/src/test/java/org/apache/ranger/authorization/hbase/TestPolicyEngine.java @@ -19,13 +19,9 @@ package org.apache.ranger.authorization.hbase; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import java.io.InputStream; import java.io.InputStreamReader;
