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.mock;
30 import static org.mockito.Mockito.when;
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.StringReader;
35 import java.util.ArrayList;
36 import java.util.List;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpSession;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.controller.CreateFirewallController;
45 import org.onap.policy.controller.PolicyController;
46 import org.onap.policy.rest.dao.CommonClassDao;
47 import org.onap.policy.rest.jpa.ActionList;
48 import org.onap.policy.rest.jpa.AddressGroup;
49 import org.onap.policy.rest.jpa.FWTagPicker;
50 import org.onap.policy.rest.jpa.GroupServiceList;
51 import org.onap.policy.rest.jpa.PrefixList;
52 import org.onap.policy.rest.jpa.SecurityZone;
53 import org.onap.policy.rest.jpa.ServiceList;
54 import org.onap.policy.rest.jpa.TermList;
55 import org.onap.portalsdk.core.domain.User;
56 import org.onap.portalsdk.core.util.SystemProperties;
57 import org.springframework.mock.web.MockHttpServletResponse;
59 public class PolicyRestControllerTest {
61 private String clRequestString;
62 private String fwRequestString;
63 private String fwViewRequestString;
64 private HttpServletRequest request;
65 private MockHttpServletResponse response;
66 private static CommonClassDao commonClassDao;
67 private List<Object> prefixListData;
68 private List<Object> actionListData;
69 private List<Object> serviceListData;
70 private List<Object> addressGroupData;
71 private List<Object> securityZoneData;
72 private List<Object> serviceGroupData;
73 private List<Object> tagListData;
74 private List<Object> termListData;
79 * @throws Exception exception
82 public void setUp() throws Exception {
83 commonClassDao = mock(CommonClassDao.class);
84 final HttpSession mockSession = mock(HttpSession.class);
85 request = mock(HttpServletRequest.class);
86 response = new MockHttpServletResponse();
87 User user = new User();
88 user.setOrgUserId("Test");
89 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
90 Mockito.when(request.getSession(false)).thenReturn(mockSession);
91 clRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
92 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
93 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
94 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
95 + "\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\","
96 + "\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
97 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
98 + "\"$$hashKey\":\"object:1439\",\"policy\":{\"policyType\":\"Config\","
99 + "\"configPolicyType\":\"ClosedLoop_Fault\",\"triggerTrapSignatures\":[1,1,2,3],"
100 + "\"triggerfaultSignatures\":[1,1,2,3],\"traptriggerSignatures\":[{\"id\":\"Trap1\","
101 + "\"$$hashKey\":\"object:1526\"},{\"id\":\"Trap2\",\"$$hashKey\":\"object:1534\"}],"
102 + "\"connecttriggerSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1554\","
103 + "\"notBox\":\"NOT\",\"connectTrap1\":\"Trap1\",\"trapCount1\":\"12\","
104 + "\"operatorBox\":\"AND\",\"connectTrap2\":\"Trap2\",\"trapCount2\":\"14\"}],"
105 + "\"faulttriggerSignatures\":[{\"id\":\"Fault1\",\"$$hashKey\":\"object:1566\"},"
106 + "{\"id\":\"Fault2\",\"$$hashKey\":\"object:1575\"}],\"connectVerificationSignatures\":"
107 + "[{\"id\":\"C1\",\"$$hashKey\":\"object:1595\",\"notBox\":\"NOT\","
108 + "\"connectTrap1\":\"Fault1\",\"trapCount1\":\"11\",\"operatorBox\":\"AND\","
109 + "\"connectTrap2\":\"Fault2\",\"trapCount2\":\"12\"}],\"jsonBodyData\":"
110 + "{\"trapMaxAge\":\"300\",\"vnfType\":\"Test\",\"closedLoopPolicyStatus\":\"Active\","
111 + "\"vUSP\":true,\"trinity\":true,\"vDNS\":true,\"mcr\":true,\"gamma\":true,"
112 + "\"actions\":\"Test\",\"timeInterval\":\"11\",\"timeOutvPRO\":\"11\",\"timeOutRuby\":\"11\","
113 + "\"retrys\":\"1\",\"agingWindow\":\"12\",\"geoLink\":\"test\","
114 + "\"emailAddress\":\"aa@test.com\",\"pepName\":\"Test\",\"pepAction\":\"test\","
115 + "\"conditions\":\"SEND\"},\"policyName\":\"SampleTest\","
116 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
117 + "\"guard\":\"True\",\"onapName\":\"SampleDemo\",\"ttlDate\":\"14/09/2017\","
118 + "\"clearTimeOut\":\"123\",\"trapMaxAge\":\"11\",\"verificationclearTimeOut\":\"13\"}},"
119 + "\"trapData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1528\","
120 + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],"
121 + "\"trap2\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1536\",\"notBox\":\"NOT\","
122 + "\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\","
123 + "\"$$hashKey\":\"object:1542\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
124 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A3\","
125 + "\"$$hashKey\":\"object:1548\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
126 + "\"operatorBox\":\"OR\",\"trigger2\":\"A2\"}]},\"faultData\":{\"trap1\":[{\"id\":\"A1\","
127 + "\"$$hashKey\":\"object:1568\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
128 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\","
129 + "\"$$hashKey\":\"object:1577\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
130 + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\",\"$$hashKey\":"
131 + "\"object:1583\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"OR\","
132 + "\"trigger2\":\"Test\"},{\"id\":\"A3\",\"$$hashKey\":\"object:1589\",\"notBox\":\"NOT\","
133 + "\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":\"A2\"}]}}";
135 fwRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
136 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
137 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
138 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"tempModel\":"
139 + "{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
140 + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
141 + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:260\","
142 + "\"policy\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\","
143 + "\"attributes\":[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\","
144 + "\"value\":\"Test\"}],\"fwattributes\":[],\"policyName\":\"SampleTest\","
145 + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
146 + "\"guard\":\"True\",\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\","
147 + "\"securityZone\":\"Test\"}}}";
149 fwViewRequestString =
150 "{\"policyData\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\",\"attributes\":"
151 + "[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],"
152 + "\"fwattributes\":[],\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\","
153 + "\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\",\"guard\":\"True\","
154 + "\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}";
156 prefixListData = new ArrayList<>();
157 PrefixList prefixList = new PrefixList();
158 prefixList.setPrefixListName("Test");
159 prefixList.setPrefixListValue("10.10.10.10/12");
160 prefixListData.add(prefixList);
161 when(commonClassDao.getData(PrefixList.class)).thenReturn(prefixListData);
163 actionListData = new ArrayList<>();
164 ActionList actionList = new ActionList();
165 actionList.setActionName("Test");
166 actionListData.add(actionList);
167 when(commonClassDao.getData(ActionList.class)).thenReturn(actionListData);
169 serviceListData = new ArrayList<>();
170 ServiceList serviceList = new ServiceList();
171 serviceList.setServiceName("Test");
172 serviceList.setServiceType("SERVICE");
173 serviceList.setServiceTransProtocol("Test");
174 serviceList.setServiceAppProtocol("Test");
175 serviceList.setServicePorts("8080");
176 serviceListData.add(serviceList);
177 when(commonClassDao.getData(ServiceList.class)).thenReturn(serviceListData);
179 addressGroupData = new ArrayList<>();
180 AddressGroup addressGroup = new AddressGroup();
181 addressGroup.setGroupName("Group_Test");
182 addressGroup.setServiceList("Test");
183 addressGroupData.add(addressGroup);
184 when(commonClassDao.getData(AddressGroup.class)).thenReturn(addressGroupData);
186 securityZoneData = new ArrayList<>();
187 SecurityZone securityZone = new SecurityZone();
188 securityZone.setZoneName("Test");
189 securityZone.setZoneValue("Test");
190 securityZoneData.add(securityZone);
191 when(commonClassDao.getData(SecurityZone.class)).thenReturn(securityZoneData);
193 serviceGroupData = new ArrayList<>();
194 GroupServiceList serviceGroup = new GroupServiceList();
195 serviceGroup.setGroupName("Group_Test");
196 serviceGroup.setServiceList("Test");
197 serviceGroupData.add(serviceGroup);
198 when(commonClassDao.getData(GroupServiceList.class)).thenReturn(serviceGroupData);
200 tagListData = new ArrayList<>();
201 FWTagPicker fwPicker = new FWTagPicker();
202 fwPicker.setTagPickerName("Test");
203 fwPicker.setTagValues("Test:8080");
204 tagListData.add(fwPicker);
205 when(commonClassDao.getData(FWTagPicker.class)).thenReturn(tagListData);
207 termListData = new ArrayList<>();
208 TermList termList = new TermList();
209 termList.setTermName("Test");
210 termList.setFromZones("Test");
211 termList.setToZones("Test");
212 termList.setSrcIPList("Test");
213 termList.setDestIPList("Test");
214 termList.setSrcPortList("Test");
215 termList.setDestPortList("Test");
216 termList.setAction("Test");
217 termListData.add(termList);
218 when(commonClassDao.getData(TermList.class)).thenReturn(termListData);
219 when(commonClassDao.getDataById(TermList.class, "termName", "Test")).thenReturn(termListData);
223 public final void testPolicyCreationController() {
224 assertNull(PolicyRestController.getCommonClassDao());
225 PolicyRestController.setCommonClassDao(commonClassDao);
226 assertNotNull(PolicyRestController.getCommonClassDao());
228 PolicyRestController controller = new PolicyRestController();
229 BufferedReader reader = new BufferedReader(new StringReader(clRequestString));
231 Mockito.when(request.getReader()).thenReturn(reader);
232 controller.policyCreationController(request, response);
233 } catch (IOException e) {
236 PolicyRestController controller1 = new PolicyRestController();
237 CreateFirewallController.setCommonClassDao(commonClassDao);
238 assertNotNull(CreateFirewallController.getCommonClassDao());
239 BufferedReader reader1 = new BufferedReader(new StringReader(fwRequestString));
241 Mockito.when(request.getReader()).thenReturn(reader1);
242 controller1.policyCreationController(request, response);
243 } catch (IOException e) {
247 CreateFirewallController fwController = new CreateFirewallController();
248 CreateFirewallController.setCommonClassDao(commonClassDao);
249 BufferedReader reader2 = new BufferedReader(new StringReader(fwViewRequestString));
251 Mockito.when(request.getReader()).thenReturn(reader2);
252 fwController.setFWViewRule(request, response);
253 } catch (IOException e) {
259 public final void testSearchPolicy() throws IOException {
260 PolicyController.setjUnit(true);
261 PolicyController.setPapUrl("http://localhost:8070/pap/");
262 PolicyRestController controller = new PolicyRestController();
263 BufferedReader reader = new BufferedReader(new StringReader("{\"foo\":\"bar\"}"));
264 Mockito.when(request.getReader()).thenReturn(reader);
265 Mockito.when(request.getRequestURI()).thenReturn("/pap/foo/");
266 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
267 controller.searchPolicy(request, response));
271 public final void testSearchDictionaryController() throws IOException {
272 PolicyRestController controller = new PolicyRestController();
273 BufferedReader reader = new BufferedReader(new StringReader("{\"foo\":\"bar\"}"));
274 Mockito.when(request.getReader()).thenReturn(reader);
275 Mockito.when(request.getRequestURI()).thenReturn("/pap/foo/");
276 assertNull(controller.searchDictionaryController(request, response));
280 public final void testDeleteElasticData() {
281 PolicyRestController controller = new PolicyRestController();
283 controller.deleteElasticData("Test");
284 } catch (Exception e) {
290 public final void testNotifyOtherPaps() {
291 PolicyRestController controller = new PolicyRestController();
292 String strReturn = controller.notifyOtherPapsToUpdateConfigurations("mode", "newName", "oldName");
293 assertNull(strReturn);