2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.test;
23 import java.io.StringReader;
24 import java.net.HttpURLConnection;
25 import java.util.Collection;
26 import java.util.HashMap;
28 import java.util.UUID;
30 import javax.json.Json;
31 import javax.json.JsonObject;
32 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;
64 import junit.framework.TestCase;
67 * The class <code>PolicyEngineInterfaceTest</code> contains tests for the
68 * class {@link <code>PolicyEngine</code>}
70 * @pattern JUnit Test Case
72 * @generatedBy CodePro at 5/27/16 10:33 AM
77 public class PolicyEngineInterfaceTest extends TestCase {
79 private static final Logger logger = FlexLogger.getLogger(PolicyEngineInterfaceTest.class);
81 private PolicyEngine policyEngine = null;
82 private PolicyEngine mockPolicyEngine = null;
83 private Collection<PolicyConfig> policyConfig = null;
84 private UUID requestID = UUID.randomUUID();
87 PolicyChangeResponse result = null;
88 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
92 * Construct new test instance
94 * @param name the test name
96 public PolicyEngineInterfaceTest(String name) {
101 * Perform pre-test initialization
105 * @see TestCase#setUp()
107 public void setUp() throws Exception {
109 policyEngine = new PolicyEngine("Test/config_pass.properties");
110 } catch (PolicyEngineException e) {
111 logger.error(e.getMessage());
112 fail("PolicyEngine Instantiation Error" + e);
114 logger.info("Loaded.. PolicyEngine");
116 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
117 HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
118 Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
123 * Perform post-test clean up
127 * @see TestCase#tearDown()
129 public void tearDown() throws Exception {
131 // Add additional tear down code here
135 * Run the Collection<PolicyConfig> getConfigByPolicyName(String) method
138 @SuppressWarnings("deprecation")
139 public void testGetConfigByPolicyName() {
140 String policyName = null;
142 policyConfig = policyEngine.getConfigByPolicyName(policyName);
143 } catch (PolicyConfigException e){
144 logger.warn(e.getMessage());
146 assertNull(policyConfig);
149 @SuppressWarnings("deprecation")
150 public void testGetConfigByPolicyName2() {
151 String policyName = null;
154 policyConfig = policyEngine.getConfigByPolicyName(policyName, requestID);
155 } catch (PolicyConfigException e){
156 logger.warn(e.getMessage());
158 assertNull(policyConfig);
162 * Run the Collection<PolicyConfig> getConfig(String) method test
164 @SuppressWarnings("deprecation")
165 public void testGetConfig() {
166 String onapName = null;
169 policyConfig = policyEngine.getConfig(onapName);
170 } catch (PolicyConfigException e){
171 logger.warn(e.getMessage());
173 assertNull(policyConfig);
176 @SuppressWarnings("deprecation")
177 public void testGetConfig2() {
178 String onapName = null;
181 policyConfig = policyEngine.getConfig(onapName,requestID);
182 } catch (PolicyConfigException e){
183 logger.warn(e.getMessage());
185 assertNull(policyConfig);
189 @SuppressWarnings("deprecation")
190 public void testGetConfig3() {
191 String onapName = null;
192 String configName = null;
195 policyConfig = policyEngine.getConfig(onapName,configName,requestID);
196 } catch (PolicyConfigException e){
197 logger.warn(e.getMessage());
199 assertNull(policyConfig);
202 @SuppressWarnings("deprecation")
203 public void testGetConfig4() {
204 String onapName = null;
205 String configName = null;
206 Map<String,String> configAttributes = null;
209 policyConfig = policyEngine.getConfig(onapName,configName,configAttributes);
210 } catch (PolicyConfigException e){
211 logger.warn(e.getMessage());
213 assertNull(policyConfig);
216 @SuppressWarnings("deprecation")
217 public void testGetConfig5() {
218 String onapName = null;
219 String configName = null;
220 Map<String,String> configAttributes = null;
223 policyConfig = policyEngine.getConfig(onapName,configName,configAttributes,requestID);
224 } catch (PolicyConfigException e){
225 logger.warn(e.getMessage());
227 assertNull(policyConfig);
230 public void testGetConfig6() {
231 ConfigRequestParameters parameters = new ConfigRequestParameters();
234 policyConfig = policyEngine.getConfig(parameters);
235 } catch (PolicyConfigException e){
236 logger.warn(e.getMessage());
238 assertNull(policyConfig);
243 * Run the Collection<PolicyResponse> sendEvent(Map<String,String>) method
246 @SuppressWarnings("deprecation")
247 public void testSendEvent()
249 Collection<PolicyResponse> result = null;
250 Collection<StdPolicyResponse> response = null;
251 Map<String,String> eventAttributes = null;
254 Mockito.when(mockPolicyEngine.sendEvent(eventAttributes)).thenReturn(result);
255 result = mockPolicyEngine.sendEvent(eventAttributes);
256 } catch (PolicyEventException e) {
257 logger.error("Exception Occured"+e);
261 assertEquals(result,response);
265 @SuppressWarnings("deprecation")
266 public void testSendEvent2()
268 Collection<PolicyResponse> result = null;
269 Collection<StdPolicyResponse> response = null;
270 Map<String,String> eventAttributes = null;
273 Mockito.when(mockPolicyEngine.sendEvent(eventAttributes,requestID)).thenReturn(result);
274 result = mockPolicyEngine.sendEvent(eventAttributes,requestID);
275 } catch (PolicyEventException e) {
276 logger.error("Exception Occured"+e);
280 assertEquals(result,response);
284 public void testSendEvent3()
286 Collection<PolicyResponse> result = null;
287 Collection<StdPolicyResponse> response = null;
288 EventRequestParameters parameters = new EventRequestParameters();
291 Mockito.when(mockPolicyEngine.sendEvent(parameters)).thenReturn(result);
292 result = mockPolicyEngine.sendEvent(parameters);
293 } catch (PolicyEventException e) {
294 logger.error("Exception Occured"+e);
297 assertEquals(result,response);
304 * Run the PolicyDecision getDecision(String, Map<String,String>) method
307 @SuppressWarnings("deprecation")
308 public void testGetDecision()
310 String onapComponentName = null;
311 Map<String,String> decisionAttributes = null;
313 DecisionResponse result = null;
316 Mockito.when(mockPolicyEngine.getDecision(onapComponentName,decisionAttributes)).thenReturn(null);
317 result = mockPolicyEngine.getDecision(onapComponentName,decisionAttributes);
318 } catch (PolicyDecisionException e) {
319 logger.error("Exception Occured"+e);
322 assertEquals(result,null);
325 @SuppressWarnings("deprecation")
326 public void testGetDecision2()
328 String onapComponentName = null;
329 Map<String,String> decisionAttributes = null;
331 DecisionResponse result = null;
334 Mockito.when(mockPolicyEngine.getDecision(onapComponentName,decisionAttributes,requestID)).thenReturn(null);
335 result = mockPolicyEngine.getDecision(onapComponentName,decisionAttributes);
336 } catch (PolicyDecisionException e) {
337 logger.error("Exception Occured"+e);
340 assertEquals(result,null);
343 public void testGetDecision3()
345 DecisionRequestParameters parameters = new DecisionRequestParameters();
346 DecisionResponse result = null;
349 Mockito.when(mockPolicyEngine.getDecision(parameters)).thenReturn(null);
350 result = mockPolicyEngine.getDecision(parameters);
351 } catch (PolicyDecisionException e) {
352 logger.error("Exception Occured"+e);
355 assertEquals(result,null);
359 * Run the void setNotification(NotificationScheme, NotificationHandler)
362 public void testSetNotification() {
363 // add test code here
365 NotificationScheme scheme = null;
366 NotificationHandler handler = null;
368 Mockito.doNothing().when(mockPolicyEngine).setNotification(scheme, handler);
369 mockPolicyEngine.setNotification(scheme, handler);
374 * Run the void clearNotification() method test
376 public void testClearNotification() {
377 // add test code here
379 Mockito.doNothing().when(mockPolicyEngine).clearNotification();
380 mockPolicyEngine.clearNotification();
385 * Run the void setScheme(NotificationScheme) method test
387 public void testSetScheme() {
388 NotificationScheme scheme = null;
390 Mockito.doNothing().when(mockPolicyEngine).setScheme(scheme);
391 mockPolicyEngine.setScheme(scheme);
396 * Run the PDPNotification getNotification() method test
398 public void testGetNotification() {
399 PDPNotification result = null;
400 StdPDPNotification response = null;
401 Mockito.when(mockPolicyEngine.getNotification()).thenReturn(response);
402 result = mockPolicyEngine.getNotification();
404 assertEquals(result,response);
408 * Run the String createConfigPolicy(String, String, String, String,
409 * Map<String,String>, String, String, String, UUID) method test
411 @SuppressWarnings("deprecation")
412 public void testCreateConfigPolicy()
414 String response = "success";
415 String result = null;
418 Mockito.when(mockPolicyEngine.createConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null)).thenReturn(response);
419 result = mockPolicyEngine.createConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null);
421 } catch (Exception e) {
422 logger.warn(e.getMessage());
424 assertEquals(result, response);
428 * Run the String updateConfigPolicy(String, String, String, String,
429 * Map<String,String>, String, String, String, UUID) method test
431 @SuppressWarnings("deprecation")
432 public void testUpdateConfigPolicy()
434 String response = "success";
435 String result = null;
438 Mockito.when(mockPolicyEngine.updateConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null)).thenReturn(response);
439 result = mockPolicyEngine.updateConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null);
441 } catch (Exception e) {
442 logger.warn(e.getMessage());
444 assertEquals(result, response);
448 * Run the String createConfigFirewallPolicy(String, JsonObject, String,
451 @SuppressWarnings("deprecation")
452 public void testCreateConfigFirewallPolicy() {
453 String response = "success";
454 String result = null;
455 String json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\",\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\",\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\",\"appProtocol\":null,\"ports\":\"514\"},{\"name\":\"SList\",\"description\":\"Service List\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"},{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},{\"name\":\"PL_CCE3\",\"description\":\"CCE Routers\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],\"toZones\":[\"Untrusted\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"value\":\"PL_CCE3\"},{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,\"log\":true}]}";
456 JsonObject jsonObj = buildJSON(json);
459 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
460 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
462 } catch (Exception e) {
463 logger.warn(e.getMessage());
465 assertEquals(result, response);
469 * Run the String updateConfigFirewallPolicy(String, JsonObject, String,
472 @SuppressWarnings("deprecation")
473 public void testUpdateConfigFirewallPolicy() {
474 String response = "success";
475 String result = null;
476 String json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\",\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\",\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\",\"appProtocol\":null,\"ports\":\"514\"},{\"name\":\"SList\",\"description\":\"Service List\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"},{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},{\"name\":\"PL_CCE3\",\"description\":\"CCE Routers\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],\"toZones\":[\"Untrusted\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"value\":\"PL_CCE3\"},{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,\"log\":true}]}";
477 JsonObject jsonObj = buildJSON(json);
480 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
481 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
483 } catch (Exception e) {
484 logger.warn(e.getMessage());
486 assertEquals(result, response);
490 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
492 public void testCreatePolicy() {
493 response.setResponseMessage("success");
494 PolicyChangeResponse result = null;
495 PolicyParameters policyParameters = new PolicyParameters();
497 policyParameters.setPolicyClass(PolicyClass.Action); //required
498 policyParameters.setPolicyName("test.junitTest"); //required
499 policyParameters.setPolicyDescription("testing"); //optional
501 //Set the Component Attributes... These are Optional
502 Map<String, String> configAttributes = new HashMap<String, String>();
503 configAttributes.put("test", "testing");
505 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
506 attributes.put(AttributeType.MATCHING, configAttributes);
507 policyParameters.setAttributes(attributes);
509 policyParameters.setActionPerformer("PEP");
510 policyParameters.setActionAttribute("testing");
511 policyParameters.setRequestID(UUID.randomUUID());
515 //stdPolicyEngine = Mockito.mock(StdPolicyEngine.class);
516 //Mockito.when(stdPolicyEngine.callPAP(newPAPPolicy, new String[] {"operation=create", "apiflag=api", "policyType=Action"}, null, "Action")).thenReturn(callPapResponse);
517 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
518 result = mockPolicyEngine.createPolicy(policyParameters);
520 } catch (Exception e) {
521 logger.warn(e.getMessage());
522 logger.error("Exception Occured"+e);
524 assertEquals(result, response);
528 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
530 public void testUpdatePolicy() {
531 response.setResponseMessage("success");
532 PolicyChangeResponse result = null;
533 PolicyParameters policyParameters = new PolicyParameters();
535 policyParameters.setPolicyClass(PolicyClass.Action); //required
536 policyParameters.setPolicyName("test.junitTest"); //required
537 policyParameters.setPolicyDescription("testing"); //optional
539 //Set the Component Attributes... These are Optional
540 Map<String, String> configAttributes = new HashMap<String, String>();
541 configAttributes.put("test", "testing");
543 Map<AttributeType, Map<String,String>> attributes = 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 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
554 result = mockPolicyEngine.updatePolicy(policyParameters);
556 } catch (Exception e) {
557 logger.warn(e.getMessage());
559 assertEquals(result, response);
563 * Run the String pushPolicy(String, String, String, String, UUID) method
566 @SuppressWarnings("deprecation")
567 public void testPushPolicy() {
568 String response = "Success";
569 String result = null;
572 Mockito.when(mockPolicyEngine.pushPolicy("testing","test","Base","default",requestID)).thenReturn(response);
573 result = mockPolicyEngine.pushPolicy("testing","test","Base","default",requestID);
575 } catch (Exception e) {
576 logger.warn(e.getMessage());
579 assertEquals(result, response);
582 public void testPushPolicy2() {
583 PushPolicyParameters policyParameters = new PushPolicyParameters();
584 PolicyChangeResponse result = null;
586 //String policyScope = null;
587 policyParameters.setPolicyName("test.junitTest");
588 policyParameters.setPolicyType("Action");
589 policyParameters.setPdpGroup("Default");
593 Mockito.when(mockPolicyEngine.pushPolicy(policyParameters)).thenReturn(response);
594 result = mockPolicyEngine.pushPolicy(policyParameters);
596 } catch (Exception e) {
597 logger.warn(e.getMessage());
600 assertEquals(result, response);
604 * Run the PolicyChangeResponse deletePolicy(DeletePolicyParameters) method
607 public void testDeletePolicy() {
608 DeletePolicyParameters policyParameters = new DeletePolicyParameters();
609 PolicyChangeResponse result = null;
611 //String policyScope = null;
612 policyParameters.setPolicyName("test.junitTest.1.xml");
613 policyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
614 policyParameters.setPolicyComponent("PAP");
615 policyParameters.setPdpGroup("Default");
619 Mockito.when(mockPolicyEngine.deletePolicy(policyParameters)).thenReturn(response);
620 result = mockPolicyEngine.deletePolicy(policyParameters);
622 } catch (Exception e) {
623 logger.warn(e.getMessage());
626 assertEquals(result, response);
630 * Run the PolicyChangeResponse policyEngineImport(ImportParameters) method
633 public void testPolicyEngineImport() {
634 ImportParameters importParameters = new ImportParameters();
635 PolicyChangeResponse result = null;
637 importParameters.setFilePath("C:\\Workspaces\\models\\TestingModel\\ControllerServiceSampleSdnlServiceInstance-v0.1.0-SNAPSHOT.zip");
638 importParameters.setServiceName("ControllerServiceSampleSdnlServiceInstance");
640 importParameters.setRequestID(UUID.randomUUID());
641 importParameters.setServiceType(IMPORT_TYPE.MICROSERVICE);
642 importParameters.setVersion("1607-2");
647 Mockito.when(mockPolicyEngine.policyEngineImport(importParameters)).thenReturn(response);
648 result = mockPolicyEngine.policyEngineImport(importParameters);
650 } catch (Exception e) {
651 logger.warn(e.getMessage());
654 assertEquals(result, response);
657 private static JsonObject buildJSON(String jsonString) {
658 JsonObject json = null;;
659 if (jsonString != null) {
660 StringReader in = null;
662 in = new StringReader(jsonString);
664 JsonReader jsonReader = Json.createReader(in);
665 json = jsonReader.readObject();