X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Ftest%2FXACMLPAPTest.java;h=fd0c157fe2cb9863c1f1ce78d3482403d725afcc;hb=95b23fd523f72ef52c8555ce6947294a43f099dd;hp=6a3a9dc8d5fca07376ba540fa97a74121a70f871;hpb=97ce3b94390c10a1af43ed5614ca976095c0097d;p=policy%2Fengine.git diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java index 6a3a9dc8d..fd0c157fe 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -65,8 +66,10 @@ import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl; import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation; import org.onap.policy.pap.xacml.rest.util.DictionaryUtils; import org.onap.policy.rest.dao.CommonClassDao; +import org.onap.policy.rest.jpa.ActionPolicyDict; import org.onap.policy.rest.jpa.BRMSParamTemplate; import org.onap.policy.rest.jpa.Category; +import org.onap.policy.rest.jpa.FunctionDefinition; import org.onap.policy.rest.jpa.PolicyEditorScopes; import org.onap.policy.rest.jpa.UserInfo; import org.onap.policy.utils.PolicyUtils; @@ -124,6 +127,7 @@ public class XACMLPAPTest { public void testInit() { httpServletRequest = Mockito.mock(HttpServletRequest.class); httpServletResponse = Mockito.mock(MockHttpServletResponse.class); + logger.info(httpServletResponse); Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers)); Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers)); CommonClassDaoImpl.setSessionfactory(sessionFactory); @@ -160,7 +164,8 @@ public class XACMLPAPTest { MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); - + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -189,6 +194,8 @@ public class XACMLPAPTest { Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); setPolicyCreation(); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -212,6 +219,8 @@ public class XACMLPAPTest { Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); setPolicyCreation(); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -235,6 +244,8 @@ public class XACMLPAPTest { Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); setPolicyCreation(); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -254,6 +265,8 @@ public class XACMLPAPTest { new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -284,6 +297,8 @@ public class XACMLPAPTest { new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -307,14 +322,16 @@ public class XACMLPAPTest { matchingAttributes.put("guardActiveEnd", "10:00"); StdPAPPolicy newPAPPolicy = - new StdPAPPolicy( - StdPAPPolicyParams.builder().policyName("testGuardMinMax").description("test rule").onapName("PDPD") - .providerComboBox("GUARD_MIN_MAX").dynamicFieldConfigAttributes(matchingAttributes) - .editPolicy(false).domain("test").highestVersion(0).build()); + new StdPAPPolicy(StdPAPPolicyParams.builder().policyName("testGuardMinMax").description("test rule") + .onapName("PDPD").providerComboBox("GUARD_MIN_MAX") + .dynamicFieldConfigAttributes(matchingAttributes).editPolicy(false).domain("test") + .highestVersion(0).build()); MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -343,6 +360,8 @@ public class XACMLPAPTest { new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } @@ -366,10 +385,71 @@ public class XACMLPAPTest { new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + logger.info(httpServletRequest); + logger.info(httpServletResponse); pap.service(httpServletRequest, httpServletResponse); Mockito.verify(httpServletResponse).addHeader("operation", "create"); } + @Test + public void testActionPolicy() throws IOException, ServletException, SQLException { + setPolicyCreation(); + httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL"); + Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT"); + Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("API"); + Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create"); + Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Action"); + + CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class); + PolicyCreation.setCommonClassDao(commonClassDao); + ActionPolicyDict actionDict = new ActionPolicyDict(); + actionDict.setBody("{\"test\":\"test\"}"); + actionDict.setHeader("test122=test12:test22=test34"); + actionDict.setType("REST"); + actionDict.setMethod("GET"); + actionDict.setUrl("testsomeurl.com"); + Mockito.when(commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", "test")) + .thenReturn(actionDict); + FunctionDefinition funcDefn = new FunctionDefinition(); + funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:and"); + Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "and")).thenReturn(funcDefn); + funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:integer-equal"); + Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "integer-equal")) + .thenReturn(funcDefn); + funcDefn.setXacmlid("urn:oasis:names:tc:xacml:3.0:function:string-contains"); + Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "string-contains")) + .thenReturn(funcDefn); + funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:integer-greater-than"); + Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "integer-greater-than")) + .thenReturn(funcDefn); + funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:or"); + Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "or")).thenReturn(funcDefn); + + Map componentAttributes = new HashMap<>(); + componentAttributes.put("java", "test"); + StdPAPPolicy newPapPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().policyName("test").description("test") + .dynamicFieldConfigAttributes(componentAttributes) + .dynamicRuleAlgorithmLabels(Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7")) + .dynamicRuleAlgorithmCombo(Arrays.asList("integer-equal", "string-contains", "integer-equal", "and", + "integer-greater-than", "or", "and")) + .dynamicRuleAlgorithmField1(Arrays.asList("cobal", "cap", "cobal", "A2", "Config", "A4", "A1")) + .dynamicRuleAlgorithmField2(Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6")) + .actionPerformer("PDP").actionAttribute("test").editPolicy(false).domain("com").highestVersion(0) + .build()); + newPapPolicy.setActionBody("{\"test\":\"test\"}"); + + MockServletInputStream mockInput = + new MockServletInputStream(PolicyUtils.objectToJsonString(newPapPolicy).getBytes()); + Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + + logger.info(httpServletRequest); + logger.info(httpServletResponse); + pap.service(httpServletRequest, httpServletResponse); + Mockito.verify(httpServletResponse).addHeader("operation", "create"); + } + + private void setPolicyCreation() { CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class); PolicyCreation.setCommonClassDao(commonClassDao); @@ -396,7 +476,6 @@ public class XACMLPAPTest { template.setRule(rule); Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy")) .thenReturn(template); - } @Test @@ -838,14 +917,14 @@ public class XACMLPAPTest { try { Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput); } catch (IOException e) { - fail(); + fail(e.getMessage()); } try { pap.service(httpServletRequest, httpServletResponse); assertTrue(true); } catch (Exception e) { - fail(); + fail(e.getMessage()); } }