2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019-2020 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.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Mockito.atLeast;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.reset;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.io.BufferedReader;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.PrintWriter;
39 import java.io.StringReader;
40 import java.net.HttpURLConnection;
41 import java.util.ArrayList;
42 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import javax.servlet.http.HttpSession;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mockito;
50 import org.onap.policy.controller.CreateFirewallController;
51 import org.onap.policy.controller.PolicyController;
52 import org.onap.policy.rest.dao.CommonClassDao;
53 import org.onap.policy.rest.jpa.ActionList;
54 import org.onap.policy.rest.jpa.AddressGroup;
55 import org.onap.policy.rest.jpa.FwTagPicker;
56 import org.onap.policy.rest.jpa.GroupServiceList;
57 import org.onap.policy.rest.jpa.PrefixList;
58 import org.onap.policy.rest.jpa.SecurityZone;
59 import org.onap.policy.rest.jpa.ServiceList;
60 import org.onap.policy.rest.jpa.TermList;
61 import org.onap.portalsdk.core.domain.User;
62 import org.onap.portalsdk.core.util.SystemProperties;
63 import org.onap.portalsdk.core.web.support.UserUtils;
64 import org.powermock.api.mockito.PowerMockito;
65 import org.powermock.core.classloader.annotations.PrepareForTest;
66 import org.powermock.modules.junit4.PowerMockRunner;
67 import org.powermock.reflect.Whitebox;
68 import org.springframework.mock.web.MockHttpServletResponse;
70 @RunWith(PowerMockRunner.class)
71 public class PolicyRestControllerTest {
73 private String clRequestString;
74 private String fwRequestString;
75 private String fwViewRequestString;
76 private HttpServletRequest request;
77 private MockHttpServletResponse response;
78 private static CommonClassDao commonClassDao;
79 private List<Object> prefixListData;
80 private List<Object> actionListData;
81 private List<Object> serviceListData;
82 private List<Object> addressGroupData;
83 private List<Object> securityZoneData;
84 private List<Object> serviceGroupData;
85 private List<Object> tagListData;
86 private List<Object> termListData;
91 * @throws Exception exception
94 public void setUp() throws Exception {
95 commonClassDao = mock(CommonClassDao.class);
96 final HttpSession mockSession = mock(HttpSession.class);
97 request = mock(HttpServletRequest.class);
98 response = new MockHttpServletResponse();
99 User user = new User();
100 user.setOrgUserId("Test");
101 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
102 Mockito.when(request.getSession(false)).thenReturn(mockSession);
103 clRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
104 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
105 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
106 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
107 + "\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\","
108 + "\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
109 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
110 + "\"$$hashKey\":\"object:1439\",\"policy\":{\"policyType\":\"Config\","
111 + "\"configPolicyType\":\"ClosedLoop_Fault\",\"triggerTrapSignatures\":[1,1,2,3],"
112 + "\"triggerfaultSignatures\":[1,1,2,3],\"traptriggerSignatures\":[{\"id\":\"Trap1\","
113 + "\"$$hashKey\":\"object:1526\"},{\"id\":\"Trap2\",\"$$hashKey\":\"object:1534\"}],"
114 + "\"connecttriggerSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1554\","
115 + "\"notBox\":\"NOT\",\"connectTrap1\":\"Trap1\",\"trapCount1\":\"12\","
116 + "\"operatorBox\":\"AND\",\"connectTrap2\":\"Trap2\",\"trapCount2\":\"14\"}],"
117 + "\"faulttriggerSignatures\":[{\"id\":\"Fault1\",\"$$hashKey\":\"object:1566\"},"
118 + "{\"id\":\"Fault2\",\"$$hashKey\":\"object:1575\"}],\"connectVerificationSignatures\":"
119 + "[{\"id\":\"C1\",\"$$hashKey\":\"object:1595\",\"notBox\":\"NOT\","
120 + "\"connectTrap1\":\"Fault1\",\"trapCount1\":\"11\",\"operatorBox\":\"AND\","
121 + "\"connectTrap2\":\"Fault2\",\"trapCount2\":\"12\"}],\"jsonBodyData\":"
122 + "{\"trapMaxAge\":\"300\",\"vnfType\":\"Test\",\"closedLoopPolicyStatus\":\"Active\","
123 + "\"vUSP\":true,\"trinity\":true,\"vDNS\":true,\"mcr\":true,\"gamma\":true,"
124 + "\"actions\":\"Test\",\"timeInterval\":\"11\",\"timeOutvPRO\":\"11\",\"timeOutRuby\":\"11\","
125 + "\"retrys\":\"1\",\"agingWindow\":\"12\",\"geoLink\":\"test\","
126 + "\"emailAddress\":\"aa@test.com\",\"pepName\":\"Test\",\"pepAction\":\"test\","
127 + "\"conditions\":\"SEND\"},\"policyName\":\"SampleTest\","
128 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
129 + "\"guard\":\"True\",\"onapName\":\"SampleDemo\",\"ttlDate\":\"14/09/2017\","
130 + "\"clearTimeOut\":\"123\",\"trapMaxAge\":\"11\",\"verificationclearTimeOut\":\"13\"}},"
131 + "\"trapData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1528\","
132 + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],"
133 + "\"trap2\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1536\",\"notBox\":\"NOT\","
134 + "\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\","
135 + "\"$$hashKey\":\"object:1542\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
136 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A3\","
137 + "\"$$hashKey\":\"object:1548\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
138 + "\"operatorBox\":\"OR\",\"trigger2\":\"A2\"}]},\"faultData\":{\"trap1\":[{\"id\":\"A1\","
139 + "\"$$hashKey\":\"object:1568\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
140 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\","
141 + "\"$$hashKey\":\"object:1577\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
142 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\",\"$$hashKey\":"
143 + "\"object:1583\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"OR\","
144 + "\"trigger2\":\"Test\"},{\"id\":\"A3\",\"$$hashKey\":\"object:1589\",\"notBox\":\"NOT\","
145 + "\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":\"A2\"}]}}";
147 fwRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
148 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
149 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
150 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"tempModel\":"
151 + "{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
152 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
153 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:260\","
154 + "\"policy\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\","
155 + "\"attributes\":[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\","
156 + "\"value\":\"Test\"}],\"fwattributes\":[],\"policyName\":\"SampleTest\","
157 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
158 + "\"guard\":\"True\",\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\","
159 + "\"securityZone\":\"Test\"}}}";
161 fwViewRequestString =
162 "{\"policyData\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\",\"attributes\":"
163 + "[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],"
164 + "\"fwattributes\":[],\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\","
165 + "\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\",\"guard\":\"True\","
166 + "\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}";
168 prefixListData = new ArrayList<>();
169 PrefixList prefixList = new PrefixList();
170 prefixList.setPrefixListName("Test");
171 prefixList.setPrefixListValue("10.10.10.10/12");
172 prefixListData.add(prefixList);
173 when(commonClassDao.getData(PrefixList.class)).thenReturn(prefixListData);
175 actionListData = new ArrayList<>();
176 ActionList actionList = new ActionList();
177 actionList.setActionName("Test");
178 actionListData.add(actionList);
179 when(commonClassDao.getData(ActionList.class)).thenReturn(actionListData);
181 serviceListData = new ArrayList<>();
182 ServiceList serviceList = new ServiceList();
183 serviceList.setServiceName("Test");
184 serviceList.setServiceType("SERVICE");
185 serviceList.setServiceTransportProtocol("Test");
186 serviceList.setServiceAppProtocol("Test");
187 serviceList.setServicePorts("8080");
188 serviceListData.add(serviceList);
189 when(commonClassDao.getData(ServiceList.class)).thenReturn(serviceListData);
191 addressGroupData = new ArrayList<>();
192 AddressGroup addressGroup = new AddressGroup();
193 addressGroup.setGroupName("Group_Test");
194 addressGroup.setServiceList("Test");
195 addressGroupData.add(addressGroup);
196 when(commonClassDao.getData(AddressGroup.class)).thenReturn(addressGroupData);
198 securityZoneData = new ArrayList<>();
199 SecurityZone securityZone = new SecurityZone();
200 securityZone.setZoneName("Test");
201 securityZone.setZoneValue("Test");
202 securityZoneData.add(securityZone);
203 when(commonClassDao.getData(SecurityZone.class)).thenReturn(securityZoneData);
205 serviceGroupData = new ArrayList<>();
206 GroupServiceList serviceGroup = new GroupServiceList();
207 serviceGroup.setGroupName("Group_Test");
208 serviceGroup.setServiceList("Test");
209 serviceGroupData.add(serviceGroup);
210 when(commonClassDao.getData(GroupServiceList.class)).thenReturn(serviceGroupData);
212 tagListData = new ArrayList<>();
213 FwTagPicker fwPicker = new FwTagPicker();
214 fwPicker.setTagPickerName("Test");
215 fwPicker.setTagValues("Test:8080");
216 tagListData.add(fwPicker);
217 when(commonClassDao.getData(FwTagPicker.class)).thenReturn(tagListData);
219 termListData = new ArrayList<>();
220 TermList termList = new TermList();
221 termList.setTermName("Test");
222 termList.setFromZone("Test");
223 termList.setToZone("Test");
224 termList.setSrcIpList("Test");
225 termList.setDestIpList("Test");
226 termList.setSrcPortList("Test");
227 termList.setDestPortList("Test");
228 termList.setAction("Test");
229 termListData.add(termList);
230 when(commonClassDao.getData(TermList.class)).thenReturn(termListData);
231 when(commonClassDao.getDataById(TermList.class, "termName", "Test")).thenReturn(termListData);
235 public final void testPolicyCreationController() {
236 assertNull(PolicyRestController.getCommonClassDao());
237 PolicyRestController.setCommonClassDao(commonClassDao);
238 assertNotNull(PolicyRestController.getCommonClassDao());
240 PolicyRestController controller = new PolicyRestController();
241 BufferedReader reader = new BufferedReader(new StringReader(clRequestString));
243 Mockito.when(request.getReader()).thenReturn(reader);
244 controller.policyCreationController(request, response);
245 } catch (IOException e) {
248 PolicyRestController controller1 = new PolicyRestController();
249 CreateFirewallController.setCommonClassDao(commonClassDao);
250 assertNotNull(CreateFirewallController.getCommonClassDao());
251 BufferedReader reader1 = new BufferedReader(new StringReader(fwRequestString));
253 Mockito.when(request.getReader()).thenReturn(reader1);
254 controller1.policyCreationController(request, response);
255 } catch (IOException e) {
259 CreateFirewallController fwController = new CreateFirewallController();
260 CreateFirewallController.setCommonClassDao(commonClassDao);
261 BufferedReader reader2 = new BufferedReader(new StringReader(fwViewRequestString));
263 Mockito.when(request.getReader()).thenReturn(reader2);
264 fwController.setFWViewRule(request, response);
265 } catch (IOException e) {
271 public final void testSearchPolicy() throws IOException {
272 PolicyController.setjUnit(true);
273 PolicyController.setPapUrl("http://localhost:8070/pap/");
274 PolicyRestController controller = new PolicyRestController();
275 BufferedReader reader = new BufferedReader(new StringReader("{\"foo\":\"bar\"}"));
276 Mockito.when(request.getReader()).thenReturn(reader);
277 Mockito.when(request.getRequestURI()).thenReturn("/pap/foo/");
278 assertThatExceptionOfType(NullPointerException.class)
279 .isThrownBy(() -> controller.searchPolicy(request, response));
283 public final void testSearchDictionaryController() throws IOException {
284 PolicyRestController controller = new PolicyRestController();
285 BufferedReader reader = new BufferedReader(new StringReader("{\"foo\":\"bar\"}"));
286 Mockito.when(request.getReader()).thenReturn(reader);
287 Mockito.when(request.getRequestURI()).thenReturn("/pap/foo/");
288 assertNull(controller.searchDictionaryController(request, response));
292 public final void testDeleteElasticData() {
293 PolicyRestController controller = new PolicyRestController();
295 controller.deleteElasticData("Test");
296 } catch (Exception e) {
302 public final void testNotifyOtherPaps() {
303 PolicyRestController controller = new PolicyRestController();
304 String strReturn = controller.notifyOtherPapsToUpdateConfigurations("mode", "newName", "oldName");
305 assertNull(strReturn);
308 @SuppressWarnings("unchecked")
310 public void testGetDictionaryController() throws IOException {
311 PolicyRestController controller = new PolicyRestController();
312 HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
313 HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
314 PrintWriter mockPrintWriter = Mockito.mock(PrintWriter.class);
316 when(mockRequest.getRequestURI()).thenReturn("//testRequestURI///getDictionary");
317 when(mockResponse.getWriter()).thenThrow(IOException.class);
318 controller.getDictionaryController(mockRequest, mockResponse);
319 verify(mockRequest, atLeast(1)).getRequestURI();
320 verify(mockResponse, atLeast(1)).getWriter();
323 when(mockRequest.getRequestURI()).thenReturn("testRequestURI///getDictionary");
324 when(mockResponse.getWriter()).thenReturn(mockPrintWriter);
325 controller.getDictionaryController(mockRequest, mockResponse);
326 verify(mockRequest, atLeast(1)).getRequestURI();
327 verify(mockResponse, atLeast(1)).getWriter();
330 @PrepareForTest(UserUtils.class)
332 public void testdeletetDictionaryController() throws IOException {
333 HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
334 HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
335 PolicyRestController controller = new PolicyRestController();
336 User mockUser = Mockito.mock(User.class);
337 PowerMockito.mockStatic(UserUtils.class);
338 PrintWriter mockPrintWriter = Mockito.mock(PrintWriter.class);
340 when(mockRequest.getRequestURI()).thenReturn("//testRequestURI///deleteDictionary");
341 when(mockResponse.getWriter()).thenReturn(mockPrintWriter);
342 when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser);
343 when(mockUser.getOrgUserId()).thenReturn("testUserId");
345 controller.deletetDictionaryController(mockRequest, mockResponse);
346 verify(mockRequest).getRequestURI();
347 verify(mockResponse).getWriter();
348 verify(mockUser).getOrgUserId();
350 when(mockRequest.getRequestURI()).thenReturn("testRequestURI///deleteDictionary");
351 controller.deletetDictionaryController(mockRequest, mockResponse);
352 verify(mockRequest, atLeast(1)).getRequestURI();
355 @PrepareForTest(UserUtils.class)
357 public void testSaveDictionaryController() throws IOException {
358 HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
359 HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
360 PolicyRestController controller = new PolicyRestController();
361 User mockUser = Mockito.mock(User.class);
362 PowerMockito.mockStatic(UserUtils.class);
363 PrintWriter mockPrintWriter = Mockito.mock(PrintWriter.class);
365 when(mockRequest.getRequestURI()).thenReturn("//testRequestURI///saveDictionary//import_dictionary");
366 when(mockResponse.getWriter()).thenReturn(mockPrintWriter);
367 when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser);
368 when(mockUser.getOrgUserId()).thenReturn("testUserId");
370 controller.saveDictionaryController(mockRequest, mockResponse);
371 verify(mockRequest).getRequestURI();
372 verify(mockResponse).getWriter();
373 verify(mockUser).getOrgUserId();
375 when(mockRequest.getRequestURI()).thenReturn("testRequestURI///saveDictionary");
376 controller.saveDictionaryController(mockRequest, mockResponse);
377 verify(mockRequest, atLeast(1)).getRequestURI();
381 public void testDoConnect() throws Exception {
382 HttpURLConnection mockConnection = Mockito.mock(HttpURLConnection.class);
383 PolicyRestController controller = new PolicyRestController();
384 Mockito.doNothing().when(mockConnection).connect();
385 InputStream mockInputStream = Mockito.mock(InputStream.class);
387 when(mockConnection.getResponseCode()).thenReturn(200);
388 when(mockConnection.getInputStream()).thenReturn(mockInputStream);
390 Whitebox.invokeMethod(controller, "doConnect", mockConnection);
391 verify(mockConnection).getResponseCode();
392 verify(mockConnection).getInputStream();
394 when(mockConnection.getResponseCode()).thenReturn(201);
395 assertNull(Whitebox.invokeMethod(controller, "doConnect", mockConnection));