Merge "use hibernate and breakup dbdao and papservlet"
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / DecisionPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26 import com.att.research.xacml.util.XACMLProperties;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.rest.adapter.RainyDayParams;
39
40 public class DecisionPolicyTest {
41
42     private static Logger logger = FlexLogger.getLogger(DecisionPolicyTest.class);
43     PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
44     RainyDayParams rainyday = new RainyDayParams();
45     Map<String, String> attributeMap = new HashMap<>();
46     Map<String, String> treatmentMap = new HashMap<>();
47     Map<String, String> settingsMap = new HashMap<>();
48     List<String> errorCodeList = new LinkedList<>();
49     List<String> treatmentList = new LinkedList<>();
50     List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
51     List<String> dynamicRuleAlgorithmCombo = new LinkedList<>();
52     List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
53     List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
54     List<Object> dynamicVariableList = new LinkedList<>();
55     List<String> dataTypeList = new LinkedList<>();
56     DecisionPolicy component = null;
57
58     /**
59      * @throws java.lang.Exception
60      */
61     @Before
62     public void setUp() throws Exception {
63         logger.info("setUp: Entering");
64         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,"src/test/resources/xacml.pap.properties");
65
66         policyAdapter.setPolicyName("Test.Decision_junitTest.1.xml");
67         policyAdapter.setPolicyDescription("testing");
68         policyAdapter.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides");
69         policyAdapter.setPolicyType("Decision");
70         policyAdapter.setEditPolicy(false);
71         policyAdapter.setDomainDir("Test");
72         policyAdapter.setNewFileName("/src/test/resources/Test/client.properties");
73         policyAdapter.setHighestVersion(1);
74         policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID());
75         policyAdapter.setOnapName("MSO");
76
77         //rainy day attributes
78         attributeMap.put("ServiceType", "S");
79         attributeMap.put("VNFType", "V");
80         attributeMap.put("BB_ID", "testBB");
81         attributeMap.put("WorkStep", "1");
82
83         dynamicRuleAlgorithmLabels.add("test1");
84         dynamicRuleAlgorithmField1.add("testField1");
85         dynamicRuleAlgorithmCombo.add("testruleCombo");
86         dynamicRuleAlgorithmField2.add("testField2");
87
88         policyAdapter.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
89         policyAdapter.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
90         policyAdapter.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
91         policyAdapter.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
92         policyAdapter.setDynamicVariableList(dynamicVariableList);
93         policyAdapter.setDynamicSettingsMap(settingsMap);
94         policyAdapter.setDataTypeList(dataTypeList);
95
96         policyAdapter.setDynamicFieldConfigAttributes(attributeMap);
97         policyAdapter.setRainydayMap(treatmentMap);
98         policyAdapter.setRainyday(rainyday);
99
100         component = new DecisionPolicy(policyAdapter);
101
102         logger.info("setUp: exit");
103     }
104
105     /**
106      * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#savePolicies()}.
107      */
108     @Test
109     public void testSavePolicies() {
110         DecisionPolicy mockDecision = Mockito.mock(component.getClass());
111
112         Map<String, String> successMap = new HashMap<>();
113         successMap.put("success", "success");
114
115         try {
116             when(mockDecision.savePolicies()).thenReturn(successMap);
117             successMap = mockDecision.savePolicies();
118         } catch (Exception e) {
119             e.printStackTrace();
120         }
121
122         assertEquals(successMap.get("success"),"success");
123     }
124
125     /**
126      * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}.
127      */
128     @Test
129     public void testPrepareToSaveRainyDay() {
130         logger.debug("test PrepareToSave Policy: enter");
131         policyAdapter.setRuleProvider("Rainy_Day");
132         component = new DecisionPolicy(policyAdapter);
133         boolean response = false;
134
135         try {
136             response = component.prepareToSave();
137         } catch (Exception e) {
138             logger.error("Exception Occured"+e);
139         }
140         assertTrue(response);
141     }
142 }