Update css file name in conf.py
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / test / PolicyEngineTest.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.onap.policy.test;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.any;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Collections;
33 import java.util.UUID;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.mockito.internal.util.reflection.Whitebox;
37 import org.onap.policy.api.ConfigRequestParameters;
38 import org.onap.policy.api.DecisionRequestParameters;
39 import org.onap.policy.api.DeletePolicyParameters;
40 import org.onap.policy.api.DictionaryParameters;
41 import org.onap.policy.api.EventRequestParameters;
42 import org.onap.policy.api.ImportParameters;
43 import org.onap.policy.api.MetricsRequestParameters;
44 import org.onap.policy.api.NotificationScheme;
45 import org.onap.policy.api.PolicyConfigException;
46 import org.onap.policy.api.PolicyEngine;
47 import org.onap.policy.api.PolicyEngineException;
48 import org.onap.policy.api.PolicyParameters;
49 import org.onap.policy.api.PushPolicyParameters;
50 import org.onap.policy.common.logging.flexlogger.FlexLogger;
51 import org.onap.policy.common.logging.flexlogger.Logger;
52 import org.onap.policy.std.StdPolicyEngine;
53
54 public class PolicyEngineTest {
55
56     private static final String ONAP_NAME_VALUE = "ONAP";
57     private static final String STD_POLICY_ENGINE_LOCAL_VARIABLE = "stdPolicyEngine";
58     private static final UUID REQUEST_UUID = UUID.randomUUID();
59     private static final String TEST_CONFIG_PASS_PROPERTIES = "Test/config_pass.properties";
60     private static final Logger logger = FlexLogger.getLogger(PolicyEngineTest.class);
61     private PolicyEngine policyEngine = null;
62     private String filePath = null;
63
64     private final StdPolicyEngine mockedStdPolicyEngine = Mockito.mock(StdPolicyEngine.class);
65
66     @Test
67     public void testPolicyEngineForFail() {
68         PolicyEngine policyEngine = null;
69         try {
70             policyEngine = new PolicyEngine(filePath);
71         } catch (final PolicyEngineException e) {
72             logger.warn(e.getMessage());
73         }
74         assertNull(policyEngine);
75         // Test even for this case.
76         filePath = "NotNull";
77         try {
78             policyEngine = new PolicyEngine(filePath);
79         } catch (final PolicyEngineException e) {
80             logger.warn(e.getMessage());
81         }
82         assertNull(policyEngine);
83     }
84
85     @Test
86     public void testPolicyEngineforPropertyFileError() {
87         filePath = "Test/config_error.property";
88         isFileAvailable(filePath);
89         try {
90             policyEngine = new PolicyEngine(filePath);
91         } catch (final PolicyEngineException e) {
92             logger.warn(e.getMessage());
93         }
94         assertNull(policyEngine);
95     }
96
97     @Test
98     public void testPolicyEngineforPDPURLError() {
99         final String filePath = "Test/config_fail.properties";
100         isFileAvailable(filePath);
101         try {
102             policyEngine = new PolicyEngine(filePath);
103         } catch (final PolicyEngineException e) {
104             logger.warn(e.getMessage());
105         }
106         assertNull(policyEngine);
107     }
108
109     @Test
110     public void testPolicyEngineForPass() {
111         final String filePath = TEST_CONFIG_PASS_PROPERTIES;
112         isFileAvailable(filePath);
113         try {
114             policyEngine = new PolicyEngine(filePath);
115         } catch (final PolicyEngineException e) {
116             logger.warn(e.getMessage());
117         }
118         assertNotNull(policyEngine);
119     }
120
121     @Test
122     public void testPolicyEngineForUEBPass() {
123         final String filePath = "Test/config_UEB_pass.properties";
124         isFileAvailable(filePath);
125         try {
126             policyEngine = new PolicyEngine(filePath);
127         } catch (final PolicyEngineException e) {
128             logger.warn(e.getMessage());
129         }
130         assertNotNull(policyEngine);
131     }
132
133     @Test
134     public void testPolicyEngineForUEBBadType() {
135         final String filePath = "Test/config_UEB_bad_type.properties";
136         isFileAvailable(filePath);
137         try {
138             policyEngine = new PolicyEngine(filePath);
139         } catch (final PolicyEngineException e) {
140             logger.warn(e.getMessage());
141         }
142         assertNotNull(policyEngine);
143     }
144
145     @Test
146     public void testPolicyEngineForUEBBadServerType() {
147         final String filePath = "Test/config_UEB_badservers.properties";
148         isFileAvailable(filePath);
149         try {
150             policyEngine = new PolicyEngine(filePath);
151         } catch (final PolicyEngineException e) {
152             logger.warn(e.getMessage());
153         }
154         assertNotNull(policyEngine);
155     }
156
157     @Test
158     public void testPolicyEngineNotficationAutoUEB() {
159         final String filePath = "Test/config_UEB_pass.properties";
160         isFileAvailable(filePath);
161         try {
162             policyEngine = new PolicyEngine(filePath);
163             policyEngine.setScheme(NotificationScheme.AUTO_ALL_NOTIFICATIONS);
164         } catch (final PolicyEngineException e) {
165             logger.warn(e.getMessage());
166         }
167         assertNotNull(policyEngine);
168     }
169
170     @Test
171     public void testPolicyEngineNotficationAuto() {
172         final String filePath = TEST_CONFIG_PASS_PROPERTIES;
173         isFileAvailable(filePath);
174         try {
175             policyEngine = new PolicyEngine(filePath);
176             policyEngine.setScheme(NotificationScheme.AUTO_ALL_NOTIFICATIONS);
177             // policyEngine.getNotification();
178         } catch (final PolicyEngineException e) {
179             logger.warn(e.getMessage());
180         }
181         assertNotNull(policyEngine);
182     }
183
184     @Test
185     public void testGetConfig_ConfigRequestParameters_StdPolicyEngineGetConfigCalled()
186             throws PolicyEngineException, PolicyConfigException {
187         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
188
189         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
190         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
191
192         objUnderTest.getConfig(new ConfigRequestParameters());
193
194         verify(mockedStdPolicyEngine).getConfig(Mockito.any(ConfigRequestParameters.class));
195
196     }
197
198     @Test
199     public void testlistConfig_ConfigRequestParameters_StdPolicyEngineListConfigCalled()
200             throws PolicyEngineException, PolicyConfigException {
201         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
202
203         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
204         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
205
206         objUnderTest.listConfig(new ConfigRequestParameters());
207
208         verify(mockedStdPolicyEngine).listConfig(any(ConfigRequestParameters.class));
209     }
210
211     @Test
212     public void testSendEvent_EventAttributes_StdPolicyEngineSendEventCalled() throws Exception {
213         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
214
215         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
216         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
217
218         objUnderTest.sendEvent(Collections.emptyMap());
219
220         verify(mockedStdPolicyEngine).sendEvent(eq(Collections.emptyMap()), eq(null));
221     }
222
223     @Test
224     public void testSendEvent_EventAttributesWithUUID_StdPolicyEngineSendEventCalled() throws Exception {
225         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
226
227         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
228         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
229
230         objUnderTest.sendEvent(Collections.emptyMap(), REQUEST_UUID);
231
232         verify(mockedStdPolicyEngine).sendEvent(eq(Collections.emptyMap()), eq(REQUEST_UUID));
233     }
234
235     @Test
236     public void testSendEvent_EventRequestParameters_StdPolicyEngineSendEventCalled() throws Exception {
237         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
238
239         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
240         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
241
242         objUnderTest.sendEvent(new EventRequestParameters());
243
244         verify(mockedStdPolicyEngine).sendEvent(any(EventRequestParameters.class));
245     }
246
247     @Test
248     public void testGetDecision_RequestParameters_StdPolicyEngineGetDecisionCalled() throws Exception {
249         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
250
251         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
252         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
253
254         objUnderTest.getDecision(ONAP_NAME_VALUE, Collections.emptyMap());
255
256         verify(mockedStdPolicyEngine).getDecision(eq(ONAP_NAME_VALUE), eq(Collections.emptyMap()), eq(null));
257     }
258
259     @Test
260     public void testGetDecision_RequestParametersWithUUID_StdPolicyEngineGetDecisionCalled() throws Exception {
261         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
262
263         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
264         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
265
266         objUnderTest.getDecision(ONAP_NAME_VALUE, Collections.emptyMap(), REQUEST_UUID);
267
268         verify(mockedStdPolicyEngine).getDecision(eq(ONAP_NAME_VALUE), eq(Collections.emptyMap()), eq(REQUEST_UUID));
269     }
270
271     @Test
272     public void testGetDecision_DecisionRequestParameters_StdPolicyEngineGetDecisionCalled() throws Exception {
273         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
274
275         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
276         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
277
278         objUnderTest.getDecision(new DecisionRequestParameters());
279
280         verify(mockedStdPolicyEngine).getDecision(any(DecisionRequestParameters.class));
281     }
282
283     @Test
284     public void testGetDecision_MetricsRequestParameters_StdPolicyEngineGetMetricsCalled() throws Exception {
285         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
286
287         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
288         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
289
290         objUnderTest.getMetrics(new MetricsRequestParameters());
291
292         verify(mockedStdPolicyEngine).getMetrics(any(MetricsRequestParameters.class));
293     }
294
295     @Test
296     public void testGetDecision_DictionaryParameters_StdPolicyEngineGetDictionaryItemCalled() throws Exception {
297         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
298
299         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
300         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
301
302         objUnderTest.getDictionaryItem(new DictionaryParameters());
303
304         verify(mockedStdPolicyEngine).getDictionaryItem(any(DictionaryParameters.class));
305     }
306
307     @Test
308     public void testCreateDictionaryItem_DictionaryParameters_StdPolicyEngineCreateDictionaryItemCalled()
309             throws Exception {
310         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
311
312         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
313         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
314
315         objUnderTest.createDictionaryItem(new DictionaryParameters());
316
317         verify(mockedStdPolicyEngine).createDictionaryItem(any(DictionaryParameters.class));
318     }
319
320     @Test
321     public void testUpdateDictionaryItem_DictionaryParameters_StdPolicyEngineDictionaryItemCalled() throws Exception {
322         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
323
324         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
325         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
326
327         objUnderTest.updateDictionaryItem(new DictionaryParameters());
328
329         verify(mockedStdPolicyEngine).updateDictionaryItem(any(DictionaryParameters.class));
330     }
331
332     @Test
333     public void testCreatePolicy_PolicyParameters_StdPolicyEngineCreatePolicyCalled() throws Exception {
334         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
335
336         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
337         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
338
339         objUnderTest.createPolicy(new PolicyParameters());
340
341         verify(mockedStdPolicyEngine).createPolicy(any(PolicyParameters.class));
342     }
343
344     @Test
345     public void testUpdatePolicy_PolicyParameters_StdPolicyEngineUpdatePolicyCalled() throws Exception {
346         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
347
348         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
349         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
350
351         objUnderTest.updatePolicy(new PolicyParameters());
352
353         verify(mockedStdPolicyEngine).updatePolicy(any(PolicyParameters.class));
354     }
355
356     @Test
357     public void testPushPolicy_PushPolicyParameters_StdPolicyEnginePushPolicyCalled() throws Exception {
358         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
359
360         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
361         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
362
363         objUnderTest.pushPolicy(new PushPolicyParameters());
364
365         verify(mockedStdPolicyEngine).pushPolicy(any(PushPolicyParameters.class));
366     }
367
368     @Test
369     public void testDeletePolicy_DeletePolicyParameters_StdPolicyEngineDeletePolicyCalled() throws Exception {
370         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
371
372         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
373         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
374
375         objUnderTest.deletePolicy(new DeletePolicyParameters());
376
377         verify(mockedStdPolicyEngine).deletePolicy(any(DeletePolicyParameters.class));
378     }
379
380     @Test
381     public void testPolicyEngineImport_DictionaryParameters_StdPolicyEnginePolicyEngineImportCalled() throws Exception {
382         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
383
384         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
385         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
386
387         objUnderTest.policyEngineImport(new ImportParameters());
388
389         verify(mockedStdPolicyEngine).policyEngineImport(any(ImportParameters.class));
390     }
391
392     @Test
393     public void testGetNotification_StdPolicyEngineGetNotificationCalled() throws Exception {
394         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
395
396         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
397         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
398
399         objUnderTest.getNotification();
400
401         verify(mockedStdPolicyEngine, Mockito.atLeastOnce()).getNotification();
402     }
403
404     @Test
405     public void testClearNotification_StdPolicyEngineClearNotificationCalled() throws Exception {
406         isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
407
408         final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
409         Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
410
411         objUnderTest.clearNotification();
412
413         verify(mockedStdPolicyEngine, Mockito.atLeastOnce()).stopNotification();
414     }
415
416     public void isFileAvailable(final String filePath) {
417         final Path file = Paths.get(filePath);
418         if (Files.notExists(file)) {
419             logger.error("File Doesn't Exist " + file.toString());
420             fail("File: " + filePath + " Not found");
421         }
422     }
423 }