Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / openecomp / policy / test / ConfigBasePolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
21 package org.openecomp.policy.test;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.openecomp.policy.api.AttributeType;
34 import org.openecomp.policy.api.PolicyChangeResponse;
35 import org.openecomp.policy.api.PolicyConfigType;
36 import org.openecomp.policy.api.PolicyEngine;
37 import org.openecomp.policy.api.PolicyEngineException;
38 import org.openecomp.policy.api.PolicyParameters;
39 import org.openecomp.policy.api.PolicyType;
40 import org.openecomp.policy.std.StdPolicyChangeResponse;
41
42 import org.openecomp.policy.common.logging.flexlogger.*; 
43
44 /**
45  * The class <code>ConfigBasePolicyTest</code> contains tests for the class
46  * {@link <code>PolicyEngine</code>}
47  *
48  * @pattern JUnit Test Case
49  * *
50  */
51 public class ConfigBasePolicyTest extends TestCase {
52
53         private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
54
55         private PolicyEngine policyEngine = null;
56         private PolicyEngine mockPolicyEngine = null;
57         
58         PolicyChangeResponse result = null;
59         StdPolicyChangeResponse response = new StdPolicyChangeResponse();
60         PolicyParameters policyParameters = new PolicyParameters();
61
62         /**
63          * Perform pre-test initialization
64          *
65          * @throws Exception
66          *
67          * @see TestCase#setUp()
68          */
69         protected void setUp() throws Exception {
70                 try {
71                         policyEngine = new PolicyEngine("Test/config_pass.properties");
72                 } catch (PolicyEngineException e) {
73                         logger.error(e.getMessage());
74                         fail("PolicyEngine Instantiation Error" + e);
75                 }
76                 logger.info("Loaded.. PolicyEngine");
77                 
78                 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
79
80         policyParameters.setPolicyConfigType(PolicyConfigType.Base); //required
81         policyParameters.setPolicyName("test.junitTest"); //required
82         policyParameters.setEcompName("test");
83         policyParameters.setConfigName("testBase");
84         policyParameters.setConfigBodyType(PolicyType.OTHER);
85         policyParameters.setConfigBody("testing");
86         policyParameters.setPolicyDescription("testing");  //optional
87         //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
88         
89         //Set the Component Attributes... These are Optional
90         Map<String, String> configAttributes = new HashMap<String, String>(); 
91         configAttributes.put("test", "testing");
92         
93         Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
94         attributes.put(AttributeType.MATCHING, configAttributes);
95         policyParameters.setAttributes(attributes);
96
97         policyParameters.setRequestID(UUID.randomUUID());
98         }
99
100         /**
101          * Perform post-test clean up
102          *
103          * @throws Exception
104          *
105          * @see TestCase#tearDown()
106          */
107         protected void tearDown() throws Exception {
108                 super.tearDown();
109                 // Add additional tear down code here
110         }
111
112         /**
113          * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
114          */
115         public void testCreatePolicy() {
116                 response.setResponseMessage("success");
117                 PolicyChangeResponse result = null;
118                 try {
119                 
120                         Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
121                         result = mockPolicyEngine.createPolicy(policyParameters);
122                         
123                 } catch (Exception e) {
124                         logger.warn(e.getMessage());
125                 }
126                 assertEquals(result, response);
127         }
128         
129         /**
130          * Run the String createConfigPolicy() method test
131          */
132         @Test
133         public void testCreateConfigPolicy() {
134                 String response = "success";
135                 String result = null;
136                 try {
137                 
138                         Mockito.when(mockPolicyEngine.createConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null)).thenReturn(response);
139                         result = mockPolicyEngine.createConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null);
140
141                 } catch (Exception e) {
142                         logger.warn(e.getMessage());
143                 }
144                 assertEquals(result, response);
145         }
146
147         /**
148          * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
149          */
150         public void testUpdatePolicy() {
151                 response.setResponseMessage("success");
152                 PolicyChangeResponse result = null;
153                 try {
154                 
155                         Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
156                         result = mockPolicyEngine.updatePolicy(policyParameters);
157                         
158                 } catch (Exception e) {
159                         logger.warn(e.getMessage());
160                 }
161                 assertEquals(result, response);
162         }
163         
164         /**
165          * Run the String updateConfigPolicy() method test
166          */
167         @Test
168         public void testUpdateConfigPolicy() {
169                 String response = "success";
170                 String result = null;
171                 try {
172                 
173                         Mockito.when(mockPolicyEngine.updateConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null)).thenReturn(response);
174                         result = mockPolicyEngine.updateConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null);
175
176                 } catch (Exception e) {
177                         logger.warn(e.getMessage());
178                 }
179                 assertEquals(result, response);
180         }
181         
182         @Test
183         public final void testCreatePolicyNullPolicyName() {
184                 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
185                 policyParameters.setPolicyName(null);
186                 try{
187                         result = policyEngine.createPolicy(policyParameters);
188                 } catch (Exception e){
189                         logger.warn(e.getMessage());
190                 }
191                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
192         }
193         
194         @Test
195         public final void testCreatePolicyNullPolicyScope() {
196                 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
197                 policyParameters.setPolicyName("test");
198                 try{
199                         result = policyEngine.createPolicy(policyParameters);
200                 } catch (Exception e){
201                         logger.warn(e.getMessage());
202                 }
203                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
204         }
205         
206         @Test
207         public final void testCreatePolicyNullEcompName() {
208                 response.setResponseMessage("PE300 - Data Issue: No ECOMP Name given.");
209                 policyParameters.setEcompName(null);
210                 try{
211                         result = policyEngine.createPolicy(policyParameters);
212                 } catch (Exception e){
213                         logger.warn(e.getMessage());
214                 }
215                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
216         }
217         
218         @Test
219         public final void testCreatePolicyNullPolicyConfigName() {
220                 response.setResponseMessage("PE300 - Data Issue: No Config Name given.");
221                 policyParameters.setConfigName(null);;
222                 try{
223                         result = policyEngine.createPolicy(policyParameters);
224                 } catch (Exception e){
225                         logger.warn(e.getMessage());
226                 }
227                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
228         }
229         
230         @Test
231         public final void testCreatePolicyNullPolicyConfigBodyType() {
232                 response.setResponseMessage("PE300 - Data Issue: No Config Body Type given.");
233                 policyParameters.setConfigBodyType(null);
234                 try{
235                         result = policyEngine.createPolicy(policyParameters);
236                 } catch (Exception e){
237                         logger.warn(e.getMessage());
238                 }
239                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
240         }
241         
242         @Test
243         public final void testCreatePolicyNullPolicyConfigBody() {
244                 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
245                 policyParameters.setConfigBody(null);
246                 try{
247                         result = policyEngine.createPolicy(policyParameters);
248                 } catch (Exception e){
249                         logger.warn(e.getMessage());
250                 }
251                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
252         }
253         
254         @Test
255         public final void testUpdatePolicyNullPolicyName() {
256                 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
257                 policyParameters.setPolicyName(null);
258                 try{
259                         result = policyEngine.updatePolicy(policyParameters);
260                 } catch (Exception e){
261                         logger.warn(e.getMessage());
262                 }
263                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
264         }
265         
266         @Test
267         public final void testUpdatePolicyNullPolicyScope() {
268                 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
269                 policyParameters.setPolicyName("test");
270                 try{
271                         result = policyEngine.updatePolicy(policyParameters);
272                 } catch (Exception e){
273                         logger.warn(e.getMessage());
274                 }
275                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
276         }
277         
278         @Test
279         public final void testUpdatePolicyNullEcompName() {
280                 response.setResponseMessage("PE300 - Data Issue: No ECOMP Name given.");
281                 policyParameters.setEcompName(null);
282                 try{
283                         result = policyEngine.updatePolicy(policyParameters);
284                 } catch (Exception e){
285                         logger.warn(e.getMessage());
286                 }
287                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
288         }
289         
290         @Test
291         public final void testUpdatePolicyNullPolicyConfigName() {
292                 response.setResponseMessage("PE300 - Data Issue: No Config Name given.");
293                 policyParameters.setConfigName(null);;
294                 try{
295                         result = policyEngine.createPolicy(policyParameters);
296                 } catch (Exception e){
297                         logger.warn(e.getMessage());
298                 }
299                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
300         }
301         
302         @Test
303         public final void testUpdatePolicyNullPolicyConfigBodyType() {
304                 response.setResponseMessage("PE300 - Data Issue: No Config Body Type given.");
305                 policyParameters.setConfigBodyType(null);
306                 try{
307                         result = policyEngine.createPolicy(policyParameters);
308                 } catch (Exception e){
309                         logger.warn(e.getMessage());
310                 }
311                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
312         }
313         
314         @Test
315         public final void testUpdatePolicyNullPolicyConfigBody() {
316                 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
317                 policyParameters.setConfigBody(null);
318                 try{
319                         result = policyEngine.createPolicy(policyParameters);
320                 } catch (Exception e){
321                         logger.warn(e.getMessage());
322                 }
323                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
324         }
325 }