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