aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP
diff options
context:
space:
mode:
authoruj426b <uj426b@att.com>2020-04-03 17:41:01 -0400
committerJim Hahn <jrh3@att.com>2020-04-16 11:13:20 -0400
commit00747e7214c75e5af92cc0977c98d2df0d6db629 (patch)
tree2e7ac4d5c4b0f04c4a622fad968f80131b64172b /POLICY-SDK-APP
parent068d0f8fe17d13e4f3000b1c9033675068e5532e (diff)
Fix policy engine JUnit that hung on ubuntu 18.04
Replaced powermock with overridable methods. Review comments: - license data Issue-ID: POLICY-2444 Change-Id: I60916a720569a293e6c91e8278cda41b93c3c843 Signed-off-by: uj426b <uj426b@att.com> Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'POLICY-SDK-APP')
-rw-r--r--POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java49
-rw-r--r--POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java82
2 files changed, 86 insertions, 45 deletions
diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java
index 224ee0793..d7d6e7829 100644
--- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java
+++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP Policy Engine
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
* Modifications Copyright (C) 2019 Bell Canada
* ================================================================================
@@ -90,6 +90,7 @@ import org.onap.policy.utils.PolicyUtils;
import org.onap.policy.utils.UserUtils.Pair;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.util.XACMLPolicyScanner;
+import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.web.support.UserUtils;
@WebServlet(
@@ -227,7 +228,7 @@ public class PolicyManagerServlet extends HttpServlet {
LOGGER.debug("doPost");
try {
// if request contains multipart-form-data
- if (ServletFileUpload.isMultipartContent(request)) {
+ if (isMultipartContent(request)) {
uploadFile(request, response);
}
// all other post request has json params in body
@@ -243,6 +244,10 @@ public class PolicyManagerServlet extends HttpServlet {
}
}
+ protected boolean isMultipartContent(HttpServletRequest request) {
+ return ServletFileUpload.isMultipartContent(request);
+ }
+
// Set Error Message for Exception
private void setError(Exception exception, HttpServletResponse response) throws IOException {
try {
@@ -285,10 +290,10 @@ public class PolicyManagerServlet extends HttpServlet {
private void processFormFile(HttpServletRequest request, FileItem item) {
String newFile;
- if (item.getName().endsWith(".xls") && item.getSize() <= PolicyController.getFileSizeLimit()) {
+ if (item.getName().endsWith(".xls") && item.getSize() <= getFileSizeLimit()) {
File file = new File(item.getName());
try (OutputStream outputStream = new FileOutputStream(file)) {
- IOUtils.copy(item.getInputStream(), outputStream);
+ copyStream(item.getInputStream(), outputStream);
newFile = file.toString();
PolicyExportAndImportController importController = new PolicyExportAndImportController();
importController.importRepositoryFile(newFile, request);
@@ -302,6 +307,14 @@ public class PolicyManagerServlet extends HttpServlet {
}
}
+ protected long copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
+ return IOUtils.copy(inputStream, outputStream);
+ }
+
+ protected long getFileSizeLimit() {
+ return PolicyController.getFileSizeLimit();
+ }
+
// File Operation Functionality
private void fileOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException {
JSONObject responseJsonObject;
@@ -322,7 +335,7 @@ public class PolicyManagerServlet extends HttpServlet {
JSONObject params = jsonObject.getJSONObject("params");
Mode mode = Mode.valueOf(params.getString("mode"));
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
LOGGER.info(
"********************Logging UserID while doing actions on Editor tab****************************");
LOGGER.info(
@@ -337,6 +350,10 @@ public class PolicyManagerServlet extends HttpServlet {
setResponse(response, responseJsonObject);
}
+ protected User getUserSession(HttpServletRequest request) {
+ return UserUtils.getUserSession(request);
+ }
+
private void setResponse(HttpServletResponse response, JSONObject responseJsonObject) {
response.setContentType(CONTENTTYPE);
try (PrintWriter out = response.getWriter()) {
@@ -410,9 +427,9 @@ public class PolicyManagerServlet extends HttpServlet {
private boolean lookupPolicyData(HttpServletRequest request, List<Object> policyData, JSONArray policyList,
PolicyController controller, List<JSONObject> resultList) {
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
List<Object> userRoles = controller.getRoles(userId);
- Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ Pair<Set<String>, List<String>> pair = checkRoleAndScope(userRoles);
List<String> roles = pair.second;
Set<String> scopes = pair.first;
if (roles.contains(ADMIN) || roles.contains(EDITOR) || roles.contains(GUEST)) {
@@ -436,6 +453,10 @@ public class PolicyManagerServlet extends HttpServlet {
return true;
}
+ protected Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
+ return org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ }
+
private void getPolicyDataForSuperRoles(List<Object> policyData, PolicyController controller,
List<JSONObject> resultList, List<String> roles, Set<String> scopes) {
if (roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)) {
@@ -518,7 +539,7 @@ public class PolicyManagerServlet extends HttpServlet {
String path = params.getString("path");
String userId = null;
try {
- userId = UserUtils.getUserSession(request).getOrgUserId();
+ userId = getUserSession(request).getOrgUserId();
} catch (Exception e) {
LOGGER.error("Exception Occured while reading userid from cookie" + e);
}
@@ -643,9 +664,9 @@ public class PolicyManagerServlet extends HttpServlet {
private JSONObject processPolicyList(JSONObject params, HttpServletRequest request) throws ServletException {
// Get the Login Id of the User from Request
String testUserID = getTestUserId();
- String userId = testUserID != null ? testUserID : UserUtils.getUserSession(request).getOrgUserId();
+ String userId = testUserID != null ? testUserID : getUserSession(request).getOrgUserId();
List<Object> userRoles = getPolicyControllerInstance().getRoles(userId);
- Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ Pair<Set<String>, List<String>> pair = checkRoleAndScope(userRoles);
List<String> roles = pair.second;
Set<String> scopes = pair.first;
Map<String, String> roleByScope = org.onap.policy.utils.UserUtils.getRoleByScope(userRoles);
@@ -861,7 +882,7 @@ public class PolicyManagerServlet extends HttpServlet {
boolean isActive = false;
List<String> policyActiveInPdp = new ArrayList<>();
Set<String> scopeOfPolicyActiveInPdp = new HashSet<>();
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
String oldPath = params.getString("path");
String newPath = params.getString("newPath");
oldPath = oldPath.substring(oldPath.indexOf('/') + 1);
@@ -1194,7 +1215,7 @@ public class PolicyManagerServlet extends HttpServlet {
// Clone the Policy
private JSONObject copy(JSONObject params, HttpServletRequest request) throws ServletException {
try {
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
String oldPath = params.getString("path");
String newPath = params.getString("newPath");
oldPath = oldPath.substring(oldPath.indexOf('/') + 1);
@@ -1286,7 +1307,7 @@ public class PolicyManagerServlet extends HttpServlet {
PolicyEntity policyEntity = null;
String policyNamewithoutExtension;
try {
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
String deleteVersion = "";
String path = params.getString("path");
LOGGER.debug("delete {}" + path);
@@ -1611,7 +1632,7 @@ public class PolicyManagerServlet extends HttpServlet {
if (entity != null) {
return error("Scope Already Exists");
}
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String userId = getUserSession(request).getOrgUserId();
UserInfo userInfo = new UserInfo();
userInfo.setUserLoginId(userId);
PolicyEditorScopes newScope = new PolicyEditorScopes();
diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java
index b20b1069a..50b806a37 100644
--- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java
+++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java
@@ -32,6 +32,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
@@ -47,15 +48,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.FixMethodOrder;
-import org.junit.Ignore;
import org.junit.Test;
-import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.Mockito;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
@@ -74,16 +72,9 @@ import org.onap.policy.rest.jpa.UserInfo;
import org.onap.policy.utils.UserUtils.Pair;
import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.util.SystemProperties;
-import org.onap.portalsdk.core.web.support.UserUtils;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PowerMockIgnore;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.springframework.mock.web.MockHttpServletResponse;
-@RunWith(PowerMockRunner.class)
-@PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PolicyManagerServletTest extends Mockito {
@@ -831,29 +822,42 @@ public class PolicyManagerServletTest extends Mockito {
verify(mockResponse).getWriter();
}
- @PrepareForTest(ServletFileUpload.class)
@Test
public void test23DoPostUploadFileException() {
- PolicyManagerServlet servlet = new PolicyManagerServlet();
+ PolicyManagerServlet servlet = new PolicyManagerServlet() {
+ private static final long serialVersionUID = 1L;
+ @Override
+ protected boolean isMultipartContent(HttpServletRequest request) {
+ return true;
+ }
+ };
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
- PowerMockito.mockStatic(ServletFileUpload.class);
- when(ServletFileUpload.isMultipartContent(mockRequest)).thenReturn(true);
servlet.doPost(mockRequest, mockResponse);
}
- @PrepareForTest({PolicyController.class, IOUtils.class})
@Test
public void test24ProcessFormFile() throws Exception {
- PolicyManagerServlet servlet = new PolicyManagerServlet();
+ long fileSizeLimit = 10;
+
+ PolicyManagerServlet servlet = new PolicyManagerServlet() {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected long getFileSizeLimit() {
+ return fileSizeLimit;
+ }
+
+ @Override
+ protected long copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
+ // don't really copy the file
+ return 0;
+ }
+ };
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
FileItem mockFileItem = Mockito.mock(FileItem.class);
- PowerMockito.mockStatic(PolicyController.class);
- PowerMockito.mockStatic(IOUtils.class);
InputStream mockInputStream = Mockito.mock(InputStream.class);
- long fileSizeLimit = 10;
- when(PolicyController.getFileSizeLimit()).thenReturn(fileSizeLimit);
when(mockFileItem.getName()).thenReturn("testFileName.xls");
when(mockFileItem.getInputStream()).thenReturn(mockInputStream);
when(mockFileItem.getSize()).thenReturn(fileSizeLimit + 1);
@@ -888,12 +892,8 @@ public class PolicyManagerServletTest extends Mockito {
assertNotNull(((JSONObject) res).get("result"));
}
- @PrepareForTest({UserUtils.class, org.onap.policy.utils.UserUtils.class})
@Test
public void test26LookupPolicyData() throws Exception {
- PowerMockito.mockStatic(UserUtils.class);
- PowerMockito.mockStatic(org.onap.policy.utils.UserUtils.class);
- PolicyManagerServlet servlet = new PolicyManagerServlet();
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
User mockUser = Mockito.mock(User.class);
UserInfo mockUserInfo = Mockito.mock(UserInfo.class);
@@ -923,10 +923,22 @@ public class PolicyManagerServletTest extends Mockito {
Set<String> setOfScopes = new HashSet<String>();
Pair<Set<String>, List<String>> pairList = new Pair<Set<String>, List<String>>(setOfScopes, listOfRoles);
+ PolicyManagerServlet servlet = new PolicyManagerServlet() {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected User getUserSession(HttpServletRequest request) {
+ return mockUser;
+ }
+
+ @Override
+ protected Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
+ return pairList;
+ }
+ };
+
PolicyManagerServlet.setPolicyController(mockPolicyController);
- PowerMockito.when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser);
when(mockPolicyController.getRoles(any(String.class))).thenReturn(rolesList);
- PowerMockito.when(org.onap.policy.utils.UserUtils.checkRoleAndScope(rolesList)).thenReturn(pairList);
when(mockPolicyController.getData(any(Class.class))).thenReturn(filterDataList);
when(mockPolicyVersion.getPolicyName()).thenReturn("sampleName");
when(mockPolicyVersion.getModifiedDate()).thenReturn(mockDate);
@@ -1044,21 +1056,26 @@ public class PolicyManagerServletTest extends Mockito {
verify(mockPolicyController, atLeast(1)).deleteData(mockPolicyEntity);
}
- @PrepareForTest(UserUtils.class)
@Test
public void test28Delete() throws Exception {
- PolicyManagerServlet servlet = new PolicyManagerServlet();
JSONObject mockJSONObject = Mockito.mock(JSONObject.class);
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
User mockUser = Mockito.mock(User.class);
PolicyController mockPolicyController = Mockito.mock(PolicyController.class);
- PowerMockito.mockStatic(UserUtils.class);
List<Object> policyEntityList = new ArrayList<Object>();
PolicyEntity mockPolicyEntity = Mockito.mock(PolicyEntity.class);
policyEntityList.add(mockPolicyEntity);
long policyId = 1;
- PowerMockito.when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser);
+ PolicyManagerServlet servlet = new PolicyManagerServlet() {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected User getUserSession(HttpServletRequest request) {
+ return mockUser;
+ }
+ };
+
PolicyManagerServlet.setPolicyController(mockPolicyController);
when(mockUser.getOrgUserId()).thenReturn("sampleUserId");
when(mockJSONObject.getString("path")).thenReturn("/sampleScope:samplePolicyName.1.xml");
@@ -1094,12 +1111,12 @@ public class PolicyManagerServletTest extends Mockito {
verify(mockJSONObject, atLeast(1)).has("deleteVersion");
}
- @Ignore
@Test
public void test29ParsePolicyList() throws Exception {
PolicyManagerServlet servlet = new PolicyManagerServlet();
List<JSONObject> resultList = new ArrayList<JSONObject>();
PolicyController mockPolicyController = Mockito.mock(PolicyController.class);
+ UserInfo mockUserInfo = Mockito.mock(UserInfo.class);
String policyName = "sampleName\\";
String policyVersion = "sampleVersion";
List<Object> activeDataList = new ArrayList<Object>();
@@ -1117,6 +1134,9 @@ public class PolicyManagerServletTest extends Mockito {
//
// This intermittently throws an NPE, even when fixing the method order
//
+ when(mockPolicyController.getEntityItem(UserInfo.class, "userLoginId", "sampleUserName"))
+ .thenReturn(mockUserInfo);
+ when(mockUserInfo.getUserName()).thenReturn("testUserName");
Whitebox.invokeMethod(servlet, "parsePolicyList", resultList, mockPolicyController, policyName, policyVersion);
verify(mockPolicyController, atLeast(1)).getDataByQuery(any(String.class), any(SimpleBindings.class));
verify(mockPolicyVersion, atLeast(1)).getPolicyName();