Merge "Sonar cleanup for PolicyEngineUtils"
[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 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 package org.onap.policy.pap.xacml.rest.components;
21
22 import static org.junit.Assert.*;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
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
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.adapter.PolicyRestAdapter;
40 import org.onap.policy.rest.adapter.RainyDayParams;
41
42 import com.att.research.xacml.util.XACMLProperties;
43
44 public class DecisionPolicyTest {
45
46         private static Logger logger = FlexLogger.getLogger(DecisionPolicyTest.class);
47         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
48         RainyDayParams rainyday = new RainyDayParams();
49         Map<String, String> attributeMap = new HashMap<>();
50         Map<String, String> treatmentMap = new HashMap<>();
51         Map<String, String> settingsMap = new HashMap<>();
52         List<String> errorCodeList = new LinkedList<>();
53         List<String> treatmentList = new LinkedList<>();
54         List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
55         List<String> dynamicRuleAlgorithmCombo = new LinkedList<>();
56         List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
57         List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
58         List<Object> dynamicVariableList = new LinkedList<>();
59         List<String> dataTypeList = new LinkedList<>();
60         DecisionPolicy component = null;
61         
62         /**
63          * @throws java.lang.Exception
64          */
65         @Before
66         public void setUp() throws Exception {
67                 logger.info("setUp: Entering");
68                 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,"src/test/resources/xacml.pap.properties");
69
70                 policyAdapter.setPolicyName("Test.Decision_junitTest.1.xml");
71                 policyAdapter.setPolicyDescription("testing");
72                 policyAdapter.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides");
73                 policyAdapter.setPolicyType("Decision");
74                 policyAdapter.setEditPolicy(false);
75                 policyAdapter.setDomainDir("Test");
76                 policyAdapter.setNewFileName("/src/test/resources/Test/client.properties");
77                 policyAdapter.setHighestVersion(1);
78                 policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID());
79                 policyAdapter.setOnapName("MSO");
80                 
81                 //rainy day attributes
82                 attributeMap.put("ServiceType", "S");
83                 attributeMap.put("VNFType", "V");
84                 attributeMap.put("BB_ID", "testBB");
85                 attributeMap.put("WorkStep", "1");
86                                 
87                 dynamicRuleAlgorithmLabels.add("test1");
88                 dynamicRuleAlgorithmField1.add("testField1");
89                 dynamicRuleAlgorithmCombo.add("testruleCombo");
90                 dynamicRuleAlgorithmField2.add("testField2");
91                 
92                 policyAdapter.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
93                 policyAdapter.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
94                 policyAdapter.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
95                 policyAdapter.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
96                 policyAdapter.setDynamicVariableList(dynamicVariableList);
97                 policyAdapter.setDynamicSettingsMap(settingsMap);
98                 policyAdapter.setDataTypeList(dataTypeList);
99
100                 policyAdapter.setDynamicFieldConfigAttributes(attributeMap);
101                 policyAdapter.setRainydayMap(treatmentMap);
102                 policyAdapter.setRainyday(rainyday);
103                         
104                 component = new DecisionPolicy(policyAdapter);
105
106         logger.info("setUp: exit");
107         }
108
109         /**
110          * @throws java.lang.Exception
111          */
112         @After
113         public void tearDown() throws Exception {
114         }
115
116         /**
117          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#savePolicies()}.
118          */
119         @Test
120         public void testSavePolicies() {
121         DecisionPolicy mockDecision = Mockito.mock(component.getClass());
122
123         Map<String, String> successMap = new HashMap<>();
124                 successMap.put("success", "success");
125                 
126         try {
127                 when(mockDecision.savePolicies()).thenReturn(successMap);
128                 successMap = mockDecision.savePolicies();
129                 } catch (Exception e) {
130                         e.printStackTrace();
131                 }
132                 
133                 assertEquals(successMap.get("success"),"success");
134         }
135
136         /**
137          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}.
138          */
139         @Test
140         public void testPrepareToSaveRainyDay() {
141                 logger.debug("test PrepareToSave Policy: enter");
142                 policyAdapter.setRuleProvider("Rainy_Day");
143                 component = new DecisionPolicy(policyAdapter);
144                 boolean response = false;
145                 
146                 try {
147                         response = component.prepareToSave();
148                 } catch (Exception e) {
149                         logger.error("Exception Occured"+e);
150                 }
151                 assertTrue(response);
152         }
153         
154         /**
155          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}.
156          */
157         @Test
158         public void testPrepareToSaveCustom() {
159                 logger.debug("test PrepareToSave Policy: enter");
160                 policyAdapter.setRuleProvider("Custom");
161                 component = new DecisionPolicy(policyAdapter);
162                 boolean response = false;
163                 
164                 try {
165                         response = component.prepareToSave();
166                 } catch (Exception e) {
167                         logger.error("Exception Occured"+e);
168                 }
169                 assertTrue(response);
170         }
171
172
173 }