+ testRemove(FWTag.class, "fwTagDictionaryDatas",
+ () -> controller.removeFirewallTagDictionary(request, response));
+ }
+
+ /**
+ * Tests a "get" function that uses commonClassDao.getDataByColumn().
+ *
+ * @param clazz
+ * @param contentData
+ * @param contentName
+ * @param func
+ */
+ private void test_WithGetDataByColumn(Class<?> clazz, String contentData, String contentName, VoidFunc func) {
+ when(commonClassDao.getDataByColumn(clazz, contentName)).thenReturn(data);
+ try {
+ func.apply();
+ assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
+ verify(commonClassDao).getDataByColumn(clazz, contentName);
+ } catch (Exception e) {
+ fail("get " + clazz.getName() + e);
+ }
+ }
+
+ /**
+ * Tests a "get" function that uses commonClassDao.getData().
+ *
+ * @param clazz
+ * @param contentData
+ * @param contentName
+ * @param func
+ */
+ private void test_WithGetData(Class<?> clazz, String contentData, VoidFunc func) {
+ when(commonClassDao.getData(clazz)).thenReturn(new ArrayList<>());
+ try {
+ func.apply();
+ assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
+ verify(commonClassDao).getData(clazz);
+ } catch (Exception e) {
+ fail("get " + clazz.getName() + e);
+ }
+ }
+
+ /**
+ * Tests a function that uses commonClassDao.save().
+ *
+ * @param clazz
+ * @param contentData
+ * @param contentName
+ * @param func
+ */
+ private void testSave(Class<?> clazz, String contentData, String contentName, VoidFunc func) {
+ testSave(clazz, contentData, contentName, "Test", func);
+ }
+
+ /**
+ * Tests a function that uses commonClassDao.save().
+ *
+ * @param clazz
+ * @param contentData
+ * @param contentName
+ * @param testName
+ * @param func
+ */
+ private void testSave(Class<?> clazz, String contentData, String contentName, String testName, VoidFunc func) {
+ try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
+ when(request.getReader()).thenReturn(br);
+ func.apply();
+ assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
+ verify(commonClassDao).checkDuplicateEntry(testName, contentName, clazz);
+ verify(commonClassDao).save(any());
+ verify(commonClassDao, never()).update(any());
+ verify(commonClassDao).getData(clazz);
+
+ } catch (IOException e) {
+ fail("save " + clazz.getName() + e);
+ }
+ }
+
+ /**
+ * Tests a function that uses commonClassDao.update().
+ *
+ * @param clazz
+ * @param contentData
+ * @param contentName
+ * @param func
+ */
+ private void testUpdate(Class<?> clazz, String contentData, String contentName, VoidFunc func) {
+ testUpdate(clazz, contentData, contentName, "Test", func);
+ }
+
+ /**
+ * Tests a function that uses commonClassDao.update().
+ *
+ * @param clazz
+ * @param contentData
+ * @param contentName
+ * @param testName
+ * @param func
+ */
+ private void testUpdate(Class<?> clazz, String contentData, String contentName, String testName, VoidFunc func) {
+ try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
+ when(request.getReader()).thenReturn(br);
+ func.apply();
+ assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
+ verify(commonClassDao).checkDuplicateEntry(testName, contentName, clazz);
+ verify(commonClassDao, never()).save(any());
+ verify(commonClassDao).update(any());
+ verify(commonClassDao).getData(clazz);
+
+ } catch (IOException e) {
+ fail("update " + clazz.getName() + e);
+ }
+ }
+
+ /**
+ * Tests a function that uses commonClassDao.delete() and
+ * commonClassDao.getData().
+ *
+ * @param clazz
+ * @param contentData
+ * @param func
+ */
+ private void testRemove(Class<?> clazz, String contentData, VoidFunc func) {
+ try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
+ when(request.getReader()).thenReturn(br);
+ func.apply();
+ assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
+ verify(commonClassDao).delete(any());
+ verify(commonClassDao).getData(clazz);
+
+ } catch (IOException e) {
+ fail("remove " + clazz.getName() + e);
+ }
+ }
+
+ /**
+ * Tests a "validate" function.
+ *
+ * @param clazz
+ * @param contentData
+ * @param func
+ */
+ private void testValidate(Class<?> clazz, String contentData, VoidFunc func) {
+ try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
+ when(request.getReader()).thenReturn(br);
+ func.apply();
+ assertTrue(response.getContentAsString() != null && response.getContentAsString().contains(contentData));
+
+ } catch (IOException e) {
+ fail("save " + clazz.getName() + e);
+ }
+ }
+
+ @FunctionalInterface
+ private static interface VoidFunc {
+ public void apply() throws IOException;