From 7dcd8b26f799452ae31bf0a6ea37ec3eb31f4656 Mon Sep 17 00:00:00 2001 From: khadgarmage Date: Thu, 19 Dec 2019 21:54:37 +0800 Subject: [PATCH] unit test for common utils (#1520) * httputil osutil encrytiontutil ut --- dolphinscheduler-common/pom.xml | 24 +++++++++++ .../common/utils/FileUtils.java | 6 +-- .../common/utils/EncryptionUtilsTest.java | 35 ++++++++++++++++ .../common/utils/FileUtilsTest.java | 41 ++++++++++++++++++- .../common/utils/HttpUtilsTest.java | 22 +++++----- .../server/worker/runner/FetchTaskThread.java | 4 +- pom.xml | 34 ++++++++++++++- 7 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/EncryptionUtilsTest.java diff --git a/dolphinscheduler-common/pom.xml b/dolphinscheduler-common/pom.xml index 49c57d506b..1481190f9e 100644 --- a/dolphinscheduler-common/pom.xml +++ b/dolphinscheduler-common/pom.xml @@ -47,6 +47,30 @@ junit test + + org.mockito + mockito-core + jar + test + + + + org.powermock + powermock-module-junit4 + test + + + + org.powermock + powermock-api-mockito2 + test + + + org.mockito + mockito-core + + + commons-configuration diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java index e7da97d8d8..5f23a1eb17 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java @@ -110,10 +110,9 @@ public class FileUtils { * create directory and user * @param execLocalPath execute local path * @param userName user name - * @param logger logger * @throws IOException errors */ - public static void createWorkDirAndUserIfAbsent(String execLocalPath, String userName, Logger logger) throws IOException{ + public static void createWorkDirAndUserIfAbsent(String execLocalPath, String userName) throws IOException{ //if work dir exists, first delete File execLocalPathFile = new File(execLocalPath); @@ -123,13 +122,14 @@ public class FileUtils { //create work dir org.apache.commons.io.FileUtils.forceMkdir(execLocalPathFile); + logger.info("create dir success {}" , execLocalPath); //if not exists this user,then create if (!OSUtils.getUserList().contains(userName)){ OSUtils.createUser(userName); } - + logger.info("create user name success {}", userName); } diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/EncryptionUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/EncryptionUtilsTest.java new file mode 100644 index 0000000000..b72e7b7d64 --- /dev/null +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/EncryptionUtilsTest.java @@ -0,0 +1,35 @@ +/* + * 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.common.utils; + +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +/** + * encryption utils + */ +public class EncryptionUtilsTest { + + + @Test + public void testGetMd5() { + Assert.assertEquals(EncryptionUtils.getMd5(null), EncryptionUtils.getMd5("")); + } + +} diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/FileUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/FileUtilsTest.java index 93faf465ad..89458f6f1c 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/FileUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/FileUtilsTest.java @@ -18,11 +18,50 @@ package org.apache.dolphinscheduler.common.utils; import org.junit.Assert; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import static org.apache.dolphinscheduler.common.Constants.YYYYMMDDHHMMSS; +@RunWith(PowerMockRunner.class) +@PrepareForTest(DateUtils.class) public class FileUtilsTest { @Test public void suffix() { Assert.assertEquals(FileUtils.suffix("ninfor.java"),"java"); } -} \ No newline at end of file + + @Test + public void testGetDownloadFilename() { + PowerMockito.mockStatic(DateUtils.class); + PowerMockito.when(DateUtils.getCurrentTime(YYYYMMDDHHMMSS)).thenReturn("20190101101059"); + Assert.assertEquals(FileUtils.getDownloadFilename("test"), + "/tmp/dolphinscheduler/download/20190101101059/test"); + } + + @Test + public void testGetUploadFilename() { + Assert.assertEquals(FileUtils.getUploadFilename("aaa","bbb"), + "/tmp/dolphinscheduler/aaa/resources/bbb"); + } + + @Test + public void testGetProcessExecDir() { + String dir = FileUtils.getProcessExecDir(1,2,3, 4); + Assert.assertEquals(dir, "/tmp/dolphinscheduler/exec/process/1/2/3/4"); + dir = FileUtils.getProcessExecDir(1,2,3); + Assert.assertEquals(dir, "/tmp/dolphinscheduler/exec/process/1/2/3"); + } + + @Test + public void testCreateWorkDirAndUserIfAbsent() { + try { + FileUtils.createWorkDirAndUserIfAbsent("/tmp/createWorkDirAndUserIfAbsent", "test123"); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.assertTrue(false); + } + } +} diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java index 9a6e366f21..20994ac99f 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; @@ -27,21 +28,20 @@ import org.slf4j.LoggerFactory; * HttpClient utils test */ public class HttpUtilsTest { - - - public static final Logger logger = LoggerFactory.getLogger(HttpUtilsTest.class); - - @Ignore - @Test - public void getTest(){ - String result = HttpUtils.get("http://192.168.xx.xx:8088/ws/v1/cluster/info"); - logger.info(result); + public static final Logger logger = LoggerFactory.getLogger(HttpUtilsTest.class); + @Test + public void testGetTest(){ + //success + String result = HttpUtils.get("https://github.com/manifest.json"); + Assert.assertNotNull(result); JSONObject jsonObject = JSON.parseObject(result); - String string = jsonObject.getJSONObject("clusterInfo").getString("haState"); - logger.info(string); + Assert.assertEquals(jsonObject.getString("name"), "GitHub"); + + result = HttpUtils.get("https://123.333.111.33/ccc"); + Assert.assertNull(result); } } diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/runner/FetchTaskThread.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/runner/FetchTaskThread.java index 68f27d76d8..7429050605 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/runner/FetchTaskThread.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/runner/FetchTaskThread.java @@ -223,7 +223,7 @@ public class FetchTaskThread implements Runnable{ // check and create users FileUtils.createWorkDirAndUserIfAbsent(execLocalPath, - tenant.getTenantCode(), logger); + tenant.getTenantCode()); logger.info("task : {} ready to submit to task scheduler thread",taskInstId); // submit task @@ -334,4 +334,4 @@ public class FetchTaskThread implements Runnable{ private int getTaskInstanceId(String taskQueueStr){ return Integer.parseInt(taskQueueStr.split(Constants.UNDERLINE)[3]); } -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index 4b10ba9c42..0bf915b8c3 100644 --- a/pom.xml +++ b/pom.xml @@ -112,6 +112,8 @@ 1.0 false 2.7 + 2.21.0 + 2.0.2 @@ -302,6 +304,36 @@ junit ${junit.version} + + org.mockito + mockito-core + ${mockito.version} + jar + test + + + + org.powermock + powermock-module-junit4 + ${powermock.version} + jar + test + + + + org.powermock + powermock-api-mockito2 + ${powermock.version} + jar + test + + + org.mockito + mockito-core + + + + mysql mysql-connector-java @@ -613,12 +645,10 @@ **/common/utils/*.java - **/common/graph/*.java **/common/queue/*.java **/api/utils/CheckUtilsTest.java **/api/utils/FileUtilsTest.java - **/alert/utils/ExcelUtilsTest.java **/alert/utils/FuncUtilsTest.java **/alert/utils/JSONUtilsTest.java