You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by li...@apache.org on 2019/12/31 15:33:37 UTC

[incubator-dolphinscheduler] branch dev updated: Add ParamUtilsTest which is the UT of ParamUtils (#1634)

This is an automated email from the ASF dual-hosted git repository.

liuxiaochun pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new b471119  Add ParamUtilsTest which is the UT of ParamUtils (#1634)
b471119 is described below

commit b4711194438ff34b3aa4d6e3430e0ec920dace06
Author: zhukai <bo...@qq.com>
AuthorDate: Tue Dec 31 23:33:27 2019 +0800

    Add ParamUtilsTest which is the UT of ParamUtils (#1634)
    
    1. Add ParamUtilsTest.
    2. Add a null check in the method convert.
    3. Add the UT path to the root pom.
---
 .../dolphinscheduler/server/utils/ParamUtils.java  |   4 +
 .../server/utils/ParamUtilsTest.java               | 129 +++++++++++++++++++++
 pom.xml                                            |   1 +
 3 files changed, 134 insertions(+)

diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ParamUtils.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ParamUtils.java
index 3965b0e..1d7a80d 100644
--- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ParamUtils.java
+++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ParamUtils.java
@@ -93,6 +93,10 @@ public class ParamUtils {
      * @return Map of converted
      */
     public static Map<String,String> convert(Map<String,Property> paramsMap){
+        if(paramsMap == null){
+            return null;
+        }
+
         Map<String,String> map = new HashMap<>();
         Iterator<Map.Entry<String, Property>> iter = paramsMap.entrySet().iterator();
         while (iter.hasNext()){
diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ParamUtilsTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ParamUtilsTest.java
new file mode 100644
index 0000000..b91bff7
--- /dev/null
+++ b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ParamUtilsTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.server.utils;
+
+import com.alibaba.fastjson.JSON;
+import org.apache.dolphinscheduler.common.enums.CommandType;
+import org.apache.dolphinscheduler.common.enums.DataType;
+import org.apache.dolphinscheduler.common.enums.Direct;
+import org.apache.dolphinscheduler.common.process.Property;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Test ParamUtils
+ */
+public class ParamUtilsTest {
+
+    private static final Logger logger = LoggerFactory.getLogger(ParamUtilsTest.class);
+
+    //Define global variables
+    public Map<String, Property> globalParams = new HashMap<>();
+
+    public Map<String, String> globalParamsMap = new HashMap<>();
+
+    public Map<String, Property> localParams = new HashMap<>();
+
+    /**
+     * Init params
+     * @throws Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+
+        Property property = new Property();
+        property.setProp("global_param");
+        property.setDirect(Direct.IN);
+        property.setType(DataType.VARCHAR);
+        property.setValue("${system.biz.date}");
+        globalParams.put("global_param", property);
+
+        globalParamsMap.put("global_param", "${system.biz.date}");
+
+        Property localProperty = new Property();
+        localProperty.setProp("local_param");
+        localProperty.setDirect(Direct.IN);
+        localProperty.setType(DataType.VARCHAR);
+        localProperty.setValue("${global_param}");
+        localParams.put("local_param", localProperty);
+    }
+
+    /**
+     * Test convert
+     */
+    @Test
+    public void testConvert() {
+
+        //The expected value
+        String expected = "{\"global_param\":{\"direct\":\"IN\",\"prop\":\"global_param\",\"type\":\"VARCHAR\",\"value\":\"20191229\"},\"local_param\":{\"direct\":\"IN\",\"prop\":\"local_param\",\"type\":\"VARCHAR\",\"value\":\"20191229\"}}";
+
+        //The expected value when globalParams is null but localParams is not null
+        String expected1 = "{\"local_param\":{\"direct\":\"IN\",\"prop\":\"local_param\",\"type\":\"VARCHAR\",\"value\":\"20191229\"}}";
+
+        //Invoke convert
+        Map<String, Property> paramsMap = ParamUtils.convert(globalParams, globalParamsMap, localParams, CommandType.START_PROCESS, new Date());
+        String result = JSON.toJSONString(paramsMap);
+        assertEquals(expected, result);
+
+        for (Map.Entry<String, Property> entry : paramsMap.entrySet()) {
+
+            String key = entry.getKey();
+            Property prop = entry.getValue();
+            logger.info(key + " : " + prop.getValue());
+        }
+
+        //Invoke convert with null globalParams
+        Map<String, Property> paramsMap1 = ParamUtils.convert(null, globalParamsMap, localParams, CommandType.START_PROCESS, new Date());
+        String result1 = JSON.toJSONString(paramsMap1);
+        assertEquals(expected1, result1);
+
+        //Null check, invoke convert with null globalParams and null localParams
+        Map<String, Property> paramsMap2 = ParamUtils.convert(null, globalParamsMap, null, CommandType.START_PROCESS, new Date());
+        assertNull(paramsMap2);
+    }
+
+    /**
+     * Test the overload method of convert
+     */
+    @Test
+    public void testConvert1() {
+
+        //The expected value
+        String expected = "{\"global_param\":\"${system.biz.date}\"}";
+
+        //Invoke convert
+        Map<String, String> paramsMap = ParamUtils.convert(globalParams);
+        String result = JSON.toJSONString(paramsMap);
+        assertEquals(expected, result);
+
+        logger.info(result);
+
+        //Null check
+        Map<String, String> paramsMap1 = ParamUtils.convert(null);
+        assertNull(paramsMap1);
+    }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 5e94b16..6892b38 100644
--- a/pom.xml
+++ b/pom.xml
@@ -682,6 +682,7 @@
 						<include>**/alert/utils/PropertyUtilsTest.java</include>
 						<include>**/server/utils/SparkArgsUtilsTest.java</include>
 						<include>**/server/utils/FlinkArgsUtilsTest.java</include>
+						<include>**/server/utils/ParamUtilsTest.java</include>
 						<include>**/dao/mapper/AccessTokenMapperTest.java</include>
 						<include>**/dao/mapper/AlertGroupMapperTest.java</include>
 						<include>**/dao/mapper/AlertMapperTest.java</include>