e305b1221bd7f8febaaa0e51c5f15b73fd98dd11
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / FirewallDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018 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.controller;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.StringReader;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import javax.servlet.http.HttpServletRequest;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
45 import org.onap.policy.rest.adapter.Term;
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.FWTag;
50 import org.onap.policy.rest.jpa.FWTagPicker;
51 import org.onap.policy.rest.jpa.FirewallDictionaryList;
52 import org.onap.policy.rest.jpa.GroupServiceList;
53 import org.onap.policy.rest.jpa.PortList;
54 import org.onap.policy.rest.jpa.PrefixList;
55 import org.onap.policy.rest.jpa.ProtocolList;
56 import org.onap.policy.rest.jpa.SecurityZone;
57 import org.onap.policy.rest.jpa.ServiceList;
58 import org.onap.policy.rest.jpa.TermList;
59 import org.onap.policy.rest.jpa.UserInfo;
60 import org.onap.policy.rest.jpa.Zone;
61 import org.springframework.mock.web.MockHttpServletResponse;
62
63 public class FirewallDictionaryControllerTest {
64
65         private static Logger logger = FlexLogger.getLogger(FirewallDictionaryControllerTest.class);
66         private static CommonClassDao commonClassDao;
67         private String jsonString = null;
68         private HttpServletRequest request = null;
69         private FirewallDictionaryController controller = null;
70         private MockHttpServletResponse response = null;
71         private UserInfo userInfo;
72         private List<String> data;
73
74         @Before
75         public void setUp() throws Exception {
76                 logger.info("setUp: Entering");
77                 commonClassDao = Mockito.mock(CommonClassDao.class);
78
79                 data = new ArrayList<>();
80                 data.add("Test");
81
82                 userInfo = new UserInfo();
83                 userInfo.setUserLoginId("Test");
84                 userInfo.setUserName("Test");
85
86                 doNothing().when(commonClassDao).delete(any(Term.class));
87                 doNothing().when(commonClassDao).save(any(Term.class));
88
89                 controller = new FirewallDictionaryController();
90                 FirewallDictionaryController.setCommonClassDao(commonClassDao);
91
92                 request = Mockito.mock(HttpServletRequest.class);
93                 response = new MockHttpServletResponse();
94                 new DictionaryUtils(commonClassDao);
95                 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
96                 mock(DictionaryUtils.class);
97                 logger.info("setUp: exit");
98         }
99
100         @Test
101         public void testGetPrefixListDictionaryEntityDataByName() {
102                 test_WithGetDataByColumn(PrefixList.class, "prefixListDictionaryDatas", "prefixListName",
103                                 () -> controller.getPrefixListDictionaryEntityDataByName(response));
104         }
105
106         @Test
107         public void testGetPrefixListDictionaryEntityData() {
108                 test_WithGetData(PrefixList.class, "prefixListDictionaryDatas",
109                                 () -> controller.getPrefixListDictionaryEntityData(response));
110         }
111
112         @Test
113         public void testGetPortListDictionaryEntityData() {
114                 test_WithGetData(PortList.class, "portListDictionaryDatas",
115                                 () -> controller.getPortListDictionaryEntityData(response));
116         }
117
118         @Test
119         public void testGetProtocolListDictionaryEntityDataByName() {
120                 test_WithGetDataByColumn(ProtocolList.class, "protocolListDictionaryDatas", "protocolName",
121                                 () -> controller.getProtocolListDictionaryEntityDataByName(response));
122         }
123
124         @Test
125         public void testGetProtocolListDictionaryEntityData() {
126                 test_WithGetData(ProtocolList.class, "protocolListDictionaryDatas",
127                                 () -> controller.getProtocolListDictionaryEntityData(response));
128         }
129
130         @Test
131         public void testGetAddressGroupDictionaryEntityDataByName() {
132                 test_WithGetDataByColumn(AddressGroup.class, "addressGroupDictionaryDatas", "name",
133                                 () -> controller.getAddressGroupDictionaryEntityDataByName(response));
134         }
135
136         @Test
137         public void testGetAddressGroupDictionaryEntityData() {
138                 test_WithGetData(AddressGroup.class, "addressGroupDictionaryDatas",
139                                 () -> controller.getAddressGroupDictionaryEntityData(response));
140         }
141
142         @Test
143         public void testGetActionListDictionaryEntityDataByName() {
144                 test_WithGetDataByColumn(ActionList.class, "actionListDictionaryDatas", "actionName",
145                                 () -> controller.getActionListDictionaryEntityDataByName(response));
146         }
147
148         @Test
149         public void testGetActionListDictionaryEntityData() {
150                 test_WithGetData(ActionList.class, "actionListDictionaryDatas",
151                                 () -> controller.getActionListDictionaryEntityData(response));
152         }
153
154         @Test
155         public void testGetServiceGroupDictionaryEntityDataByName() {
156                 test_WithGetDataByColumn(GroupServiceList.class, "serviceGroupDictionaryDatas", "name",
157                                 () -> controller.getServiceGroupDictionaryEntityDataByName(response));
158         }
159
160         @Test
161         public void testGetServiceGroupDictionaryEntityData() {
162                 test_WithGetData(GroupServiceList.class, "serviceGroupDictionaryDatas",
163                                 () -> controller.getServiceGroupDictionaryEntityData(response));
164         }
165
166         @Test
167         public void testGetSecurityZoneDictionaryEntityDataByName() {
168                 test_WithGetDataByColumn(SecurityZone.class, "securityZoneDictionaryDatas", "zoneName",
169                                 () -> controller.getSecurityZoneDictionaryEntityDataByName(response));
170         }
171
172         @Test
173         public void testGetSecurityZoneDictionaryEntityData() {
174                 test_WithGetData(SecurityZone.class, "securityZoneDictionaryDatas",
175                                 () -> controller.getSecurityZoneDictionaryEntityData(response));
176         }
177
178         @Test
179         public void testGetServiceListDictionaryEntityDataByName() {
180                 test_WithGetDataByColumn(ServiceList.class, "serviceListDictionaryDatas", "serviceName",
181                                 () -> controller.getServiceListDictionaryEntityDataByName(response));
182         }
183
184         @Test
185         public void testGetServiceListDictionaryEntityData() {
186                 test_WithGetData(ServiceList.class, "serviceListDictionaryDatas",
187                                 () -> controller.getServiceListDictionaryEntityData(response));
188         }
189
190         @Test
191         public void testGetZoneDictionaryEntityDataByName() {
192                 test_WithGetDataByColumn(Zone.class, "zoneDictionaryDatas", "zoneName",
193                                 () -> controller.getZoneDictionaryEntityDataByName(response));
194         }
195
196         @Test
197         public void testGetZoneDictionaryEntityData() {
198                 test_WithGetData(Zone.class, "zoneDictionaryDatas", () -> controller.getZoneDictionaryEntityData(response));
199         }
200
201         @Test
202         public void testGetTermListDictionaryEntityDataByName() {
203                 test_WithGetDataByColumn(TermList.class, "termListDictionaryDatas", "termName",
204                                 () -> controller.getTermListDictionaryEntityDataByName(response));
205         }
206
207         @Test
208         public void testGetTermListDictionaryEntityData() {
209                 test_WithGetData(TermList.class, "termListDictionaryDatas",
210                                 () -> controller.getTermListDictionaryEntityData(response));
211         }
212
213         @Test
214         public void testGetFWDictListDictionaryEntityDataByName() {
215                 test_WithGetDataByColumn(FirewallDictionaryList.class, "fwDictListDictionaryDatas", "parentItemName",
216                                 () -> controller.getFWDictListDictionaryEntityDataByName(response));
217         }
218
219         @Test
220         public void testGetFWDictionaryListEntityData() {
221                 test_WithGetData(FirewallDictionaryList.class, "fwDictListDictionaryDatas",
222                                 () -> controller.getFWDictionaryListEntityData(response));
223         }
224
225         @Test
226         public void testGetTagPickerNameEntityDataByName() {
227                 test_WithGetDataByColumn(FWTagPicker.class, "fwTagPickerDictionaryDatas", "tagPickerName",
228                                 () -> controller.getTagPickerNameEntityDataByName(response));
229         }
230
231         @Test
232         public void testGetTagPickerDictionaryEntityData() {
233                 test_WithGetData(FWTagPicker.class, "fwTagPickerDictionaryDatas",
234                                 () -> controller.getTagPickerDictionaryEntityData(response));
235         }
236
237         @Test
238         public void testGetTagNameEntityDataByName() {
239                 test_WithGetDataByColumn(FWTag.class, "fwTagDictionaryDatas", "fwTagName",
240                                 () -> controller.getTagNameEntityDataByName(response));
241         }
242
243         @Test
244         public void testGetTagDictionaryEntityData() {
245                 test_WithGetData(FWTag.class, "fwTagDictionaryDatas", () -> controller.getTagDictionaryEntityData(response));
246         }
247
248         @Test
249         public void testSavePrefixListDictionary() {
250                 jsonString = "{\"userid\":\"demo\",\"prefixListDictionaryData\":{\"description\":\"test\",\"prefixListName\":\"Test\"}}";
251                 testSave(PrefixList.class, "prefixListDictionaryDatas", "prefixListName",
252                                 () -> controller.savePrefixListDictionary(request, response));
253         }
254
255         @Test
256         public void testUpdatePrefixListDictionary() {
257                 jsonString = "{\"userid\":\"demo\",\"prefixListDictionaryData\":{\"id\":1,\"description\":\"test\",\"prefixListName\":\"Test\"}}";
258                 testUpdate(PrefixList.class, "prefixListDictionaryDatas", "prefixListName",
259                                 () -> controller.savePrefixListDictionary(request, response));
260         }
261
262         @Test
263         public void testRemovePrefixListDictionary() {
264                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"prefixListName\":\"Test\"}}";
265                 testRemove(PrefixList.class, "prefixListDictionaryDatas",
266                                 () -> controller.removePrefixListDictionary(request, response));
267         }
268
269         @Test
270         public void testValidatePrefixListDictionary() {
271                 jsonString = "{\"userid\":\"demo\",\"prefixListDictionaryData\":{\"id\":1,\"description\":\"test\",\"prefixListName\":\"Test\",\"prefixListValue\":\"10.10.10\"}}";
272                 testValidate(PrefixList.class, "result", () -> controller.validatePrefixListDictionary(request, response));
273         }
274
275         @Test
276         public void testSavePortListDictionary() {
277                 jsonString = "{\"userid\":\"demo\",\"portListDictionaryData\":{\"description\":\"test\",\"portName\":\"Test\"}}";
278                 testSave(PortList.class, "portListDictionaryDatas", "portName",
279                                 () -> controller.savePortListDictionary(request, response));
280         }
281
282         @Test
283         public void testUpdatePortListDictionary() {
284                 jsonString = "{\"userid\":\"demo\",\"portListDictionaryData\":{\"id\":1,\"description\":\"test\",\"portName\":\"Test\"}}";
285                 testUpdate(PortList.class, "portListDictionaryDatas", "portName",
286                                 () -> controller.savePortListDictionary(request, response));
287         }
288
289         @Test
290         public void testRemovePortListDictionary() {
291                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"portName\":\"Test\"}}";
292                 testRemove(PortList.class, "portListDictionaryDatas",
293                                 () -> controller.removePortListDictionary(request, response));
294         }
295
296         @Test
297         public void testSaveProtocolListDictionary() {
298                 jsonString = "{\"userid\":\"demo\",\"protocolListDictionaryData\":{\"description\":\"test\",\"protocolName\":\"Test\"}}";
299                 testSave(ProtocolList.class, "protocolListDictionaryDatas", "protocolName",
300                                 () -> controller.saveProtocolListDictionary(request, response));
301         }
302
303         @Test
304         public void testUpdateProtocolListDictionary() {
305                 jsonString = "{\"userid\":\"demo\",\"protocolListDictionaryData\":{\"id\":1,\"description\":\"test\",\"protocolName\":\"Test\"}}";
306                 testUpdate(ProtocolList.class, "protocolListDictionaryDatas", "protocolName",
307                                 () -> controller.saveProtocolListDictionary(request, response));
308         }
309
310         @Test
311         public void testRemoveProtocolListDictionary() {
312                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"protocolName\":\"Test\"}}";
313                 testRemove(ProtocolList.class, "protocolListDictionaryDatas",
314                                 () -> controller.removeProtocolListDictionary(request, response));
315         }
316
317         @Test
318         public void testSaveAddressGroupDictionary() {
319                 jsonString = "{\"addressGroupDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:409\",\"id\":\"choice1\",\"option\":\"Test\"}],\"description\":\"test\",\"groupName\":\"Test\"},\"userid\":\"demo\"}";
320                 testSave(AddressGroup.class, "addressGroupDictionaryDatas", "name", "Group_Test",
321                                 () -> controller.saveAddressGroupDictionary(request, response));
322         }
323
324         @Test
325         public void testUpdateAddressGroupDictionary() {
326                 jsonString = "{\"addressGroupDictionaryData\":{\"id\":1, \"attributes\":[{\"$$hashKey\":\"object:409\",\"id\":\"choice1\",\"option\":\"Test\"}],\"description\":\"test\",\"groupName\":\"Test\"},\"userid\":\"demo\"}";
327                 testUpdate(AddressGroup.class, "addressGroupDictionaryDatas", "name", "Group_Test",
328                                 () -> controller.saveAddressGroupDictionary(request, response));
329         }
330
331         @Test
332         public void testRemoveAddressGroupDictionary() {
333                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
334                 testRemove(AddressGroup.class, "addressGroupDictionaryDatas",
335                                 () -> controller.removeAddressGroupDictionary(request, response));
336         }
337
338         @Test
339         public void testSaveActionListDictionary() {
340                 jsonString = "{\"userid\":\"demo\",\"actionListDictionaryData\":{\"description\":\"test\",\"actionName\":\"Test\"}}";
341                 testSave(ActionList.class, "actionListDictionaryDatas", "actionName",
342                                 () -> controller.saveActionListDictionary(request, response));
343         }
344
345         @Test
346         public void testUpdateActionListDictionary() {
347                 jsonString = "{\"userid\":\"demo\",\"actionListDictionaryData\":{\"id\":1,\"description\":\"test\",\"actionName\":\"Test\"}}";
348                 testUpdate(ActionList.class, "actionListDictionaryDatas", "actionName",
349                                 () -> controller.saveActionListDictionary(request, response));
350         }
351
352         @Test
353         public void testRemoveActionListDictionary() {
354                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"actionName\":\"Test\"}}";
355                 testRemove(ActionList.class, "actionListDictionaryDatas",
356                                 () -> controller.removeActionListDictionary(request, response));
357         }
358
359         @Test
360         public void testSaveServiceGroupDictionary() {
361                 jsonString = "{\"serviceGroupDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:657\",\"id\":\"choice1\",\"option\":\"Test\"}],\"groupName\":\"Test\"},\"userid\":\"demo\"}";
362                 testSave(GroupServiceList.class, "serviceGroupDictionaryDatas", "name", "Group_Test",
363                                 () -> controller.saveServiceGroupDictionary(request, response));
364         }
365
366         @Test
367         public void testUpdateServiceGroupDictionary() {
368                 jsonString = "{\"serviceGroupDictionaryData\":{\"id\":1, \"attributes\":[{\"$$hashKey\":\"object:657\",\"id\":\"choice1\",\"option\":\"Test\"}],\"groupName\":\"Test\"},\"userid\":\"demo\"}";
369                 testUpdate(GroupServiceList.class, "serviceGroupDictionaryDatas", "name", "Group_Test",
370                                 () -> controller.saveServiceGroupDictionary(request, response));
371         }
372
373         @Test
374         public void testRemoveServiceGroupDictionary() {
375                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
376                 testRemove(GroupServiceList.class, "serviceGroupDictionaryDatas",
377                                 () -> controller.removeServiceGroupDictionary(request, response));
378         }
379
380         @Test
381         public void testSaveSecurityZoneDictionary() {
382                 jsonString = "{\"userid\":\"demo\",\"securityZoneDictionaryData\":{\"description\":\"test\",\"zoneName\":\"Test\"}}";
383                 testSave(SecurityZone.class, "securityZoneDictionaryDatas", "zoneName",
384                                 () -> controller.saveSecurityZoneDictionary(request, response));
385         }
386
387         @Test
388         public void testUpdateSecurityZoneDictionary() {
389                 jsonString = "{\"userid\":\"demo\",\"securityZoneDictionaryData\":{\"id\":1,\"description\":\"test\",\"zoneName\":\"Test\"}}";
390                 testUpdate(SecurityZone.class, "securityZoneDictionaryDatas", "zoneName",
391                                 () -> controller.saveSecurityZoneDictionary(request, response));
392         }
393
394         @Test
395         public void testRemoveSecurityZoneDictionary() {
396                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"zoneName\":\"Test\"}}";
397                 testRemove(SecurityZone.class, "securityZoneDictionaryDatas",
398                                 () -> controller.removeSecurityZoneDictionary(request, response));
399         }
400
401         @Test
402         public void testSaveServiceListDictionary() {
403                 jsonString = "{\"serviceListDictionaryData\":{\"appProtocols\":[{\"$$hashKey\":\"object:560\",\"id\":\"choice1\",\"option\":\"Test\"}],\"serviceDescription\":\"test\",\"serviceName\":\"Test\",\"servicePorts\":\"1010\",\"transportProtocols\":[{\"$$hashKey\":\"object:555\",\"id\":\"choice1\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
404                 testSave(ServiceList.class, "serviceListDictionaryDatas", "serviceName",
405                                 () -> controller.saveServiceListDictionary(request, response));
406         }
407
408         @Test
409         public void testUpdateServiceListDictionary() {
410                 jsonString = "{\"serviceListDictionaryData\":{\"appProtocols\":[{\"$$hashKey\":\"object:560\",\"id\":\"choice1\",\"option\":\"Test\"}],\"serviceDescription\":\"test\",\"id\":1,\"serviceName\":\"Test\",\"servicePorts\":\"1010\",\"transportProtocols\":[{\"$$hashKey\":\"object:555\",\"id\":\"choice1\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
411                 testUpdate(ServiceList.class, "serviceListDictionaryDatas", "serviceName",
412                                 () -> controller.saveServiceListDictionary(request, response));
413         }
414
415         @Test
416         public void testRemoveServiceListDictionary() {
417                 jsonString = "{\"data\":{\"appProtocols\":[{\"$$hashKey\":\"object:560\",\"id\":\"choice1\",\"option\":\"Test\"}],\"serviceDescription\":\"test\",\"id\":1,\"serviceName\":\"Test\",\"servicePorts\":\"1010\",\"transportProtocols\":[{\"$$hashKey\":\"object:555\",\"id\":\"choice1\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
418                 testRemove(ServiceList.class, "serviceListDictionaryDatas",
419                                 () -> controller.removeServiceListDictionary(request, response));
420         }
421
422         @Test
423         public void testSaveZoneDictionary() {
424                 jsonString = "{\"userid\":\"demo\",\"zoneDictionaryData\":{\"zoneValue\":\"test\",\"zoneName\":\"Test\"}}";
425                 testSave(Zone.class, "zoneDictionaryDatas", "zoneName", () -> controller.saveZoneDictionary(request, response));
426         }
427
428         @Test
429         public void testUpdateZoneDictionary() {
430                 jsonString = "{\"userid\":\"demo\",\"zoneDictionaryData\":{\"id\":1,\"zoneValue\":\"test\",\"zoneName\":\"Test\"}}";
431                 testUpdate(Zone.class, "zoneDictionaryDatas", "zoneName",
432                                 () -> controller.saveZoneDictionary(request, response));
433         }
434
435         @Test
436         public void testRemoveZoneDictionary() {
437                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"zoneValue\":\"test\",\"zoneName\":\"Test\"}}";
438                 testRemove(Zone.class, "zoneDictionaryDatas", () -> controller.removeZoneDictionary(request, response));
439         }
440
441         @Test
442         public void testSaveTermListDictionary() {
443                 jsonString = "{\"termListDictionaryData\":{\"actionListDatas\":[{\"$$hashKey\":\"object:1220\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"destinationListDatas\":[{\"$$hashKey\":\"object:1220\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"destinationServiceDatas\":[{\"$$hashKey\":\"object:1230\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"fromZoneDatas\":[{\"$$hashKey\":\"object:1245\",\"id\":\"choice1\",\"option\":\"Test\"}],\"sourceListDatas\":[{\"$$hashKey\":\"object:1215\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"sourceServiceDatas\":[{\"$$hashKey\":\"object:1225\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"termDescription\":\"test\",\"termName\":\"Test\",\"toZoneDatas\":[{\"$$hashKey\":\"object:1240\",\"id\":\"choice1\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
444                 testSave(TermList.class, "termListDictionaryDatas", "termName",
445                                 () -> controller.saveTermListDictionary(request, response));
446         }
447
448         @Test
449         public void testUpdateTermListDictionary() {
450                 jsonString = "{\"termListDictionaryData\":{\"id\":1,\"actionListDatas\":[{\"$$hashKey\":\"object:1220\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"destinationListDatas\":[{\"$$hashKey\":\"object:1220\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"destinationServiceDatas\":[{\"$$hashKey\":\"object:1230\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"fromZoneDatas\":[{\"$$hashKey\":\"object:1245\",\"id\":\"choice1\",\"option\":\"Test\"}],\"sourceListDatas\":[{\"$$hashKey\":\"object:1215\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"sourceServiceDatas\":[{\"$$hashKey\":\"object:1225\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"termDescription\":\"test\",\"termName\":\"Test\",\"toZoneDatas\":[{\"$$hashKey\":\"object:1240\",\"id\":\"choice1\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
451                 testUpdate(TermList.class, "termListDictionaryDatas", "termName",
452                                 () -> controller.saveTermListDictionary(request, response));
453         }
454
455         @Test
456         public void testRemoveTermListDictionary() {
457                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"termDescription\":\"test\",\"termName\":\"Test\"}}";
458                 testRemove(TermList.class, "termListDictionaryDatas",
459                                 () -> controller.removeTermListDictionary(request, response));
460         }
461
462         @Test
463         public void testSaveFWDictionaryList() {
464                 jsonString = "{\"fwDictListDictionaryData\":{\"alAttributes\":[{\"$$hashKey\":\"object:1379\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"attributes\":[{\"$$hashKey\":\"object:1374\",\"id\":\"choice1\",\"option\":\"Test\"}],\"description\":\"test\",\"parentItemName\":\"Test\"},\"userid\":\"demo\"}";
465                 testSave(FirewallDictionaryList.class, "fwDictListDictionaryDatas", "parentItemName",
466                                 () -> controller.saveFWDictionaryList(request, response));
467         }
468
469         @Test
470         public void testUpdateFWDictionaryList() {
471                 jsonString = "{\"fwDictListDictionaryData\":{\"id\":1,\"alAttributes\":[{\"$$hashKey\":\"object:1379\",\"id\":\"choice1\",\"option\":\"Group_Test\"}],\"attributes\":[{\"$$hashKey\":\"object:1374\",\"id\":\"choice1\",\"option\":\"Test\"}],\"description\":\"test\",\"parentItemName\":\"Test\"},\"userid\":\"demo\"}";
472                 testUpdate(FirewallDictionaryList.class, "fwDictListDictionaryDatas", "parentItemName",
473                                 () -> controller.saveFWDictionaryList(request, response));
474         }
475
476         @Test
477         public void testRemoveFWDictionaryList() {
478                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"parentItemName\":\"Test\"}}";
479                 testRemove(FirewallDictionaryList.class, "fwDictListDictionaryDatas",
480                                 () -> controller.removeFWDictionaryList(request, response));
481         }
482
483         @Test
484         public void testSaveFirewallTagPickerDictionary() {
485                 jsonString = "{\"fwTagPickerDictionaryData\":{\"description\":\"test\",\"networkRole\":\"test\",\"tagPickerName\":\"Test\",\"tags\":[{\"$$hashKey\":\"object:1855\",\"id\":\"choice1\",\"number\":\"test\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
486                 testSave(FWTagPicker.class, "fwTagPickerDictionaryDatas", "tagPickerName",
487                                 () -> controller.saveFirewallTagPickerDictionary(request, response));
488         }
489
490         @Test
491         public void testUpdateFirewallTagPickerDictionary() {
492                 jsonString = "{\"fwTagPickerDictionaryData\":{\"id\":1,\"description\":\"test\",\"networkRole\":\"test\",\"tagPickerName\":\"Test\",\"tags\":[{\"$$hashKey\":\"object:1855\",\"id\":\"choice1\",\"number\":\"test\",\"option\":\"Test\"}]},\"userid\":\"demo\"}";
493                 testUpdate(FWTagPicker.class, "fwTagPickerDictionaryDatas", "tagPickerName",
494                                 () -> controller.saveFirewallTagPickerDictionary(request, response));
495         }
496
497         @Test
498         public void testRemoveFirewallTagPickerDictionary() {
499                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"tagPickerName\":\"Test\"}}";
500                 testRemove(FWTagPicker.class, "fwTagPickerDictionaryDatas",
501                                 () -> controller.removeFirewallTagPickerDictionary(request, response));
502         }
503
504         @Test
505         public void testSaveFirewallTagDictionary() {
506                 jsonString = "{\"fwTagDictionaryData\":{\"description\":\"test\",\"fwTagName\":\"Test\",\"tags\":[{\"$$hashKey\":\"object:1690\",\"id\":\"choice1\",\"tags\":\"test\"}]},\"userid\":\"demo\"}";
507                 testSave(FWTag.class, "fwTagDictionaryDatas", "fwTagName",
508                                 () -> controller.saveFirewallTagDictionary(request, response));
509         }
510
511         @Test
512         public void testUpdateFirewallTagDictionary() {
513                 jsonString = "{\"fwTagDictionaryData\":{\"id\":1,\"description\":\"test\",\"fwTagName\":\"Test\",\"tags\":[{\"$$hashKey\":\"object:1690\",\"id\":\"choice1\",\"tags\":\"test\"}]},\"userid\":\"demo\"}";
514                 testUpdate(FWTag.class, "fwTagDictionaryDatas", "fwTagName",
515                                 () -> controller.saveFirewallTagDictionary(request, response));
516         }
517
518         @Test
519         public void testRemoveFirewallTagDictionary() {
520                 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"fwTagName\":\"Test\"}}";
521                 testRemove(FWTag.class, "fwTagDictionaryDatas",
522                                 () -> controller.removeFirewallTagDictionary(request, response));
523         }
524
525         /**
526          * Tests a "get" function that uses commonClassDao.getDataByColumn().
527          * 
528          * @param clazz
529          * @param contentData
530          * @param contentName
531          * @param func
532          */
533         private void test_WithGetDataByColumn(Class<?> clazz, String contentData, String contentName, VoidFunc func) {
534                 when(commonClassDao.getDataByColumn(clazz, contentName)).thenReturn(data);
535                 try {
536                         func.apply();
537                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
538                         verify(commonClassDao).getDataByColumn(clazz, contentName);
539                 } catch (Exception e) {
540                         fail("get " + clazz.getName() + e);
541                 }
542         }
543
544         /**
545          * Tests a "get" function that uses commonClassDao.getData().
546          * 
547          * @param clazz
548          * @param contentData
549          * @param contentName
550          * @param func
551          */
552         private void test_WithGetData(Class<?> clazz, String contentData, VoidFunc func) {
553                 when(commonClassDao.getData(clazz)).thenReturn(new ArrayList<>());
554                 try {
555                         func.apply();
556                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
557                         verify(commonClassDao).getData(clazz);
558                 } catch (Exception e) {
559                         fail("get " + clazz.getName() + e);
560                 }
561         }
562
563         /**
564          * Tests a function that uses commonClassDao.save().
565          * 
566          * @param clazz
567          * @param contentData
568          * @param contentName
569          * @param func
570          */
571         private void testSave(Class<?> clazz, String contentData, String contentName, VoidFunc func) {
572                 testSave(clazz, contentData, contentName, "Test", func);
573         }
574
575         /**
576          * Tests a function that uses commonClassDao.save().
577          * 
578          * @param clazz
579          * @param contentData
580          * @param contentName
581          * @param testName
582          * @param func
583          */
584         private void testSave(Class<?> clazz, String contentData, String contentName, String testName, VoidFunc func) {
585                 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
586                         when(request.getReader()).thenReturn(br);
587                         func.apply();
588                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
589                         verify(commonClassDao).checkDuplicateEntry(testName, contentName, clazz);
590                         verify(commonClassDao).save(any());
591                         verify(commonClassDao, never()).update(any());
592                         verify(commonClassDao).getData(clazz);
593
594                 } catch (IOException e) {
595                         fail("save " + clazz.getName() + e);
596                 }
597         }
598
599         /**
600          * Tests a function that uses commonClassDao.update().
601          * 
602          * @param clazz
603          * @param contentData
604          * @param contentName
605          * @param func
606          */
607         private void testUpdate(Class<?> clazz, String contentData, String contentName, VoidFunc func) {
608                 testUpdate(clazz, contentData, contentName, "Test", func);
609         }
610
611         /**
612          * Tests a function that uses commonClassDao.update().
613          * 
614          * @param clazz
615          * @param contentData
616          * @param contentName
617          * @param testName
618          * @param func
619          */
620         private void testUpdate(Class<?> clazz, String contentData, String contentName, String testName, VoidFunc func) {
621                 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
622                         when(request.getReader()).thenReturn(br);
623                         func.apply();
624                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
625                         verify(commonClassDao).checkDuplicateEntry(testName, contentName, clazz);
626                         verify(commonClassDao, never()).save(any());
627                         verify(commonClassDao).update(any());
628                         verify(commonClassDao).getData(clazz);
629
630                 } catch (IOException e) {
631                         fail("update " + clazz.getName() + e);
632                 }
633         }
634
635         /**
636          * Tests a function that uses commonClassDao.delete() and
637          * commonClassDao.getData().
638          * 
639          * @param clazz
640          * @param contentData
641          * @param func
642          */
643         private void testRemove(Class<?> clazz, String contentData, VoidFunc func) {
644                 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
645                         when(request.getReader()).thenReturn(br);
646                         func.apply();
647                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
648                         verify(commonClassDao).delete(any());
649                         verify(commonClassDao).getData(clazz);
650
651                 } catch (IOException e) {
652                         fail("remove " + clazz.getName() + e);
653                 }
654         }
655
656         /**
657          * Tests a "validate" function.
658          * 
659          * @param clazz
660          * @param contentData
661          * @param func
662          */
663         private void testValidate(Class<?> clazz, String contentData, VoidFunc func) {
664                 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
665                         when(request.getReader()).thenReturn(br);
666                         func.apply();
667                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
668
669                 } catch (IOException e) {
670                         fail("save " + clazz.getName() + e);
671                 }
672         }
673
674         @FunctionalInterface
675         private static interface VoidFunc {
676                 public void apply() throws IOException;
677         }
678 }