2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.std;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.spy;
33 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.CONFIG_NAME;
34 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.ONAP_NAME;
35 import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_ID_PROP_NAME;
36 import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_KEY_PROP_NAME;
37 import static org.onap.policy.std.utils.PolicyConfigConstants.COMMA;
38 import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_DICTIONARY_ITEM_RESOURCE_NAME;
39 import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_POLICY_RESOURCE_NAME;
40 import static org.onap.policy.std.utils.PolicyConfigConstants.DELETE_POLICY_RESOURCE_NAME;
41 import static org.onap.policy.std.utils.PolicyConfigConstants.DMAAP;
42 import static org.onap.policy.std.utils.PolicyConfigConstants.ENVIRONMENT_PROP_NAME;
43 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_CONFIG_RESOURCE_NAME;
44 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DECISION_RESOURCE_NAME;
45 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DICTIONARY_ITEMS_RESOURCE_NAME;
46 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_METRICS_RESOURCE_NAME;
47 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_SERVERS_PROP_NAME;
48 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TOPIC_PROP_NAME;
49 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TYPE_PROP_NAME;
50 import static org.onap.policy.std.utils.PolicyConfigConstants.PDP_URL_PROP_NAME;
51 import static org.onap.policy.std.utils.PolicyConfigConstants.POLICY_ENGINE_IMPORT_RESOURCE_NAME;
52 import static org.onap.policy.std.utils.PolicyConfigConstants.PUSH_POLICY_RESOURCE_NAME;
53 import static org.onap.policy.std.utils.PolicyConfigConstants.SEND_EVENT_RESOURCE_NAME;
54 import static org.onap.policy.std.utils.PolicyConfigConstants.UEB;
55 import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_DICTIONARY_ITEM_RESOURCE_NAME;
56 import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_POLICY_RESOURCE_NAME;
57 import java.io.BufferedWriter;
59 import java.io.IOException;
60 import java.io.StringReader;
61 import java.nio.file.Files;
62 import java.util.Arrays;
63 import java.util.Collection;
64 import java.util.Collections;
65 import java.util.Date;
66 import java.util.HashMap;
68 import java.util.Properties;
69 import java.util.UUID;
70 import javax.json.Json;
71 import javax.json.JsonObject;
72 import javax.json.JsonReader;
73 import org.junit.Rule;
74 import org.junit.Test;
75 import org.junit.rules.TemporaryFolder;
76 import org.junit.runner.RunWith;
77 import org.mockito.Mockito;
78 import org.onap.policy.api.ConfigRequestParameters;
79 import org.onap.policy.api.DecisionRequestParameters;
80 import org.onap.policy.api.DecisionResponse;
81 import org.onap.policy.api.DeletePolicyParameters;
82 import org.onap.policy.api.DictionaryParameters;
83 import org.onap.policy.api.DictionaryResponse;
84 import org.onap.policy.api.EventRequestParameters;
85 import org.onap.policy.api.ImportParameters;
86 import org.onap.policy.api.MetricsRequestParameters;
87 import org.onap.policy.api.MetricsResponse;
88 import org.onap.policy.api.NotificationScheme;
89 import org.onap.policy.api.PolicyChangeResponse;
90 import org.onap.policy.api.PolicyConfig;
91 import org.onap.policy.api.PolicyConfigException;
92 import org.onap.policy.api.PolicyConfigStatus;
93 import org.onap.policy.api.PolicyConfigType;
94 import org.onap.policy.api.PolicyDecision;
95 import org.onap.policy.api.PolicyDecisionException;
96 import org.onap.policy.api.PolicyEngineException;
97 import org.onap.policy.api.PolicyEventException;
98 import org.onap.policy.api.PolicyException;
99 import org.onap.policy.api.PolicyParameters;
100 import org.onap.policy.api.PolicyResponse;
101 import org.onap.policy.api.PolicyResponseStatus;
102 import org.onap.policy.api.PolicyType;
103 import org.onap.policy.api.PushPolicyParameters;
104 import org.onap.policy.models.APIDictionaryResponse;
105 import org.onap.policy.models.APIPolicyConfigResponse;
106 import org.onap.policy.std.utils.PolicyConfigConstants;
107 import org.powermock.core.classloader.annotations.PowerMockIgnore;
108 import org.powermock.core.classloader.annotations.PrepareForTest;
109 import org.powermock.modules.junit4.PowerMockRunner;
110 import org.springframework.http.HttpMethod;
111 import org.springframework.http.HttpStatus;
112 import org.springframework.http.ResponseEntity;
113 import org.springframework.web.client.HttpClientErrorException;
114 import org.springframework.web.client.RestClientException;
116 @RunWith(PowerMockRunner.class)
117 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
118 @PrepareForTest(StdPolicyEngine.class)
119 public class StdPolicyEngineTest {
121 private static final String ONAP_NAME_VAL = "ONAP_NAME";
122 private static final String POLICY_VERSION = "1.0.0";
123 private static final String POLICY_NAME = "ONAP";
124 private static final String COMMENTS = "";
125 private static final UUID REQUEST_UUID = UUID.randomUUID();
126 private static final String SERVER_NAME = "localhost.com";
127 private static final String PDP_PROP_VALUE = "http://localhost:8092/pdp/ , test, test";
128 private static final String PDP_PROP_VALUE_1 = "https://localhost:8091/pdp/ , onap, onap";
129 private static final String JSON_CONFIGURATION = "{\"name\":\"value\"}";
130 private static final String XML_CONFIGURATION =
131 "<map><entry><string>name</string><string>value</string></entry></map>";
134 public TemporaryFolder temporaryFolder = new TemporaryFolder();
136 private static final String CONFIG_PROPERTIES_FILE = "config_pass.properties";
137 private static final String INVALID_CONFIG_PROPERTY_FILE = "config_fail.properties";
140 public void testStdPolicyEngineInitialize_noException() throws Exception {
141 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
143 creatPropertyFile(file, getDefaultProperties());
145 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
146 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
147 assertEquals("TEST", StdPolicyEngine.getEnvironment());
148 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
149 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
150 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME),
151 policyEngine.getNotificationURLList());
155 public void testStdPolicyEngineWithPropertiesInitialize_noException() throws Exception {
156 final StdPolicyEngine policyEngine =
157 new StdPolicyEngine(getDefaultProperties(), (String) null);
158 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
159 assertEquals("TEST", StdPolicyEngine.getEnvironment());
160 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
161 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
162 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME),
163 policyEngine.getNotificationURLList());
167 public void testStdPolicyEngineInitializeWithSingleServerName_noException() throws Exception {
168 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
170 final Properties properties = new Properties();
171 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
172 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
173 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
174 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
175 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
176 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
177 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
178 creatPropertyFile(file, properties);
180 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
181 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
182 assertEquals(Arrays.asList(SERVER_NAME), policyEngine.getNotificationURLList());
186 public void testStdPolicyEngineInitializeWithSingleNotificationType_noException()
188 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
190 final Properties properties = new Properties();
191 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
192 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
193 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
194 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
195 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
196 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
197 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
198 creatPropertyFile(file, properties);
200 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
201 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
202 assertEquals(Arrays.asList(UEB), policyEngine.getNotificationType());
205 @Test(expected = PolicyEngineException.class)
206 public void testStdPolicyEngineInitialize_InvalidPropertyFile_Exception()
207 throws PolicyEngineException {
208 new StdPolicyEngine("Invalid.properties", (String) null);
211 @Test(expected = PolicyEngineException.class)
212 public void testStdPolicyEngineInitialize_InvalidPropertyFileWithExt_Exception()
214 final File emptyFile = temporaryFolder.newFile("EmptyFile.txt");
215 new StdPolicyEngine(emptyFile.toString(), (String) null);
218 @Test(expected = PolicyEngineException.class)
219 public void testStdPolicyEngineInitialize_NullArguments_Exception() throws Exception {
220 new StdPolicyEngine((String) null, (String) null);
223 @Test(expected = PolicyEngineException.class)
224 public void testStdPolicyEngineWithPropertiesInitialize_NullArguments_Exception()
226 new StdPolicyEngine((Properties) null, (String) null);
229 @Test(expected = PolicyEngineException.class)
230 public void testStdPolicyEngineInitialize_PropertyFileMissingMandatoryProperties_Exception()
232 final File file = temporaryFolder.newFile(INVALID_CONFIG_PROPERTY_FILE);
233 final Properties properties = new Properties();
234 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
235 creatPropertyFile(file, properties);
237 new StdPolicyEngine(file.toString(), (String) null);
241 public void testStdPolicyEngineInitialize_MultiplePdp_noException() throws Exception {
242 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
244 final Properties properties = new Properties();
245 properties.setProperty(PDP_URL_PROP_NAME,
246 PDP_PROP_VALUE + PolicyConfigConstants.SEMICOLLON + PDP_PROP_VALUE_1);
247 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
248 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
249 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
250 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
251 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
252 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
253 creatPropertyFile(file, properties);
255 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
256 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
257 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
258 StdPolicyEngine.rotatePDPList();
259 assertEquals("https://localhost:8091/pdp/", StdPolicyEngine.getPDPURL());
260 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
261 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME),
262 policyEngine.getNotificationURLList());
265 @Test(expected = PolicyEngineException.class)
266 public void testStdPolicyEngineInitialize_NoPDP_noException() throws Exception {
267 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
269 final Properties properties = new Properties();
270 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
271 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
272 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
273 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
274 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
275 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
276 creatPropertyFile(file, properties);
278 new StdPolicyEngine(file.toString(), (String) null);
282 public void testStdPolicyEngineInitializeNotificationTypeDMMAP_noException() throws Exception {
283 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
285 final Properties properties = new Properties();
286 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
287 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
288 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
289 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, DMAAP);
290 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
291 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
292 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
293 creatPropertyFile(file, properties);
295 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
296 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
298 assertEquals(Arrays.asList(DMAAP), policyEngine.getNotificationType());
303 public void testStdPolicyEngineSendEvent_noException() throws Exception {
304 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
305 doReturn(new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED))
306 .when(spyPolicyEngine)
307 .callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
309 final Collection<PolicyResponse> actualPolicyResponses =
310 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
312 assertEquals(1, actualPolicyResponses.size());
316 @Test(expected = PolicyEventException.class)
317 public void testStdPolicyEngineSendEvent_NullEventRequestParameters_Exception()
319 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
320 spyPolicyEngine.sendEvent((EventRequestParameters) null);
323 @Test(expected = PolicyEventException.class)
324 public void testStdPolicyEngineSendEvent_EventRequestParameters_CallPDPThrow401Exception_Exception()
326 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
327 Mockito.doThrow(new RuntimeException(new RestClientException("Error 401")))
328 .when(spyPolicyEngine).callNewPDP(any(), any(), any(), any());
329 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
333 public void testStdPolicyEngineSendEvent_EventRequestParameters_noException() throws Exception {
334 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
336 final ResponseEntity<StdPolicyResponse[]> stubbedResponse =
337 new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED);
339 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(SEND_EVENT_RESOURCE_NAME),
340 any(), any(), any());
342 final EventRequestParameters eventRequestParameters = new EventRequestParameters();
343 eventRequestParameters.setEventAttributes(Collections.emptyMap());
344 eventRequestParameters.setRequestID(REQUEST_UUID);
346 final Collection<PolicyResponse> actualPolicyResponses =
347 spyPolicyEngine.sendEvent(eventRequestParameters);
349 assertEquals(1, actualPolicyResponses.size());
354 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigJSON_noException()
356 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
358 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
359 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION),
360 HttpStatus.ACCEPTED);
362 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
363 eq(HttpMethod.POST), any(), any());
365 final Collection<PolicyConfig> actualPolicyResponses =
366 spyPolicyEngine.getConfig(new ConfigRequestParameters());
368 assertEquals(1, actualPolicyResponses.size());
370 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
371 assertNotNull(actualPolicyConfig.toJSON());
376 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigOther_noException()
378 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
380 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
381 getAPIPolicyConfigResponse(PolicyType.OTHER, COMMENTS), HttpStatus.ACCEPTED);
383 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
384 eq(HttpMethod.POST), any(), any());
386 final Collection<PolicyConfig> actualPolicyResponses =
387 spyPolicyEngine.getConfig(new ConfigRequestParameters());
389 assertEquals(1, actualPolicyResponses.size());
391 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
392 assertNotNull(actualPolicyConfig.toOther());
397 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigXML_noException()
399 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
401 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
402 getAPIPolicyConfigResponse(PolicyType.XML, XML_CONFIGURATION), HttpStatus.ACCEPTED);
404 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
405 eq(HttpMethod.POST), any(), any());
407 final Collection<PolicyConfig> actualPolicyResponses =
408 spyPolicyEngine.getConfig(new ConfigRequestParameters());
410 assertEquals(1, actualPolicyResponses.size());
412 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
413 assertNotNull(actualPolicyConfig.toXML());
418 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigProperties_noException()
420 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
422 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
423 getAPIPolicyConfigResponse(PolicyType.PROPERTIES, COMMENTS), HttpStatus.ACCEPTED);
425 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
426 eq(HttpMethod.POST), any(), any());
428 final Collection<PolicyConfig> actualPolicyResponses =
429 spyPolicyEngine.getConfig(new ConfigRequestParameters());
431 assertEquals(1, actualPolicyResponses.size());
433 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
434 assertNotNull(actualPolicyConfig.toProperties());
438 @Test(expected = PolicyConfigException.class)
439 public void testStdPolicyEngineSendEvent_ConfigRequestParameters_CallPDPThrow404Exception_Exception()
441 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
442 doThrow(new RuntimeException(new RestClientException("Error 404"))).when(spyPolicyEngine)
443 .callNewPDP(any(), any(), any(), any());
444 spyPolicyEngine.getConfig(new ConfigRequestParameters());
448 public void testStdPolicyEngineListConfig_ConfigRequestParametersPolicyConfigProperties_noException()
450 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
452 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
453 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION);
455 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
456 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
458 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
459 eq(HttpMethod.POST), any(), any());
461 final Collection<String> actualResponse =
462 spyPolicyEngine.listConfig(new ConfigRequestParameters());
463 assertEquals(1, actualResponse.size());
464 assertNotNull(actualResponse.iterator().next());
469 public void testStdPolicyEngineListConfig_ConfigRequestParametersMessageConfigContainsPE300_noException()
471 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
473 final APIPolicyConfigResponse[] apiPolicyConfigResponse = getAPIPolicyConfigResponse(
474 PolicyType.JSON, JSON_CONFIGURATION, PolicyConfigConstants.PE300);
476 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
477 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
479 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
480 eq(HttpMethod.POST), any(), any());
482 final Collection<String> actualResponse =
483 spyPolicyEngine.listConfig(new ConfigRequestParameters());
485 assertEquals(1, actualResponse.size());
490 public void testStdPolicyEngineListConfig_ConfigRequestParametersWithTestProperty_noException()
492 final Properties defaultProperties = getDefaultProperties();
493 defaultProperties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "test");
494 final StdPolicyEngine spyPolicyEngine =
495 getSpyPolicyEngine("test" + CONFIG_PROPERTIES_FILE, defaultProperties);
497 final Collection<String> actualResponse =
498 spyPolicyEngine.listConfig(new ConfigRequestParameters());
500 assertEquals(1, actualResponse.size());
505 public void testStdPolicyEnginGetDecision_PolicyDecision_noException() throws Exception {
507 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
509 final ResponseEntity<StdDecisionResponse> stubbedResponse =
510 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
512 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME),
513 eq(HttpMethod.POST), any(), any());
515 final DecisionResponse actualResponse =
516 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
518 assertNotNull(actualResponse);
521 @Test(expected = PolicyDecisionException.class)
522 public void testStdPolicyEngineGetDecision_PolicyDecision_CallPDPThrow400Exception_Exception()
524 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
525 doThrow(new RuntimeException(new RestClientException("Error 400"))).when(spyPolicyEngine)
526 .callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
527 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
531 public void testStdPolicyEnginGetDecision_DecisionRequestParameters_noException()
534 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
536 final ResponseEntity<StdDecisionResponse> stubbedResponse =
537 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
539 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME),
540 eq(HttpMethod.POST), any(), any());
542 final DecisionRequestParameters requestParameters = new DecisionRequestParameters();
543 requestParameters.setOnapName(ONAP_NAME_VAL);
544 requestParameters.setRequestID(REQUEST_UUID);
545 requestParameters.setDecisionAttributes(Collections.emptyMap());
547 final DecisionResponse actualResponse = spyPolicyEngine.getDecision(requestParameters);
549 assertNotNull(actualResponse);
552 @Test(expected = PolicyDecisionException.class)
553 public void ttestStdPolicyEnginGetDecision_NullDecisionRequestParameters_Exception()
555 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
556 spyPolicyEngine.getDecision((DecisionRequestParameters) null);
560 public void testStdPolicyEnginGetMetrics_MetricsRequestParameters_noException()
563 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
565 final ResponseEntity<String> stubbedResponse =
566 new ResponseEntity<>("Metrics", HttpStatus.ACCEPTED);
568 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME),
569 eq(HttpMethod.GET), any(), any());
571 final MetricsResponse actualResponse =
572 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
573 assertNotNull(actualResponse);
574 assertEquals(HttpStatus.ACCEPTED.value(), actualResponse.getResponseCode());
579 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowHttpException_ResponseWithHttpCode()
581 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
582 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY)))
583 .when(spyPolicyEngine)
584 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
586 final MetricsResponse actualResponse =
587 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
588 assertNotNull(actualResponse);
589 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
593 @Test(expected = PolicyException.class)
594 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowPolicyException_Exception()
596 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
597 doThrow(PolicyException.class).when(spyPolicyEngine)
598 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
600 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
605 public void testStdPolicyEnginPushPolicy_PushPolicyParameters_noException() throws Exception {
607 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
609 final ResponseEntity<String> stubbedResponse =
610 new ResponseEntity<>("Successful", HttpStatus.OK);
612 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
613 eq(HttpMethod.PUT), any(), any());
615 final PolicyChangeResponse actualResponse =
616 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
618 assertNotNull(actualResponse);
619 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
623 public void testStdPolicyEnginePushPolicy_PushPolicyParametersThrowsHttpClientErrorException_ResponseWithHttpCode()
625 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
626 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY)))
627 .when(spyPolicyEngine)
628 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
630 final PolicyChangeResponse actualResponse =
631 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
633 assertNotNull(actualResponse);
634 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
638 @Test(expected = PolicyException.class)
639 public void testStdPolicyEnginePushPolicy_PushPolicyParameters_Exception() throws Exception {
640 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
641 doThrow(PolicyException.class).when(spyPolicyEngine)
642 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
644 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
649 public void testStdPolicyEnginDeletePolicy_DeletePolicyParameters_noException()
652 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
654 final ResponseEntity<String> stubbedResponse =
655 new ResponseEntity<>("Successful", HttpStatus.OK);
657 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(DELETE_POLICY_RESOURCE_NAME),
658 eq(HttpMethod.DELETE), any(), any());
660 final PolicyChangeResponse actualResponse =
661 spyPolicyEngine.deletePolicy(new DeletePolicyParameters());
663 assertNotNull(actualResponse);
664 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
668 public void testStdPolicyEnginGetDictionaryItem_DictionaryParameters_noException()
671 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
673 final ResponseEntity<APIDictionaryResponse> stubbedResponse =
674 new ResponseEntity<>(getAPIDictionaryResponse(), HttpStatus.OK);
676 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
677 eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
679 final DictionaryResponse actualResponse =
680 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
682 assertNotNull(actualResponse);
686 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp400ExceptionThrown_ResponseWithHttpCode()
688 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
689 doThrow(new RuntimeException(
690 new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Error 400")))
691 .when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
692 eq(HttpMethod.POST), any(), any());
694 final DictionaryResponse actualResponse =
695 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
697 assertNotNull(actualResponse);
698 assertEquals(HttpStatus.BAD_REQUEST.value(), actualResponse.getResponseCode());
703 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp401ExceptionThrown_ResponseWithHttpCode()
705 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
706 doThrow(new RuntimeException(
707 new HttpClientErrorException(HttpStatus.UNAUTHORIZED, "Error 401")))
708 .when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
709 eq(HttpMethod.POST), any(), any());
711 final DictionaryResponse actualResponse =
712 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
714 assertNotNull(actualResponse);
715 assertEquals(HttpStatus.UNAUTHORIZED.value(), actualResponse.getResponseCode());
720 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithRunTimeExceptionThrown_ResponseWithHttpCode()
722 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
723 doThrow(new RuntimeException(
724 new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error")))
725 .when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
726 eq(HttpMethod.POST), any(), any());
728 final DictionaryResponse actualResponse =
729 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
731 assertNotNull(actualResponse);
732 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), actualResponse.getResponseCode());
737 public void testStdPolicyEnginCreateDictionaryItem_DictionaryParameters_noException()
740 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
742 final ResponseEntity<String> stubbedResponse =
743 new ResponseEntity<>("Successful", HttpStatus.OK);
745 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
746 eq(CREATE_DICTIONARY_ITEM_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
748 final PolicyChangeResponse actualResponse =
749 spyPolicyEngine.createDictionaryItem(new DictionaryParameters());
751 assertNotNull(actualResponse);
752 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
756 public void testStdPolicyEnginUpdateDictionaryItem_DictionaryParameters_noException()
759 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
761 final ResponseEntity<String> stubbedResponse =
762 new ResponseEntity<>("Successful", HttpStatus.OK);
764 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
765 eq(UPDATE_DICTIONARY_ITEM_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
767 final PolicyChangeResponse actualResponse =
768 spyPolicyEngine.updateDictionaryItem(new DictionaryParameters());
770 assertNotNull(actualResponse);
771 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
775 public void testStdPolicyEnginPolicyEngineImport_ImportParameters_noException()
777 final File emptyfile = temporaryFolder.newFile("emptyFile.txt");
778 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
780 final ResponseEntity<String> stubbedResponse =
781 new ResponseEntity<>("Successful", HttpStatus.OK);
783 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
784 eq(POLICY_ENGINE_IMPORT_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
786 final ImportParameters importParameters = new ImportParameters();
787 importParameters.setFilePath(emptyfile.toString());
788 final PolicyChangeResponse actualResponse =
789 spyPolicyEngine.policyEngineImport(importParameters);
791 assertNotNull(actualResponse);
792 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
796 public void testStdPolicyEnginCreatePolicy_PolicyParameters_noException() throws Exception {
797 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
799 final ResponseEntity<String> stubbedResponse =
800 new ResponseEntity<>("Successful", HttpStatus.OK);
802 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_POLICY_RESOURCE_NAME),
803 eq(HttpMethod.PUT), any(), any());
805 final PolicyChangeResponse actualResponse =
806 spyPolicyEngine.createPolicy(new PolicyParameters());
808 assertNotNull(actualResponse);
809 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
813 public void testStdPolicyEnginUpdatePolicy_PolicyParameters_noException() throws Exception {
814 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
816 final ResponseEntity<String> stubbedResponse =
817 new ResponseEntity<>("Successful", HttpStatus.OK);
819 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
820 eq(HttpMethod.PUT), any(), any());
822 final PolicyChangeResponse actualResponse =
823 spyPolicyEngine.updatePolicy(new PolicyParameters());
825 assertNotNull(actualResponse);
826 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
829 @Test(expected = PolicyException.class)
830 public void testStdPolicyEnginPushPolicy_NullValues_Exception() throws Exception {
831 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
833 spyPolicyEngine.pushPolicy(null, null, null, null, null);
836 @Test(expected = PolicyException.class)
837 public void testStdPolicyEnginPushPolicy_EmptyPolicyScope_Exception() throws Exception {
838 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
840 spyPolicyEngine.pushPolicy("", null, null, null, null);
843 @Test(expected = PolicyException.class)
844 public void testStdPolicyEnginPushPolicy_EmptyPolicyName_Exception() throws Exception {
845 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
847 spyPolicyEngine.pushPolicy("POLICY_SCOPE", "", null, null, null);
851 public void testStdPolicyEnginPushPolicy_PolicyParameters_noException() throws Exception {
852 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
854 final ResponseEntity<String> stubbedResponse =
855 new ResponseEntity<>("Successful", HttpStatus.OK);
857 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
858 eq(HttpMethod.PUT), any(), any());
860 final String actualResponse = spyPolicyEngine.pushPolicy("POLICY_SCOPE", ONAP_NAME_VAL,
861 "POLICY_TYPE", "POLICY_GROUP", REQUEST_UUID);
863 assertNotNull(actualResponse);
867 public void testStdPolicyEnginCreateUpdateConfigPolicy_PolicyParameters_noException()
869 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
871 final ResponseEntity<String> stubbedResponse =
872 new ResponseEntity<>("Successful", HttpStatus.OK);
874 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
875 eq(HttpMethod.PUT), any(), any());
877 final String actualResponse = spyPolicyEngine.createUpdateConfigPolicy("POLICY_NAME",
878 ONAP_NAME_VAL, ONAP_NAME_VAL, "CONFIG_NAME", Collections.emptyMap(),
879 PolicyType.JSON.toString().toUpperCase(), "", "POLICY_SCOPE", REQUEST_UUID, "", "",
880 "", new Date().toString(), true);
882 assertNotNull(actualResponse);
885 @Test(expected = PolicyException.class)
886 public void testStdPolicyEnginCreateUpdateConfigPolicy_NullPolicyName_Exception()
888 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
890 spyPolicyEngine.createUpdateConfigPolicy(null, null, null, null, null, null, null, null,
891 null, null, null, null, null, true);
895 public void testStdPolicyEnginCreateUpdateConfigFirewallPolicy_PolicyParameters_noException()
897 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
899 final ResponseEntity<String> stubbedResponse =
900 new ResponseEntity<>("Successful", HttpStatus.OK);
902 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
903 eq(HttpMethod.PUT), any(), any());
905 final String actualResponse = spyPolicyEngine.createUpdateConfigFirewallPolicy(
906 "POLICY_NAME", getJsonObject(JSON_CONFIGURATION), "POLICY_SCOPE", REQUEST_UUID, "",
907 "", "", new Date().toString(), true);
909 assertNotNull(actualResponse);
912 private JsonObject getJsonObject(final String jsonString) {
913 try (final JsonReader jsonReader = Json.createReader(new StringReader(jsonString));) {
914 return jsonReader.readObject();
918 private StdPolicyEngine getSpyPolicyEngine() throws IOException, PolicyEngineException {
919 return getSpyPolicyEngine(CONFIG_PROPERTIES_FILE, getDefaultProperties());
922 private StdPolicyEngine getSpyPolicyEngine(final String filename, final Properties properties)
923 throws IOException, PolicyEngineException {
924 final File file = temporaryFolder.newFile(filename);
926 creatPropertyFile(file, properties);
928 final StdPolicyEngine spyPolicyEngine =
929 spy(new StdPolicyEngine(file.toString(), (String) null));
930 spyPolicyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
931 return spyPolicyEngine;
934 private Properties getDefaultProperties() {
935 final Properties properties = new Properties();
936 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
937 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
938 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
939 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
940 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
941 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
942 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
943 properties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "false");
947 private StdDecisionResponse getStdDecisionResponse() {
948 final StdDecisionResponse response = new StdDecisionResponse();
949 response.setDecision(PolicyDecision.PERMIT);
950 response.setDetails(PolicyDecision.PERMIT.name());
955 private APIDictionaryResponse getAPIDictionaryResponse() {
956 final APIDictionaryResponse response = new APIDictionaryResponse();
957 response.setResponseCode(0);
958 response.setResponseMessage("");
959 response.setDictionaryData(Collections.<String, String>emptyMap());
960 response.setDictionaryJson(Collections.<String, String>emptyMap());
964 private StdPolicyResponse[] getStdPolicyResponse() {
965 final StdPolicyResponse response = new StdPolicyResponse();
966 response.setPolicyResponseStatus(PolicyResponseStatus.ACTION_TAKEN);
967 return new StdPolicyResponse[] {response};
970 private void creatPropertyFile(final File file, final Properties properties)
972 try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) {
973 properties.store(bufferedWriter, COMMENTS);
977 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
978 final String configuration) {
980 return getAPIPolicyConfigResponse(policyType, configuration, null);
983 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
984 final String configuration, final String policyConfigMessage) {
985 final APIPolicyConfigResponse configResponse = new APIPolicyConfigResponse();
986 configResponse.setConfig(configuration);
987 configResponse.setMatchingConditions(getMatchingConditions());
988 configResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED);
989 configResponse.setPolicyName(POLICY_NAME);
990 configResponse.setPolicyType(PolicyConfigType.BRMS_RAW);
991 configResponse.setType(policyType);
992 configResponse.setPolicyVersion(POLICY_VERSION);
993 configResponse.setProperty(Collections.emptyMap());
994 configResponse.setPolicyConfigMessage(policyConfigMessage);
996 return new APIPolicyConfigResponse[] {configResponse};
999 private Map<String, String> getMatchingConditions() {
1000 final Map<String, String> attributes = new HashMap<>();
1001 attributes.put(ONAP_NAME, POLICY_NAME);
1002 attributes.put(CONFIG_NAME, "Configuration_name");