diff options
Diffstat (limited to 'POLICY-SDK-APP/src/test/java/org/onap')
3 files changed, 368 insertions, 14 deletions
diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/components/HumanPolicyComponentTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/components/HumanPolicyComponentTest.java new file mode 100644 index 000000000..1df02d2f4 --- /dev/null +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/components/HumanPolicyComponentTest.java @@ -0,0 +1,232 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2019 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.components; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.att.research.xacml.util.XACMLPolicyScanner.CallbackResult; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.xml.bind.JAXBElement; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +public class HumanPolicyComponentTest { + + private AttributeIdentifiers attrIds; + private HtmlProcessor processor; + private static File temp; + private static File tempAction; + private static File tempConfig; + + @Test + public void testAttributeIdentifiers() { + String testCategory = "testCategory"; + String testID = "testId"; + String testType = "testType"; + String newTestType = "testNewType"; + + attrIds = new AttributeIdentifiers(testCategory, testType, testID); + assertEquals(testCategory, attrIds.category); + assertEquals(testID, attrIds.id); + assertEquals(testType, attrIds.getType()); + + attrIds.setType(newTestType); + assertEquals(newTestType, attrIds.getType()); + } + + @BeforeClass + public static void setup() throws IOException { + temp = File.createTempFile("tmpFile", ".tmp"); + tempAction = File.createTempFile("Action_test", ".tmp"); + tempConfig = File.createTempFile("Config_test", ".tmp"); + temp.deleteOnExit(); + tempAction.deleteOnExit(); + tempConfig.deleteOnExit(); + } + + @SuppressWarnings("unchecked") + @Test + public void testHumanPolicyComponentException() throws IOException { + JAXBElement<PolicySetType> mockRoot = Mockito.mock(JAXBElement.class); + when(mockRoot.getValue()).thenReturn(null); + assertNull(HumanPolicyComponent.DescribePolicy(temp)); + } + + @Test(expected = IllegalArgumentException.class) + public void testHtmlProcessorNull() throws IOException { + processor = new HtmlProcessor(null, null); + } + + @Test(expected = IllegalArgumentException.class) + public void testHtmlProcessor() throws IOException { + File tempFile = File.createTempFile("testFile", ".tmp"); + tempFile.delete(); + processor = new HtmlProcessor(tempFile, null); + } + + @Test(expected = IllegalArgumentException.class) + public void testHtmlProcessorInvalidObject() throws IOException { + processor = new HtmlProcessor(temp, null); + } + + @Test + public void testHtmlProcessorConfigPolicySetType() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + processor = new HtmlProcessor(tempConfig, mockPolicySetType); + processor.onFinishScan(mockPolicySetType); + verify(mockPolicySetType).getVersion(); + } + + @Test + public void testHtmlProcessorActionPolicySetType() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + processor = new HtmlProcessor(tempAction, mockPolicySetType); + processor.onFinishScan(mockPolicySetType); + verify(mockPolicySetType).getVersion(); + } + + @Test + public void testHtmlProcessorConfigPolicyType() throws IOException { + PolicyType mockPolicyType = Mockito.mock(PolicyType.class); + processor = new HtmlProcessor(tempConfig, mockPolicyType); + verify(mockPolicyType).getVersion(); + } + + @Test + public void testHtmlProcessorActionPolicyType() throws IOException { + PolicyType mockPolicyType = Mockito.mock(PolicyType.class); + processor = new HtmlProcessor(tempAction, mockPolicyType); + assertNotNull(processor.getAttributeIdentifiersMap()); + verify(mockPolicyType).getVersion(); + } + + @Test + public void testHtmlProcessorOnPreVisitPolicySet() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + PolicySetType mockPolicyParent = Mockito.mock(PolicySetType.class); + when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(Collections.emptyList()); + processor = new HtmlProcessor(temp, mockPolicySetType); + + CallbackResult preResult = processor.onPreVisitPolicySet(mockPolicyParent, mockPolicySetType); + assertEquals("CONTINUE", preResult.name()); + + CallbackResult postResult = processor.onPostVisitPolicySet(mockPolicyParent, mockPolicySetType); + assertEquals("CONTINUE", postResult.name()); + } + + @Test + public void testHtmlProcessorOnPreVisitPolicySetNullParent() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + PolicySetType mockPolicyParent = null; + JAXBElement<?> mockElement = Mockito.mock(JAXBElement.class); + List<JAXBElement<?>> testList = new ArrayList<JAXBElement<?>>(); + testList.add(mockElement); + when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(testList); + processor = new HtmlProcessor(temp, mockPolicySetType); + + CallbackResult res = processor.onPreVisitPolicySet(mockPolicyParent, mockPolicySetType); + assertEquals("CONTINUE", res.name()); + + CallbackResult postResult = processor.onPostVisitPolicySet(mockPolicyParent, mockPolicySetType); + assertEquals("CONTINUE", postResult.name()); + } + + @Test + public void testHtmlProcessorPolicy() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + PolicyType mockPolicyType = Mockito.mock(PolicyType.class); + when(mockPolicyType.getRuleCombiningAlgId()).thenReturn(null); + when(mockPolicyType.getPolicyId()).thenReturn(null); + when(mockPolicyType.getVersion()).thenReturn(null); + when(mockPolicyType.getTarget()).thenReturn(null); + + processor = new HtmlProcessor(temp, mockPolicySetType); + processor.policy(mockPolicyType); + verify(mockPolicyType).getRuleCombiningAlgId(); + verify(mockPolicyType).getPolicyId(); + verify(mockPolicyType).getVersion(); + verify(mockPolicyType).getTarget(); + } + + @Test + public void testHtmlProcessorPolicySet() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + when(mockPolicySetType.getPolicyCombiningAlgId()).thenReturn(""); + when(mockPolicySetType.getPolicySetId()).thenReturn(""); + when(mockPolicySetType.getVersion()).thenReturn(""); + + processor = new HtmlProcessor(temp, mockPolicySetType); + processor.policySet(mockPolicySetType, ""); + verify(mockPolicySetType, atLeast(1)).getPolicyCombiningAlgId(); + verify(mockPolicySetType, atLeast(1)).getPolicySetId(); + verify(mockPolicySetType, atLeast(1)).getVersion(); + } + + @Test + public void testHtmlProcessorRule() throws IOException { + PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); + RuleType mockRuleType = Mockito.mock(RuleType.class); + ConditionType mockConditionType = Mockito.mock(ConditionType.class); + ObligationExpressionsType mockOESType = Mockito.mock(ObligationExpressionsType.class); + ObligationExpressionType mockOEType = Mockito.mock(ObligationExpressionType.class); + EffectType effectTypePermit = EffectType.PERMIT; + + List<ObligationExpressionType> oblList = new ArrayList<ObligationExpressionType>(); + oblList.add(mockOEType); + + when(mockRuleType.getEffect()).thenReturn(effectTypePermit); + + when(mockRuleType.getRuleId()).thenReturn(null); + when(mockRuleType.getTarget()).thenReturn(null); + when(mockRuleType.getCondition()).thenReturn(mockConditionType); + when(mockRuleType.getObligationExpressions()).thenReturn(mockOESType); + when(mockOESType.getObligationExpression()).thenReturn(oblList); + when(mockOEType.getFulfillOn()).thenReturn(effectTypePermit); + + processor = new HtmlProcessor(temp, mockPolicySetType); + processor.rule(mockRuleType); + + verify(mockRuleType, atLeast(1)).getRuleId(); + verify(mockRuleType, atLeast(1)).getTarget(); + verify(mockRuleType, atLeast(1)).getCondition(); + verify(mockRuleType, atLeast(1)).getObligationExpressions(); + verify(mockOESType, atLeast(1)).getObligationExpression(); + verify(mockOEType, atLeast(1)).getFulfillOn(); + } + +} diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java index 85337f18b..b4a4130ab 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java @@ -23,20 +23,22 @@ package org.onap.policy.controller; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.BufferedReader; +import java.io.IOException; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; - import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; - import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -54,6 +56,7 @@ public class AdminTabControllerTest { private static CommonClassDao commonClassDao; private HttpServletRequest request; private MockHttpServletResponse response; + private AdminTabController admin; /** * Before. @@ -62,21 +65,17 @@ public class AdminTabControllerTest { */ @Before public void setUp() throws Exception { - + admin = new AdminTabController(); logger.info("setUp: Entering"); commonClassDao = mock(CommonClassDao.class); - request = mock(HttpServletRequest.class); response = new MockHttpServletResponse(); - HttpSession mockSession = mock(HttpSession.class); 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); - AdminTabController.setCommonClassDao(commonClassDao); - GlobalRoleSettings globalRole = new GlobalRoleSettings(); globalRole.setLockdown(true); globalRole.setRole("super-admin"); @@ -87,7 +86,6 @@ public class AdminTabControllerTest { @Test public void testGetAdminRole() { - AdminTabController admin = new AdminTabController(); assertNotNull(AdminTabController.getCommonClassDao()); try { admin.getAdminTabEntityData(request, response); @@ -100,7 +98,6 @@ public class AdminTabControllerTest { @Test public void testSaveAdminRole() throws Exception { - AdminTabController admin = new AdminTabController(); String data = "{\"lockdowndata\":{\"lockdown\":true}}"; BufferedReader reader = new BufferedReader(new StringReader(data)); try { @@ -113,4 +110,18 @@ public class AdminTabControllerTest { fail(); } } + + @SuppressWarnings("unchecked") + @Test + public void testGetAdminTabEntityDataException() throws IOException { + HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); + when(mockResponse.getWriter()).thenThrow(Exception.class); + admin.getAdminTabEntityData(request, mockResponse); + verify(mockResponse).getWriter(); + } + + @Test + public void testSaveAdminTabLockdownValueException() throws IOException { + assertNull(admin.saveAdminTabLockdownValue(request, response)); + } } diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java index 64c3b04ff..5e390da53 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java @@ -24,20 +24,33 @@ package org.onap.policy.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import com.att.research.xacml.api.pap.PAPException; import com.mockrunner.mock.web.MockHttpServletRequest; import com.mockrunner.mock.web.MockHttpServletResponse; - import java.io.IOException; - +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - +import javax.servlet.http.HttpSession; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.onap.policy.model.Roles; +import org.onap.policy.xacml.api.pap.OnapPDPGroup; +import org.onap.policy.xacml.api.pap.PAPPolicyEngine; import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.web.support.AppUtils; import org.onap.portalsdk.core.web.support.UserUtils; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -48,12 +61,27 @@ public class AutoPushControllerTest { private PolicyController controller = new PolicyController(); private AutoPushController apController = new AutoPushController(); + private static HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); + private static HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); + private static PolicyController mockPolicyController = Mockito.mock(PolicyController.class); + private static HttpSession mockSession = Mockito.mock(HttpSession.class); + private static User mockUser = Mockito.mock(User.class); + private static List<Object> rolesList = new ArrayList<>(); + @Rule public ExpectedException thrown = ExpectedException.none(); + @BeforeClass + public static void setupMocks() { + when(mockRequest.getSession(false)).thenReturn(mockSession); + when(AppUtils.getSession(mockRequest)).thenReturn(mockSession); + when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser); + when(mockUser.getOrgUserId()).thenReturn(""); + when(mockPolicyController.getRoles(any(String.class))).thenReturn(rolesList); + } + @Test public void testAutoPushSetGet() throws IOException { - // Get and set tests apController.setPolicyController(controller); assertEquals(apController.getPolicyController(), controller); } @@ -80,7 +108,7 @@ public class AutoPushControllerTest { } @Test(expected = NullPointerException.class) - public void testRefresh() throws IOException { + public void testRefreshGroupsNull() throws IOException { apController.refreshGroups(); } @@ -111,4 +139,87 @@ public class AutoPushControllerTest { apController.removePDPGroup(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); } + + @Test + public void testRefreshGroupsSuccess() throws PAPException { + PolicyController mockPolicyController = Mockito.mock(PolicyController.class); + PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class); + Set<OnapPDPGroup> onapPDPGroups = new HashSet<>(); + when(mockPolicyController.getPapEngine()).thenReturn(mockPAPPolicyEngine); + when(mockPAPPolicyEngine.getOnapPDPGroups()).thenReturn(onapPDPGroups); + apController.setPolicyController(mockPolicyController); + apController.refreshGroups(); + verify(mockPolicyController).getPapEngine(); + verify(mockPAPPolicyEngine).getOnapPDPGroups(); + } + + @SuppressWarnings("unchecked") + @Test + public void testRefreshGroups() throws PAPException { + PolicyController mockPolicyController = Mockito.mock(PolicyController.class); + PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class); + when(mockPolicyController.getPapEngine()).thenReturn(mockPAPPolicyEngine); + when(mockPAPPolicyEngine.getOnapPDPGroups()).thenThrow(PAPException.class); + apController.setPolicyController(mockPolicyController); + apController.refreshGroups(); + verify(mockPolicyController).getPapEngine(); + verify(mockPAPPolicyEngine).getOnapPDPGroups(); + } + + @Test + public void testGetPolicyGroupContainerData() throws Exception { + Roles superAdmin = new Roles(); + Roles superEditor = new Roles(); + Roles superGuest = new Roles(); + rolesList.add(superAdmin); + rolesList.add(superEditor); + rolesList.add(superGuest); + + apController.setPolicyController(mockPolicyController); + apController.getPolicyGroupContainerData(mockRequest, mockResponse); + + verify(mockRequest, atLeast(1)).getSession(false); + verify(mockUser, atLeast(1)).getOrgUserId(); + verify(mockPolicyController, atLeast(1)).getRoles(any(String.class)); + } + + @Test + public void testGetPolicyGroupContainerDataWithScope() throws Exception { + Roles superAdmin = new Roles(); + superAdmin.setScope("super-admin-scope"); + Roles superEditor = new Roles(); + superEditor.setScope("super-editor-scope"); + Roles superGuest = new Roles(); + superGuest.setScope("super-guest-scope"); + rolesList.add(superAdmin); + rolesList.add(superEditor); + rolesList.add(superGuest); + + apController.setPolicyController(mockPolicyController); + apController.getPolicyGroupContainerData(mockRequest, mockResponse); + + verify(mockRequest, atLeast(1)).getSession(false); + verify(mockUser, atLeast(1)).getOrgUserId(); + verify(mockPolicyController, atLeast(1)).getRoles(any(String.class)); + } + + @Test + public void testGetPolicyGroupContainerDataWithRole() throws Exception { + Roles superAdmin = new Roles(); + superAdmin.setRole("super-admin"); + Roles superEditor = new Roles(); + superEditor.setRole("super-editor"); + Roles superGuest = new Roles(); + superGuest.setRole("super-guest"); + rolesList.add(superAdmin); + rolesList.add(superEditor); + rolesList.add(superGuest); + + apController.setPolicyController(mockPolicyController); + apController.getPolicyGroupContainerData(mockRequest, mockResponse); + + verify(mockRequest, atLeast(1)).getSession(false); + verify(mockUser, atLeast(1)).getOrgUserId(); + verify(mockPolicyController, atLeast(1)).getRoles(any(String.class)); + } } |