2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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.admin;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.StringReader;
32 import java.util.ArrayList;
33 import java.util.List;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpSession;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.policy.controller.CreateFirewallController;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.ActionList;
44 import org.onap.policy.rest.jpa.AddressGroup;
45 import org.onap.policy.rest.jpa.FWTagPicker;
46 import org.onap.policy.rest.jpa.GroupServiceList;
47 import org.onap.policy.rest.jpa.PrefixList;
48 import org.onap.policy.rest.jpa.SecurityZone;
49 import org.onap.policy.rest.jpa.ServiceList;
50 import org.onap.policy.rest.jpa.TermList;
51 import org.onap.portalsdk.core.domain.User;
52 import org.onap.portalsdk.core.util.SystemProperties;
53 import org.springframework.mock.web.MockHttpServletResponse;
55 public class PolicyRestControllerTest {
57 private String clRequestString;
58 private String fwRequestString;
59 private String fwViewRequestString;
60 private HttpServletRequest request;
61 private MockHttpServletResponse response;
62 private static CommonClassDao commonClassDao;
63 private List<Object> prefixListData;
64 private List<Object> actionListData;
65 private List<Object> serviceListData;
66 private List<Object> addressGroupData;
67 private List<Object> securityZoneData;
68 private List<Object> serviceGroupData;
69 private List<Object> tagListData;
70 private List<Object> termListData;
73 public void setUp() throws Exception {
74 commonClassDao = mock(CommonClassDao.class);
75 HttpSession mockSession = mock(HttpSession.class);
76 request = mock(HttpServletRequest.class);
77 response = new MockHttpServletResponse();
78 User user = new User();
79 user.setOrgUserId("Test");
80 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
81 Mockito.when(request.getSession(false)).thenReturn(mockSession);
82 clRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
83 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
84 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
85 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
86 + "\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\","
87 + "\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
88 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
89 + "\"$$hashKey\":\"object:1439\",\"policy\":{\"policyType\":\"Config\","
90 + "\"configPolicyType\":\"ClosedLoop_Fault\",\"triggerTrapSignatures\":[1,1,2,3],"
91 + "\"triggerfaultSignatures\":[1,1,2,3],\"traptriggerSignatures\":[{\"id\":\"Trap1\","
92 + "\"$$hashKey\":\"object:1526\"},{\"id\":\"Trap2\",\"$$hashKey\":\"object:1534\"}],"
93 + "\"connecttriggerSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1554\","
94 + "\"notBox\":\"NOT\",\"connectTrap1\":\"Trap1\",\"trapCount1\":\"12\","
95 + "\"operatorBox\":\"AND\",\"connectTrap2\":\"Trap2\",\"trapCount2\":\"14\"}],"
96 + "\"faulttriggerSignatures\":[{\"id\":\"Fault1\",\"$$hashKey\":\"object:1566\"},"
97 + "{\"id\":\"Fault2\",\"$$hashKey\":\"object:1575\"}],\"connectVerificationSignatures\":"
98 + "[{\"id\":\"C1\",\"$$hashKey\":\"object:1595\",\"notBox\":\"NOT\","
99 + "\"connectTrap1\":\"Fault1\",\"trapCount1\":\"11\",\"operatorBox\":\"AND\","
100 + "\"connectTrap2\":\"Fault2\",\"trapCount2\":\"12\"}],\"jsonBodyData\":"
101 + "{\"trapMaxAge\":\"300\",\"vnfType\":\"Test\",\"closedLoopPolicyStatus\":\"Active\","
102 + "\"vUSP\":true,\"trinity\":true,\"vDNS\":true,\"mcr\":true,\"gamma\":true,"
103 + "\"actions\":\"Test\",\"timeInterval\":\"11\",\"timeOutvPRO\":\"11\",\"timeOutRuby\":\"11\","
104 + "\"retrys\":\"1\",\"agingWindow\":\"12\",\"geoLink\":\"test\","
105 + "\"emailAddress\":\"aa@test.com\",\"pepName\":\"Test\",\"pepAction\":\"test\","
106 + "\"conditions\":\"SEND\"},\"policyName\":\"SampleTest\","
107 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
108 + "\"guard\":\"True\",\"onapName\":\"SampleDemo\",\"ttlDate\":\"14/09/2017\","
109 + "\"clearTimeOut\":\"123\",\"trapMaxAge\":\"11\",\"verificationclearTimeOut\":\"13\"}},"
110 + "\"trapData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1528\","
111 + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],"
112 + "\"trap2\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1536\",\"notBox\":\"NOT\","
113 + "\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\","
114 + "\"$$hashKey\":\"object:1542\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
115 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A3\","
116 + "\"$$hashKey\":\"object:1548\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
117 + "\"operatorBox\":\"OR\",\"trigger2\":\"A2\"}]},\"faultData\":{\"trap1\":[{\"id\":\"A1\","
118 + "\"$$hashKey\":\"object:1568\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
119 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\","
120 + "\"$$hashKey\":\"object:1577\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
121 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\",\"$$hashKey\":"
122 + "\"object:1583\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"OR\","
123 + "\"trigger2\":\"Test\"},{\"id\":\"A3\",\"$$hashKey\":\"object:1589\",\"notBox\":\"NOT\","
124 + "\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":\"A2\"}]}}";
126 fwRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
127 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
128 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
129 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"tempModel\":"
130 + "{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
131 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
132 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:260\","
133 + "\"policy\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\","
134 + "\"attributes\":[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\","
135 + "\"value\":\"Test\"}],\"fwattributes\":[],\"policyName\":\"SampleTest\","
136 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
137 + "\"guard\":\"True\",\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\","
138 + "\"securityZone\":\"Test\"}}}";
140 fwViewRequestString =
141 "{\"policyData\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\",\"attributes\":"
142 + "[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],"
143 + "\"fwattributes\":[],\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\","
144 + "\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\",\"guard\":\"True\","
145 + "\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}";
147 prefixListData = new ArrayList<>();
148 PrefixList prefixList = new PrefixList();
149 prefixList.setPrefixListName("Test");
150 prefixList.setPrefixListValue("10.10.10.10/12");
151 prefixListData.add(prefixList);
152 when(commonClassDao.getData(PrefixList.class)).thenReturn(prefixListData);
154 actionListData = new ArrayList<>();
155 ActionList actionList = new ActionList();
156 actionList.setActionName("Test");
157 actionListData.add(actionList);
158 when(commonClassDao.getData(ActionList.class)).thenReturn(actionListData);
160 serviceListData = new ArrayList<>();
161 ServiceList serviceList = new ServiceList();
162 serviceList.setServiceName("Test");
163 serviceList.setServiceType("SERVICE");
164 serviceList.setServiceTransProtocol("Test");
165 serviceList.setServiceAppProtocol("Test");
166 serviceList.setServicePorts("8080");
167 serviceListData.add(serviceList);
168 when(commonClassDao.getData(ServiceList.class)).thenReturn(serviceListData);
170 addressGroupData = new ArrayList<>();
171 AddressGroup addressGroup = new AddressGroup();
172 addressGroup.setGroupName("Group_Test");
173 addressGroup.setServiceList("Test");
174 addressGroupData.add(addressGroup);
175 when(commonClassDao.getData(AddressGroup.class)).thenReturn(addressGroupData);
177 securityZoneData = new ArrayList<>();
178 SecurityZone securityZone = new SecurityZone();
179 securityZone.setZoneName("Test");
180 securityZone.setZoneValue("Test");
181 securityZoneData.add(securityZone);
182 when(commonClassDao.getData(SecurityZone.class)).thenReturn(securityZoneData);
184 serviceGroupData = new ArrayList<>();
185 GroupServiceList serviceGroup = new GroupServiceList();
186 serviceGroup.setGroupName("Group_Test");
187 serviceGroup.setServiceList("Test");
188 serviceGroupData.add(serviceGroup);
189 when(commonClassDao.getData(GroupServiceList.class)).thenReturn(serviceGroupData);
191 tagListData = new ArrayList<>();
192 FWTagPicker fwPicker = new FWTagPicker();
193 fwPicker.setTagPickerName("Test");
194 fwPicker.setTagValues("Test:8080");
195 tagListData.add(fwPicker);
196 when(commonClassDao.getData(FWTagPicker.class)).thenReturn(tagListData);
198 termListData = new ArrayList<>();
199 TermList termList = new TermList();
200 termList.setTermName("Test");
201 termList.setFromZones("Test");
202 termList.setToZones("Test");
203 termList.setSrcIPList("Test");
204 termList.setDestIPList("Test");
205 termList.setSrcPortList("Test");
206 termList.setDestPortList("Test");
207 termList.setAction("Test");
208 termListData.add(termList);
209 when(commonClassDao.getData(TermList.class)).thenReturn(termListData);
210 when(commonClassDao.getDataById(TermList.class, "termName", "Test")).thenReturn(termListData);
214 public final void testPolicyCreationController() {
215 PolicyRestController controller = new PolicyRestController();
216 BufferedReader reader = new BufferedReader(new StringReader(clRequestString));
218 Mockito.when(request.getReader()).thenReturn(reader);
219 controller.policyCreationController(request, response);
220 } catch (IOException e) {
223 PolicyRestController controller1 = new PolicyRestController();
224 CreateFirewallController.setCommonClassDao(commonClassDao);
225 BufferedReader reader1 = new BufferedReader(new StringReader(fwRequestString));
227 Mockito.when(request.getReader()).thenReturn(reader1);
228 controller1.policyCreationController(request, response);
229 } catch (IOException e) {
233 CreateFirewallController fwController = new CreateFirewallController();
234 CreateFirewallController.setCommonClassDao(commonClassDao);
235 BufferedReader reader2 = new BufferedReader(new StringReader(fwViewRequestString));
237 Mockito.when(request.getReader()).thenReturn(reader2);
238 fwController.setFWViewRule(request, response);
239 } catch (IOException e) {
245 public final void testDeleteElasticData() {
246 PolicyRestController controller = new PolicyRestController();
248 controller.deleteElasticData("Test");
249 } catch (Exception e) {