diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java index e682fde2e7..186a983e05 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java @@ -16,9 +16,16 @@ */ package org.apache.dolphinscheduler.alert.utils; +import org.apache.commons.lang.StringUtils; + public class FuncUtils { static public String mkString(Iterable list, String split) { + + if (null == list || StringUtils.isEmpty(split)){ + return null; + } + StringBuilder sb = new StringBuilder(); boolean first = true; for (String item : list) { diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/FuncUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/FuncUtilsTest.java new file mode 100644 index 0000000000..e2b25d961d --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/FuncUtilsTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.alert.utils; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class FuncUtilsTest { + + private static final Logger logger = LoggerFactory.getLogger(FuncUtilsTest.class); + + /** + * Test mkString + */ + @Test + public void testMKString() { + + //Define users list + Iterable users = Arrays.asList("user1", "user2", "user3"); + //Define split + String split = "|"; + + //Invoke mkString with correctParams + String result = FuncUtils.mkString(users, split); + logger.info(result); + + //Expected result string + assertEquals(result, "user1|user2|user3"); + + //Null list expected return null + result = FuncUtils.mkString(null, split); + assertNull(result); + + //Null split expected return null + result = FuncUtils.mkString(users, null); + assertNull(result); + + } +} \ No newline at end of file diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java new file mode 100644 index 0000000000..cb63a22d79 --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.alert.utils; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class JSONUtilsTest { + + private static final Logger logger = LoggerFactory.getLogger(JSONUtilsTest.class); + + public List> list = new ArrayList<>(); + + public String expected = null; + + @Before + public void setUp() throws Exception { + + //Define expected json string + expected = "[{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"port\":\"3306\",\"no index of number\":\"80\",\"database client connections\":\"190\"}]"; + + //Initial map + LinkedHashMap map = new LinkedHashMap<>(); + map.put("mysql service name","mysql200"); + map.put("mysql address","192.168.xx.xx"); + map.put("port","3306"); + map.put("no index of number","80"); + map.put("database client connections","190"); + + //Add map into list + list.add(map); + } + + + /** + * Test toJsonString + */ + @Test + public void testToJsonString() { + + //Invoke toJsonString + String result = JSONUtils.toJsonString(list); + logger.info(result); + + //Equal result with expected string + assertEquals(result,expected); + + //If param is null, then return null string + result = JSONUtils.toJsonString(null); + logger.info(result); + + assertEquals(result,"null"); + + } + + /** + * Test toList + */ + @Test + public void testToList() { + + //Invoke toList + List result = JSONUtils.toList(expected ,LinkedHashMap.class); + //Equal list size=1 + assertEquals(result.size(),1); + + //Transform entity to LinkedHashMap + LinkedHashMap entity = result.get(0); + + //Equal expected values + assertEquals(entity.get("mysql service name"),"mysql200"); + assertEquals(entity.get("mysql address"),"192.168.xx.xx"); + assertEquals(entity.get("port"),"3306"); + assertEquals(entity.get("no index of number"),"80"); + assertEquals(entity.get("database client connections"),"190"); + + //If param is null, then return null + result = JSONUtils.toList(null ,LinkedHashMap.class); + assertNull(result); + + //If param is incorrect, then return null and log error message + result = JSONUtils.toList("}{" ,LinkedHashMap.class); + assertNull(result); + + } + +} diff --git a/pom.xml b/pom.xml index a56fe6e892..4b10ba9c42 100644 --- a/pom.xml +++ b/pom.xml @@ -613,14 +613,15 @@ **/common/utils/*.java - **/api/utils/CheckUtilsTest.java - **/api/utils/FileUtilsTest.java **/common/graph/*.java **/common/queue/*.java **/api/utils/CheckUtilsTest.java **/api/utils/FileUtilsTest.java - **/alert/utils/ExcelUtilsTest.java + + **/alert/utils/ExcelUtilsTest.java + **/alert/utils/FuncUtilsTest.java + **/alert/utils/JSONUtilsTest.java