2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.std;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.eq;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.spy;
32 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.CONFIG_NAME;
33 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.ONAP_NAME;
34 import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_ID_PROP_NAME;
35 import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_KEY_PROP_NAME;
36 import static org.onap.policy.std.utils.PolicyConfigConstants.COMMA;
37 import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_DICTIONARY_ITEM_RESOURCE_NAME;
38 import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_POLICY_RESOURCE_NAME;
39 import static org.onap.policy.std.utils.PolicyConfigConstants.DELETE_POLICY_RESOURCE_NAME;
40 import static org.onap.policy.std.utils.PolicyConfigConstants.DMAAP;
41 import static org.onap.policy.std.utils.PolicyConfigConstants.ENVIRONMENT_PROP_NAME;
42 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_CONFIG_RESOURCE_NAME;
43 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DECISION_RESOURCE_NAME;
44 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DICTIONARY_ITEMS_RESOURCE_NAME;
45 import static org.onap.policy.std.utils.PolicyConfigConstants.GET_METRICS_RESOURCE_NAME;
46 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_SERVERS_PROP_NAME;
47 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TOPIC_PROP_NAME;
48 import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TYPE_PROP_NAME;
49 import static org.onap.policy.std.utils.PolicyConfigConstants.PDP_URL_PROP_NAME;
50 import static org.onap.policy.std.utils.PolicyConfigConstants.POLICY_ENGINE_IMPORT_RESOURCE_NAME;
51 import static org.onap.policy.std.utils.PolicyConfigConstants.PUSH_POLICY_RESOURCE_NAME;
52 import static org.onap.policy.std.utils.PolicyConfigConstants.SEND_EVENT_RESOURCE_NAME;
53 import static org.onap.policy.std.utils.PolicyConfigConstants.UEB;
54 import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_DICTIONARY_ITEM_RESOURCE_NAME;
55 import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_POLICY_RESOURCE_NAME;
56 import java.io.BufferedWriter;
58 import java.io.IOException;
59 import java.io.StringReader;
60 import java.nio.file.Files;
61 import java.util.Arrays;
62 import java.util.Collection;
63 import java.util.Collections;
64 import java.util.Date;
65 import java.util.HashMap;
67 import java.util.Properties;
68 import java.util.UUID;
69 import javax.json.Json;
70 import javax.json.JsonObject;
71 import javax.json.JsonReader;
72 import org.junit.Rule;
73 import org.junit.Test;
74 import org.junit.rules.TemporaryFolder;
75 import org.junit.runner.RunWith;
76 import org.mockito.Mockito;
77 import org.onap.policy.api.ConfigRequestParameters;
78 import org.onap.policy.api.DecisionRequestParameters;
79 import org.onap.policy.api.DecisionResponse;
80 import org.onap.policy.api.DeletePolicyParameters;
81 import org.onap.policy.api.DictionaryParameters;
82 import org.onap.policy.api.DictionaryResponse;
83 import org.onap.policy.api.EventRequestParameters;
84 import org.onap.policy.api.ImportParameters;
85 import org.onap.policy.api.MetricsRequestParameters;
86 import org.onap.policy.api.MetricsResponse;
87 import org.onap.policy.api.NotificationScheme;
88 import org.onap.policy.api.PolicyChangeResponse;
89 import org.onap.policy.api.PolicyConfig;
90 import org.onap.policy.api.PolicyConfigException;
91 import org.onap.policy.api.PolicyConfigStatus;
92 import org.onap.policy.api.PolicyConfigType;
93 import org.onap.policy.api.PolicyDecision;
94 import org.onap.policy.api.PolicyDecisionException;
95 import org.onap.policy.api.PolicyEngineException;
96 import org.onap.policy.api.PolicyEventException;
97 import org.onap.policy.api.PolicyException;
98 import org.onap.policy.api.PolicyParameters;
99 import org.onap.policy.api.PolicyResponse;
100 import org.onap.policy.api.PolicyResponseStatus;
101 import org.onap.policy.api.PolicyType;
102 import org.onap.policy.api.PushPolicyParameters;
103 import org.onap.policy.models.APIDictionaryResponse;
104 import org.onap.policy.models.APIPolicyConfigResponse;
105 import org.onap.policy.std.utils.PolicyConfigConstants;
106 import org.powermock.modules.junit4.PowerMockRunner;
107 import org.springframework.http.HttpMethod;
108 import org.springframework.http.HttpStatus;
109 import org.springframework.http.ResponseEntity;
110 import org.springframework.web.client.HttpClientErrorException;
111 import org.springframework.web.client.RestClientException;
113 @RunWith(PowerMockRunner.class)
114 public class StdPolicyEngineTest {
116 private static final String ONAP_NAME_VAL = "ONAP_NAME";
117 private static final String POLICY_VERSION = "1.0.0";
118 private static final String POLICY_NAME = "ONAP";
119 private static final String COMMENTS = "";
120 private static final UUID REQUEST_UUID = UUID.randomUUID();
121 private static final String SERVER_NAME = "localhost.com";
122 private static final String PDP_PROP_VALUE = "http://localhost:8092/pdp/ , test, test";
123 private static final String PDP_PROP_VALUE_1 = "https://localhost:8091/pdp/ , onap, onap";
124 private static final String JSON_CONFIGURATION = "{\"name\":\"value\"}";
125 private static final String XML_CONFIGURATION =
126 "<map><entry><string>name</string><string>value</string></entry></map>";
129 public TemporaryFolder temporaryFolder = new TemporaryFolder();
131 private static final String CONFIG_PROPERTIES_FILE = "config_pass.properties";
132 private static final String INVALID_CONFIG_PROPERTY_FILE = "config_fail.properties";
135 public void testStdPolicyEngineInitialize_noException() throws Exception {
136 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
138 creatPropertyFile(file, getDefaultProperties());
140 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
141 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
142 assertEquals("TEST", StdPolicyEngine.getEnvironment());
143 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
144 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
145 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME),
146 policyEngine.getNotificationURLList());
150 public void testStdPolicyEngineWithPropertiesInitialize_noException() throws Exception {
151 final StdPolicyEngine policyEngine =
152 new StdPolicyEngine(getDefaultProperties(), (String) null);
153 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
154 assertEquals("TEST", StdPolicyEngine.getEnvironment());
155 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
156 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
157 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME),
158 policyEngine.getNotificationURLList());
162 public void testStdPolicyEngineInitializeWithSingleServerName_noException() throws Exception {
163 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
165 final Properties properties = new Properties();
166 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
167 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
168 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
169 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
170 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
171 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
172 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
173 creatPropertyFile(file, properties);
175 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
176 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
177 assertEquals(Arrays.asList(SERVER_NAME), policyEngine.getNotificationURLList());
181 public void testStdPolicyEngineInitializeWithSingleNotificationType_noException()
183 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
185 final Properties properties = new Properties();
186 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
187 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
188 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
189 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
190 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
191 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
192 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
193 creatPropertyFile(file, properties);
195 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
196 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
197 assertEquals(Arrays.asList(UEB), policyEngine.getNotificationType());
200 @Test(expected = PolicyEngineException.class)
201 public void testStdPolicyEngineInitialize_InvalidPropertyFile_Exception()
202 throws PolicyEngineException {
203 new StdPolicyEngine("Invalid.properties", (String) null);
206 @Test(expected = PolicyEngineException.class)
207 public void testStdPolicyEngineInitialize_InvalidPropertyFileWithExt_Exception()
209 final File emptyFile = temporaryFolder.newFile("EmptyFile.txt");
210 new StdPolicyEngine(emptyFile.toString(), (String) null);
213 @Test(expected = PolicyEngineException.class)
214 public void testStdPolicyEngineInitialize_NullArguments_Exception() throws Exception {
215 new StdPolicyEngine((String) null, (String) null);
218 @Test(expected = PolicyEngineException.class)
219 public void testStdPolicyEngineWithPropertiesInitialize_NullArguments_Exception()
221 new StdPolicyEngine((Properties) null, (String) null);
224 @Test(expected = PolicyEngineException.class)
225 public void testStdPolicyEngineInitialize_PropertyFileMissingMandatoryProperties_Exception()
227 final File file = temporaryFolder.newFile(INVALID_CONFIG_PROPERTY_FILE);
228 final Properties properties = new Properties();
229 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
230 creatPropertyFile(file, properties);
232 new StdPolicyEngine(file.toString(), (String) null);
236 public void testStdPolicyEngineInitialize_MultiplePdp_noException() throws Exception {
237 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
239 final Properties properties = new Properties();
240 properties.setProperty(PDP_URL_PROP_NAME,
241 PDP_PROP_VALUE + PolicyConfigConstants.SEMICOLLON + PDP_PROP_VALUE_1);
242 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
243 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
244 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
245 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
246 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
247 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
248 creatPropertyFile(file, properties);
250 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
251 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
252 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
253 StdPolicyEngine.rotatePDPList();
254 assertEquals("https://localhost:8091/pdp/", StdPolicyEngine.getPDPURL());
255 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
256 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME),
257 policyEngine.getNotificationURLList());
260 @Test(expected = PolicyEngineException.class)
261 public void testStdPolicyEngineInitialize_NoPDP_noException() throws Exception {
262 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
264 final Properties properties = new Properties();
265 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
266 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
267 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
268 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
269 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
270 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
271 creatPropertyFile(file, properties);
273 new StdPolicyEngine(file.toString(), (String) null);
277 public void testStdPolicyEngineInitializeNotificationTypeDMMAP_noException() throws Exception {
278 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
280 final Properties properties = new Properties();
281 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
282 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
283 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
284 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, DMAAP);
285 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
286 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
287 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
288 creatPropertyFile(file, properties);
290 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
291 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
293 assertEquals(Arrays.asList(DMAAP), policyEngine.getNotificationType());
298 public void testStdPolicyEngineSendEvent_noException() throws Exception {
299 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
300 doReturn(new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED))
301 .when(spyPolicyEngine)
302 .callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
304 final Collection<PolicyResponse> actualPolicyResponses =
305 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
307 assertEquals(1, actualPolicyResponses.size());
311 @Test(expected = PolicyEventException.class)
312 public void testStdPolicyEngineSendEvent_NullEventRequestParameters_Exception()
314 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
315 spyPolicyEngine.sendEvent((EventRequestParameters) null);
318 @Test(expected = PolicyEventException.class)
319 public void testStdPolicyEngineSendEvent_EventRequestParameters_CallPDPThrow401Exception_Exception()
321 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
322 Mockito.doThrow(new RuntimeException(new RestClientException("Error 401")))
323 .when(spyPolicyEngine).callNewPDP(any(), any(), any(), any());
324 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
328 public void testStdPolicyEngineSendEvent_EventRequestParameters_noException() throws Exception {
329 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
331 final ResponseEntity<StdPolicyResponse[]> stubbedResponse =
332 new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED);
334 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(SEND_EVENT_RESOURCE_NAME),
335 any(), any(), any());
337 final EventRequestParameters eventRequestParameters = new EventRequestParameters();
338 eventRequestParameters.setEventAttributes(Collections.emptyMap());
339 eventRequestParameters.setRequestID(REQUEST_UUID);
341 final Collection<PolicyResponse> actualPolicyResponses =
342 spyPolicyEngine.sendEvent(eventRequestParameters);
344 assertEquals(1, actualPolicyResponses.size());
349 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigJSON_noException()
351 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
353 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
354 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION),
355 HttpStatus.ACCEPTED);
357 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
358 eq(HttpMethod.POST), any(), any());
360 final Collection<PolicyConfig> actualPolicyResponses =
361 spyPolicyEngine.getConfig(new ConfigRequestParameters());
363 assertEquals(1, actualPolicyResponses.size());
365 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
366 assertNotNull(actualPolicyConfig.toJSON());
371 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigOther_noException()
373 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
375 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
376 getAPIPolicyConfigResponse(PolicyType.OTHER, COMMENTS), HttpStatus.ACCEPTED);
378 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
379 eq(HttpMethod.POST), any(), any());
381 final Collection<PolicyConfig> actualPolicyResponses =
382 spyPolicyEngine.getConfig(new ConfigRequestParameters());
384 assertEquals(1, actualPolicyResponses.size());
386 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
387 assertNotNull(actualPolicyConfig.toOther());
392 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigXML_noException()
394 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
396 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
397 getAPIPolicyConfigResponse(PolicyType.XML, XML_CONFIGURATION), HttpStatus.ACCEPTED);
399 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
400 eq(HttpMethod.POST), any(), any());
402 final Collection<PolicyConfig> actualPolicyResponses =
403 spyPolicyEngine.getConfig(new ConfigRequestParameters());
405 assertEquals(1, actualPolicyResponses.size());
407 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
408 assertNotNull(actualPolicyConfig.toXML());
413 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigProperties_noException()
415 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
417 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
418 getAPIPolicyConfigResponse(PolicyType.PROPERTIES, COMMENTS), HttpStatus.ACCEPTED);
420 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
421 eq(HttpMethod.POST), any(), any());
423 final Collection<PolicyConfig> actualPolicyResponses =
424 spyPolicyEngine.getConfig(new ConfigRequestParameters());
426 assertEquals(1, actualPolicyResponses.size());
428 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
429 assertNotNull(actualPolicyConfig.toProperties());
433 @Test(expected = PolicyConfigException.class)
434 public void testStdPolicyEngineSendEvent_ConfigRequestParameters_CallPDPThrow404Exception_Exception()
436 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
437 doThrow(new RuntimeException(new RestClientException("Error 404"))).when(spyPolicyEngine)
438 .callNewPDP(any(), any(), any(), any());
439 spyPolicyEngine.getConfig(new ConfigRequestParameters());
443 public void testStdPolicyEngineListConfig_ConfigRequestParametersPolicyConfigProperties_noException()
445 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
447 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
448 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION);
450 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
451 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
453 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
454 eq(HttpMethod.POST), any(), any());
456 final Collection<String> actualResponse =
457 spyPolicyEngine.listConfig(new ConfigRequestParameters());
458 assertEquals(1, actualResponse.size());
459 assertNotNull(actualResponse.iterator().next());
464 public void testStdPolicyEngineListConfig_ConfigRequestParametersMessageConfigContainsPE300_noException()
466 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
468 final APIPolicyConfigResponse[] apiPolicyConfigResponse = getAPIPolicyConfigResponse(
469 PolicyType.JSON, JSON_CONFIGURATION, PolicyConfigConstants.PE300);
471 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
472 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
474 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME),
475 eq(HttpMethod.POST), any(), any());
477 final Collection<String> actualResponse =
478 spyPolicyEngine.listConfig(new ConfigRequestParameters());
480 assertEquals(1, actualResponse.size());
485 public void testStdPolicyEngineListConfig_ConfigRequestParametersWithTestProperty_noException()
487 final Properties defaultProperties = getDefaultProperties();
488 defaultProperties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "test");
489 final StdPolicyEngine spyPolicyEngine =
490 getSpyPolicyEngine("test" + CONFIG_PROPERTIES_FILE, defaultProperties);
492 final Collection<String> actualResponse =
493 spyPolicyEngine.listConfig(new ConfigRequestParameters());
495 assertEquals(1, actualResponse.size());
500 public void testStdPolicyEnginGetDecision_PolicyDecision_noException() throws Exception {
502 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
504 final ResponseEntity<StdDecisionResponse> stubbedResponse =
505 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
507 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME),
508 eq(HttpMethod.POST), any(), any());
510 final DecisionResponse actualResponse =
511 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
513 assertNotNull(actualResponse);
516 @Test(expected = PolicyDecisionException.class)
517 public void testStdPolicyEngineGetDecision_PolicyDecision_CallPDPThrow400Exception_Exception()
519 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
520 doThrow(new RuntimeException(new RestClientException("Error 400"))).when(spyPolicyEngine)
521 .callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
522 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
526 public void testStdPolicyEnginGetDecision_DecisionRequestParameters_noException()
529 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
531 final ResponseEntity<StdDecisionResponse> stubbedResponse =
532 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
534 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME),
535 eq(HttpMethod.POST), any(), any());
537 final DecisionRequestParameters requestParameters = new DecisionRequestParameters();
538 requestParameters.setOnapName(ONAP_NAME_VAL);
539 requestParameters.setRequestID(REQUEST_UUID);
540 requestParameters.setDecisionAttributes(Collections.emptyMap());
542 final DecisionResponse actualResponse = spyPolicyEngine.getDecision(requestParameters);
544 assertNotNull(actualResponse);
547 @Test(expected = PolicyDecisionException.class)
548 public void ttestStdPolicyEnginGetDecision_NullDecisionRequestParameters_Exception()
550 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
551 spyPolicyEngine.getDecision((DecisionRequestParameters) null);
555 public void testStdPolicyEnginGetMetrics_MetricsRequestParameters_noException()
558 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
560 final ResponseEntity<String> stubbedResponse =
561 new ResponseEntity<>("Metrics", HttpStatus.ACCEPTED);
563 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME),
564 eq(HttpMethod.GET), any(), any());
566 final MetricsResponse actualResponse =
567 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
568 assertNotNull(actualResponse);
569 assertEquals(HttpStatus.ACCEPTED.value(), actualResponse.getResponseCode());
574 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowHttpException_ResponseWithHttpCode()
576 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
577 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY)))
578 .when(spyPolicyEngine)
579 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
581 final MetricsResponse actualResponse =
582 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
583 assertNotNull(actualResponse);
584 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
588 @Test(expected = PolicyException.class)
589 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowPolicyException_Exception()
591 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
592 doThrow(PolicyException.class).when(spyPolicyEngine)
593 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
595 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
600 public void testStdPolicyEnginPushPolicy_PushPolicyParameters_noException() throws Exception {
602 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
604 final ResponseEntity<String> stubbedResponse =
605 new ResponseEntity<>("Successful", HttpStatus.OK);
607 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
608 eq(HttpMethod.PUT), any(), any());
610 final PolicyChangeResponse actualResponse =
611 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
613 assertNotNull(actualResponse);
614 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
618 public void testStdPolicyEnginePushPolicy_PushPolicyParametersThrowsHttpClientErrorException_ResponseWithHttpCode()
620 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
621 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY)))
622 .when(spyPolicyEngine)
623 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
625 final PolicyChangeResponse actualResponse =
626 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
628 assertNotNull(actualResponse);
629 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
633 @Test(expected = PolicyException.class)
634 public void testStdPolicyEnginePushPolicy_PushPolicyParameters_Exception() throws Exception {
635 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
636 doThrow(PolicyException.class).when(spyPolicyEngine)
637 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
639 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
644 public void testStdPolicyEnginDeletePolicy_DeletePolicyParameters_noException()
647 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
649 final ResponseEntity<String> stubbedResponse =
650 new ResponseEntity<>("Successful", HttpStatus.OK);
652 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(DELETE_POLICY_RESOURCE_NAME),
653 eq(HttpMethod.DELETE), any(), any());
655 final PolicyChangeResponse actualResponse =
656 spyPolicyEngine.deletePolicy(new DeletePolicyParameters());
658 assertNotNull(actualResponse);
659 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
663 public void testStdPolicyEnginGetDictionaryItem_DictionaryParameters_noException()
666 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
668 final ResponseEntity<APIDictionaryResponse> stubbedResponse =
669 new ResponseEntity<>(getAPIDictionaryResponse(), HttpStatus.OK);
671 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
672 eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
674 final DictionaryResponse actualResponse =
675 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
677 assertNotNull(actualResponse);
681 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp400ExceptionThrown_ResponseWithHttpCode()
683 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
684 doThrow(new RuntimeException(
685 new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Error 400")))
686 .when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
687 eq(HttpMethod.POST), any(), any());
689 final DictionaryResponse actualResponse =
690 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
692 assertNotNull(actualResponse);
693 assertEquals(HttpStatus.BAD_REQUEST.value(), actualResponse.getResponseCode());
698 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp401ExceptionThrown_ResponseWithHttpCode()
700 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
701 doThrow(new RuntimeException(
702 new HttpClientErrorException(HttpStatus.UNAUTHORIZED, "Error 401")))
703 .when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
704 eq(HttpMethod.POST), any(), any());
706 final DictionaryResponse actualResponse =
707 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
709 assertNotNull(actualResponse);
710 assertEquals(HttpStatus.UNAUTHORIZED.value(), actualResponse.getResponseCode());
715 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithRunTimeExceptionThrown_ResponseWithHttpCode()
717 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
718 doThrow(new RuntimeException(
719 new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error")))
720 .when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
721 eq(HttpMethod.POST), any(), any());
723 final DictionaryResponse actualResponse =
724 spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
726 assertNotNull(actualResponse);
727 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), actualResponse.getResponseCode());
732 public void testStdPolicyEnginCreateDictionaryItem_DictionaryParameters_noException()
735 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
737 final ResponseEntity<String> stubbedResponse =
738 new ResponseEntity<>("Successful", HttpStatus.OK);
740 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
741 eq(CREATE_DICTIONARY_ITEM_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
743 final PolicyChangeResponse actualResponse =
744 spyPolicyEngine.createDictionaryItem(new DictionaryParameters());
746 assertNotNull(actualResponse);
747 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
751 public void testStdPolicyEnginUpdateDictionaryItem_DictionaryParameters_noException()
754 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
756 final ResponseEntity<String> stubbedResponse =
757 new ResponseEntity<>("Successful", HttpStatus.OK);
759 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
760 eq(UPDATE_DICTIONARY_ITEM_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
762 final PolicyChangeResponse actualResponse =
763 spyPolicyEngine.updateDictionaryItem(new DictionaryParameters());
765 assertNotNull(actualResponse);
766 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
770 public void testStdPolicyEnginPolicyEngineImport_ImportParameters_noException()
772 final File emptyfile = temporaryFolder.newFile("emptyFile.txt");
773 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
775 final ResponseEntity<String> stubbedResponse =
776 new ResponseEntity<>("Successful", HttpStatus.OK);
778 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(
779 eq(POLICY_ENGINE_IMPORT_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
781 final ImportParameters importParameters = new ImportParameters();
782 importParameters.setFilePath(emptyfile.toString());
783 final PolicyChangeResponse actualResponse =
784 spyPolicyEngine.policyEngineImport(importParameters);
786 assertNotNull(actualResponse);
787 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
791 public void testStdPolicyEnginCreatePolicy_PolicyParameters_noException() throws Exception {
792 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
794 final ResponseEntity<String> stubbedResponse =
795 new ResponseEntity<>("Successful", HttpStatus.OK);
797 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_POLICY_RESOURCE_NAME),
798 eq(HttpMethod.PUT), any(), any());
800 final PolicyChangeResponse actualResponse =
801 spyPolicyEngine.createPolicy(new PolicyParameters());
803 assertNotNull(actualResponse);
804 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
808 public void testStdPolicyEnginUpdatePolicy_PolicyParameters_noException() throws Exception {
809 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
811 final ResponseEntity<String> stubbedResponse =
812 new ResponseEntity<>("Successful", HttpStatus.OK);
814 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
815 eq(HttpMethod.PUT), any(), any());
817 final PolicyChangeResponse actualResponse =
818 spyPolicyEngine.updatePolicy(new PolicyParameters());
820 assertNotNull(actualResponse);
821 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
824 @Test(expected = PolicyException.class)
825 public void testStdPolicyEnginPushPolicy_NullValues_Exception() throws Exception {
826 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
828 spyPolicyEngine.pushPolicy(null, null, null, null, null);
831 @Test(expected = PolicyException.class)
832 public void testStdPolicyEnginPushPolicy_EmptyPolicyScope_Exception() throws Exception {
833 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
835 spyPolicyEngine.pushPolicy("", null, null, null, null);
838 @Test(expected = PolicyException.class)
839 public void testStdPolicyEnginPushPolicy_EmptyPolicyName_Exception() throws Exception {
840 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
842 spyPolicyEngine.pushPolicy("POLICY_SCOPE", "", null, null, null);
846 public void testStdPolicyEnginPushPolicy_PolicyParameters_noException() throws Exception {
847 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
849 final ResponseEntity<String> stubbedResponse =
850 new ResponseEntity<>("Successful", HttpStatus.OK);
852 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
853 eq(HttpMethod.PUT), any(), any());
855 final String actualResponse = spyPolicyEngine.pushPolicy("POLICY_SCOPE", ONAP_NAME_VAL,
856 "POLICY_TYPE", "POLICY_GROUP", REQUEST_UUID);
858 assertNotNull(actualResponse);
862 public void testStdPolicyEnginCreateUpdateConfigPolicy_PolicyParameters_noException()
864 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
866 final ResponseEntity<String> stubbedResponse =
867 new ResponseEntity<>("Successful", HttpStatus.OK);
869 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
870 eq(HttpMethod.PUT), any(), any());
872 final String actualResponse = spyPolicyEngine.createUpdateConfigPolicy("POLICY_NAME",
873 ONAP_NAME_VAL, ONAP_NAME_VAL, "CONFIG_NAME", Collections.emptyMap(),
874 PolicyType.JSON.toString().toUpperCase(), "", "POLICY_SCOPE", REQUEST_UUID, "", "",
875 "", new Date().toString(), true);
877 assertNotNull(actualResponse);
880 @Test(expected = PolicyException.class)
881 public void testStdPolicyEnginCreateUpdateConfigPolicy_NullPolicyName_Exception()
883 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
885 spyPolicyEngine.createUpdateConfigPolicy(null, null, null, null, null, null, null, null,
886 null, null, null, null, null, true);
890 public void testStdPolicyEnginCreateUpdateConfigFirewallPolicy_PolicyParameters_noException()
892 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
894 final ResponseEntity<String> stubbedResponse =
895 new ResponseEntity<>("Successful", HttpStatus.OK);
897 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME),
898 eq(HttpMethod.PUT), any(), any());
900 final String actualResponse = spyPolicyEngine.createUpdateConfigFirewallPolicy(
901 "POLICY_NAME", getJsonObject(JSON_CONFIGURATION), "POLICY_SCOPE", REQUEST_UUID, "",
902 "", "", new Date().toString(), true);
904 assertNotNull(actualResponse);
907 private JsonObject getJsonObject(final String jsonString) {
908 try (final JsonReader jsonReader = Json.createReader(new StringReader(jsonString));) {
909 return jsonReader.readObject();
913 private StdPolicyEngine getSpyPolicyEngine() throws IOException, PolicyEngineException {
914 return getSpyPolicyEngine(CONFIG_PROPERTIES_FILE, getDefaultProperties());
917 private StdPolicyEngine getSpyPolicyEngine(final String filename, final Properties properties)
918 throws IOException, PolicyEngineException {
919 final File file = temporaryFolder.newFile(filename);
921 creatPropertyFile(file, properties);
923 final StdPolicyEngine spyPolicyEngine =
924 spy(new StdPolicyEngine(file.toString(), (String) null));
925 spyPolicyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
926 return spyPolicyEngine;
929 private Properties getDefaultProperties() {
930 final Properties properties = new Properties();
931 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
932 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
933 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
934 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
935 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
936 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
937 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
938 properties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "false");
942 private StdDecisionResponse getStdDecisionResponse() {
943 final StdDecisionResponse response = new StdDecisionResponse();
944 response.setDecision(PolicyDecision.PERMIT);
945 response.setDetails(PolicyDecision.PERMIT.name());
950 private APIDictionaryResponse getAPIDictionaryResponse() {
951 final APIDictionaryResponse response = new APIDictionaryResponse();
952 response.setResponseCode(0);
953 response.setResponseMessage("");
954 response.setDictionaryData(Collections.<String, String>emptyMap());
955 response.setDictionaryJson(Collections.<String, String>emptyMap());
959 private StdPolicyResponse[] getStdPolicyResponse() {
960 final StdPolicyResponse response = new StdPolicyResponse();
961 response.setPolicyResponseStatus(PolicyResponseStatus.ACTION_TAKEN);
962 return new StdPolicyResponse[] {response};
965 private void creatPropertyFile(final File file, final Properties properties)
967 try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) {
968 properties.store(bufferedWriter, COMMENTS);
972 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
973 final String configuration) {
975 return getAPIPolicyConfigResponse(policyType, configuration, null);
978 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
979 final String configuration, final String policyConfigMessage) {
980 final APIPolicyConfigResponse configResponse = new APIPolicyConfigResponse();
981 configResponse.setConfig(configuration);
982 configResponse.setMatchingConditions(getMatchingConditions());
983 configResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED);
984 configResponse.setPolicyName(POLICY_NAME);
985 configResponse.setPolicyType(PolicyConfigType.BRMS_RAW);
986 configResponse.setType(policyType);
987 configResponse.setPolicyVersion(POLICY_VERSION);
988 configResponse.setProperty(Collections.emptyMap());
989 configResponse.setPolicyConfigMessage(policyConfigMessage);
991 return new APIPolicyConfigResponse[] {configResponse};
994 private Map<String, String> getMatchingConditions() {
995 final Map<String, String> attributes = new HashMap<>();
996 attributes.put(ONAP_NAME, POLICY_NAME);
997 attributes.put(CONFIG_NAME, "Configuration_name");