Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / test / XACMLPAPTest.java
index 6a3a9dc..ebfbf55 100644 (file)
@@ -24,22 +24,27 @@ package org.onap.policy.pap.test;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.when;
+
 import com.mockrunner.mock.web.MockServletInputStream;
+
 import java.io.BufferedReader;
 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;
 import java.util.Map;
 import java.util.Properties;
+
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
@@ -65,8 +70,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;
@@ -76,7 +83,6 @@ import org.springframework.mock.web.MockHttpServletResponse;
 import org.springframework.mock.web.MockServletConfig;
 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
 
-
 public class XACMLPAPTest {
     private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
 
@@ -90,10 +96,6 @@ public class XACMLPAPTest {
     private static SessionFactory sessionFactory;
     private static CommonClassDao commonClassDao;
 
-    private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
-    private static final String DEFAULT_DB_USER = "sa";
-    private static final String DEFAULT_DB_PWD = "";
-
     @BeforeClass
     public static void beforeClassSetup() throws ServletException {
         sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("xacmlpaptest");
@@ -124,6 +126,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 +163,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 +193,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 +218,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 +243,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 +264,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 +296,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,19 +321,20 @@ 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");
     }
 
-
     @Test
     public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
         httpServletRequest = Mockito.mock(HttpServletRequest.class);
@@ -343,6 +358,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,6 +383,66 @@ 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<String, String> 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");
     }
@@ -396,7 +473,6 @@ public class XACMLPAPTest {
         template.setRule(rule);
         Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
                 .thenReturn(template);
-
     }
 
     @Test
@@ -619,7 +695,6 @@ public class XACMLPAPTest {
         // Verify
         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
 
-
         httpServletRequest = Mockito.mock(HttpServletRequest.class);
         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
         json = "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl"
@@ -838,14 +913,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());
         }
     }