mysql connector to mariadb changes
[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("src/test/resources/client.properties");
76                 policyAdapter.setDomain("Test");
77                 policyAdapter.setNewFileName("/src/test/resources/Test/client.properties");
78                 policyAdapter.setHighestVersion(1);
79                 policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID());
80                 policyAdapter.setOnapName("MSO");
81                 
82                 //rainy day attributes
83                 attributeMap.put("ServiceType", "S");
84                 attributeMap.put("VNFType", "V");
85                 attributeMap.put("BB_ID", "testBB");
86                 attributeMap.put("WorkStep", "1");
87                                 
88                 dynamicRuleAlgorithmLabels.add("test1");
89                 dynamicRuleAlgorithmField1.add("testField1");
90                 dynamicRuleAlgorithmCombo.add("testruleCombo");
91                 dynamicRuleAlgorithmField2.add("testField2");
92                 
93                 policyAdapter.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
94                 policyAdapter.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
95                 policyAdapter.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
96                 policyAdapter.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
97                 policyAdapter.setDynamicVariableList(dynamicVariableList);
98                 policyAdapter.setDynamicSettingsMap(settingsMap);
99                 policyAdapter.setDataTypeList(dataTypeList);
100
101                 policyAdapter.setDynamicFieldConfigAttributes(attributeMap);
102                 policyAdapter.setRainydayMap(treatmentMap);
103                 policyAdapter.setRainyday(rainyday);
104                         
105                 component = new DecisionPolicy(policyAdapter);
106
107         logger.info("setUp: exit");
108         }
109
110         /**
111          * @throws java.lang.Exception
112          */
113         @After
114         public void tearDown() throws Exception {
115         }
116
117         /**
118          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#savePolicies()}.
119          */
120         @Test
121         public void testSavePolicies() {
122         DecisionPolicy mockDecision = Mockito.mock(component.getClass());
123
124         Map<String, String> successMap = new HashMap<>();
125                 successMap.put("success", "success");
126                 
127         try {
128                 when(mockDecision.savePolicies()).thenReturn(successMap);
129                 successMap = mockDecision.savePolicies();
130                 } catch (Exception e) {
131                         e.printStackTrace();
132                 }
133                 
134                 assertEquals(successMap.get("success"),"success");
135         }
136
137         /**
138          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}.
139          */
140         @Test
141         public void testPrepareToSaveRainyDay() {
142                 logger.debug("test PrepareToSave Policy: enter");
143                 policyAdapter.setRuleProvider("Rainy_Day");
144                 component = new DecisionPolicy(policyAdapter);
145                 boolean response = false;
146                 
147                 try {
148                         response = component.prepareToSave();
149                 } catch (Exception e) {
150                         logger.error("Exception Occured"+e);
151                 }
152                 assertTrue(response);
153         }
154         
155         /**
156          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}.
157          */
158         @Test
159         public void testPrepareToSaveCustom() {
160                 logger.debug("test PrepareToSave Policy: enter");
161                 policyAdapter.setRuleProvider("Custom");
162                 component = new DecisionPolicy(policyAdapter);
163                 boolean response = false;
164                 
165                 try {
166                         response = component.prepareToSave();
167                 } catch (Exception e) {
168                         logger.error("Exception Occured"+e);
169                 }
170                 assertTrue(response);
171         }
172
173
174 }