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