2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.test;
25 import java.io.StringReader;
26 import java.net.HttpURLConnection;
27 import java.util.Collection;
28 import java.util.HashMap;
30 import java.util.UUID;
31 import javax.json.Json;
32 import javax.json.JsonObject;
33 import javax.json.JsonReader;
34 import org.mockito.Mockito;
35 import org.onap.policy.api.AttributeType;
36 import org.onap.policy.api.ConfigRequestParameters;
37 import org.onap.policy.api.DecisionRequestParameters;
38 import org.onap.policy.api.DecisionResponse;
39 import org.onap.policy.api.DeletePolicyCondition;
40 import org.onap.policy.api.DeletePolicyParameters;
41 import org.onap.policy.api.EventRequestParameters;
42 import org.onap.policy.api.ImportParameters;
43 import org.onap.policy.api.ImportParameters.IMPORT_TYPE;
44 import org.onap.policy.api.NotificationHandler;
45 import org.onap.policy.api.NotificationScheme;
46 import org.onap.policy.api.PDPNotification;
47 import org.onap.policy.api.PolicyChangeResponse;
48 import org.onap.policy.api.PolicyClass;
49 import org.onap.policy.api.PolicyConfig;
50 import org.onap.policy.api.PolicyConfigException;
51 import org.onap.policy.api.PolicyDecisionException;
52 import org.onap.policy.api.PolicyEngine;
53 import org.onap.policy.api.PolicyEngineException;
54 import org.onap.policy.api.PolicyEventException;
55 import org.onap.policy.api.PolicyParameters;
56 import org.onap.policy.api.PolicyResponse;
57 import org.onap.policy.api.PushPolicyParameters;
58 import org.onap.policy.common.logging.flexlogger.FlexLogger;
59 import org.onap.policy.common.logging.flexlogger.Logger;
60 import org.onap.policy.std.StdPDPNotification;
61 import org.onap.policy.std.StdPolicyChangeResponse;
62 import org.onap.policy.std.StdPolicyResponse;
63 import junit.framework.TestCase;
66 * The class <code>PolicyEngineInterfaceTest</code> contains tests for the class
67 * {@link <code>PolicyEngine</code>}
69 * @pattern JUnit Test Case
71 * @generatedBy CodePro at 5/27/16 10:33 AM
76 public class PolicyEngineInterfaceTest extends TestCase {
78 private static final Logger logger = FlexLogger.getLogger(PolicyEngineInterfaceTest.class);
80 private PolicyEngine policyEngine = null;
81 private PolicyEngine mockPolicyEngine = null;
82 private Collection<PolicyConfig> policyConfig = null;
83 private UUID requestID = UUID.randomUUID();
86 PolicyChangeResponse result = null;
87 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
91 * Construct new test instance
93 * @param name the test name
95 public PolicyEngineInterfaceTest(String name) {
100 * Perform pre-test initialization
104 * @see TestCase#setUp()
106 public void setUp() throws Exception {
108 policyEngine = new PolicyEngine("Test/config_pass.properties");
109 } catch (PolicyEngineException e) {
110 logger.error(e.getMessage());
111 fail("PolicyEngine Instantiation Error" + e);
113 logger.info("Loaded.. PolicyEngine");
115 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
116 HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
117 Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
122 * Perform post-test clean up
126 * @see TestCase#tearDown()
128 public void tearDown() throws Exception {
130 // Add additional tear down code here
134 * Run the Collection<PolicyConfig> getConfigByPolicyName(String) method test
136 @SuppressWarnings("deprecation")
137 public void testGetConfigByPolicyName() {
138 String policyName = null;
140 policyConfig = policyEngine.getConfigByPolicyName(policyName);
141 } catch (PolicyConfigException e) {
142 logger.warn(e.getMessage());
144 assertNull(policyConfig);
147 @SuppressWarnings("deprecation")
148 public void testGetConfigByPolicyName2() {
149 String policyName = null;
152 policyConfig = policyEngine.getConfigByPolicyName(policyName, requestID);
153 } catch (PolicyConfigException e) {
154 logger.warn(e.getMessage());
156 assertNull(policyConfig);
160 * Run the Collection<PolicyConfig> getConfig(String) method test
162 @SuppressWarnings("deprecation")
163 public void testGetConfig() {
164 String onapName = null;
167 policyConfig = policyEngine.getConfig(onapName);
168 } catch (PolicyConfigException e) {
169 logger.warn(e.getMessage());
171 assertNull(policyConfig);
174 @SuppressWarnings("deprecation")
175 public void testGetConfig2() {
176 String onapName = null;
179 policyConfig = policyEngine.getConfig(onapName, requestID);
180 } catch (PolicyConfigException e) {
181 logger.warn(e.getMessage());
183 assertNull(policyConfig);
187 @SuppressWarnings("deprecation")
188 public void testGetConfig3() {
189 String onapName = null;
190 String configName = null;
193 policyConfig = policyEngine.getConfig(onapName, configName, requestID);
194 } catch (PolicyConfigException e) {
195 logger.warn(e.getMessage());
197 assertNull(policyConfig);
200 @SuppressWarnings("deprecation")
201 public void testGetConfig4() {
202 String onapName = null;
203 String configName = null;
204 Map<String, String> configAttributes = null;
207 policyConfig = policyEngine.getConfig(onapName, configName, configAttributes);
208 } catch (PolicyConfigException e) {
209 logger.warn(e.getMessage());
211 assertNull(policyConfig);
214 @SuppressWarnings("deprecation")
215 public void testGetConfig5() {
216 String onapName = null;
217 String configName = null;
218 Map<String, String> configAttributes = null;
222 policyEngine.getConfig(onapName, configName, configAttributes, requestID);
223 } catch (PolicyConfigException e) {
224 logger.warn(e.getMessage());
226 assertNull(policyConfig);
229 public void testGetConfig6() {
230 ConfigRequestParameters parameters = new ConfigRequestParameters();
233 policyConfig = policyEngine.getConfig(parameters);
234 } catch (PolicyConfigException e) {
235 logger.warn(e.getMessage());
237 assertNull(policyConfig);
242 * Run the Collection<PolicyResponse> sendEvent(Map<String,String>) method test
244 @SuppressWarnings("deprecation")
245 public void testSendEvent() {
246 Collection<PolicyResponse> result = null;
247 Collection<StdPolicyResponse> response = null;
248 Map<String, String> eventAttributes = null;
251 Mockito.when(mockPolicyEngine.sendEvent(eventAttributes)).thenReturn(result);
252 result = mockPolicyEngine.sendEvent(eventAttributes);
253 } catch (PolicyEventException e) {
254 logger.error("Exception Occured" + e);
258 assertEquals(result, response);
262 @SuppressWarnings("deprecation")
263 public void testSendEvent2() {
264 Collection<PolicyResponse> result = null;
265 Collection<StdPolicyResponse> response = null;
266 Map<String, String> eventAttributes = null;
269 Mockito.when(mockPolicyEngine.sendEvent(eventAttributes, requestID)).thenReturn(result);
270 result = mockPolicyEngine.sendEvent(eventAttributes, requestID);
271 } catch (PolicyEventException e) {
272 logger.error("Exception Occured" + e);
276 assertEquals(result, response);
280 public void testSendEvent3() {
281 Collection<PolicyResponse> result = null;
282 Collection<StdPolicyResponse> response = null;
283 EventRequestParameters parameters = new EventRequestParameters();
286 Mockito.when(mockPolicyEngine.sendEvent(parameters)).thenReturn(result);
287 result = mockPolicyEngine.sendEvent(parameters);
288 } catch (PolicyEventException e) {
289 logger.error("Exception Occured" + e);
292 assertEquals(result, response);
299 * Run the PolicyDecision getDecision(String, Map<String,String>) method test
301 @SuppressWarnings("deprecation")
302 public void testGetDecision() {
303 String onapComponentName = null;
304 Map<String, String> decisionAttributes = null;
306 DecisionResponse result = null;
309 Mockito.when(mockPolicyEngine.getDecision(onapComponentName, decisionAttributes))
311 result = mockPolicyEngine.getDecision(onapComponentName, decisionAttributes);
312 } catch (PolicyDecisionException e) {
313 logger.error("Exception Occured" + e);
316 assertEquals(result, null);
319 @SuppressWarnings("deprecation")
320 public void testGetDecision2() {
321 String onapComponentName = null;
322 Map<String, String> decisionAttributes = null;
324 DecisionResponse result = null;
328 mockPolicyEngine.getDecision(onapComponentName, decisionAttributes, requestID))
330 result = mockPolicyEngine.getDecision(onapComponentName, decisionAttributes);
331 } catch (PolicyDecisionException e) {
332 logger.error("Exception Occured" + e);
335 assertEquals(result, null);
338 public void testGetDecision3() {
339 DecisionRequestParameters parameters = new DecisionRequestParameters();
340 DecisionResponse result = null;
343 Mockito.when(mockPolicyEngine.getDecision(parameters)).thenReturn(null);
344 result = mockPolicyEngine.getDecision(parameters);
345 } catch (PolicyDecisionException e) {
346 logger.error("Exception Occured" + e);
349 assertEquals(result, null);
353 * Run the void setNotification(NotificationScheme, NotificationHandler) method test
355 public void testSetNotification() {
356 // add test code here
358 NotificationScheme scheme = null;
359 NotificationHandler handler = null;
361 Mockito.doNothing().when(mockPolicyEngine).setNotification(scheme, handler);
362 mockPolicyEngine.setNotification(scheme, handler);
367 * Run the void clearNotification() method test
369 public void testClearNotification() {
370 // add test code here
372 Mockito.doNothing().when(mockPolicyEngine).clearNotification();
373 mockPolicyEngine.clearNotification();
378 * Run the void setScheme(NotificationScheme) method test
380 public void testSetScheme() {
381 NotificationScheme scheme = null;
383 Mockito.doNothing().when(mockPolicyEngine).setScheme(scheme);
384 mockPolicyEngine.setScheme(scheme);
389 * Run the PDPNotification getNotification() method test
391 public void testGetNotification() {
392 PDPNotification result = null;
393 StdPDPNotification response = null;
394 Mockito.when(mockPolicyEngine.getNotification()).thenReturn(response);
395 result = mockPolicyEngine.getNotification();
397 assertEquals(result, response);
401 * Run the String createConfigPolicy(String, String, String, String, Map<String,String>, String,
402 * String, String, UUID) method test
404 @SuppressWarnings("deprecation")
405 public void testCreateConfigPolicy() {
406 String response = "success";
407 String result = null;
410 Mockito.when(mockPolicyEngine.createConfigPolicy("testPolicy", "test", "test",
411 "testConfig", null, "OTHER", "test", "test", null, null, null, null, null))
412 .thenReturn(response);
413 result = mockPolicyEngine.createConfigPolicy("testPolicy", "test", "test", "testConfig",
414 null, "OTHER", "test", "test", null, null, null, null, null);
416 } catch (Exception e) {
417 logger.warn(e.getMessage());
419 assertEquals(result, response);
423 * Run the String updateConfigPolicy(String, String, String, String, Map<String,String>, String,
424 * String, String, UUID) method test
426 @SuppressWarnings("deprecation")
427 public void testUpdateConfigPolicy() {
428 String response = "success";
429 String result = null;
432 Mockito.when(mockPolicyEngine.updateConfigPolicy("testPolicy", "test", "test",
433 "testConfig", null, "OTHER", "test", "test", null, null, null, null, null))
434 .thenReturn(response);
435 result = mockPolicyEngine.updateConfigPolicy("testPolicy", "test", "test", "testConfig",
436 null, "OTHER", "test", "test", null, null, null, null, null);
438 } catch (Exception e) {
439 logger.warn(e.getMessage());
441 assertEquals(result, response);
445 * Run the String createConfigFirewallPolicy(String, JsonObject, String, UUID) method test
447 @SuppressWarnings("deprecation")
448 public void testCreateConfigFirewallPolicy() {
449 String response = "success";
450 String result = null;
452 "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":"
453 + "{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":"
454 + "[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\","
455 + "\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\","
456 + "\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\","
457 + "\"appProtocol\":null,\"ports\":\"514\"},{\"name\":\"SList\","
458 + "\"description\":\"Service List\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\","
459 + "\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":[{\"name\":\"1607Group\","
460 + "\"description\":null,\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"},"
461 + "{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},{\"name\":\"PL_CCE3\","
462 + "\"description\":\"CCE Routers\",\"members\":[{\"type\":\"SUBNET\","
463 + "\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":[{\"position\":\"1\","
464 + "\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],\"toZones\":[\"Untrusted\"],"
465 + "\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\","
466 + "\"value\":\"PL_CCE3\"},{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],"
467 + "\"destinationList\":[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],"
468 + "\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],"
469 + "\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,"
471 JsonObject jsonObj = buildJSON(json);
474 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy", jsonObj, "test",
475 null, null, null, null, null)).thenReturn(response);
476 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy", jsonObj, "test",
477 null, null, null, null, null);
479 } catch (Exception e) {
480 logger.warn(e.getMessage());
482 assertEquals(result, response);
486 * Run the String updateConfigFirewallPolicy(String, JsonObject, String, UUID) method test
488 @SuppressWarnings("deprecation")
489 public void testUpdateConfigFirewallPolicy() {
490 String response = "success";
491 String result = null;
493 "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":"
494 + "{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":"
495 + "[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\","
496 + "\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\","
497 + "\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\","
498 + "\"appProtocol\":null,\"ports\":\"514\"},{\"name\":\"SList\","
499 + "\"description\":\"Service List\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\","
500 + "\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":[{\"name\":\"1607Group\","
501 + "\"description\":null,\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"},"
502 + "{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},{\"name\":\"PL_CCE3\","
503 + "\"description\":\"CCE Routers\",\"members\":[{\"type\":\"SUBNET\","
504 + "\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":[{\"position\":\"1\","
505 + "\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],\"toZones\":[\"Untrusted\"],"
506 + "\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\","
507 + "\"value\":\"PL_CCE3\"},{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],"
508 + "\"destinationList\":[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],"
509 + "\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],"
510 + "\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,"
512 JsonObject jsonObj = buildJSON(json);
515 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy", jsonObj, "test",
516 null, null, null, null, null)).thenReturn(response);
517 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy", jsonObj, "test",
518 null, null, null, null, null);
520 } catch (Exception e) {
521 logger.warn(e.getMessage());
523 assertEquals(result, response);
527 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
529 public void testCreatePolicy() {
530 response.setResponseMessage("success");
531 PolicyChangeResponse result = null;
532 PolicyParameters policyParameters = new PolicyParameters();
534 policyParameters.setPolicyClass(PolicyClass.Action); // required
535 policyParameters.setPolicyName("test.junitTest"); // required
536 policyParameters.setPolicyDescription("testing"); // optional
538 // Set the Component Attributes... These are Optional
539 Map<String, String> configAttributes = new HashMap<String, String>();
540 configAttributes.put("test", "testing");
542 Map<AttributeType, Map<String, String>> attributes =
543 new HashMap<AttributeType, Map<String, String>>();
544 attributes.put(AttributeType.MATCHING, configAttributes);
545 policyParameters.setAttributes(attributes);
547 policyParameters.setActionPerformer("PEP");
548 policyParameters.setActionAttribute("testing");
549 policyParameters.setRequestID(UUID.randomUUID());
553 // stdPolicyEngine = Mockito.mock(StdPolicyEngine.class);
554 // Mockito.when(stdPolicyEngine.callPAP(newPAPPolicy, new String[] {"operation=create",
555 // "apiflag=api", "policyType=Action"}, null, "Action")).thenReturn(callPapResponse);
556 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
557 result = mockPolicyEngine.createPolicy(policyParameters);
559 } catch (Exception e) {
560 logger.warn(e.getMessage());
561 logger.error("Exception Occured" + e);
563 assertEquals(result, response);
567 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
569 public void testUpdatePolicy() {
570 response.setResponseMessage("success");
571 PolicyChangeResponse result = null;
572 PolicyParameters policyParameters = new PolicyParameters();
574 policyParameters.setPolicyClass(PolicyClass.Action); // required
575 policyParameters.setPolicyName("test.junitTest"); // required
576 policyParameters.setPolicyDescription("testing"); // optional
578 // Set the Component Attributes... These are Optional
579 Map<String, String> configAttributes = new HashMap<String, String>();
580 configAttributes.put("test", "testing");
582 Map<AttributeType, Map<String, String>> attributes =
583 new HashMap<AttributeType, Map<String, String>>();
584 attributes.put(AttributeType.MATCHING, configAttributes);
585 policyParameters.setAttributes(attributes);
587 policyParameters.setActionPerformer("PEP");
588 policyParameters.setActionAttribute("testing");
589 policyParameters.setRequestID(UUID.randomUUID());
593 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
594 result = mockPolicyEngine.updatePolicy(policyParameters);
596 } catch (Exception e) {
597 logger.warn(e.getMessage());
599 assertEquals(result, response);
603 * Run the String pushPolicy(String, String, String, String, UUID) method test
605 @SuppressWarnings("deprecation")
606 public void testPushPolicy() {
607 String response = "Success";
608 String result = null;
612 mockPolicyEngine.pushPolicy("testing", "test", "Base", "default", requestID))
613 .thenReturn(response);
614 result = mockPolicyEngine.pushPolicy("testing", "test", "Base", "default", requestID);
616 } catch (Exception e) {
617 logger.warn(e.getMessage());
620 assertEquals(result, response);
623 public void testPushPolicy2() {
624 PushPolicyParameters policyParameters = new PushPolicyParameters();
625 PolicyChangeResponse result = null;
627 // String policyScope = null;
628 policyParameters.setPolicyName("test.junitTest");
629 policyParameters.setPolicyType("Action");
630 policyParameters.setPdpGroup("Default");
634 Mockito.when(mockPolicyEngine.pushPolicy(policyParameters)).thenReturn(response);
635 result = mockPolicyEngine.pushPolicy(policyParameters);
637 } catch (Exception e) {
638 logger.warn(e.getMessage());
641 assertEquals(result, response);
645 * Run the PolicyChangeResponse deletePolicy(DeletePolicyParameters) method test
647 public void testDeletePolicy() {
648 DeletePolicyParameters policyParameters = new DeletePolicyParameters();
649 PolicyChangeResponse result = null;
651 // String policyScope = null;
652 policyParameters.setPolicyName("test.junitTest.1.xml");
653 policyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
654 policyParameters.setPolicyComponent("PAP");
655 policyParameters.setPdpGroup("Default");
659 Mockito.when(mockPolicyEngine.deletePolicy(policyParameters)).thenReturn(response);
660 result = mockPolicyEngine.deletePolicy(policyParameters);
662 } catch (Exception e) {
663 logger.warn(e.getMessage());
666 assertEquals(result, response);
670 * Run the PolicyChangeResponse policyEngineImport(ImportParameters) method test
672 public void testPolicyEngineImport() {
673 ImportParameters importParameters = new ImportParameters();
674 PolicyChangeResponse result = null;
676 importParameters.setFilePath(
677 "C:\\Workspaces\\models\\TestingModel\\ControllerServiceSampleSdnlServiceInstance-v0.1.0-SNAPSHOT.zip");
678 importParameters.setServiceName("ControllerServiceSampleSdnlServiceInstance");
680 importParameters.setRequestID(UUID.randomUUID());
681 importParameters.setServiceType(IMPORT_TYPE.MICROSERVICE);
682 importParameters.setVersion("1607-2");
686 Mockito.when(mockPolicyEngine.policyEngineImport(importParameters))
687 .thenReturn(response);
688 result = mockPolicyEngine.policyEngineImport(importParameters);
690 } catch (Exception e) {
691 logger.warn(e.getMessage());
694 assertEquals(result, response);
697 private static JsonObject buildJSON(String jsonString) {
698 JsonObject json = null;;
699 if (jsonString != null) {
700 StringReader in = null;
702 in = new StringReader(jsonString);
704 JsonReader jsonReader = Json.createReader(in);
705 json = jsonReader.readObject();