You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by me...@apache.org on 2018/06/14 14:29:02 UTC
[2/2] ranger git commit: RANGER-2132 : Add unit tests for
org.apache.ranger.common package
RANGER-2132 : Add unit tests for org.apache.ranger.common package
Signed-off-by: Mehul Parikh <me...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/ranger/commit/4e04da65
Tree: http://git-wip-us.apache.org/repos/asf/ranger/tree/4e04da65
Diff: http://git-wip-us.apache.org/repos/asf/ranger/diff/4e04da65
Branch: refs/heads/master
Commit: 4e04da6556c53aeb62af5f480db221933075b4d2
Parents: 282c1d5
Author: Bhavik Patel <bh...@gmail.com>
Authored: Thu Jun 14 14:43:42 2018 +0530
Committer: Mehul Parikh <me...@apache.org>
Committed: Thu Jun 14 19:58:00 2018 +0530
----------------------------------------------------------------------
.../org/apache/ranger/common/ContextUtil.java | 2 +-
.../org/apache/ranger/common/ServiceUtil.java | 1 +
.../apache/ranger/common/TestContextUtil.java | 105 +
.../org/apache/ranger/common/TestDateUtil.java | 89 +
.../org/apache/ranger/common/TestJSONUtil.java | 49 +-
.../ranger/common/TestPropertiesUtil.java | 15 +-
.../apache/ranger/common/TestServiceUtil.java | 1904 ++++++++++++++++++
7 files changed, 2162 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ranger/blob/4e04da65/security-admin/src/main/java/org/apache/ranger/common/ContextUtil.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/common/ContextUtil.java b/security-admin/src/main/java/org/apache/ranger/common/ContextUtil.java
index 18c8ba8..9554b4f 100644
--- a/security-admin/src/main/java/org/apache/ranger/common/ContextUtil.java
+++ b/security-admin/src/main/java/org/apache/ranger/common/ContextUtil.java
@@ -27,7 +27,7 @@ public class ContextUtil {
/**
* Singleton class
*/
- private ContextUtil() {
+ public ContextUtil() {
}
public static Long getCurrentUserId() {
http://git-wip-us.apache.org/repos/asf/ranger/blob/4e04da65/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java b/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java
index 0e99be1..0292881 100644
--- a/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java
+++ b/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java
@@ -1252,6 +1252,7 @@ public class ServiceUtil {
mapResource.put("table", tableName);
mapResource.put("column-family", colFamily);
mapResource.put("column", qualifier);
+ ret.setResource(mapResource);
}
http://git-wip-us.apache.org/repos/asf/ranger/blob/4e04da65/security-admin/src/test/java/org/apache/ranger/common/TestContextUtil.java
----------------------------------------------------------------------
diff --git a/security-admin/src/test/java/org/apache/ranger/common/TestContextUtil.java b/security-admin/src/test/java/org/apache/ranger/common/TestContextUtil.java
new file mode 100644
index 0000000..73bce2d
--- /dev/null
+++ b/security-admin/src/test/java/org/apache/ranger/common/TestContextUtil.java
@@ -0,0 +1,105 @@
+/*
+ * 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.common;
+
+import org.apache.ranger.entity.XXPortalUser;
+import org.apache.ranger.security.context.RangerContextHolder;
+import org.apache.ranger.security.context.RangerSecurityContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestContextUtil {
+
+ @InjectMocks
+ ContextUtil contextUtil = new ContextUtil();
+
+ UserSessionBase currentUserSession = new UserSessionBase();
+ XXPortalUser gjUser = new XXPortalUser();
+ RangerSecurityContext context = new RangerSecurityContext();
+
+ @Before
+ public void setup(){
+ gjUser.setId(1L);
+ currentUserSession.setXXPortalUser(gjUser);
+ context.setUserSession(currentUserSession);
+ RangerContextHolder.setSecurityContext(context);
+ }
+
+ @SuppressWarnings("static-access")
+ @Test
+ public void testGetCurrentUserId(){
+ Long expectedId = 1L;
+ Long id = contextUtil.getCurrentUserId();
+
+ Assert.assertEquals(expectedId, id);
+ }
+
+ @SuppressWarnings("static-access")
+ @Test
+ public void testGetCurrentUserPublicName(){
+ String expectedName = "rangerAdmin";
+ gjUser.setPublicScreenName("rangerAdmin");
+
+ String publicName = contextUtil.getCurrentUserPublicName();
+ Assert.assertEquals(expectedName, publicName);
+
+ }
+
+ @SuppressWarnings("static-access")
+ @Test
+ public void testCurrentUserSession(){
+ UserSessionBase expectedUserSession = contextUtil.getCurrentUserSession();
+ Assert.assertNotNull(expectedUserSession);
+ }
+
+ @SuppressWarnings("static-access")
+ @Test
+ public void testCurrentUserSessionAsNull(){
+ context.setUserSession(null);
+ UserSessionBase expectedUserSession = contextUtil.getCurrentUserSession();
+ Assert.assertNull(expectedUserSession);
+ }
+
+ @SuppressWarnings("static-access")
+ @Test
+ public void testCurrentRequestContext(){
+ RequestContext requestContext = new RequestContext();
+ context.setRequestContext(requestContext);
+ RequestContext expectedContext = contextUtil.getCurrentRequestContext();
+ Assert.assertNotNull(expectedContext);
+
+ }
+
+ @SuppressWarnings("static-access")
+ @Test
+ public void testCurrentUserLoginId(){
+ String expectedLoginId = "rangerAdmin";
+ gjUser.setLoginId("rangerAdmin");
+ String loginId = contextUtil.getCurrentUserLoginId();
+ Assert.assertEquals(expectedLoginId, loginId);
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/ranger/blob/4e04da65/security-admin/src/test/java/org/apache/ranger/common/TestDateUtil.java
----------------------------------------------------------------------
diff --git a/security-admin/src/test/java/org/apache/ranger/common/TestDateUtil.java b/security-admin/src/test/java/org/apache/ranger/common/TestDateUtil.java
index 9d2ef55..8b7d34f 100644
--- a/security-admin/src/test/java/org/apache/ranger/common/TestDateUtil.java
+++ b/security-admin/src/test/java/org/apache/ranger/common/TestDateUtil.java
@@ -17,7 +17,10 @@
package org.apache.ranger.common;
import java.text.SimpleDateFormat;
+import java.util.Calendar;
import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
import org.junit.Assert;
import org.junit.Ignore;
@@ -27,6 +30,8 @@ import org.springframework.beans.factory.annotation.Autowired;
@SuppressWarnings("deprecation")
public class TestDateUtil {
+ private static final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT+0");
+
@Autowired
DateUtil dateUtil = new DateUtil();
@@ -42,6 +47,28 @@ public class TestDateUtil {
Assert.assertEquals(dateCheck.getHours(), hourse);
}
+ @Test
+ public void testDateFromNow(){
+
+ int days = 2;
+ int hours = 3;
+ int minutes = 50;
+
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DATE, days);
+ cal.add(Calendar.HOUR, hours);
+ cal.add(Calendar.MINUTE, minutes);
+ Date expectedDate = cal.getTime();
+
+
+ Date actualDate = dateUtil.getDateFromNow(days, hours, minutes);
+
+ Assert.assertEquals(expectedDate.getYear(), actualDate.getYear());
+ Assert.assertEquals(expectedDate.getDay(), actualDate.getDay());
+ Assert.assertEquals(expectedDate.getMonth(), actualDate.getMonth());
+
+ }
+
@Test
public void testDateToString() {
Date date = new Date();
@@ -74,4 +101,66 @@ public class TestDateUtil {
Assert.assertEquals(currentDate.getMinutes(),mins);
}
+ @Test
+ public void testStringToDate(){
+ String dateString = "2018-05-31";
+ String dateFormat = "yyyy-MM-dd";
+
+ Calendar cal = Calendar.getInstance();
+ cal.set(2018 - 1900, 04, 31);
+ Date expectedDate = new Date(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
+
+ Date actualDate = dateUtil.stringToDate(dateString, dateFormat);
+ Assert.assertEquals(expectedDate.getYear(), actualDate.getYear());
+ Assert.assertEquals(expectedDate.getDay(), actualDate.getDay());
+ Assert.assertEquals(expectedDate.getMonth(), actualDate.getMonth());
+
+ }
+
+ @Test
+ public void testGetUTCDate(){
+
+ Calendar local=Calendar.getInstance();
+ int offset = local.getTimeZone().getOffset(local.getTimeInMillis());
+ GregorianCalendar utc = new GregorianCalendar(gmtTimeZone);
+ utc.setTimeInMillis(local.getTimeInMillis());
+ utc.add(Calendar.MILLISECOND, -offset);
+ Date expectedDate = utc.getTime();
+
+ Date actualDate = dateUtil.getUTCDate();
+ Assert.assertEquals(actualDate.getDate(),expectedDate.getDate());
+ Assert.assertEquals(actualDate.getMinutes(),expectedDate.getMinutes());
+ }
+
+ @Test
+ public void testGetUTCDateEpoh(){
+
+ Calendar local=Calendar.getInstance();
+ int offset = local.getTimeZone().getOffset(2008);
+ GregorianCalendar utc = new GregorianCalendar(gmtTimeZone);
+ utc.setTimeInMillis(2008);
+ utc.add(Calendar.MILLISECOND, -offset);
+ Date expectedDate = utc.getTime();
+
+ Date actualDate = dateUtil.getUTCDate(2008);
+ Assert.assertEquals(actualDate.getDate(),expectedDate.getDate());
+ Assert.assertEquals(actualDate.getMinutes(),expectedDate.getMinutes());
+ }
+
+ @Test
+ public void testGetLocalDateForUTCDate(){
+ Date dt = new Date();
+ Calendar local=Calendar.getInstance();
+ int offset = local.getTimeZone().getOffset(local.getTimeInMillis());
+ GregorianCalendar utc = new GregorianCalendar(gmtTimeZone);
+ utc.setTimeInMillis(dt.getTime());
+ utc.add(Calendar.MILLISECOND, -offset);
+ Date expectedDate = utc.getTime();
+
+ Date actualDate = dateUtil.getLocalDateForUTCDate(dt);
+ Assert.assertEquals(actualDate.getDate(),expectedDate.getDate());
+ Assert.assertEquals(actualDate.getMinutes(),expectedDate.getMinutes());
+
+ }
+
}
http://git-wip-us.apache.org/repos/asf/ranger/blob/4e04da65/security-admin/src/test/java/org/apache/ranger/common/TestJSONUtil.java
----------------------------------------------------------------------
diff --git a/security-admin/src/test/java/org/apache/ranger/common/TestJSONUtil.java b/security-admin/src/test/java/org/apache/ranger/common/TestJSONUtil.java
index 5b4787d..0daf7f1 100644
--- a/security-admin/src/test/java/org/apache/ranger/common/TestJSONUtil.java
+++ b/security-admin/src/test/java/org/apache/ranger/common/TestJSONUtil.java
@@ -16,9 +16,16 @@
*/
package org.apache.ranger.common;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.ranger.view.VXResponse;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Rule;
@@ -63,5 +70,45 @@ public class TestJSONUtil {
Map<?, ?> map = new HashMap<Object, Object>();
String value = jsonUtil.readMapToString(map);
Assert.assertNotNull(value);
- }
+ }
+
+ @Test
+ public void testReadListToString() {
+ String expectedJsonString = "[\"hdfs\",\"hive\",\"knox\"]";
+ List<String> testList = new ArrayList<String>();
+
+ testList.add("hdfs");
+ testList.add("hive");
+ testList.add("knox");
+
+ String actualJsonString = jsonUtil.readListToString(testList);
+
+ Assert.assertEquals(expectedJsonString, actualJsonString);
+ }
+
+ @Test
+ public void testWriteObjectAsString(){
+ String expectedJsonString = "{\"statusCode\":200,\"msgDesc\":\"Logout Successful\"}";
+ VXResponse vXResponse = new VXResponse();
+ vXResponse.setStatusCode(HttpServletResponse.SC_OK);
+ vXResponse.setMsgDesc("Logout Successful");
+ String actualJsonString = jsonUtil.writeObjectAsString(vXResponse);
+
+ Assert.assertEquals(expectedJsonString, actualJsonString);
+
+ }
+
+ @Test
+ public void testWriteJsonToJavaObject(){
+ String jsonString = "[\"hdfs\",\"hive\",\"knox\"]";
+ String expectedSetString = "[hive, hdfs, knox]";
+ Set<String> testSet = new HashSet<>();
+ Set<String> expectedSet = new HashSet<>();
+ expectedSet = jsonUtil.writeJsonToJavaObject(jsonString, testSet.getClass());
+
+ String actualSetString = expectedSet.toString();
+ Assert.assertEquals(expectedSetString, actualSetString);
+
+
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ranger/blob/4e04da65/security-admin/src/test/java/org/apache/ranger/common/TestPropertiesUtil.java
----------------------------------------------------------------------
diff --git a/security-admin/src/test/java/org/apache/ranger/common/TestPropertiesUtil.java b/security-admin/src/test/java/org/apache/ranger/common/TestPropertiesUtil.java
index 4fb3a19..959fede 100644
--- a/security-admin/src/test/java/org/apache/ranger/common/TestPropertiesUtil.java
+++ b/security-admin/src/test/java/org/apache/ranger/common/TestPropertiesUtil.java
@@ -110,10 +110,23 @@ public class TestPropertiesUtil {
}
@Test
- public void testGetPropertyStringList(){
+ public void testGetPropertyStringListForNull(){
String key = null;
PropertiesUtil.getPropertyStringList(key);
Assert.assertNull(key);
}
+ @Test
+ public void testGetPropertyStringList(){
+ String key = "ranger.users.roles.list";
+
+ PropertiesUtil.getPropertiesMap().put("ranger.users.roles.list", "read,write,access");
+ String[] actualroles = PropertiesUtil.getPropertyStringList(key);
+
+ Assert.assertEquals("read", actualroles[0]);
+ Assert.assertEquals("write", actualroles[1]);
+ Assert.assertEquals("access", actualroles[2]);
+
+ }
+
}
\ No newline at end of file