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.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.PrintWriter;
38 import java.io.StringReader;
39 import java.util.ArrayList;
40 import java.util.List;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47 import org.onap.policy.controller.CreateFirewallController;
48 import org.onap.policy.controller.PolicyController;
49 import org.onap.policy.rest.dao.CommonClassDao;
50 import org.onap.policy.rest.jpa.ActionList;
51 import org.onap.policy.rest.jpa.AddressGroup;
52 import org.onap.policy.rest.jpa.FwTagPicker;
53 import org.onap.policy.rest.jpa.GroupServiceList;
54 import org.onap.policy.rest.jpa.PrefixList;
55 import org.onap.policy.rest.jpa.SecurityZone;
56 import org.onap.policy.rest.jpa.ServiceList;
57 import org.onap.policy.rest.jpa.TermList;
58 import org.onap.portalsdk.core.domain.User;
59 import org.onap.portalsdk.core.util.SystemProperties;
60 import org.springframework.mock.web.MockHttpServletResponse;
62 public class PolicyRestControllerTest {
64 private String clRequestString;
65 private String fwRequestString;
66 private String fwViewRequestString;
67 private HttpServletRequest request;
68 private MockHttpServletResponse response;
69 private static CommonClassDao commonClassDao;
70 private List<Object> prefixListData;
71 private List<Object> actionListData;
72 private List<Object> serviceListData;
73 private List<Object> addressGroupData;
74 private List<Object> securityZoneData;
75 private List<Object> serviceGroupData;
76 private List<Object> tagListData;
77 private List<Object> termListData;
82 * @throws Exception exception
85 public void setUp() throws Exception {
86 commonClassDao = mock(CommonClassDao.class);
87 final HttpSession mockSession = mock(HttpSession.class);
88 request = mock(HttpServletRequest.class);
89 response = new MockHttpServletResponse();
90 User user = new User();
91 user.setOrgUserId("Test");
92 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
93 Mockito.when(request.getSession(false)).thenReturn(mockSession);
94 clRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
95 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
96 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
97 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
98 + "\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\","
99 + "\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
100 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
101 + "\"$$hashKey\":\"object:1439\",\"policy\":{\"policyType\":\"Config\","
102 + "\"configPolicyType\":\"ClosedLoop_Fault\",\"triggerTrapSignatures\":[1,1,2,3],"
103 + "\"triggerfaultSignatures\":[1,1,2,3],\"traptriggerSignatures\":[{\"id\":\"Trap1\","
104 + "\"$$hashKey\":\"object:1526\"},{\"id\":\"Trap2\",\"$$hashKey\":\"object:1534\"}],"
105 + "\"connecttriggerSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1554\","
106 + "\"notBox\":\"NOT\",\"connectTrap1\":\"Trap1\",\"trapCount1\":\"12\","
107 + "\"operatorBox\":\"AND\",\"connectTrap2\":\"Trap2\",\"trapCount2\":\"14\"}],"
108 + "\"faulttriggerSignatures\":[{\"id\":\"Fault1\",\"$$hashKey\":\"object:1566\"},"
109 + "{\"id\":\"Fault2\",\"$$hashKey\":\"object:1575\"}],\"connectVerificationSignatures\":"
110 + "[{\"id\":\"C1\",\"$$hashKey\":\"object:1595\",\"notBox\":\"NOT\","
111 + "\"connectTrap1\":\"Fault1\",\"trapCount1\":\"11\",\"operatorBox\":\"AND\","
112 + "\"connectTrap2\":\"Fault2\",\"trapCount2\":\"12\"}],\"jsonBodyData\":"
113 + "{\"trapMaxAge\":\"300\",\"vnfType\":\"Test\",\"closedLoopPolicyStatus\":\"Active\","
114 + "\"vUSP\":true,\"trinity\":true,\"vDNS\":true,\"mcr\":true,\"gamma\":true,"
115 + "\"actions\":\"Test\",\"timeInterval\":\"11\",\"timeOutvPRO\":\"11\",\"timeOutRuby\":\"11\","
116 + "\"retrys\":\"1\",\"agingWindow\":\"12\",\"geoLink\":\"test\","
117 + "\"emailAddress\":\"aa@test.com\",\"pepName\":\"Test\",\"pepAction\":\"test\","
118 + "\"conditions\":\"SEND\"},\"policyName\":\"SampleTest\","
119 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
120 + "\"guard\":\"True\",\"onapName\":\"SampleDemo\",\"ttlDate\":\"14/09/2017\","
121 + "\"clearTimeOut\":\"123\",\"trapMaxAge\":\"11\",\"verificationclearTimeOut\":\"13\"}},"
122 + "\"trapData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1528\","
123 + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],"
124 + "\"trap2\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1536\",\"notBox\":\"NOT\","
125 + "\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\","
126 + "\"$$hashKey\":\"object:1542\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
127 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A3\","
128 + "\"$$hashKey\":\"object:1548\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
129 + "\"operatorBox\":\"OR\",\"trigger2\":\"A2\"}]},\"faultData\":{\"trap1\":[{\"id\":\"A1\","
130 + "\"$$hashKey\":\"object:1568\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
131 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\","
132 + "\"$$hashKey\":\"object:1577\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
133 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\",\"$$hashKey\":"
134 + "\"object:1583\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"OR\","
135 + "\"trigger2\":\"Test\"},{\"id\":\"A3\",\"$$hashKey\":\"object:1589\",\"notBox\":\"NOT\","
136 + "\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":\"A2\"}]}}";
138 fwRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
139 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
140 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
141 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"tempModel\":"
142 + "{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
143 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
144 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:260\","
145 + "\"policy\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\","
146 + "\"attributes\":[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\","
147 + "\"value\":\"Test\"}],\"fwattributes\":[],\"policyName\":\"SampleTest\","
148 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
149 + "\"guard\":\"True\",\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\","
150 + "\"securityZone\":\"Test\"}}}";
152 fwViewRequestString =
153 "{\"policyData\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\",\"attributes\":"
154 + "[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],"
155 + "\"fwattributes\":[],\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\","
156 + "\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\",\"guard\":\"True\","
157 + "\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}";
159 prefixListData = new ArrayList<>();
160 PrefixList prefixList = new PrefixList();
161 prefixList.setPrefixListName("Test");
162 prefixList.setPrefixListValue("10.10.10.10/12");
163 prefixListData.add(prefixList);
164 when(commonClassDao.getData(PrefixList.class)).thenReturn(prefixListData);
166 actionListData = new ArrayList<>();
167 ActionList actionList = new ActionList();
168 actionList.setActionName("Test");
169 actionListData.add(actionList);
170 when(commonClassDao.getData(ActionList.class)).thenReturn(actionListData);
172 serviceListData = new ArrayList<>();
173 ServiceList serviceList = new ServiceList();
174 serviceList.setServiceName("Test");
175 serviceList.setServiceType("SERVICE");
176 serviceList.setServiceTransportProtocol("Test");
177 serviceList.setServiceAppProtocol("Test");
178 serviceList.setServicePorts("8080");
179 serviceListData.add(serviceList);
180 when(commonClassDao.getData(ServiceList.class)).thenReturn(serviceListData);
182 addressGroupData = new ArrayList<>();
183 AddressGroup addressGroup = new AddressGroup();
184 addressGroup.setGroupName("Group_Test");
185 addressGroup.setServiceList("Test");
186 addressGroupData.add(addressGroup);
187 when(commonClassDao.getData(AddressGroup.class)).thenReturn(addressGroupData);
189 securityZoneData = new ArrayList<>();
190 SecurityZone securityZone = new SecurityZone();
191 securityZone.setZoneName("Test");
192 securityZone.setZoneValue("Test");
193 securityZoneData.add(securityZone);
194 when(commonClassDao.getData(SecurityZone.class)).thenReturn(securityZoneData);
196 serviceGroupData = new ArrayList<>();
197 GroupServiceList serviceGroup = new GroupServiceList();
198 serviceGroup.setGroupName("Group_Test");
199 serviceGroup.setServiceList("Test");
200 serviceGroupData.add(serviceGroup);
201 when(commonClassDao.getData(GroupServiceList.class)).thenReturn(serviceGroupData);
203 tagListData = new ArrayList<>();
204 FwTagPicker fwPicker = new FwTagPicker();
205 fwPicker.setTagPickerName("Test");
206 fwPicker.setTagValues("Test:8080");
207 tagListData.add(fwPicker);
208 when(commonClassDao.getData(FwTagPicker.class)).thenReturn(tagListData);
210 termListData = new ArrayList<>();
211 TermList termList = new TermList();
212 termList.setTermName("Test");
213 termList.setFromZone("Test");
214 termList.setToZone("Test");
215 termList.setSrcIpList("Test");
216 termList.setDestIpList("Test");
217 termList.setSrcPortList("Test");
218 termList.setDestPortList("Test");
219 termList.setAction("Test");
220 termListData.add(termList);
221 when(commonClassDao.getData(TermList.class)).thenReturn(termListData);
222 when(commonClassDao.getDataById(TermList.class, "termName", "Test")).thenReturn(termListData);
226 public final void testPolicyCreationController() {
227 assertNull(PolicyRestController.getCommonClassDao());
228 PolicyRestController.setCommonClassDao(commonClassDao);
229 assertNotNull(PolicyRestController.getCommonClassDao());
231 PolicyRestController controller = new PolicyRestController();
232 BufferedReader reader = new BufferedReader(new StringReader(clRequestString));
234 Mockito.when(request.getReader()).thenReturn(reader);
235 controller.policyCreationController(request, response);
236 } catch (IOException e) {
239 PolicyRestController controller1 = new PolicyRestController();
240 CreateFirewallController.setCommonClassDao(commonClassDao);
241 assertNotNull(CreateFirewallController.getCommonClassDao());
242 BufferedReader reader1 = new BufferedReader(new StringReader(fwRequestString));
244 Mockito.when(request.getReader()).thenReturn(reader1);
245 controller1.policyCreationController(request, response);
246 } catch (IOException e) {
250 CreateFirewallController fwController = new CreateFirewallController();
251 CreateFirewallController.setCommonClassDao(commonClassDao);
252 BufferedReader reader2 = new BufferedReader(new StringReader(fwViewRequestString));
254 Mockito.when(request.getReader()).thenReturn(reader2);
255 fwController.setFWViewRule(request, response);
256 } catch (IOException e) {
262 public final void testSearchPolicy() throws IOException {
263 PolicyController.setjUnit(true);
264 PolicyController.setPapUrl("http://localhost:8070/pap/");
265 PolicyRestController controller = new PolicyRestController();
266 BufferedReader reader = new BufferedReader(new StringReader("{\"foo\":\"bar\"}"));
267 Mockito.when(request.getReader()).thenReturn(reader);
268 Mockito.when(request.getRequestURI()).thenReturn("/pap/foo/");
269 assertThatExceptionOfType(NullPointerException.class)
270 .isThrownBy(() -> controller.searchPolicy(request, response));
274 public final void testSearchDictionaryController() throws IOException {
275 PolicyRestController controller = new PolicyRestController();
276 BufferedReader reader = new BufferedReader(new StringReader("{\"foo\":\"bar\"}"));
277 Mockito.when(request.getReader()).thenReturn(reader);
278 Mockito.when(request.getRequestURI()).thenReturn("/pap/foo/");
279 assertNull(controller.searchDictionaryController(request, response));
283 public final void testDeleteElasticData() {
284 PolicyRestController controller = new PolicyRestController();
286 controller.deleteElasticData("Test");
287 } catch (Exception e) {
293 public final void testNotifyOtherPaps() {
294 PolicyRestController controller = new PolicyRestController();
295 String strReturn = controller.notifyOtherPapsToUpdateConfigurations("mode", "newName", "oldName");
296 assertNull(strReturn);
299 @SuppressWarnings("unchecked")
301 public void testGetDictionaryController() throws IOException {
302 PolicyRestController controller = new PolicyRestController();
303 HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
304 HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
305 PrintWriter mockPrintWriter = Mockito.mock(PrintWriter.class);
307 when(mockRequest.getRequestURI()).thenReturn("//testRequestURI///getDictionary");
308 when(mockResponse.getWriter()).thenThrow(IOException.class);
309 controller.getDictionaryController(mockRequest, mockResponse);
310 verify(mockRequest, atLeast(1)).getRequestURI();
311 verify(mockResponse, atLeast(1)).getWriter();
314 when(mockRequest.getRequestURI()).thenReturn("testRequestURI///getDictionary");
315 when(mockResponse.getWriter()).thenReturn(mockPrintWriter);
316 controller.getDictionaryController(mockRequest, mockResponse);
317 verify(mockRequest, atLeast(1)).getRequestURI();
318 verify(mockResponse, atLeast(1)).getWriter();