2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. 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.std;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.spy;
30 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.CONFIG_NAME;
31 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.ONAP_NAME;
32 import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_ID_PROP_NAME;
33 import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_KEY_PROP_NAME;
34 import static org.onap.policy.std.utils.PolicyConfigConstants.COMMA;
35 import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_DICTIONARY_ITEM_RESOURCE_NAME;
36 import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_POLICY_RESOURCE_NAME;
37 import static org.onap.policy.std.utils.PolicyConfigConstants.DELETE_POLICY_RESOURCE_NAME;
38 import static org.onap.policy.std.utils.PolicyConfigConstants.DMAAP;
39 import static org.onap.policy.std.utils.PolicyConfigConstants.ENVIRONMENT_PROP_NAME;
40 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_CONFIG_RESOURCE_NAME;
41 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DECISION_RESOURCE_NAME;
42 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DICTIONARY_ITEMS_RESOURCE_NAME;
43 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_METRICS_RESOURCE_NAME;
44 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_SERVERS_PROP_NAME;
45 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TOPIC_PROP_NAME;
46 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TYPE_PROP_NAME;
47 import static org.onap.policy.std.utils.PolicyConfigConstants.PDP_URL_PROP_NAME;
48 import static org.onap.policy.std.utils.PolicyConfigConstants.POLICY_ENGINE_IMPORT_RESOURCE_NAME;
49 import static org.onap.policy.std.utils.PolicyConfigConstants.PUSH_POLICY_RESOURCE_NAME;
50 import static org.onap.policy.std.utils.PolicyConfigConstants.SEND_EVENT_RESOURCE_NAME;
51 import static org.onap.policy.std.utils.PolicyConfigConstants.UEB;
52 import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_DICTIONARY_ITEM_RESOURCE_NAME;
53 import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_POLICY_RESOURCE_NAME;
54 import java.io.BufferedWriter;
56 import java.io.IOException;
57 import java.io.StringReader;
58 import java.nio.file.Files;
59 import java.util.Arrays;
60 import java.util.Collection;
61 import java.util.Collections;
62 import java.util.Date;
63 import java.util.HashMap;
65 import java.util.Properties;
66 import java.util.UUID;
67 import javax.json.Json;
68 import javax.json.JsonObject;
69 import javax.json.JsonReader;
70 import org.junit.Rule;
71 import org.junit.Test;
72 import org.junit.rules.TemporaryFolder;
73 import org.junit.runner.RunWith;
74 import org.mockito.Mockito;
75 import org.onap.policy.api.ConfigRequestParameters;
76 import org.onap.policy.api.DecisionRequestParameters;
77 import org.onap.policy.api.DecisionResponse;
78 import org.onap.policy.api.DeletePolicyParameters;
79 import org.onap.policy.api.DictionaryParameters;
80 import org.onap.policy.api.DictionaryResponse;
81 import org.onap.policy.api.EventRequestParameters;
82 import org.onap.policy.api.ImportParameters;
83 import org.onap.policy.api.MetricsRequestParameters;
84 import org.onap.policy.api.MetricsResponse;
85 import org.onap.policy.api.NotificationScheme;
86 import org.onap.policy.api.PolicyChangeResponse;
87 import org.onap.policy.api.PolicyConfig;
88 import org.onap.policy.api.PolicyConfigException;
89 import org.onap.policy.api.PolicyConfigStatus;
90 import org.onap.policy.api.PolicyConfigType;
91 import org.onap.policy.api.PolicyDecision;
92 import org.onap.policy.api.PolicyDecisionException;
93 import org.onap.policy.api.PolicyEngineException;
94 import org.onap.policy.api.PolicyEventException;
95 import org.onap.policy.api.PolicyException;
96 import org.onap.policy.api.PolicyParameters;
97 import org.onap.policy.api.PolicyResponse;
98 import org.onap.policy.api.PolicyResponseStatus;
99 import org.onap.policy.api.PolicyType;
100 import org.onap.policy.api.PushPolicyParameters;
101 import org.onap.policy.models.APIDictionaryResponse;
102 import org.onap.policy.models.APIPolicyConfigResponse;
103 import org.onap.policy.std.utils.PolicyConfigConstants;
104 import org.powermock.modules.junit4.PowerMockRunner;
105 import org.springframework.http.HttpMethod;
106 import org.springframework.http.HttpStatus;
107 import org.springframework.http.ResponseEntity;
108 import org.springframework.web.client.HttpClientErrorException;
109 import org.springframework.web.client.RestClientException;
111 @RunWith(PowerMockRunner.class)
112 public class StdPolicyEngineTest {
114 private static final String ONAP_NAME_VAL = "ONAP_NAME";
115 private static final String POLICY_VERSION = "1.0.0";
116 private static final String POLICY_NAME = "ONAP";
117 private static final String COMMENTS = "";
118 private static final UUID REQUEST_UUID = UUID.randomUUID();
119 private static final String SERVER_NAME = "localhost.com";
120 private static final String PDP_PROP_VALUE = "http://localhost:8092/pdp/ , test, test";
121 private static final String PDP_PROP_VALUE_1 = "https://localhost:8091/pdp/ , onap, onap";
122 private static final String JSON_CONFIGURATION = "{\"name\":\"value\"}";
123 private static final String XML_CONFIGURATION =
124 "<map><entry><string>name</string><string>value</string></entry></map>";
127 public TemporaryFolder temporaryFolder = new TemporaryFolder();
129 private static final String CONFIG_PROPERTIES_FILE = "config_pass.properties";
130 private static final String INVALID_CONFIG_PROPERTY_FILE = "config_fail.properties";
133 public void testStdPolicyEngineInitialize_noException() throws Exception {
134 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
136 creatPropertyFile(file, getDefaultProperties());
138 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
139 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
140 assertEquals("TEST", StdPolicyEngine.getEnvironment());
141 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
142 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
143 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
147 public void testStdPolicyEngineWithPropertiesInitialize_noException() throws Exception {
148 final StdPolicyEngine policyEngine = new StdPolicyEngine(getDefaultProperties(), (String) null);
149 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
150 assertEquals("TEST", StdPolicyEngine.getEnvironment());
151 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
152 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
153 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
157 public void testStdPolicyEngineInitializeWithSingleServerName_noException() throws Exception {
158 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
160 final Properties properties = new Properties();
161 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
162 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
163 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
164 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
165 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
166 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
167 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
168 creatPropertyFile(file, properties);
170 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
171 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
172 assertEquals(Arrays.asList(SERVER_NAME), policyEngine.getNotificationURLList());
176 public void testStdPolicyEngineInitializeWithSingleNotificationType_noException()
178 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
180 final Properties properties = new Properties();
181 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
182 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
183 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
184 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
185 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
186 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
187 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
188 creatPropertyFile(file, properties);
190 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
191 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
192 assertEquals(Arrays.asList(UEB), policyEngine.getNotificationType());
195 @Test(expected = PolicyEngineException.class)
196 public void testStdPolicyEngineInitialize_InvalidPropertyFile_Exception()
197 throws PolicyEngineException {
198 new StdPolicyEngine("Invalid.properties", (String) null);
201 @Test(expected = PolicyEngineException.class)
202 public void testStdPolicyEngineInitialize_InvalidPropertyFileWithExt_Exception()
204 final File emptyFile = temporaryFolder.newFile("EmptyFile.txt");
205 new StdPolicyEngine(emptyFile.toString(), (String) null);
208 @Test(expected = PolicyEngineException.class)
209 public void testStdPolicyEngineInitialize_NullArguments_Exception() throws Exception {
210 new StdPolicyEngine((String) null, (String) null);
213 @Test(expected = PolicyEngineException.class)
214 public void testStdPolicyEngineWithPropertiesInitialize_NullArguments_Exception()
216 new StdPolicyEngine((Properties) null, (String) null);
219 @Test(expected = PolicyEngineException.class)
220 public void testStdPolicyEngineInitialize_PropertyFileMissingMandatoryProperties_Exception()
222 final File file = temporaryFolder.newFile(INVALID_CONFIG_PROPERTY_FILE);
223 final Properties properties = new Properties();
224 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
225 creatPropertyFile(file, properties);
227 new StdPolicyEngine(file.toString(), (String) null);
231 public void testStdPolicyEngineInitialize_MultiplePdp_noException() throws Exception {
232 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
234 final Properties properties = new Properties();
235 properties.setProperty(PDP_URL_PROP_NAME,
236 PDP_PROP_VALUE + PolicyConfigConstants.SEMICOLLON + PDP_PROP_VALUE_1);
237 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
238 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
239 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
240 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
241 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
242 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
243 creatPropertyFile(file, properties);
245 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
246 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
247 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
248 StdPolicyEngine.rotatePDPList();
249 assertEquals("https://localhost:8091/pdp/", StdPolicyEngine.getPDPURL());
250 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
251 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
254 @Test(expected = PolicyEngineException.class)
255 public void testStdPolicyEngineInitialize_NoPDP_noException() throws Exception {
256 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
258 final Properties properties = new Properties();
259 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
260 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
261 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
262 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
263 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
264 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
265 creatPropertyFile(file, properties);
267 new StdPolicyEngine(file.toString(), (String) null);
271 public void testStdPolicyEngineInitializeNotificationTypeDMMAP_noException() throws Exception {
272 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
274 final Properties properties = new Properties();
275 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
276 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
277 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
278 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, DMAAP);
279 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
280 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
281 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
282 creatPropertyFile(file, properties);
284 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
285 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
287 assertEquals(Arrays.asList(DMAAP), policyEngine.getNotificationType());
292 public void testStdPolicyEngineSendEvent_noException() throws Exception {
293 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
294 doReturn(new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED))
295 .when(spyPolicyEngine).callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
297 final Collection<PolicyResponse> actualPolicyResponses =
298 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
300 assertEquals(1, actualPolicyResponses.size());
304 @Test(expected = PolicyEventException.class)
305 public void testStdPolicyEngineSendEvent_NullEventRequestParameters_Exception() throws Exception {
306 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
307 spyPolicyEngine.sendEvent((EventRequestParameters) null);
310 @Test(expected = PolicyEventException.class)
311 public void testStdPolicyEngineSendEvent_EventRequestParameters_CallPDPThrow401Exception_Exception()
313 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
314 Mockito.doThrow(new RuntimeException(new RestClientException("Error 401")))
315 .when(spyPolicyEngine).callNewPDP(any(), any(), any(), any());
316 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
320 public void testStdPolicyEngineSendEvent_EventRequestParameters_noException() throws Exception {
321 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
323 final ResponseEntity<StdPolicyResponse[]> stubbedResponse =
324 new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED);
326 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(),
329 final EventRequestParameters eventRequestParameters = new EventRequestParameters();
330 eventRequestParameters.setEventAttributes(Collections.emptyMap());
331 eventRequestParameters.setRequestID(REQUEST_UUID);
333 final Collection<PolicyResponse> actualPolicyResponses =
334 spyPolicyEngine.sendEvent(eventRequestParameters);
336 assertEquals(1, actualPolicyResponses.size());
341 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigJSON_noException()
343 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
345 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
346 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION), HttpStatus.ACCEPTED);
348 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
349 eq(HttpMethod.POST), any(), any());
351 final Collection<PolicyConfig> actualPolicyResponses =
352 spyPolicyEngine.getConfig(new ConfigRequestParameters());
354 assertEquals(1, actualPolicyResponses.size());
356 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
357 assertNotNull(actualPolicyConfig.toJSON());
362 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigOther_noException()
364 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
366 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
367 getAPIPolicyConfigResponse(PolicyType.OTHER, COMMENTS), HttpStatus.ACCEPTED);
369 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
370 eq(HttpMethod.POST), any(), any());
372 final Collection<PolicyConfig> actualPolicyResponses =
373 spyPolicyEngine.getConfig(new ConfigRequestParameters());
375 assertEquals(1, actualPolicyResponses.size());
377 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
378 assertNotNull(actualPolicyConfig.toOther());
383 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigXML_noException()
385 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
387 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
388 getAPIPolicyConfigResponse(PolicyType.XML, XML_CONFIGURATION), HttpStatus.ACCEPTED);
390 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
391 eq(HttpMethod.POST), any(), any());
393 final Collection<PolicyConfig> actualPolicyResponses =
394 spyPolicyEngine.getConfig(new ConfigRequestParameters());
396 assertEquals(1, actualPolicyResponses.size());
398 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
399 assertNotNull(actualPolicyConfig.toXML());
404 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigProperties_noException()
406 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
408 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
409 getAPIPolicyConfigResponse(PolicyType.PROPERTIES, COMMENTS), HttpStatus.ACCEPTED);
411 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
412 eq(HttpMethod.POST), any(), any());
414 final Collection<PolicyConfig> actualPolicyResponses =
415 spyPolicyEngine.getConfig(new ConfigRequestParameters());
417 assertEquals(1, actualPolicyResponses.size());
419 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
420 assertNotNull(actualPolicyConfig.toProperties());
424 @Test(expected = PolicyConfigException.class)
425 public void testStdPolicyEngineSendEvent_ConfigRequestParameters_CallPDPThrow404Exception_Exception()
427 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
428 doThrow(new RuntimeException(new RestClientException("Error 404"))).when(spyPolicyEngine)
429 .callNewPDP(any(), any(), any(), any());
430 spyPolicyEngine.getConfig(new ConfigRequestParameters());
434 public void testStdPolicyEngineListConfig_ConfigRequestParametersPolicyConfigProperties_noException()
436 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
438 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
439 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION);
441 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
442 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
444 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
445 eq(HttpMethod.POST), any(), any());
447 final Collection<String> actualResponse =
448 spyPolicyEngine.listConfig(new ConfigRequestParameters());
449 assertEquals(1, actualResponse.size());
450 assertNotNull(actualResponse.iterator().next());
455 public void testStdPolicyEngineListConfig_ConfigRequestParametersMessageConfigContainsPE300_noException()
457 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
459 final APIPolicyConfigResponse[] apiPolicyConfigResponse = getAPIPolicyConfigResponse(
460 PolicyType.JSON, JSON_CONFIGURATION, PolicyConfigConstants.PE300);
462 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
463 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
465 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
466 eq(HttpMethod.POST), any(), any());
468 final Collection<String> actualResponse =
469 spyPolicyEngine.listConfig(new ConfigRequestParameters());
471 assertEquals(1, actualResponse.size());
476 public void testStdPolicyEngineListConfig_ConfigRequestParametersWithTestProperty_noException()
478 final Properties defaultProperties = getDefaultProperties();
479 defaultProperties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "test");
480 final StdPolicyEngine spyPolicyEngine =
481 getSpyPolicyEngine("test" + CONFIG_PROPERTIES_FILE, defaultProperties);
483 final Collection<String> actualResponse =
484 spyPolicyEngine.listConfig(new ConfigRequestParameters());
486 assertEquals(1, actualResponse.size());
491 public void testStdPolicyEnginGetDecision_PolicyDecision_noException() throws Exception {
493 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
495 final ResponseEntity<StdDecisionResponse> stubbedResponse =
496 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
498 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME),
499 eq(HttpMethod.POST), any(), any());
501 final DecisionResponse actualResponse =
502 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
504 assertNotNull(actualResponse);
507 @Test(expected = PolicyDecisionException.class)
508 public void testStdPolicyEngineGetDecision_PolicyDecision_CallPDPThrow400Exception_Exception()
510 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
511 doThrow(new RuntimeException(new RestClientException("Error 400"))).when(spyPolicyEngine)
512 .callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
513 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
517 public void testStdPolicyEnginGetDecision_DecisionRequestParameters_noException()
520 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
522 final ResponseEntity<StdDecisionResponse> stubbedResponse =
523 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
525 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME),
526 eq(HttpMethod.POST), any(), any());
528 final DecisionRequestParameters requestParameters = new DecisionRequestParameters();
529 requestParameters.setOnapName(ONAP_NAME_VAL);
530 requestParameters.setRequestID(REQUEST_UUID);
531 requestParameters.setDecisionAttributes(Collections.emptyMap());
533 final DecisionResponse actualResponse = spyPolicyEngine.getDecision(requestParameters);
535 assertNotNull(actualResponse);
538 @Test(expected = PolicyDecisionException.class)
539 public void ttestStdPolicyEnginGetDecision_NullDecisionRequestParameters_Exception()
541 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
542 spyPolicyEngine.getDecision((DecisionRequestParameters) null);
546 public void testStdPolicyEnginGetMetrics_MetricsRequestParameters_noException() throws Exception {
548 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
550 final ResponseEntity<String> stubbedResponse =
551 new ResponseEntity<>("Metrics", HttpStatus.ACCEPTED);
553 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME),
554 eq(HttpMethod.GET), any(), any());
556 final MetricsResponse actualResponse =
557 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
558 assertNotNull(actualResponse);
559 assertEquals(HttpStatus.ACCEPTED.value(), actualResponse.getResponseCode());
564 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowHttpException_ResponseWithHttpCode()
566 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
567 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY)))
568 .when(spyPolicyEngine)
569 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
571 final MetricsResponse actualResponse =
572 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
573 assertNotNull(actualResponse);
574 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
578 @Test(expected = PolicyException.class)
579 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowPolicyException_Exception()
581 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
582 doThrow(PolicyException.class).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME),
583 eq(HttpMethod.GET), any(), any());
585 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
590 public void testStdPolicyEnginPushPolicy_PushPolicyParameters_noException() throws Exception {
592 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
594 final ResponseEntity<String> stubbedResponse =
595 new ResponseEntity<>("Successful", HttpStatus.OK);
597 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
598 eq(HttpMethod.PUT), any(), any());
600 final PolicyChangeResponse actualResponse =
601 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
603 assertNotNull(actualResponse);
604 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
608 public void testStdPolicyEnginePushPolicy_PushPolicyParametersThrowsHttpClientErrorException_ResponseWithHttpCode()
610 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
611 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY)))
612 .when(spyPolicyEngine)
613 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
615 final PolicyChangeResponse actualResponse =
616 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
618 assertNotNull(actualResponse);
619 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
623 @Test(expected = PolicyException.class)
624 public void testStdPolicyEnginePushPolicy_PushPolicyParameters_Exception() throws Exception {
625 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
626 doThrow(PolicyException.class).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
627 eq(HttpMethod.PUT), any(), any());
629 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
634 public void testStdPolicyEnginDeletePolicy_DeletePolicyParameters_noException() throws Exception {
636 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
638 final ResponseEntity<String> stubbedResponse =
639 new ResponseEntity<>("Successful", HttpStatus.OK);
641 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(DELETE_POLICY_RESOURCE_NAME),
642 eq(HttpMethod.DELETE), any(), any());
644 final PolicyChangeResponse actualResponse =
645 spyPolicyEngine.deletePolicy(new DeletePolicyParameters());
647 assertNotNull(actualResponse);
648 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
652 public void testStdPolicyEnginGetDictionaryItem_DictionaryParameters_noException()
655 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
657 final ResponseEntity<APIDictionaryResponse> stubbedResponse =
658 new ResponseEntity<>(getAPIDictionaryResponse(), HttpStatus.OK);
660 doReturn(stubbedResponse).when(spyPolicyEngine)
661 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
663 final DictionaryResponse actualResponse =
664 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
666 assertNotNull(actualResponse);
670 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp400ExceptionThrown_ResponseWithHttpCode()
672 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
673 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Error 400")))
674 .when(spyPolicyEngine)
675 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
677 final DictionaryResponse actualResponse =
678 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
680 assertNotNull(actualResponse);
681 assertEquals(HttpStatus.BAD_REQUEST.value(), actualResponse.getResponseCode());
686 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp401ExceptionThrown_ResponseWithHttpCode()
688 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
690 new RuntimeException(new HttpClientErrorException(HttpStatus.UNAUTHORIZED, "Error 401")))
691 .when(spyPolicyEngine)
692 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
694 final DictionaryResponse actualResponse =
695 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
697 assertNotNull(actualResponse);
698 assertEquals(HttpStatus.UNAUTHORIZED.value(), actualResponse.getResponseCode());
703 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithRunTimeExceptionThrown_ResponseWithHttpCode()
705 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
706 doThrow(new RuntimeException(
707 new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error")))
708 .when(spyPolicyEngine)
709 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
711 final DictionaryResponse actualResponse =
712 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
714 assertNotNull(actualResponse);
715 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), actualResponse.getResponseCode());
720 public void testStdPolicyEnginCreateDictionaryItem_DictionaryParameters_noException()
723 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
725 final ResponseEntity<String> stubbedResponse =
726 new ResponseEntity<>("Successful", HttpStatus.OK);
728 doReturn(stubbedResponse).when(spyPolicyEngine)
729 .callNewPDP(eq(CREATE_DICTIONARY_ITEM_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
731 final PolicyChangeResponse actualResponse =
732 spyPolicyEngine.createDictionaryItem(new DictionaryParameters());
734 assertNotNull(actualResponse);
735 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
739 public void testStdPolicyEnginUpdateDictionaryItem_DictionaryParameters_noException()
742 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
744 final ResponseEntity<String> stubbedResponse =
745 new ResponseEntity<>("Successful", HttpStatus.OK);
747 doReturn(stubbedResponse).when(spyPolicyEngine)
748 .callNewPDP(eq(UPDATE_DICTIONARY_ITEM_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
750 final PolicyChangeResponse actualResponse =
751 spyPolicyEngine.updateDictionaryItem(new DictionaryParameters());
753 assertNotNull(actualResponse);
754 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
758 public void testStdPolicyEnginPolicyEngineImport_ImportParameters_noException() throws Exception {
759 final File emptyfile = temporaryFolder.newFile("emptyFile.txt");
760 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
762 final ResponseEntity<String> stubbedResponse =
763 new ResponseEntity<>("Successful", HttpStatus.OK);
765 doReturn(stubbedResponse).when(spyPolicyEngine)
766 .callNewPDP(eq(POLICY_ENGINE_IMPORT_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
768 final ImportParameters importParameters = new ImportParameters();
769 importParameters.setFilePath(emptyfile.toString());
770 final PolicyChangeResponse actualResponse =
771 spyPolicyEngine.policyEngineImport(importParameters);
773 assertNotNull(actualResponse);
774 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
778 public void testStdPolicyEnginCreatePolicy_PolicyParameters_noException() throws Exception {
779 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
781 final ResponseEntity<String> stubbedResponse =
782 new ResponseEntity<>("Successful", HttpStatus.OK);
784 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_POLICY_RESOURCE_NAME),
785 eq(HttpMethod.PUT), any(), any());
787 final PolicyChangeResponse actualResponse =
788 spyPolicyEngine.createPolicy(new PolicyParameters());
790 assertNotNull(actualResponse);
791 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
795 public void testStdPolicyEnginUpdatePolicy_PolicyParameters_noException() throws Exception {
796 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
798 final ResponseEntity<String> stubbedResponse =
799 new ResponseEntity<>("Successful", HttpStatus.OK);
801 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
802 eq(HttpMethod.PUT), any(), any());
804 final PolicyChangeResponse actualResponse =
805 spyPolicyEngine.updatePolicy(new PolicyParameters());
807 assertNotNull(actualResponse);
808 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
811 @Test(expected = PolicyException.class)
812 public void testStdPolicyEnginPushPolicy_NullValues_Exception() throws Exception {
813 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
815 spyPolicyEngine.pushPolicy(null, null, null, null, null);
818 @Test(expected = PolicyException.class)
819 public void testStdPolicyEnginPushPolicy_EmptyPolicyScope_Exception() throws Exception {
820 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
822 spyPolicyEngine.pushPolicy("", null, null, null, null);
825 @Test(expected = PolicyException.class)
826 public void testStdPolicyEnginPushPolicy_EmptyPolicyName_Exception() throws Exception {
827 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
829 spyPolicyEngine.pushPolicy("POLICY_SCOPE", "", null, null, null);
833 public void testStdPolicyEnginPushPolicy_PolicyParameters_noException() throws Exception {
834 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
836 final ResponseEntity<String> stubbedResponse =
837 new ResponseEntity<>("Successful", HttpStatus.OK);
839 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
840 eq(HttpMethod.PUT), any(), any());
842 final String actualResponse = spyPolicyEngine.pushPolicy("POLICY_SCOPE", ONAP_NAME_VAL,
843 "POLICY_TYPE", "POLICY_GROUP", REQUEST_UUID);
845 assertNotNull(actualResponse);
849 public void testStdPolicyEnginCreateUpdateConfigPolicy_PolicyParameters_noException()
851 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
853 final ResponseEntity<String> stubbedResponse =
854 new ResponseEntity<>("Successful", HttpStatus.OK);
856 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
857 eq(HttpMethod.PUT), any(), any());
859 final String actualResponse =
860 spyPolicyEngine.createUpdateConfigPolicy("POLICY_NAME", ONAP_NAME_VAL, ONAP_NAME_VAL,
861 "CONFIG_NAME", Collections.emptyMap(), PolicyType.JSON.toString().toUpperCase(), "",
862 "POLICY_SCOPE", REQUEST_UUID, "", "", "", new Date().toString(), true);
864 assertNotNull(actualResponse);
867 @Test(expected = PolicyException.class)
868 public void testStdPolicyEnginCreateUpdateConfigPolicy_NullPolicyName_Exception()
870 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
872 spyPolicyEngine.createUpdateConfigPolicy(null, null, null, null, null, null, null, null, null,
873 null, null, null, null, true);
877 public void testStdPolicyEnginCreateUpdateConfigFirewallPolicy_PolicyParameters_noException()
879 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
881 final ResponseEntity<String> stubbedResponse =
882 new ResponseEntity<>("Successful", HttpStatus.OK);
884 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
885 eq(HttpMethod.PUT), any(), any());
887 final String actualResponse = spyPolicyEngine.createUpdateConfigFirewallPolicy("POLICY_NAME",
888 getJsonObject(JSON_CONFIGURATION), "POLICY_SCOPE", REQUEST_UUID, "", "", "",
889 new Date().toString(), true);
891 assertNotNull(actualResponse);
894 private JsonObject getJsonObject(final String jsonString) {
895 try (final JsonReader jsonReader = Json.createReader(new StringReader(jsonString));) {
896 return jsonReader.readObject();
900 private StdPolicyEngine getSpyPolicyEngine() throws IOException, PolicyEngineException {
901 return getSpyPolicyEngine(CONFIG_PROPERTIES_FILE, getDefaultProperties());
904 private StdPolicyEngine getSpyPolicyEngine(final String filename, final Properties properties)
905 throws IOException, PolicyEngineException {
906 final File file = temporaryFolder.newFile(filename);
908 creatPropertyFile(file, properties);
910 final StdPolicyEngine spyPolicyEngine =
911 spy(new StdPolicyEngine(file.toString(), (String) null));
912 spyPolicyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
913 return spyPolicyEngine;
916 private Properties getDefaultProperties() {
917 final Properties properties = new Properties();
918 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
919 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
920 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
921 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
922 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
923 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
924 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
925 properties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "false");
929 private StdDecisionResponse getStdDecisionResponse() {
930 final StdDecisionResponse response = new StdDecisionResponse();
931 response.setDecision(PolicyDecision.PERMIT);
932 response.setDetails(PolicyDecision.PERMIT.name());
937 private APIDictionaryResponse getAPIDictionaryResponse() {
938 final APIDictionaryResponse response = new APIDictionaryResponse();
939 response.setResponseCode(0);
940 response.setResponseMessage("");
941 response.setDictionaryData(Collections.<String, String>emptyMap());
942 response.setDictionaryJson(Collections.<String, String>emptyMap());
946 private StdPolicyResponse[] getStdPolicyResponse() {
947 final StdPolicyResponse response = new StdPolicyResponse();
948 response.setPolicyResponseStatus(PolicyResponseStatus.ACTION_TAKEN);
949 return new StdPolicyResponse[] {response};
952 private void creatPropertyFile(final File file, final Properties properties) throws IOException {
953 try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) {
954 properties.store(bufferedWriter, COMMENTS);
958 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
959 final String configuration) {
961 return getAPIPolicyConfigResponse(policyType, configuration, null);
964 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
965 final String configuration, final String policyConfigMessage) {
966 final APIPolicyConfigResponse configResponse = new APIPolicyConfigResponse();
967 configResponse.setConfig(configuration);
968 configResponse.setMatchingConditions(getMatchingConditions());
969 configResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED);
970 configResponse.setPolicyName(POLICY_NAME);
971 configResponse.setPolicyType(PolicyConfigType.BRMS_RAW);
972 configResponse.setType(policyType);
973 configResponse.setPolicyVersion(POLICY_VERSION);
974 configResponse.setProperty(Collections.emptyMap());
975 configResponse.setPolicyConfigMessage(policyConfigMessage);
977 return new APIPolicyConfigResponse[] {configResponse};
980 private Map<String, String> getMatchingConditions() {
981 final Map<String, String> attributes = new HashMap<>();
982 attributes.put(ONAP_NAME, POLICY_NAME);
983 attributes.put(CONFIG_NAME, "Configuration_name");