From b4711194438ff34b3aa4d6e3430e0ec920dace06 Mon Sep 17 00:00:00 2001 From: zhukai Date: Tue, 31 Dec 2019 23:33:27 +0800 Subject: [PATCH] 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. --- .../server/utils/ParamUtils.java | 4 + .../server/utils/ParamUtilsTest.java | 129 ++++++++++++++++++ pom.xml | 1 + 3 files changed, 134 insertions(+) create mode 100644 dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ParamUtilsTest.java 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 3965b0e8f4..1d7a80daf0 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 convert(Map paramsMap){ + if(paramsMap == null){ + return null; + } + Map map = new HashMap<>(); Iterator> 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 0000000000..b91bff7659 --- /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 globalParams = new HashMap<>(); + + public Map globalParamsMap = new HashMap<>(); + + public Map 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 paramsMap = ParamUtils.convert(globalParams, globalParamsMap, localParams, CommandType.START_PROCESS, new Date()); + String result = JSON.toJSONString(paramsMap); + assertEquals(expected, result); + + for (Map.Entry entry : paramsMap.entrySet()) { + + String key = entry.getKey(); + Property prop = entry.getValue(); + logger.info(key + " : " + prop.getValue()); + } + + //Invoke convert with null globalParams + Map 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 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 paramsMap = ParamUtils.convert(globalParams); + String result = JSON.toJSONString(paramsMap); + assertEquals(expected, result); + + logger.info(result); + + //Null check + Map paramsMap1 = ParamUtils.convert(null); + assertNull(paramsMap1); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5e94b16768..6892b387f1 100644 --- a/pom.xml +++ b/pom.xml @@ -682,6 +682,7 @@ **/alert/utils/PropertyUtilsTest.java **/server/utils/SparkArgsUtilsTest.java **/server/utils/FlinkArgsUtilsTest.java + **/server/utils/ParamUtilsTest.java **/dao/mapper/AccessTokenMapperTest.java **/dao/mapper/AlertGroupMapperTest.java **/dao/mapper/AlertMapperTest.java