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.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.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.mockito.junit.MockitoJUnitRunner;
76 import org.onap.policy.api.ConfigRequestParameters;
77 import org.onap.policy.api.DecisionRequestParameters;
78 import org.onap.policy.api.DecisionResponse;
79 import org.onap.policy.api.DeletePolicyParameters;
80 import org.onap.policy.api.DictionaryParameters;
81 import org.onap.policy.api.DictionaryResponse;
82 import org.onap.policy.api.EventRequestParameters;
83 import org.onap.policy.api.ImportParameters;
84 import org.onap.policy.api.MetricsRequestParameters;
85 import org.onap.policy.api.MetricsResponse;
86 import org.onap.policy.api.NotificationScheme;
87 import org.onap.policy.api.PolicyChangeResponse;
88 import org.onap.policy.api.PolicyConfig;
89 import org.onap.policy.api.PolicyConfigException;
90 import org.onap.policy.api.PolicyConfigStatus;
91 import org.onap.policy.api.PolicyConfigType;
92 import org.onap.policy.api.PolicyDecision;
93 import org.onap.policy.api.PolicyDecisionException;
94 import org.onap.policy.api.PolicyEngineException;
95 import org.onap.policy.api.PolicyEventException;
96 import org.onap.policy.api.PolicyException;
97 import org.onap.policy.api.PolicyParameters;
98 import org.onap.policy.api.PolicyResponse;
99 import org.onap.policy.api.PolicyResponseStatus;
100 import org.onap.policy.api.PolicyType;
101 import org.onap.policy.api.PushPolicyParameters;
102 import org.onap.policy.models.APIDictionaryResponse;
103 import org.onap.policy.models.APIPolicyConfigResponse;
104 import org.onap.policy.std.utils.PolicyConfigConstants;
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(MockitoJUnitRunner.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() throws Exception {
177 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
179 final Properties properties = new Properties();
180 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
181 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
182 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
183 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
184 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
185 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
186 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
187 creatPropertyFile(file, properties);
189 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
190 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
191 assertEquals(Arrays.asList(UEB), policyEngine.getNotificationType());
194 @Test(expected = PolicyEngineException.class)
195 public void testStdPolicyEngineInitialize_InvalidPropertyFile_Exception() throws PolicyEngineException {
196 new StdPolicyEngine("Invalid.properties", (String) null);
199 @Test(expected = PolicyEngineException.class)
200 public void testStdPolicyEngineInitialize_InvalidPropertyFileWithExt_Exception() throws Exception {
201 final File emptyFile = temporaryFolder.newFile("EmptyFile.txt");
202 new StdPolicyEngine(emptyFile.toString(), (String) null);
205 @Test(expected = PolicyEngineException.class)
206 public void testStdPolicyEngineInitialize_NullArguments_Exception() throws Exception {
207 new StdPolicyEngine((String)null, (String) null);
210 @Test(expected = PolicyEngineException.class)
211 public void testStdPolicyEngineWithPropertiesInitialize_NullArguments_Exception() throws Exception {
212 new StdPolicyEngine((Properties)null, (String) null);
215 @Test(expected = PolicyEngineException.class)
216 public void testStdPolicyEngineInitialize_PropertyFileMissingMandatoryProperties_Exception() throws Exception {
217 final File file = temporaryFolder.newFile(INVALID_CONFIG_PROPERTY_FILE);
218 final Properties properties = new Properties();
219 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
220 creatPropertyFile(file, properties);
222 new StdPolicyEngine(file.toString(), (String) null);
226 public void testStdPolicyEngineInitialize_MultiplePdp_noException() throws Exception {
227 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
229 final Properties properties = new Properties();
230 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE + PolicyConfigConstants.SEMICOLLON + PDP_PROP_VALUE_1);
231 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
232 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
233 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
234 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
235 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
236 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
237 creatPropertyFile(file, properties);
239 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
240 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
241 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
242 StdPolicyEngine.rotatePDPList();
243 assertEquals("https://localhost:8091/pdp/", StdPolicyEngine.getPDPURL());
244 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
245 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
248 @Test(expected = PolicyEngineException.class)
249 public void testStdPolicyEngineInitialize_NoPDP_noException() throws Exception {
250 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
252 final Properties properties = new Properties();
253 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
254 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
255 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
256 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
257 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
258 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
259 creatPropertyFile(file, properties);
261 new StdPolicyEngine(file.toString(), (String) null);
265 public void testStdPolicyEngineInitializeNotificationTypeDMMAP_noException() throws Exception {
266 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
268 final Properties properties = new Properties();
269 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
270 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
271 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
272 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, 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 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
279 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
281 assertEquals(Arrays.asList(DMAAP), policyEngine.getNotificationType());
286 public void testStdPolicyEngineSendEvent_noException() throws Exception {
287 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
288 doReturn(new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED)).when(spyPolicyEngine)
289 .callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
291 final Collection<PolicyResponse> actualPolicyResponses =
292 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
294 assertEquals(1, actualPolicyResponses.size());
298 @Test(expected = PolicyEventException.class)
299 public void testStdPolicyEngineSendEvent_NullEventRequestParameters_Exception() throws Exception {
300 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
301 spyPolicyEngine.sendEvent((EventRequestParameters) null);
304 @Test(expected = PolicyEventException.class)
305 public void testStdPolicyEngineSendEvent_EventRequestParameters_CallPDPThrow401Exception_Exception()
307 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
308 Mockito.doThrow(new RuntimeException(new RestClientException("Error 401"))).when(spyPolicyEngine)
309 .callNewPDP(any(), any(), any(), any());
310 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
314 public void testStdPolicyEngineSendEvent_EventRequestParameters_noException() throws Exception {
315 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
317 final ResponseEntity<StdPolicyResponse[]> stubbedResponse =
318 new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED);
320 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
322 final EventRequestParameters eventRequestParameters = new EventRequestParameters();
323 eventRequestParameters.setEventAttributes(Collections.emptyMap());
324 eventRequestParameters.setRequestID(REQUEST_UUID);
326 final Collection<PolicyResponse> actualPolicyResponses = spyPolicyEngine.sendEvent(eventRequestParameters);
328 assertEquals(1, actualPolicyResponses.size());
333 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigJSON_noException() throws Exception {
334 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
336 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
337 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION), HttpStatus.ACCEPTED);
339 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
342 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
344 assertEquals(1, actualPolicyResponses.size());
346 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
347 assertNotNull(actualPolicyConfig.toJSON());
352 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigOther_noException() throws Exception {
353 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
355 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
356 new ResponseEntity<>(getAPIPolicyConfigResponse(PolicyType.OTHER, COMMENTS), HttpStatus.ACCEPTED);
358 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
361 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
363 assertEquals(1, actualPolicyResponses.size());
365 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
366 assertNotNull(actualPolicyConfig.toOther());
371 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigXML_noException() throws Exception {
372 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
374 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
375 getAPIPolicyConfigResponse(PolicyType.XML, XML_CONFIGURATION), HttpStatus.ACCEPTED);
377 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
380 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
382 assertEquals(1, actualPolicyResponses.size());
384 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
385 assertNotNull(actualPolicyConfig.toXML());
390 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigProperties_noException()
392 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
394 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
395 new ResponseEntity<>(getAPIPolicyConfigResponse(PolicyType.PROPERTIES, COMMENTS), HttpStatus.ACCEPTED);
397 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
400 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
402 assertEquals(1, actualPolicyResponses.size());
404 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
405 assertNotNull(actualPolicyConfig.toProperties());
409 @Test(expected = PolicyConfigException.class)
410 public void testStdPolicyEngineSendEvent_ConfigRequestParameters_CallPDPThrow404Exception_Exception()
412 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
413 doThrow(new RuntimeException(new RestClientException("Error 404"))).when(spyPolicyEngine).callNewPDP(any(),
414 any(), any(), any());
415 spyPolicyEngine.getConfig(new ConfigRequestParameters());
419 public void testStdPolicyEngineListConfig_ConfigRequestParametersPolicyConfigProperties_noException()
421 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
423 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
424 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION);
426 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
427 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
429 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
432 final Collection<String> actualResponse = spyPolicyEngine.listConfig(new ConfigRequestParameters());
433 assertEquals(1, actualResponse.size());
434 assertNotNull(actualResponse.iterator().next());
439 public void testStdPolicyEngineListConfig_ConfigRequestParametersMessageConfigContainsPE300_noException()
441 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
443 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
444 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION, PolicyConfigConstants.PE300);
446 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
447 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
449 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
452 final Collection<String> actualResponse = spyPolicyEngine.listConfig(new ConfigRequestParameters());
454 assertEquals(1, actualResponse.size());
459 public void testStdPolicyEngineListConfig_ConfigRequestParametersWithTestProperty_noException() throws Exception {
460 final Properties defaultProperties = getDefaultProperties();
461 defaultProperties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "test");
462 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine("test" + CONFIG_PROPERTIES_FILE, defaultProperties);
464 final Collection<String> actualResponse = spyPolicyEngine.listConfig(new ConfigRequestParameters());
466 assertEquals(1, actualResponse.size());
471 public void testStdPolicyEnginGetDecision_PolicyDecision_noException() throws Exception {
473 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
475 final ResponseEntity<StdDecisionResponse> stubbedResponse =
476 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
478 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST),
481 final DecisionResponse actualResponse =
482 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
484 assertNotNull(actualResponse);
487 @Test(expected = PolicyDecisionException.class)
488 public void testStdPolicyEngineGetDecision_PolicyDecision_CallPDPThrow400Exception_Exception() throws Exception {
489 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
490 doThrow(new RuntimeException(new RestClientException("Error 400"))).when(spyPolicyEngine)
491 .callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
492 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
496 public void testStdPolicyEnginGetDecision_DecisionRequestParameters_noException() throws Exception {
498 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
500 final ResponseEntity<StdDecisionResponse> stubbedResponse =
501 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
503 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST),
506 final DecisionRequestParameters requestParameters = new DecisionRequestParameters();
507 requestParameters.setOnapName(ONAP_NAME_VAL);
508 requestParameters.setRequestID(REQUEST_UUID);
509 requestParameters.setDecisionAttributes(Collections.emptyMap());
511 final DecisionResponse actualResponse = spyPolicyEngine.getDecision(requestParameters);
513 assertNotNull(actualResponse);
516 @Test(expected = PolicyDecisionException.class)
517 public void ttestStdPolicyEnginGetDecision_NullDecisionRequestParameters_Exception() throws Exception {
518 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
519 spyPolicyEngine.getDecision((DecisionRequestParameters) null);
523 public void testStdPolicyEnginGetMetrics_MetricsRequestParameters_noException() throws Exception {
525 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
527 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Metrics", HttpStatus.ACCEPTED);
529 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET),
532 final MetricsResponse actualResponse = spyPolicyEngine.getMetrics(new MetricsRequestParameters());
533 assertNotNull(actualResponse);
534 assertEquals(HttpStatus.ACCEPTED.value(), actualResponse.getResponseCode());
539 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowHttpException_ResponseWithHttpCode()
541 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
542 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY))).when(spyPolicyEngine)
543 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
545 final MetricsResponse actualResponse = spyPolicyEngine.getMetrics(new MetricsRequestParameters());
546 assertNotNull(actualResponse);
547 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
551 @Test(expected = PolicyException.class)
552 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowPolicyException_Exception()
554 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
555 doThrow(PolicyException.class).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME),
556 eq(HttpMethod.GET), any(), any());
558 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
563 public void testStdPolicyEnginPushPolicy_PushPolicyParameters_noException() throws Exception {
565 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
567 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
569 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
572 final PolicyChangeResponse actualResponse = spyPolicyEngine.pushPolicy(new PushPolicyParameters());
574 assertNotNull(actualResponse);
575 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
579 public void testStdPolicyEnginePushPolicy_PushPolicyParametersThrowsHttpClientErrorException_ResponseWithHttpCode()
581 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
582 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY))).when(spyPolicyEngine)
583 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
585 final PolicyChangeResponse actualResponse = spyPolicyEngine.pushPolicy(new PushPolicyParameters());
587 assertNotNull(actualResponse);
588 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
592 @Test(expected = PolicyException.class)
593 public void testStdPolicyEnginePushPolicy_PushPolicyParameters_Exception() throws Exception {
594 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
595 doThrow(PolicyException.class).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
596 eq(HttpMethod.PUT), any(), any());
598 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
603 public void testStdPolicyEnginDeletePolicy_DeletePolicyParameters_noException() throws Exception {
605 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
607 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
609 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(DELETE_POLICY_RESOURCE_NAME),
610 eq(HttpMethod.DELETE), any(), any());
612 final PolicyChangeResponse actualResponse = spyPolicyEngine.deletePolicy(new DeletePolicyParameters());
614 assertNotNull(actualResponse);
615 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
619 public void testStdPolicyEnginGetDictionaryItem_DictionaryParameters_noException() throws Exception {
621 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
623 final ResponseEntity<APIDictionaryResponse> stubbedResponse =
624 new ResponseEntity<>(getAPIDictionaryResponse(), HttpStatus.OK);
626 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
627 eq(HttpMethod.POST), any(), any());
629 final DictionaryResponse actualResponse = spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
631 assertNotNull(actualResponse);
635 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp400ExceptionThrown_ResponseWithHttpCode()
637 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
638 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Error 400")))
639 .when(spyPolicyEngine)
640 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
642 final DictionaryResponse actualResponse = spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
644 assertNotNull(actualResponse);
645 assertEquals(HttpStatus.BAD_REQUEST.value(), actualResponse.getResponseCode());
650 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp401ExceptionThrown_ResponseWithHttpCode()
652 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
653 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.UNAUTHORIZED, "Error 401")))
654 .when(spyPolicyEngine)
655 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
657 final DictionaryResponse actualResponse = spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
659 assertNotNull(actualResponse);
660 assertEquals(HttpStatus.UNAUTHORIZED.value(), actualResponse.getResponseCode());
665 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithRunTimeExceptionThrown_ResponseWithHttpCode()
667 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
668 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error")))
669 .when(spyPolicyEngine)
670 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
672 final DictionaryResponse actualResponse = spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
674 assertNotNull(actualResponse);
675 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), actualResponse.getResponseCode());
680 public void testStdPolicyEnginCreateDictionaryItem_DictionaryParameters_noException() throws Exception {
682 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
684 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
686 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_DICTIONARY_ITEM_RESOURCE_NAME),
687 eq(HttpMethod.PUT), any(), any());
689 final PolicyChangeResponse actualResponse = spyPolicyEngine.createDictionaryItem(new DictionaryParameters());
691 assertNotNull(actualResponse);
692 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
696 public void testStdPolicyEnginUpdateDictionaryItem_DictionaryParameters_noException() throws Exception {
698 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
700 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
702 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_DICTIONARY_ITEM_RESOURCE_NAME),
703 eq(HttpMethod.PUT), any(), any());
705 final PolicyChangeResponse actualResponse = spyPolicyEngine.updateDictionaryItem(new DictionaryParameters());
707 assertNotNull(actualResponse);
708 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
712 public void testStdPolicyEnginPolicyEngineImport_ImportParameters_noException() throws Exception {
713 final File emptyfile = temporaryFolder.newFile("emptyFile.txt");
714 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
716 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
718 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(POLICY_ENGINE_IMPORT_RESOURCE_NAME),
719 eq(HttpMethod.POST), any(), any());
721 final ImportParameters importParameters = new ImportParameters();
722 importParameters.setFilePath(emptyfile.toString());
723 final PolicyChangeResponse actualResponse = spyPolicyEngine.policyEngineImport(importParameters);
725 assertNotNull(actualResponse);
726 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
730 public void testStdPolicyEnginCreatePolicy_PolicyParameters_noException() throws Exception {
731 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
733 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
735 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
738 final PolicyChangeResponse actualResponse = spyPolicyEngine.createPolicy(new PolicyParameters());
740 assertNotNull(actualResponse);
741 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
745 public void testStdPolicyEnginUpdatePolicy_PolicyParameters_noException() throws Exception {
746 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
748 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
750 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
753 final PolicyChangeResponse actualResponse = spyPolicyEngine.updatePolicy(new PolicyParameters());
755 assertNotNull(actualResponse);
756 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
759 @Test(expected = PolicyException.class)
760 public void testStdPolicyEnginPushPolicy_NullValues_Exception() throws Exception {
761 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
763 spyPolicyEngine.pushPolicy(null, null, null, null, null);
766 @Test(expected = PolicyException.class)
767 public void testStdPolicyEnginPushPolicy_EmptyPolicyScope_Exception() throws Exception {
768 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
770 spyPolicyEngine.pushPolicy("", null, null, null, null);
773 @Test(expected = PolicyException.class)
774 public void testStdPolicyEnginPushPolicy_EmptyPolicyName_Exception() throws Exception {
775 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
777 spyPolicyEngine.pushPolicy("POLICY_SCOPE", "", null, null, null);
781 public void testStdPolicyEnginPushPolicy_PolicyParameters_noException() throws Exception {
782 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
784 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
786 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
789 final String actualResponse =
790 spyPolicyEngine.pushPolicy("POLICY_SCOPE", ONAP_NAME_VAL, "POLICY_TYPE", "POLICY_GROUP", REQUEST_UUID);
792 assertNotNull(actualResponse);
796 public void testStdPolicyEnginCreateUpdateConfigPolicy_PolicyParameters_noException() throws Exception {
797 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
799 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
801 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
804 final String actualResponse = spyPolicyEngine.createUpdateConfigPolicy("POLICY_NAME", ONAP_NAME_VAL,
805 ONAP_NAME_VAL, "CONFIG_NAME", Collections.emptyMap(), PolicyType.JSON.toString().toUpperCase(), "",
806 "POLICY_SCOPE", REQUEST_UUID, "", "", "", new Date().toString(), true);
808 assertNotNull(actualResponse);
811 @Test(expected = PolicyException.class)
812 public void testStdPolicyEnginCreateUpdateConfigPolicy_NullPolicyName_Exception() throws Exception {
813 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
815 spyPolicyEngine.createUpdateConfigPolicy(null, null, null, null, null, null, null, null, null, null, null, null,
820 public void testStdPolicyEnginCreateUpdateConfigFirewallPolicy_PolicyParameters_noException() throws Exception {
821 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
823 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
825 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
828 final String actualResponse =
829 spyPolicyEngine.createUpdateConfigFirewallPolicy("POLICY_NAME", getJsonObject(JSON_CONFIGURATION),
830 "POLICY_SCOPE", REQUEST_UUID, "", "", "", new Date().toString(), true);
832 assertNotNull(actualResponse);
835 private JsonObject getJsonObject(final String jsonString) {
836 try (final JsonReader jsonReader = Json.createReader(new StringReader(jsonString));) {
837 return jsonReader.readObject();
841 private StdPolicyEngine getSpyPolicyEngine() throws IOException, PolicyEngineException {
842 return getSpyPolicyEngine(CONFIG_PROPERTIES_FILE, getDefaultProperties());
845 private StdPolicyEngine getSpyPolicyEngine(final String filename, final Properties properties)
846 throws IOException, PolicyEngineException {
847 final File file = temporaryFolder.newFile(filename);
849 creatPropertyFile(file, properties);
851 final StdPolicyEngine spyPolicyEngine = spy(new StdPolicyEngine(file.toString(), (String) null));
852 spyPolicyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
853 return spyPolicyEngine;
856 private Properties getDefaultProperties() {
857 final Properties properties = new Properties();
858 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
859 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
860 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
861 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
862 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
863 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
864 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
865 properties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "false");
869 private StdDecisionResponse getStdDecisionResponse() {
870 final StdDecisionResponse response = new StdDecisionResponse();
871 response.setDecision(PolicyDecision.PERMIT);
872 response.setDetails(PolicyDecision.PERMIT.name());
877 private APIDictionaryResponse getAPIDictionaryResponse() {
878 final APIDictionaryResponse response = new APIDictionaryResponse();
879 response.setResponseCode(0);
880 response.setResponseMessage("");
881 response.setDictionaryData(Collections.<String, String>emptyMap());
882 response.setDictionaryJson(Collections.<String, String>emptyMap());
886 private StdPolicyResponse[] getStdPolicyResponse() {
887 final StdPolicyResponse response = new StdPolicyResponse();
888 response.setPolicyResponseStatus(PolicyResponseStatus.ACTION_TAKEN);
889 return new StdPolicyResponse[] {response};
892 private void creatPropertyFile(final File file, final Properties properties) throws IOException {
893 try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) {
894 properties.store(bufferedWriter, COMMENTS);
898 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
899 final String configuration) {
901 return getAPIPolicyConfigResponse(policyType, configuration, null);
904 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
905 final String configuration, final String policyConfigMessage) {
906 final APIPolicyConfigResponse configResponse = new APIPolicyConfigResponse();
907 configResponse.setConfig(configuration);
908 configResponse.setMatchingConditions(getMatchingConditions());
909 configResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED);
910 configResponse.setPolicyName(POLICY_NAME);
911 configResponse.setPolicyType(PolicyConfigType.BRMS_RAW);
912 configResponse.setType(policyType);
913 configResponse.setPolicyVersion(POLICY_VERSION);
914 configResponse.setProperty(Collections.emptyMap());
915 configResponse.setPolicyConfigMessage(policyConfigMessage);
917 return new APIPolicyConfigResponse[] {configResponse};
920 private Map<String, String> getMatchingConditions() {
921 final Map<String, String> attributes = new HashMap<>();
922 attributes.put(ONAP_NAME, POLICY_NAME);
923 attributes.put(CONFIG_NAME, "Configuration_name");