New min/max Guard Policy
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / test / XACMLPAPTest.java
index 80772e2..00553e0 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP-PAP-REST
  * ================================================================================
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -181,8 +182,20 @@ public class XACMLPAPTest {
         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
         StdPAPPolicy newPAPPolicy =
-                new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0,
-                        "5", "default", "false", "");
+                new StdPAPPolicy(StdPAPPolicyParams.builder()
+                        .configPolicyType("Firewall Config")
+                        .policyName("test")
+                        .description("testDescription")
+                        .configName("Test")
+                        .editPolicy(false)
+                        .domain("test")
+                        .jsonBody(json)
+                        .highestVersion(0)
+                        .riskLevel("5")
+                        .riskType("default")
+                        .guard("false")
+                        .ttlDate("")
+                        .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -209,11 +222,26 @@ public class XACMLPAPTest {
         ruleAttributes.put("templateName", "testPolicy");
         ruleAttributes.put("samPoll", "5");
         ruleAttributes.put("value", "test");
-        StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param", "test", "testing",
-                "BRMS_PARAM_RULE", false, "test",
-                matchingAttributes, 0, "DROOLS",
-                null, ruleAttributes, "5",
-                "default", "false", "", null, null);
+        //Creating BRMS Param Policies from the Admin Console
+        StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
+                .configPolicyType("BRMS_Param")
+                .policyName("test")
+                .description("testing")
+                .configName("BRMS_PARAM_RULE")
+                .editPolicy(false)
+                .domain("test")
+                .dynamicFieldConfigAttributes(matchingAttributes)
+                .highestVersion(0)
+                .onapName("DROOLS")
+                .configBodyData(null)
+                .drlRuleAndUIParams(ruleAttributes)
+                .riskLevel("5")
+                .riskType("default")
+                .guard("false")
+                .ttlDate("")
+                .brmsController(null)
+                .brmsDependency(null)
+                .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -238,10 +266,21 @@ public class XACMLPAPTest {
         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
         Map<String, String> ruleAttributes = new HashMap<>();
         ruleAttributes.put("value", "test");
-        StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw", "test", "testig description",
-                "BRMS_RAW_RULE", false, "test", ruleAttributes, 0, "DROOLS",
-                "test", "4",
-                "default", "false", null, null, null);
+        StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
+                .configPolicyType("BRMS_Raw")
+                .policyName("test")
+                .description("testig description")
+                .configName("BRMS_RAW_RULE")
+                .editPolicy(false)
+                .domain("test")
+                .dynamicFieldConfigAttributes(ruleAttributes)
+                .highestVersion(0)
+                .onapName("DROOLS")
+                .configBodyData("test")
+                .riskLevel("4")
+                .riskType("default")
+                .guard("false")
+                .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -265,9 +304,24 @@ public class XACMLPAPTest {
         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
         String json = "{\"test\":\"java\"}";
-        StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap",
-                json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
-                "default", "true", "");
+        //Creating CloseLoop_Fault and Performance Metric Policies
+        StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
+                .configPolicyType("ClosedLoop_PM")
+                .policyName("test")
+                .description("testing")
+                .onapName("onap")
+                .jsonBody(json)
+                .draft(false)
+                .oldPolicyFileName(null)
+                .serviceType("Registration Failure(Trinity)")
+                .editPolicy(false)
+                .domain("test")
+                .highestVersion(0)
+                .riskLevel(null)
+                .riskType("default")
+                .guard("true")
+                .ttlDate("")
+                .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -290,8 +344,15 @@ public class XACMLPAPTest {
         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
-        StdPAPPolicy newPAPPolicy = new StdPAPPolicy("test", "test rule", "ONAP", "AAF", null, null, null,
-                null, null, null, null, null, null, null, false, "test", 0);
+        StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
+                .policyName("test")
+                .description("test rule")
+                .onapName("ONAP")
+                .providerComboBox("AAF")
+                .editPolicy(false)
+                .domain("test")
+                .highestVersion(0)
+                .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -324,8 +385,17 @@ public class XACMLPAPTest {
         matchingAttributes.put("guardActiveStart", "05:00");
         matchingAttributes.put("guardActiveEnd", "10:00");
         StdPAPPolicy newPAPPolicy =
-                new StdPAPPolicy("testGuard", "test rule", "PDPD", "GUARD_YAML", matchingAttributes, null, null,
-                        null, null, null, null, null, null, null, false, "test", 0);
+
+                new StdPAPPolicy(StdPAPPolicyParams.builder()
+                        .policyName("testGuard")
+                        .description("test rule")
+                        .onapName("PDPD")
+                        .providerComboBox("GUARD_YAML")
+                        .dynamicFieldConfigAttributes(matchingAttributes)
+                        .editPolicy(false)
+                        .domain("test")
+                        .highestVersion(0)
+                        .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -338,6 +408,43 @@ public class XACMLPAPTest {
         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
     }
+    
+    @Test
+    public void testDecisonGuardMinMaxPolicy() throws IOException, ServletException, SQLException {
+        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("Decision");
+        Map<String, String> matchingAttributes = new HashMap<>();
+        matchingAttributes.put("actor", "test");
+        matchingAttributes.put("recipe", "scaleOut");
+        matchingAttributes.put("targets", "test,test1");
+        matchingAttributes.put("clname", "test");
+        matchingAttributes.put("min", "1");
+        matchingAttributes.put("max", "5");
+        matchingAttributes.put("guardActiveStart", "05:00");
+        matchingAttributes.put("guardActiveEnd", "10:00");
+        StdPAPPolicy newPAPPolicy =
+
+                new StdPAPPolicy(
+                        StdPAPPolicyParams.builder().policyName("testGuard").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);
+
+        // set DBDao
+        setDBDao();
+        pap.service(httpServletRequest, httpServletResponse);
+
+        Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
+        Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
+        Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
+    }
+
 
     @Test
     public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
@@ -355,8 +462,16 @@ public class XACMLPAPTest {
         matchingAttributes.put("guardActiveEnd", "10:00");
         matchingAttributes.put("blackList", "bl1,bl2");
         StdPAPPolicy newPAPPolicy =
-                new StdPAPPolicy("testblGuard", "test rule", "PDPD", "GUARD_BL_YAML", matchingAttributes, null, null,
-                        null, null, null, null, null, null, null, false, "test", 0);
+                new StdPAPPolicy(StdPAPPolicyParams.builder()
+                        .policyName("testblGuard")
+                        .description("test rule")
+                        .onapName("PDPD")
+                        .providerComboBox("GUARD_BL_YAML")
+                        .dynamicFieldConfigAttributes(matchingAttributes)
+                        .editPolicy(false)
+                        .domain("test")
+                        .highestVersion(0)
+                        .build());
         MockServletInputStream mockInput =
                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
@@ -386,7 +501,7 @@ public class XACMLPAPTest {
                 .description("test rule")
                 .onapName("TEST")
                 .configName("config")
-                .attributes(configAttributes)
+                .dynamicFieldConfigAttributes(configAttributes)
                 .configType("OTHER")
                 .configBodyData("test body")
                 .editPolicy(false)