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 testStdPolicyEngineInitializeWithSingleServerName_noException() throws Exception {
148 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
150 final Properties properties = new Properties();
151 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
152 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
153 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
154 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
155 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
156 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
157 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
158 creatPropertyFile(file, properties);
160 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
161 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
162 assertEquals(Arrays.asList(SERVER_NAME), policyEngine.getNotificationURLList());
166 public void testStdPolicyEngineInitializeWithSingleNotificationType_noException() throws Exception {
167 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
169 final Properties properties = new Properties();
170 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
171 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
172 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
173 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB);
174 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME);
175 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
176 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
177 creatPropertyFile(file, properties);
179 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
180 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
181 assertEquals(Arrays.asList(UEB), policyEngine.getNotificationType());
184 @Test(expected = PolicyEngineException.class)
185 public void testStdPolicyEngineInitialize_InvalidPropertyFile_Exception() throws PolicyEngineException {
186 new StdPolicyEngine("Invalid.properties", (String) null);
189 @Test(expected = PolicyEngineException.class)
190 public void testStdPolicyEngineInitialize_InvalidPropertyFileWithExt_Exception() throws Exception {
191 final File emptyFile = temporaryFolder.newFile("EmptyFile.txt");
192 new StdPolicyEngine(emptyFile.toString(), (String) null);
195 @Test(expected = PolicyEngineException.class)
196 public void testStdPolicyEngineInitialize_NullArguments_Exception() throws Exception {
197 new StdPolicyEngine(null, (String) null);
200 @Test(expected = PolicyEngineException.class)
201 public void testStdPolicyEngineInitialize_PropertyFileMissingMandatoryProperties_Exception() throws Exception {
202 final File file = temporaryFolder.newFile(INVALID_CONFIG_PROPERTY_FILE);
203 final Properties properties = new Properties();
204 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
205 creatPropertyFile(file, properties);
207 new StdPolicyEngine(file.toString(), (String) null);
211 public void testStdPolicyEngineInitialize_MultiplePdp_noException() throws Exception {
212 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
214 final Properties properties = new Properties();
215 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE + PolicyConfigConstants.SEMICOLLON + PDP_PROP_VALUE_1);
216 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
217 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
218 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
219 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
220 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
221 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
222 creatPropertyFile(file, properties);
224 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
225 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
226 assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
227 StdPolicyEngine.rotatePDPList();
228 assertEquals("https://localhost:8091/pdp/", StdPolicyEngine.getPDPURL());
229 assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
230 assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
233 @Test(expected = PolicyEngineException.class)
234 public void testStdPolicyEngineInitialize_NoPDP_noException() throws Exception {
235 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
237 final Properties properties = new Properties();
238 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
239 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
240 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
241 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
242 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
243 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
244 creatPropertyFile(file, properties);
246 new StdPolicyEngine(file.toString(), (String) null);
250 public void testStdPolicyEngineInitializeNotificationTypeDMMAP_noException() throws Exception {
251 final File file = temporaryFolder.newFile(CONFIG_PROPERTIES_FILE);
253 final Properties properties = new Properties();
254 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
255 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
256 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
257 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, DMAAP);
258 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
259 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
260 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
261 creatPropertyFile(file, properties);
263 final StdPolicyEngine policyEngine = new StdPolicyEngine(file.toString(), (String) null);
264 policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
266 assertEquals(Arrays.asList(DMAAP), policyEngine.getNotificationType());
271 public void testStdPolicyEngineSendEvent_noException() throws Exception {
272 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
273 doReturn(new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED)).when(spyPolicyEngine)
274 .callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
276 final Collection<PolicyResponse> actualPolicyResponses =
277 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
279 assertEquals(1, actualPolicyResponses.size());
283 @Test(expected = PolicyEventException.class)
284 public void testStdPolicyEngineSendEvent_NullEventRequestParameters_Exception() throws Exception {
285 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
286 spyPolicyEngine.sendEvent((EventRequestParameters) null);
289 @Test(expected = PolicyEventException.class)
290 public void testStdPolicyEngineSendEvent_EventRequestParameters_CallPDPThrow401Exception_Exception()
292 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
293 Mockito.doThrow(new RuntimeException(new RestClientException("Error 401"))).when(spyPolicyEngine)
294 .callNewPDP(any(), any(), any(), any());
295 spyPolicyEngine.sendEvent(Collections.emptyMap(), REQUEST_UUID);
299 public void testStdPolicyEngineSendEvent_EventRequestParameters_noException() throws Exception {
300 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
302 final ResponseEntity<StdPolicyResponse[]> stubbedResponse =
303 new ResponseEntity<>(getStdPolicyResponse(), HttpStatus.ACCEPTED);
305 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(SEND_EVENT_RESOURCE_NAME), any(), any(), any());
307 final EventRequestParameters eventRequestParameters = new EventRequestParameters();
308 eventRequestParameters.setEventAttributes(Collections.emptyMap());
309 eventRequestParameters.setRequestID(REQUEST_UUID);
311 final Collection<PolicyResponse> actualPolicyResponses = spyPolicyEngine.sendEvent(eventRequestParameters);
313 assertEquals(1, actualPolicyResponses.size());
318 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigJSON_noException() throws Exception {
319 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
321 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
322 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION), HttpStatus.ACCEPTED);
324 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
327 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
329 assertEquals(1, actualPolicyResponses.size());
331 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
332 assertNotNull(actualPolicyConfig.toJSON());
337 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigOther_noException() throws Exception {
338 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
340 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
341 new ResponseEntity<>(getAPIPolicyConfigResponse(PolicyType.OTHER, COMMENTS), HttpStatus.ACCEPTED);
343 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
346 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
348 assertEquals(1, actualPolicyResponses.size());
350 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
351 assertNotNull(actualPolicyConfig.toOther());
356 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigXML_noException() throws Exception {
357 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
359 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse = new ResponseEntity<>(
360 getAPIPolicyConfigResponse(PolicyType.XML, XML_CONFIGURATION), HttpStatus.ACCEPTED);
362 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
365 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
367 assertEquals(1, actualPolicyResponses.size());
369 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
370 assertNotNull(actualPolicyConfig.toXML());
375 public void testStdPolicyEngineGetConfig_ConfigRequestParametersPolicyConfigProperties_noException()
377 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
379 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
380 new ResponseEntity<>(getAPIPolicyConfigResponse(PolicyType.PROPERTIES, COMMENTS), HttpStatus.ACCEPTED);
382 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
385 final Collection<PolicyConfig> actualPolicyResponses = spyPolicyEngine.getConfig(new ConfigRequestParameters());
387 assertEquals(1, actualPolicyResponses.size());
389 final PolicyConfig actualPolicyConfig = actualPolicyResponses.iterator().next();
390 assertNotNull(actualPolicyConfig.toProperties());
394 @Test(expected = PolicyConfigException.class)
395 public void testStdPolicyEngineSendEvent_ConfigRequestParameters_CallPDPThrow404Exception_Exception()
397 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
398 doThrow(new RuntimeException(new RestClientException("Error 404"))).when(spyPolicyEngine).callNewPDP(any(),
399 any(), any(), any());
400 spyPolicyEngine.getConfig(new ConfigRequestParameters());
404 public void testStdPolicyEngineListConfig_ConfigRequestParametersPolicyConfigProperties_noException()
406 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
408 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
409 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION);
411 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
412 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
414 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
417 final Collection<String> actualResponse = spyPolicyEngine.listConfig(new ConfigRequestParameters());
418 assertEquals(1, actualResponse.size());
419 assertNotNull(actualResponse.iterator().next());
424 public void testStdPolicyEngineListConfig_ConfigRequestParametersMessageConfigContainsPE300_noException()
426 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
428 final APIPolicyConfigResponse[] apiPolicyConfigResponse =
429 getAPIPolicyConfigResponse(PolicyType.JSON, JSON_CONFIGURATION, PolicyConfigConstants.PE300);
431 final ResponseEntity<APIPolicyConfigResponse[]> stubbedResponse =
432 new ResponseEntity<>(apiPolicyConfigResponse, HttpStatus.ACCEPTED);
434 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_CONFIG_RESOURCE_NAME), eq(HttpMethod.POST),
437 final Collection<String> actualResponse = spyPolicyEngine.listConfig(new ConfigRequestParameters());
439 assertEquals(1, actualResponse.size());
444 public void testStdPolicyEngineListConfig_ConfigRequestParametersWithTestProperty_noException() throws Exception {
445 final Properties defaultProperties = getDefaultProperties();
446 defaultProperties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "test");
447 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine("test" + CONFIG_PROPERTIES_FILE, defaultProperties);
449 final Collection<String> actualResponse = spyPolicyEngine.listConfig(new ConfigRequestParameters());
451 assertEquals(1, actualResponse.size());
456 public void testStdPolicyEnginGetDecision_PolicyDecision_noException() throws Exception {
458 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
460 final ResponseEntity<StdDecisionResponse> stubbedResponse =
461 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
463 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST),
466 final DecisionResponse actualResponse =
467 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
469 assertNotNull(actualResponse);
472 @Test(expected = PolicyDecisionException.class)
473 public void testStdPolicyEngineGetDecision_PolicyDecision_CallPDPThrow400Exception_Exception() throws Exception {
474 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
475 doThrow(new RuntimeException(new RestClientException("Error 400"))).when(spyPolicyEngine)
476 .callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
477 spyPolicyEngine.getDecision(ONAP_NAME_VAL, Collections.emptyMap(), REQUEST_UUID);
481 public void testStdPolicyEnginGetDecision_DecisionRequestParameters_noException() throws Exception {
483 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
485 final ResponseEntity<StdDecisionResponse> stubbedResponse =
486 new ResponseEntity<>(getStdDecisionResponse(), HttpStatus.ACCEPTED);
488 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DECISION_RESOURCE_NAME), eq(HttpMethod.POST),
491 final DecisionRequestParameters requestParameters = new DecisionRequestParameters();
492 requestParameters.setOnapName(ONAP_NAME_VAL);
493 requestParameters.setRequestID(REQUEST_UUID);
494 requestParameters.setDecisionAttributes(Collections.emptyMap());
496 final DecisionResponse actualResponse = spyPolicyEngine.getDecision(requestParameters);
498 assertNotNull(actualResponse);
501 @Test(expected = PolicyDecisionException.class)
502 public void ttestStdPolicyEnginGetDecision_NullDecisionRequestParameters_Exception() throws Exception {
503 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
504 spyPolicyEngine.getDecision((DecisionRequestParameters) null);
508 public void testStdPolicyEnginGetMetrics_MetricsRequestParameters_noException() throws Exception {
510 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
512 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Metrics", HttpStatus.ACCEPTED);
514 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET),
517 final MetricsResponse actualResponse = spyPolicyEngine.getMetrics(new MetricsRequestParameters());
518 assertNotNull(actualResponse);
519 assertEquals(HttpStatus.ACCEPTED.value(), actualResponse.getResponseCode());
524 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowHttpException_ResponseWithHttpCode()
526 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
527 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY))).when(spyPolicyEngine)
528 .callNewPDP(eq(GET_METRICS_RESOURCE_NAME), eq(HttpMethod.GET), any(), any());
530 final MetricsResponse actualResponse = spyPolicyEngine.getMetrics(new MetricsRequestParameters());
531 assertNotNull(actualResponse);
532 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
536 @Test(expected = PolicyException.class)
537 public void testStdPolicyEngineGetMetrics_MetricsRequestParametersCallPDPThrowPolicyException_Exception()
539 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
540 doThrow(PolicyException.class).when(spyPolicyEngine).callNewPDP(eq(GET_METRICS_RESOURCE_NAME),
541 eq(HttpMethod.GET), any(), any());
543 spyPolicyEngine.getMetrics(new MetricsRequestParameters());
548 public void testStdPolicyEnginPushPolicy_PushPolicyParameters_noException() throws Exception {
550 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
552 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
554 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
557 final PolicyChangeResponse actualResponse = spyPolicyEngine.pushPolicy(new PushPolicyParameters());
559 assertNotNull(actualResponse);
560 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
564 public void testStdPolicyEnginePushPolicy_PushPolicyParametersThrowsHttpClientErrorException_ResponseWithHttpCode()
566 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
567 doThrow(new PolicyException(new HttpClientErrorException(HttpStatus.BAD_GATEWAY))).when(spyPolicyEngine)
568 .callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT), any(), any());
570 final PolicyChangeResponse actualResponse = spyPolicyEngine.pushPolicy(new PushPolicyParameters());
572 assertNotNull(actualResponse);
573 assertEquals(HttpStatus.BAD_GATEWAY.value(), actualResponse.getResponseCode());
577 @Test(expected = PolicyException.class)
578 public void testStdPolicyEnginePushPolicy_PushPolicyParameters_Exception() throws Exception {
579 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
580 doThrow(PolicyException.class).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME),
581 eq(HttpMethod.PUT), any(), any());
583 spyPolicyEngine.pushPolicy(new PushPolicyParameters());
588 public void testStdPolicyEnginDeletePolicy_DeletePolicyParameters_noException() throws Exception {
590 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
592 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
594 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(DELETE_POLICY_RESOURCE_NAME),
595 eq(HttpMethod.DELETE), any(), any());
597 final PolicyChangeResponse actualResponse = spyPolicyEngine.deletePolicy(new DeletePolicyParameters());
599 assertNotNull(actualResponse);
600 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
604 public void testStdPolicyEnginGetDictionaryItem_DictionaryParameters_noException() throws Exception {
606 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
608 final ResponseEntity<APIDictionaryResponse> stubbedResponse =
609 new ResponseEntity<>(getAPIDictionaryResponse(), HttpStatus.OK);
611 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME),
612 eq(HttpMethod.POST), any(), any());
614 final DictionaryResponse actualResponse = spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
616 assertNotNull(actualResponse);
620 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp400ExceptionThrown_ResponseWithHttpCode()
622 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
623 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Error 400")))
624 .when(spyPolicyEngine)
625 .callNewPDP(eq(GET_DICTIONARY_ITEMS_RESOURCE_NAME), eq(HttpMethod.POST), any(), any());
627 final DictionaryResponse actualResponse = spyPolicyEngine.getDictionaryItem(new DictionaryParameters());
629 assertNotNull(actualResponse);
630 assertEquals(HttpStatus.BAD_REQUEST.value(), actualResponse.getResponseCode());
635 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithHttp401ExceptionThrown_ResponseWithHttpCode()
637 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
638 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.UNAUTHORIZED, "Error 401")))
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.UNAUTHORIZED.value(), actualResponse.getResponseCode());
650 public void testStdPolicyGetDictionaryItem_DictionaryParametersWithRunTimeExceptionThrown_ResponseWithHttpCode()
652 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
653 doThrow(new RuntimeException(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error")))
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.INTERNAL_SERVER_ERROR.value(), actualResponse.getResponseCode());
665 public void testStdPolicyEnginCreateDictionaryItem_DictionaryParameters_noException() throws Exception {
667 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
669 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
671 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_DICTIONARY_ITEM_RESOURCE_NAME),
672 eq(HttpMethod.PUT), any(), any());
674 final PolicyChangeResponse actualResponse = spyPolicyEngine.createDictionaryItem(new DictionaryParameters());
676 assertNotNull(actualResponse);
677 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
681 public void testStdPolicyEnginUpdateDictionaryItem_DictionaryParameters_noException() throws Exception {
683 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
685 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
687 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_DICTIONARY_ITEM_RESOURCE_NAME),
688 eq(HttpMethod.PUT), any(), any());
690 final PolicyChangeResponse actualResponse = spyPolicyEngine.updateDictionaryItem(new DictionaryParameters());
692 assertNotNull(actualResponse);
693 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
697 public void testStdPolicyEnginPolicyEngineImport_ImportParameters_noException() throws Exception {
698 final File emptyfile = temporaryFolder.newFile("emptyFile.txt");
699 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
701 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
703 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(POLICY_ENGINE_IMPORT_RESOURCE_NAME),
704 eq(HttpMethod.POST), any(), any());
706 final ImportParameters importParameters = new ImportParameters();
707 importParameters.setFilePath(emptyfile.toString());
708 final PolicyChangeResponse actualResponse = spyPolicyEngine.policyEngineImport(importParameters);
710 assertNotNull(actualResponse);
711 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
715 public void testStdPolicyEnginCreatePolicy_PolicyParameters_noException() throws Exception {
716 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
718 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
720 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(CREATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
723 final PolicyChangeResponse actualResponse = spyPolicyEngine.createPolicy(new PolicyParameters());
725 assertNotNull(actualResponse);
726 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
730 public void testStdPolicyEnginUpdatePolicy_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(UPDATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
738 final PolicyChangeResponse actualResponse = spyPolicyEngine.updatePolicy(new PolicyParameters());
740 assertNotNull(actualResponse);
741 assertEquals(HttpStatus.OK.value(), actualResponse.getResponseCode());
744 @Test(expected = PolicyException.class)
745 public void testStdPolicyEnginPushPolicy_NullValues_Exception() throws Exception {
746 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
748 spyPolicyEngine.pushPolicy(null, null, null, null, null);
751 @Test(expected = PolicyException.class)
752 public void testStdPolicyEnginPushPolicy_EmptyPolicyScope_Exception() throws Exception {
753 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
755 spyPolicyEngine.pushPolicy("", null, null, null, null);
758 @Test(expected = PolicyException.class)
759 public void testStdPolicyEnginPushPolicy_EmptyPolicyName_Exception() throws Exception {
760 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
762 spyPolicyEngine.pushPolicy("POLICY_SCOPE", "", null, null, null);
766 public void testStdPolicyEnginPushPolicy_PolicyParameters_noException() throws Exception {
767 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
769 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
771 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(PUSH_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
774 final String actualResponse =
775 spyPolicyEngine.pushPolicy("POLICY_SCOPE", ONAP_NAME_VAL, "POLICY_TYPE", "POLICY_GROUP", REQUEST_UUID);
777 assertNotNull(actualResponse);
781 public void testStdPolicyEnginCreateUpdateConfigPolicy_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(UPDATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
789 final String actualResponse = spyPolicyEngine.createUpdateConfigPolicy("POLICY_NAME", ONAP_NAME_VAL,
790 ONAP_NAME_VAL, "CONFIG_NAME", Collections.emptyMap(), PolicyType.JSON.toString().toUpperCase(), "",
791 "POLICY_SCOPE", REQUEST_UUID, "", "", "", new Date().toString(), true);
793 assertNotNull(actualResponse);
796 @Test(expected = PolicyException.class)
797 public void testStdPolicyEnginCreateUpdateConfigPolicy_NullPolicyName_Exception() throws Exception {
798 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
800 spyPolicyEngine.createUpdateConfigPolicy(null, null, null, null, null, null, null, null, null, null, null, null,
805 public void testStdPolicyEnginCreateUpdateConfigFirewallPolicy_PolicyParameters_noException() throws Exception {
806 final StdPolicyEngine spyPolicyEngine = getSpyPolicyEngine();
808 final ResponseEntity<String> stubbedResponse = new ResponseEntity<>("Successful", HttpStatus.OK);
810 doReturn(stubbedResponse).when(spyPolicyEngine).callNewPDP(eq(UPDATE_POLICY_RESOURCE_NAME), eq(HttpMethod.PUT),
813 final String actualResponse =
814 spyPolicyEngine.createUpdateConfigFirewallPolicy("POLICY_NAME", getJsonObject(JSON_CONFIGURATION),
815 "POLICY_SCOPE", REQUEST_UUID, "", "", "", new Date().toString(), true);
817 assertNotNull(actualResponse);
820 private JsonObject getJsonObject(final String jsonString) {
821 try (final JsonReader jsonReader = Json.createReader(new StringReader(jsonString));) {
822 return jsonReader.readObject();
826 private StdPolicyEngine getSpyPolicyEngine() throws IOException, PolicyEngineException {
827 return getSpyPolicyEngine(CONFIG_PROPERTIES_FILE, getDefaultProperties());
830 private StdPolicyEngine getSpyPolicyEngine(final String filename, final Properties properties)
831 throws IOException, PolicyEngineException {
832 final File file = temporaryFolder.newFile(filename);
834 creatPropertyFile(file, properties);
836 final StdPolicyEngine spyPolicyEngine = spy(new StdPolicyEngine(file.toString(), (String) null));
837 spyPolicyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
838 return spyPolicyEngine;
841 private Properties getDefaultProperties() {
842 final Properties properties = new Properties();
843 properties.setProperty(PDP_URL_PROP_NAME, PDP_PROP_VALUE);
844 properties.setProperty(CLIENT_ID_PROP_NAME, "test");
845 properties.setProperty(CLIENT_KEY_PROP_NAME, "test");
846 properties.setProperty(NOTIFICATION_TYPE_PROP_NAME, UEB + COMMA + DMAAP);
847 properties.setProperty(NOTIFICATION_SERVERS_PROP_NAME, SERVER_NAME + COMMA + SERVER_NAME);
848 properties.setProperty(NOTIFICATION_TOPIC_PROP_NAME, "test");
849 properties.setProperty(ENVIRONMENT_PROP_NAME, "TEST");
850 properties.setProperty(PolicyConfigConstants.JUNIT_PROP_NAME, "false");
854 private StdDecisionResponse getStdDecisionResponse() {
855 final StdDecisionResponse response = new StdDecisionResponse();
856 response.setDecision(PolicyDecision.PERMIT);
857 response.setDetails(PolicyDecision.PERMIT.name());
862 private APIDictionaryResponse getAPIDictionaryResponse() {
863 final APIDictionaryResponse response = new APIDictionaryResponse();
864 response.setResponseCode(0);
865 response.setResponseMessage("");
866 response.setDictionaryData(Collections.<String, String>emptyMap());
867 response.setDictionaryJson(Collections.<String, String>emptyMap());
871 private StdPolicyResponse[] getStdPolicyResponse() {
872 final StdPolicyResponse response = new StdPolicyResponse();
873 response.setPolicyResponseStatus(PolicyResponseStatus.ACTION_TAKEN);
874 return new StdPolicyResponse[] {response};
877 private void creatPropertyFile(final File file, final Properties properties) throws IOException {
878 try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) {
879 properties.store(bufferedWriter, COMMENTS);
883 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
884 final String configuration) {
886 return getAPIPolicyConfigResponse(policyType, configuration, null);
889 private APIPolicyConfigResponse[] getAPIPolicyConfigResponse(final PolicyType policyType,
890 final String configuration, final String policyConfigMessage) {
891 final APIPolicyConfigResponse configResponse = new APIPolicyConfigResponse();
892 configResponse.setConfig(configuration);
893 configResponse.setMatchingConditions(getMatchingConditions());
894 configResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED);
895 configResponse.setPolicyName(POLICY_NAME);
896 configResponse.setPolicyType(PolicyConfigType.BRMS_RAW);
897 configResponse.setType(policyType);
898 configResponse.setPolicyVersion(POLICY_VERSION);
899 configResponse.setProperty(Collections.emptyMap());
900 configResponse.setPolicyConfigMessage(policyConfigMessage);
902 return new APIPolicyConfigResponse[] {configResponse};
905 private Map<String, String> getMatchingConditions() {
906 final Map<String, String> attributes = new HashMap<>();
907 attributes.put(ONAP_NAME, POLICY_NAME);
908 attributes.put(CONFIG_NAME, "Configuration_name");