From a342c15e49481499ee224c205227c28c370a0a27 Mon Sep 17 00:00:00 2001 From: rb7147 Date: Tue, 19 Sep 2017 22:06:26 -0400 Subject: Added Junits for POLICY-SDK-APP Issue-Id: POLICY-52 Change-Id: Ic13cc5a6a32e65e301f1dcdc437f1de8ba6c00d8 Signed-off-by: rb7147 --- .../policy/admin/PolicyManagerServletTest.java | 40 +++++++++----- .../policy/admin/PolicyUserInfoControllerTest.java | 64 ++++++++++++++++++++++ .../policy/controller/PolicyControllerTest.java | 29 +++++++++- .../onap/policy/daoImp/CommonClassDaoImplTest.java | 47 ++++++++++++++-- 4 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java (limited to 'POLICY-SDK-APP/src/test/java/org') 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 a034c8ae9..970b67878 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 @@ -28,7 +28,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import javax.script.SimpleBindings; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -147,11 +146,12 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); - + PolicyManagerServlet.setjUnit(true); BufferedReader reader = new BufferedReader(new StringReader("{params: { mode: 'DESCRIBEPOLICYFILE', path: 'com.Config_SampleTest1206.1.xml'}}")); try { when(request.getReader()).thenReturn(reader); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_SampleTest1206.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(basePolicyData); + String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0"; + when(controller.getDataByQuery(query, null)).thenReturn(basePolicyData); servlet.setPolicyController(controller); servlet.doPost(request, response); } catch (Exception e1) { @@ -168,6 +168,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'LIST', path: '/', onlyFolders: false}}"); list.add("{params: { mode: 'LIST', path: '/com', onlyFolders: false}}"); @@ -176,9 +177,9 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("from PolicyEditorScopes", new SimpleBindings())).thenReturn(policyEditorScopes); - when(controller.getDataByQuery("from PolicyEditorScopes where SCOPENAME like 'com%'", new SimpleBindings())).thenReturn(policyEditorScopes); - when(controller.getDataByQuery("from PolicyVersion where POLICY_NAME like 'com%'", new SimpleBindings())).thenReturn(policyVersion); + when(controller.getDataByQuery("from PolicyEditorScopes", null)).thenReturn(policyEditorScopes); + when(controller.getDataByQuery("from PolicyEditorScopes where SCOPENAME like :scopeName", null)).thenReturn(policyEditorScopes); + when(controller.getDataByQuery("from PolicyVersion where POLICY_NAME like :scopeName", null)).thenReturn(policyVersion); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -196,6 +197,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_SampleTest1206.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -203,7 +205,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_SampleTest1206.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(basePolicyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(basePolicyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -242,6 +244,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_BRMS_Param_BRMSParamvFWDemoPolicy.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -249,7 +252,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_BRMS_Param_BRMSParamvFWDemoPolicy.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -288,6 +291,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_BRMS_Raw_TestBRMSRawPolicy.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -295,7 +299,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_BRMS_Raw_TestBRMSRawPolicy.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -334,6 +338,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_Fault_TestClosedLoopPolicy.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -341,7 +346,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_Fault_TestClosedLoopPolicy.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -380,6 +385,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_PM_TestClosedLoopPMPolicy.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -387,7 +393,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_PM_TestClosedLoopPMPolicy.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -433,6 +439,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_MS_vFirewall.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -441,7 +448,7 @@ public class PolicyManagerServletTest extends Mockito{ when(request.getReader()).thenReturn(reader); when(commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", "resource=SampleResource,service=SampleService,type=SampleType,closedLoopControlName=SampleClosedLoop")).thenReturn(groupListData); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_MS_vFirewall.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -480,6 +487,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Config_FW_TestFireWallPolicy.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -487,7 +495,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Config_FW_TestFireWallPolicy.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -524,6 +532,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Action_TestActionPolicy.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -531,7 +540,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Action_TestActionPolicy.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); @@ -562,6 +571,7 @@ public class PolicyManagerServletTest extends Mockito{ HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PolicyController controller = mock(PolicyController.class); + PolicyManagerServlet.setjUnit(true); List list = new ArrayList<>(); list.add("{params: { mode: 'EDITFILE', path: '/com/Decision_TestDecisionPolicyWithRuleAlgorithms.1.xml', onlyFolders: false}}"); for(int i =0; i < list.size(); i++){ @@ -569,7 +579,7 @@ public class PolicyManagerServletTest extends Mockito{ try { when(request.getReader()).thenReturn(reader); when(controller.getRoles("Test")).thenReturn(rolesdata); - when(controller.getDataByQuery("FROM PolicyEntity where policyName = 'Decision_TestDecisionPolicyWithRuleAlgorithms.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(policyData); + when(controller.getDataByQuery("FROM PolicyEntity where policyName = :split_1 and scope = :split_0", null)).thenReturn(policyData); servlet.setPolicyController(controller); servlet.setTestUserId("Test"); servlet.doPost(request, response); diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java new file mode 100644 index 000000000..4931d20d7 --- /dev/null +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.admin; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.portalsdk.core.domain.User; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.springframework.mock.web.MockHttpServletResponse; + +public class PolicyUserInfoControllerTest { + + private HttpServletRequest request; + private MockHttpServletResponse response; + + @Before + public void setUp() throws Exception { + HttpSession mockSession = mock(HttpSession.class); + request = mock(HttpServletRequest.class); + response = new MockHttpServletResponse(); + User user = new User(); + user.setOrgUserId("Test"); + Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user); + Mockito.when(request.getSession(false)).thenReturn(mockSession); + } + + @Test + public final void testGetPolicyUserInfo() { + PolicyUserInfoController controller = new PolicyUserInfoController(); + controller.getPolicyUserInfo(request, response); + try{ + assertTrue(response.getStatus() == 200); + }catch(Exception e){ + fail(); + } + + } + +} diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyControllerTest.java index 382637c1b..6f20a6770 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyControllerTest.java @@ -19,6 +19,8 @@ */ package org.onap.policy.controller; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -33,6 +35,8 @@ import org.junit.Test; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.dao.CommonClassDao; +import org.onap.policy.rest.jpa.Datatype; +import org.onap.policy.rest.jpa.FunctionDefinition; import org.onap.policy.rest.jpa.PolicyEntity; public class PolicyControllerTest { @@ -44,6 +48,7 @@ public class PolicyControllerTest { public void setUp() throws Exception{ logger.info("setUp: Entering"); commonClassDao = mock(CommonClassDao.class); + PolicyController.setCommonClassDao(commonClassDao); List data = new ArrayList<>(); String policyData = ""; try { @@ -57,12 +62,30 @@ public class PolicyControllerTest { entity.setPolicyData(policyData); entity.setScope("com"); data.add(entity); - + when(commonClassDao.getDataByQuery("FROM PolicyEntity where policyName = 'Config_SampleTest1206.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(data); + + FunctionDefinition fnDefinition = new FunctionDefinition(); + fnDefinition.setXacmlid("Test"); + fnDefinition.setShortname("Test"); + Datatype dataType = new Datatype(); + dataType.setXacmlId("Test"); + fnDefinition.setDatatypeBean(dataType); + List fnObjects = new ArrayList<>(); + fnObjects.add(fnDefinition); + when(commonClassDao.getData(FunctionDefinition.class)).thenReturn(fnObjects); } @Test - public void dummy(){ - System.out.println("Dummy"); + public void testInit(){ + PolicyController controller = new PolicyController(); + PolicyController.setjUnit(true); + controller.init(); + try{ + assertTrue(PolicyController.dropDownMap.size() > 0); + }catch(Exception e){ + logger.error("Exception Occured"+e); + fail(); + } } } diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java index 78dd20af2..d4b0c650b 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java @@ -37,10 +37,13 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.conf.HibernateSession; +import org.onap.policy.controller.PolicyController; import org.onap.policy.daoImp.CommonClassDaoImpl; import org.onap.policy.rest.jpa.OnapName; import org.onap.policy.rest.jpa.PolicyEntity; import org.onap.policy.rest.jpa.PolicyVersion; +import org.onap.policy.rest.jpa.SystemLogDB; import org.onap.policy.rest.jpa.UserInfo; import org.onap.policy.rest.jpa.WatchPolicyNotificationTable; import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; @@ -79,7 +82,22 @@ public class CommonClassDaoImplTest{ // Set up dao with SessionFactory commonClassDao = new CommonClassDaoImpl(); CommonClassDaoImpl.setSessionfactory(sessionFactory); - + PolicyController.setLogTableLimit("1"); + HibernateSession.setSession(sessionFactory); + SystemLogDB data1 = new SystemLogDB(); + data1.setDate(new Date()); + data1.setLogtype("INFO"); + data1.setRemote("Test"); + data1.setSystem("Test"); + data1.setType("Test"); + SystemLogDB data2 = new SystemLogDB(); + data2.setDate(new Date()); + data2.setLogtype("error"); + data2.setRemote("Test"); + data2.setSystem("Test"); + data2.setType("Test"); + HibernateSession.getSession().save(data1); + HibernateSession.getSession().save(data2); // Create TCP server for troubleshooting server = Server.createTcpServer("-tcpAllowOthers").start(); System.out.println("URL: jdbc:h2:" + server.getURL() + "/mem:test"); @@ -329,10 +347,10 @@ public class CommonClassDaoImplTest{ } } - /* - * Test for SQL Injection Protection - * + + /* Test for SQL Injection Protection */ + @Test @Transactional @Rollback(true) @@ -375,6 +393,27 @@ public class CommonClassDaoImplTest{ } } + @Test + public final void testGetLoggingData() { + SystemLogDbDaoImpl system = new SystemLogDbDaoImpl(); + SystemLogDbDaoImpl.setjUnit(true); + try{ + assertTrue(system.getLoggingData() != null); + }catch(Exception e){ + fail(); + } + } + + @Test + public final void testGetSystemAlertData() { + SystemLogDbDaoImpl system = new SystemLogDbDaoImpl(); + SystemLogDbDaoImpl.setjUnit(true); + try{ + assertTrue(system.getSystemAlertData() != null); + }catch(Exception e){ + fail(); + } + } @After public void deleteDB(){ -- cgit 1.2.3-korg