2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.test;
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;
54 public class PolicyEngineTest {
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;
64 private final StdPolicyEngine mockedStdPolicyEngine = Mockito.mock(StdPolicyEngine.class);
67 public void testPolicyEngineForFail() {
68 PolicyEngine policyEngine = null;
70 policyEngine = new PolicyEngine(filePath);
71 } catch (final PolicyEngineException e) {
72 logger.warn(e.getMessage());
74 assertNull(policyEngine);
75 // Test even for this case.
78 policyEngine = new PolicyEngine(filePath);
79 } catch (final PolicyEngineException e) {
80 logger.warn(e.getMessage());
82 assertNull(policyEngine);
86 public void testPolicyEngineforPropertyFileError() {
87 filePath = "Test/config_error.property";
88 isFileAvailable(filePath);
90 policyEngine = new PolicyEngine(filePath);
91 } catch (final PolicyEngineException e) {
92 logger.warn(e.getMessage());
94 assertNull(policyEngine);
98 public void testPolicyEngineforPDPURLError() {
99 final String filePath = "Test/config_fail.properties";
100 isFileAvailable(filePath);
102 policyEngine = new PolicyEngine(filePath);
103 } catch (final PolicyEngineException e) {
104 logger.warn(e.getMessage());
106 assertNull(policyEngine);
110 public void testPolicyEngineForPass() {
111 final String filePath = TEST_CONFIG_PASS_PROPERTIES;
112 isFileAvailable(filePath);
114 policyEngine = new PolicyEngine(filePath);
115 } catch (final PolicyEngineException e) {
116 logger.warn(e.getMessage());
118 assertNotNull(policyEngine);
122 public void testPolicyEngineForUEBPass() {
123 final String filePath = "Test/config_UEB_pass.properties";
124 isFileAvailable(filePath);
126 policyEngine = new PolicyEngine(filePath);
127 } catch (final PolicyEngineException e) {
128 logger.warn(e.getMessage());
130 assertNotNull(policyEngine);
134 public void testPolicyEngineForUEBBadType() {
135 final String filePath = "Test/config_UEB_bad_type.properties";
136 isFileAvailable(filePath);
138 policyEngine = new PolicyEngine(filePath);
139 } catch (final PolicyEngineException e) {
140 logger.warn(e.getMessage());
142 assertNotNull(policyEngine);
146 public void testPolicyEngineForUEBBadServerType() {
147 final String filePath = "Test/config_UEB_badservers.properties";
148 isFileAvailable(filePath);
150 policyEngine = new PolicyEngine(filePath);
151 } catch (final PolicyEngineException e) {
152 logger.warn(e.getMessage());
154 assertNotNull(policyEngine);
158 public void testPolicyEngineNotficationAutoUEB() {
159 final String filePath = "Test/config_UEB_pass.properties";
160 isFileAvailable(filePath);
162 policyEngine = new PolicyEngine(filePath);
163 policyEngine.setScheme(NotificationScheme.AUTO_ALL_NOTIFICATIONS);
164 } catch (final PolicyEngineException e) {
165 logger.warn(e.getMessage());
167 assertNotNull(policyEngine);
171 public void testPolicyEngineNotficationAuto() {
172 final String filePath = TEST_CONFIG_PASS_PROPERTIES;
173 isFileAvailable(filePath);
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());
181 assertNotNull(policyEngine);
185 public void testGetConfig_ConfigRequestParameters_StdPolicyEngineGetConfigCalled()
186 throws PolicyEngineException, PolicyConfigException {
187 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
189 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
190 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
192 objUnderTest.getConfig(new ConfigRequestParameters());
194 verify(mockedStdPolicyEngine).getConfig(Mockito.any(ConfigRequestParameters.class));
199 public void testlistConfig_ConfigRequestParameters_StdPolicyEngineListConfigCalled()
200 throws PolicyEngineException, PolicyConfigException {
201 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
203 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
204 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
206 objUnderTest.listConfig(new ConfigRequestParameters());
208 verify(mockedStdPolicyEngine).listConfig(any(ConfigRequestParameters.class));
212 public void testSendEvent_EventAttributes_StdPolicyEngineSendEventCalled() throws Exception {
213 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
215 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
216 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
218 objUnderTest.sendEvent(Collections.emptyMap());
220 verify(mockedStdPolicyEngine).sendEvent(eq(Collections.emptyMap()), eq(null));
224 public void testSendEvent_EventAttributesWithUUID_StdPolicyEngineSendEventCalled() throws Exception {
225 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
227 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
228 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
230 objUnderTest.sendEvent(Collections.emptyMap(), REQUEST_UUID);
232 verify(mockedStdPolicyEngine).sendEvent(eq(Collections.emptyMap()), eq(REQUEST_UUID));
236 public void testSendEvent_EventRequestParameters_StdPolicyEngineSendEventCalled() throws Exception {
237 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
239 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
240 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
242 objUnderTest.sendEvent(new EventRequestParameters());
244 verify(mockedStdPolicyEngine).sendEvent(any(EventRequestParameters.class));
248 public void testGetDecision_RequestParameters_StdPolicyEngineGetDecisionCalled() throws Exception {
249 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
251 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
252 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
254 objUnderTest.getDecision(ONAP_NAME_VALUE, Collections.emptyMap());
256 verify(mockedStdPolicyEngine).getDecision(eq(ONAP_NAME_VALUE), eq(Collections.emptyMap()), eq(null));
260 public void testGetDecision_RequestParametersWithUUID_StdPolicyEngineGetDecisionCalled() throws Exception {
261 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
263 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
264 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
266 objUnderTest.getDecision(ONAP_NAME_VALUE, Collections.emptyMap(), REQUEST_UUID);
268 verify(mockedStdPolicyEngine).getDecision(eq(ONAP_NAME_VALUE), eq(Collections.emptyMap()), eq(REQUEST_UUID));
272 public void testGetDecision_DecisionRequestParameters_StdPolicyEngineGetDecisionCalled() throws Exception {
273 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
275 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
276 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
278 objUnderTest.getDecision(new DecisionRequestParameters());
280 verify(mockedStdPolicyEngine).getDecision(any(DecisionRequestParameters.class));
284 public void testGetDecision_MetricsRequestParameters_StdPolicyEngineGetMetricsCalled() throws Exception {
285 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
287 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
288 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
290 objUnderTest.getMetrics(new MetricsRequestParameters());
292 verify(mockedStdPolicyEngine).getMetrics(any(MetricsRequestParameters.class));
296 public void testGetDecision_DictionaryParameters_StdPolicyEngineGetDictionaryItemCalled() throws Exception {
297 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
299 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
300 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
302 objUnderTest.getDictionaryItem(new DictionaryParameters());
304 verify(mockedStdPolicyEngine).getDictionaryItem(any(DictionaryParameters.class));
308 public void testCreateDictionaryItem_DictionaryParameters_StdPolicyEngineCreateDictionaryItemCalled()
310 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
312 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
313 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
315 objUnderTest.createDictionaryItem(new DictionaryParameters());
317 verify(mockedStdPolicyEngine).createDictionaryItem(any(DictionaryParameters.class));
321 public void testUpdateDictionaryItem_DictionaryParameters_StdPolicyEngineDictionaryItemCalled() throws Exception {
322 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
324 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
325 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
327 objUnderTest.updateDictionaryItem(new DictionaryParameters());
329 verify(mockedStdPolicyEngine).updateDictionaryItem(any(DictionaryParameters.class));
333 public void testCreatePolicy_PolicyParameters_StdPolicyEngineCreatePolicyCalled() throws Exception {
334 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
336 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
337 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
339 objUnderTest.createPolicy(new PolicyParameters());
341 verify(mockedStdPolicyEngine).createPolicy(any(PolicyParameters.class));
345 public void testUpdatePolicy_PolicyParameters_StdPolicyEngineUpdatePolicyCalled() throws Exception {
346 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
348 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
349 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
351 objUnderTest.updatePolicy(new PolicyParameters());
353 verify(mockedStdPolicyEngine).updatePolicy(any(PolicyParameters.class));
357 public void testPushPolicy_PushPolicyParameters_StdPolicyEnginePushPolicyCalled() throws Exception {
358 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
360 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
361 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
363 objUnderTest.pushPolicy(new PushPolicyParameters());
365 verify(mockedStdPolicyEngine).pushPolicy(any(PushPolicyParameters.class));
369 public void testDeletePolicy_DeletePolicyParameters_StdPolicyEngineDeletePolicyCalled() throws Exception {
370 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
372 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
373 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
375 objUnderTest.deletePolicy(new DeletePolicyParameters());
377 verify(mockedStdPolicyEngine).deletePolicy(any(DeletePolicyParameters.class));
381 public void testPolicyEngineImport_DictionaryParameters_StdPolicyEnginePolicyEngineImportCalled() throws Exception {
382 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
384 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
385 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
387 objUnderTest.policyEngineImport(new ImportParameters());
389 verify(mockedStdPolicyEngine).policyEngineImport(any(ImportParameters.class));
393 public void testGetNotification_StdPolicyEngineGetNotificationCalled() throws Exception {
394 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
396 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
397 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
399 objUnderTest.getNotification();
401 verify(mockedStdPolicyEngine, Mockito.atLeastOnce()).getNotification();
405 public void testClearNotification_StdPolicyEngineClearNotificationCalled() throws Exception {
406 isFileAvailable(TEST_CONFIG_PASS_PROPERTIES);
408 final PolicyEngine objUnderTest = new PolicyEngine(TEST_CONFIG_PASS_PROPERTIES);
409 Whitebox.setInternalState(objUnderTest, STD_POLICY_ENGINE_LOCAL_VARIABLE, mockedStdPolicyEngine);
411 objUnderTest.clearNotification();
413 verify(mockedStdPolicyEngine, Mockito.atLeastOnce()).stopNotification();
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");