99285e77f1ba1e227b15a58fc67801236e46d6f9
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / FirewallConfigPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pap.xacml.rest.components;
21
22 import static org.junit.Assert.*;
23 import static org.mockito.Mockito.when;
24
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.HashMap;
28
29 import java.util.Map;
30 import java.util.UUID;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.rest.adapter.PolicyRestAdapter;
39
40 import com.att.research.xacml.util.XACMLProperties;
41
42
43 public class FirewallConfigPolicyTest {
44         
45         private static Logger logger = FlexLogger.getLogger(FirewallConfigPolicyTest.class);
46         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
47         Map<String, String> attributeMap = new HashMap<>();
48         FirewallConfigPolicy component = null;
49     FirewallConfigPolicy mockFWConfig = null;
50
51
52         /**
53          * @throws java.lang.Exception
54          */
55         @Before
56         public void setUp() throws Exception {
57                 logger.info("setup: enter");
58                 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,"src/test/resources/xacml.pap.properties");
59
60                 policyAdapter.setPolicyName("FWjunitTest");
61                 policyAdapter.setPolicyDescription("test");
62                 policyAdapter.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides");
63                 policyAdapter.setPolicyType("Config");
64                 policyAdapter.setConfigPolicyType("Firewall Config");
65                 policyAdapter.setEditPolicy(false);
66                 policyAdapter.setDomainDir("src/test/resources/client.properties");
67                 policyAdapter.setDomain("Test");
68                 policyAdapter.setNewFileName("Test.Config_FW_junitTest.1.xml");
69                 policyAdapter.setHighestVersion(1);
70                 policyAdapter.setVersion(String.valueOf(1));
71                 policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID());
72                 policyAdapter.setRuleID("");
73                 policyAdapter.setConfigName("testname");
74                 policyAdapter.setGuard("True");
75                 policyAdapter.setRiskLevel("3");
76                 policyAdapter.setRiskType("RiskTest");
77                 policyAdapter.setSecurityZone("CraigSecurityZone");
78                 policyAdapter.setUserId("API");
79                 
80                 attributeMap.put("testJunits", "test");
81                 policyAdapter.setDynamicFieldConfigAttributes(attributeMap);
82
83                 component = new FirewallConfigPolicy(policyAdapter);
84                 mockFWConfig = Mockito.mock(FirewallConfigPolicy.class);
85         logger.info("setUp: exit");
86                 
87         }
88
89         /**
90          * @throws java.lang.Exception
91          */
92         @After
93         public void tearDown() throws Exception {
94         }
95
96         /**
97          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.FirewallConfigPolicy#savePolicies()}.
98          */
99         @Test
100         public void testSavePolicies() {
101         Map<String, String> successMap = new HashMap<>();
102         successMap.put("success", "success");
103                 try {
104                 when(mockFWConfig.savePolicies()).thenReturn(successMap);
105                 successMap = mockFWConfig.savePolicies();
106                 } catch (Exception e) {
107                         logger.error("Exception Occured"+e);
108                 }
109         }
110
111         /**
112          * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.FirewallConfigPolicy#prepareToSave()}.
113          */
114         @Test
115         public void testPrepareToSave() {
116                 logger.debug("test prepareToSave Policy: enter");
117
118                 boolean response = false;
119                 try {
120                 when(mockFWConfig.prepareToSave()).thenReturn(true);
121                         response = mockFWConfig.prepareToSave();
122                 } catch (Exception e) {
123                         logger.error("Exception Occured"+e);
124                 }
125                 assertTrue(response);           
126                 
127         }
128         
129         @Test
130         public void testUpdateJson() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
131             FirewallConfigPolicy firewallConfigPolicy = new FirewallConfigPolicy();
132             Method method = firewallConfigPolicy.getClass().getDeclaredMethod("updateFirewallDictionaryData", String.class , String.class);
133             method.setAccessible(true);
134             String jsonBody= "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\",\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}],\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\",\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"],\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\",\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true,\"log\":true}]}";
135             String prevJsonBody = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicy1Config\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev\",\"vendorServiceId\":\"test\",\"vendorSpecificData\":{\"idMap\":[{\"Id\":\"cloudsite:dev1a\",\"vendorId\":\"deviceGroup:dev\"}]},\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Ssh service entry in service list\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}],\"addressGroups\":[{\"name\":\"Test\",\"description\":\"Destination Test\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\",\"description\":\"Source TestServers for first testing\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"FWRuleTestServerTot\",\"fromZones\":[\"UntrustedZoneTestName\"],\"toZones\":[\"TrustedZoneTName\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TServers\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\",\"description\":\"FW rule for HOHO source to CiscoVCE destination\",\"enabled\":true,\"log\":true}]}";
136         assertFalse((Boolean) method.invoke(firewallConfigPolicy, jsonBody, prevJsonBody));
137         }
138         
139         @Test
140     public void testInsertJson() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
141         FirewallConfigPolicy firewallConfigPolicy = new FirewallConfigPolicy();
142         Method method = firewallConfigPolicy.getClass().getDeclaredMethod("insertFirewallDicionaryData", String.class);
143         method.setAccessible(true);
144         String jsonBody= "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\",\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}],\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\",\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"],\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\",\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true,\"log\":true}]}";
145         assertFalse((Boolean) method.invoke(firewallConfigPolicy, jsonBody));
146     }
147
148 }