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.openecomp.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 junit.framework.TestCase;
36 import org.mockito.Mockito;
37 import org.openecomp.policy.api.AttributeType;
38 import org.openecomp.policy.api.ConfigRequestParameters;
39 import org.openecomp.policy.api.DecisionRequestParameters;
40 import org.openecomp.policy.api.DecisionResponse;
41 import org.openecomp.policy.api.DeletePolicyCondition;
42 import org.openecomp.policy.api.DeletePolicyParameters;
43 import org.openecomp.policy.api.EventRequestParameters;
44 import org.openecomp.policy.api.ImportParameters;
45 import org.openecomp.policy.api.NotificationHandler;
46 import org.openecomp.policy.api.NotificationScheme;
47 import org.openecomp.policy.api.PDPNotification;
48 import org.openecomp.policy.api.PolicyChangeResponse;
49 import org.openecomp.policy.api.PolicyClass;
50 import org.openecomp.policy.api.PolicyConfig;
51 import org.openecomp.policy.api.PolicyConfigException;
52 import org.openecomp.policy.api.PolicyDecision;
53 import org.openecomp.policy.api.PolicyDecisionException;
54 import org.openecomp.policy.api.PolicyEngine;
55 import org.openecomp.policy.api.PolicyEngineException;
56 import org.openecomp.policy.api.PolicyEventException;
57 import org.openecomp.policy.api.PolicyParameters;
58 import org.openecomp.policy.api.PolicyResponse;
59 import org.openecomp.policy.api.PushPolicyParameters;
60 import org.openecomp.policy.api.ImportParameters.IMPORT_TYPE;
61 import org.openecomp.policy.std.StdPDPNotification;
62 import org.openecomp.policy.std.StdPolicyChangeResponse;
63 import org.openecomp.policy.std.StdPolicyEngine;
64 import org.openecomp.policy.std.StdPolicyResponse;
66 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
68 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
69 import org.openecomp.policy.common.logging.flexlogger.Logger;
72 * The class <code>PolicyEngineInterfaceTest</code> contains tests for the
73 * class {@link <code>PolicyEngine</code>}
75 * @pattern JUnit Test Case
77 * @generatedBy CodePro at 5/27/16 10:33 AM
82 public class PolicyEngineInterfaceTest extends TestCase {
84 private static final Logger logger = FlexLogger.getLogger(PolicyEngineInterfaceTest.class);
86 private PolicyEngine policyEngine = null;
87 private StdPolicyEngine stdPolicyEngine = null;
88 private PolicyEngine mockPolicyEngine = null;
89 private Collection<PolicyConfig> policyConfig = null;
90 private UUID requestID = UUID.randomUUID();
93 PolicyChangeResponse result = null;
94 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
98 * Construct new test instance
100 * @param name the test name
102 public PolicyEngineInterfaceTest(String name) {
107 * Perform pre-test initialization
111 * @see TestCase#setUp()
113 protected void setUp() throws Exception {
115 policyEngine = new PolicyEngine("Test/config_pass.properties");
116 } catch (PolicyEngineException e) {
117 logger.error(e.getMessage());
118 fail("PolicyEngine Instantiation Error" + e);
120 logger.info("Loaded.. PolicyEngine");
122 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
123 HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
124 Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
129 * Perform post-test clean up
133 * @see TestCase#tearDown()
135 protected void tearDown() throws Exception {
137 // Add additional tear down code here
141 * Run the Collection<PolicyConfig> getConfigByPolicyName(String) method
144 public void testGetConfigByPolicyName() {
145 String policyName = null;
147 policyConfig = policyEngine.getConfigByPolicyName(policyName);
148 } catch (PolicyConfigException e){
149 logger.warn(e.getMessage());
151 assertNull(policyConfig);
154 public void testGetConfigByPolicyName2() {
155 String policyName = null;
158 policyConfig = policyEngine.getConfigByPolicyName(policyName, requestID);
159 } catch (PolicyConfigException e){
160 logger.warn(e.getMessage());
162 assertNull(policyConfig);
166 * Run the Collection<PolicyConfig> getConfig(String) method test
168 public void testGetConfig() {
169 String ecompName = null;
172 policyConfig = policyEngine.getConfig(ecompName);
173 } catch (PolicyConfigException e){
174 logger.warn(e.getMessage());
176 assertNull(policyConfig);
179 public void testGetConfig2() {
180 String ecompName = null;
183 policyConfig = policyEngine.getConfig(ecompName,requestID);
184 } catch (PolicyConfigException e){
185 logger.warn(e.getMessage());
187 assertNull(policyConfig);
191 public void testGetConfig3() {
192 String ecompName = null;
193 String configName = null;
196 policyConfig = policyEngine.getConfig(ecompName,configName,requestID);
197 } catch (PolicyConfigException e){
198 logger.warn(e.getMessage());
200 assertNull(policyConfig);
203 public void testGetConfig4() {
204 String ecompName = null;
205 String configName = null;
206 Map<String,String> configAttributes = null;
209 policyConfig = policyEngine.getConfig(ecompName,configName,configAttributes);
210 } catch (PolicyConfigException e){
211 logger.warn(e.getMessage());
213 assertNull(policyConfig);
216 public void testGetConfig5() {
217 String ecompName = null;
218 String configName = null;
219 Map<String,String> configAttributes = null;
222 policyConfig = policyEngine.getConfig(ecompName,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
245 public void testSendEvent()
247 Collection<PolicyResponse> result = null;
248 Collection<StdPolicyResponse> response = null;
249 Map<String,String> eventAttributes = null;
252 Mockito.when(mockPolicyEngine.sendEvent(eventAttributes)).thenReturn(result);
253 result = mockPolicyEngine.sendEvent(eventAttributes);
254 } catch (PolicyEventException e) {
255 // TODO Auto-generated catch block
260 assertEquals(result,response);
264 public void testSendEvent2()
266 Collection<PolicyResponse> result = null;
267 Collection<StdPolicyResponse> response = null;
268 Map<String,String> eventAttributes = null;
271 Mockito.when(mockPolicyEngine.sendEvent(eventAttributes,requestID)).thenReturn(result);
272 result = mockPolicyEngine.sendEvent(eventAttributes,requestID);
273 } catch (PolicyEventException e) {
274 // TODO Auto-generated catch block
279 assertEquals(result,response);
283 public void testSendEvent3()
285 Collection<PolicyResponse> result = null;
286 Collection<StdPolicyResponse> response = null;
287 EventRequestParameters parameters = new EventRequestParameters();
290 Mockito.when(mockPolicyEngine.sendEvent(parameters)).thenReturn(result);
291 result = mockPolicyEngine.sendEvent(parameters);
292 } catch (PolicyEventException e) {
293 // TODO Auto-generated catch block
297 assertEquals(result,response);
304 * Run the PolicyDecision getDecision(String, Map<String,String>) method
307 public void testGetDecision()
309 String eCOMPComponentName = null;
310 Map<String,String> decisionAttributes = null;
312 DecisionResponse result = null;
315 Mockito.when(mockPolicyEngine.getDecision(eCOMPComponentName,decisionAttributes)).thenReturn(null);
316 result = mockPolicyEngine.getDecision(eCOMPComponentName,decisionAttributes);
317 } catch (PolicyDecisionException e) {
318 // TODO Auto-generated catch block
322 assertEquals(result,null);
325 public void testGetDecision2()
327 String eCOMPComponentName = null;
328 Map<String,String> decisionAttributes = null;
330 DecisionResponse result = null;
333 Mockito.when(mockPolicyEngine.getDecision(eCOMPComponentName,decisionAttributes,requestID)).thenReturn(null);
334 result = mockPolicyEngine.getDecision(eCOMPComponentName,decisionAttributes);
335 } catch (PolicyDecisionException e) {
336 // TODO Auto-generated catch block
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 // TODO Auto-generated catch block
356 assertEquals(result,null);
360 * Run the void setNotification(NotificationScheme, NotificationHandler)
363 public void testSetNotification() {
364 // add test code here
366 NotificationScheme scheme = null;
367 NotificationHandler handler = null;
369 Mockito.doNothing().when(mockPolicyEngine).setNotification(scheme, handler);
370 mockPolicyEngine.setNotification(scheme, handler);
375 * Run the void clearNotification() method test
377 public void testClearNotification() {
378 // add test code here
380 Mockito.doNothing().when(mockPolicyEngine).clearNotification();
381 mockPolicyEngine.clearNotification();
386 * Run the void setScheme(NotificationScheme) method test
388 public void testSetScheme() {
389 NotificationScheme scheme = null;
391 Mockito.doNothing().when(mockPolicyEngine).setScheme(scheme);
392 mockPolicyEngine.setScheme(scheme);
397 * Run the PDPNotification getNotification() method test
399 public void testGetNotification() {
400 PDPNotification result = null;
401 StdPDPNotification response = null;
402 Mockito.when(mockPolicyEngine.getNotification()).thenReturn(response);
403 result = mockPolicyEngine.getNotification();
405 assertEquals(result,response);
409 * Run the String createConfigPolicy(String, String, String, String,
410 * Map<String,String>, String, String, String, UUID) method test
412 @SuppressWarnings("deprecation")
413 public void testCreateConfigPolicy()
415 String response = "success";
416 String result = null;
419 Mockito.when(mockPolicyEngine.createConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null)).thenReturn(response);
420 result = mockPolicyEngine.createConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null);
422 } catch (Exception e) {
423 logger.warn(e.getMessage());
425 assertEquals(result, response);
429 * Run the String updateConfigPolicy(String, String, String, String,
430 * Map<String,String>, String, String, String, UUID) method test
432 @SuppressWarnings("deprecation")
433 public void testUpdateConfigPolicy()
435 String response = "success";
436 String result = null;
439 Mockito.when(mockPolicyEngine.updateConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null)).thenReturn(response);
440 result = mockPolicyEngine.updateConfigPolicy("testPolicy","test","test","testConfig",null,"OTHER","test","test",null, null, null, null, null);
442 } catch (Exception e) {
443 logger.warn(e.getMessage());
445 assertEquals(result, response);
449 * Run the String createConfigFirewallPolicy(String, JsonObject, String,
452 @SuppressWarnings("deprecation")
453 public void testCreateConfigFirewallPolicy() {
454 String response = "success";
455 String result = null;
456 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}]}";
457 JsonObject jsonObj = buildJSON(json);
460 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
461 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
463 } catch (Exception e) {
464 logger.warn(e.getMessage());
466 assertEquals(result, response);
470 * Run the String updateConfigFirewallPolicy(String, JsonObject, String,
473 @SuppressWarnings("deprecation")
474 public void testUpdateConfigFirewallPolicy() {
475 String response = "success";
476 String result = null;
477 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}]}";
478 JsonObject jsonObj = buildJSON(json);
481 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
482 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
484 } catch (Exception e) {
485 logger.warn(e.getMessage());
487 assertEquals(result, response);
491 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
493 public void testCreatePolicy() {
494 response.setResponseMessage("success");
495 String callPapResponse = "success";
496 PolicyChangeResponse result = null;
497 PolicyParameters policyParameters = new PolicyParameters();
498 StdPAPPolicy newPAPPolicy = null;
500 policyParameters.setPolicyClass(PolicyClass.Action); //required
501 policyParameters.setPolicyName("test.junitTest"); //required
502 policyParameters.setPolicyDescription("testing"); //optional
504 //Set the Component Attributes... These are Optional
505 Map<String, String> configAttributes = new HashMap<String, String>();
506 configAttributes.put("test", "testing");
508 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
509 attributes.put(AttributeType.MATCHING, configAttributes);
510 policyParameters.setAttributes(attributes);
512 policyParameters.setActionPerformer("PEP");
513 policyParameters.setActionAttribute("testing");
514 policyParameters.setRequestID(UUID.randomUUID());
518 //stdPolicyEngine = Mockito.mock(StdPolicyEngine.class);
519 //Mockito.when(stdPolicyEngine.callPAP(newPAPPolicy, new String[] {"operation=create", "apiflag=api", "policyType=Action"}, null, "Action")).thenReturn(callPapResponse);
520 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
521 result = mockPolicyEngine.createPolicy(policyParameters);
523 } catch (Exception e) {
524 logger.warn(e.getMessage());
527 assertEquals(result, response);
531 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
533 public void testUpdatePolicy() {
534 response.setResponseMessage("success");
535 PolicyChangeResponse result = null;
536 PolicyParameters policyParameters = new PolicyParameters();
538 policyParameters.setPolicyClass(PolicyClass.Action); //required
539 policyParameters.setPolicyName("test.junitTest"); //required
540 policyParameters.setPolicyDescription("testing"); //optional
542 //Set the Component Attributes... These are Optional
543 Map<String, String> configAttributes = new HashMap<String, String>();
544 configAttributes.put("test", "testing");
546 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
547 attributes.put(AttributeType.MATCHING, configAttributes);
548 policyParameters.setAttributes(attributes);
550 policyParameters.setActionPerformer("PEP");
551 policyParameters.setActionAttribute("testing");
552 policyParameters.setRequestID(UUID.randomUUID());
556 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
557 result = mockPolicyEngine.updatePolicy(policyParameters);
559 } catch (Exception e) {
560 logger.warn(e.getMessage());
562 assertEquals(result, response);
566 * Run the String pushPolicy(String, String, String, String, UUID) method
569 public void testPushPolicy() {
570 String response = "Success";
571 String result = null;
574 Mockito.when(mockPolicyEngine.pushPolicy("testing","test","Base","default",requestID)).thenReturn(response);
575 result = mockPolicyEngine.pushPolicy("testing","test","Base","default",requestID);
577 } catch (Exception e) {
578 logger.warn(e.getMessage());
581 assertEquals(result, response);
584 public void testPushPolicy2() {
585 PushPolicyParameters policyParameters = new PushPolicyParameters();
586 PolicyChangeResponse result = null;
588 //String policyScope = null;
589 policyParameters.setPolicyName("test.junitTest");
590 policyParameters.setPolicyType("Action");
591 policyParameters.setPdpGroup("Default");
595 Mockito.when(mockPolicyEngine.pushPolicy(policyParameters)).thenReturn(response);
596 result = mockPolicyEngine.pushPolicy(policyParameters);
598 } catch (Exception e) {
599 logger.warn(e.getMessage());
602 assertEquals(result, response);
606 * Run the PolicyChangeResponse deletePolicy(DeletePolicyParameters) method
609 public void testDeletePolicy() {
610 DeletePolicyParameters policyParameters = new DeletePolicyParameters();
611 PolicyChangeResponse result = null;
613 //String policyScope = null;
614 policyParameters.setPolicyName("test.junitTest.1.xml");
615 policyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
616 policyParameters.setPolicyComponent("PAP");
617 policyParameters.setPdpGroup("Default");
621 Mockito.when(mockPolicyEngine.deletePolicy(policyParameters)).thenReturn(response);
622 result = mockPolicyEngine.deletePolicy(policyParameters);
624 } catch (Exception e) {
625 logger.warn(e.getMessage());
628 assertEquals(result, response);
632 * Run the PolicyChangeResponse policyEngineImport(ImportParameters) method
635 public void testPolicyEngineImport() {
636 ImportParameters importParameters = new ImportParameters();
637 PolicyChangeResponse result = null;
639 importParameters.setFilePath("C:\\Workspaces\\models\\TestingModel\\ControllerServiceSampleSdnlServiceInstance-v0.1.0-SNAPSHOT.zip");
640 importParameters.setServiceName("ControllerServiceSampleSdnlServiceInstance");
642 importParameters.setRequestID(UUID.randomUUID());
643 importParameters.setServiceType(IMPORT_TYPE.MICROSERVICE);
644 importParameters.setVersion("1607-2");
649 Mockito.when(mockPolicyEngine.policyEngineImport(importParameters)).thenReturn(response);
650 result = mockPolicyEngine.policyEngineImport(importParameters);
652 } catch (Exception e) {
653 logger.warn(e.getMessage());
656 assertEquals(result, response);
659 private static JsonObject buildJSON(String jsonString) {
660 JsonObject json = null;;
661 if (jsonString != null) {
662 StringReader in = null;
664 in = new StringReader(jsonString);
666 JsonReader jsonReader = Json.createReader(in);
667 json = jsonReader.readObject();