From d1e409e9a2b7756bb73659d067dc3e0e7b0bb98f Mon Sep 17 00:00:00 2001 From: Eric Gao Date: Thu, 29 Sep 2022 08:16:23 +0800 Subject: [PATCH] [Improvement][Test] Remove powermock in service and server modules (#12164) * Remove powermock and refactor some related code in dolphinscheduler-service and dolphinscheduler-server modules * Remove redundant comments * Add null check --- dolphinscheduler-server/pom.xml | 53 ++++--- .../server/utils/ProcessUtils.java | 28 ++-- .../server/utils/ProcessUtilsTest.java | 47 +++--- dolphinscheduler-service/pom.xml | 36 +++-- .../service/alert/AlertClientService.java | 17 +-- .../factory/NettyRemotingClientFactory.java | 38 +++++ .../service/log/LogClient.java | 6 +- .../service/process/ProcessServiceImpl.java | 17 +-- .../service/alert/AlertClientServiceTest.java | 36 +++-- .../alert/ProcessAlertManagerTest.java | 4 +- .../service/log/LogClientTest.java | 144 ++++++++++-------- .../service/process/ProcessServiceTest.java | 28 ++-- 12 files changed, 261 insertions(+), 193 deletions(-) create mode 100644 dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/factory/NettyRemotingClientFactory.java diff --git a/dolphinscheduler-server/pom.xml b/dolphinscheduler-server/pom.xml index 40c8d166b7..5a0ee9972e 100644 --- a/dolphinscheduler-server/pom.xml +++ b/dolphinscheduler-server/pom.xml @@ -15,7 +15,6 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - 4.0.0 @@ -25,8 +24,20 @@ dev-SNAPSHOT dolphinscheduler-server - dolphinscheduler-server jar + dolphinscheduler-server + + + + + org.apache.dolphinscheduler + dolphinscheduler-bom + ${project.version} + pom + import + + + @@ -47,7 +58,13 @@ mockito-core test - + + org.mockito + mockito-inline + 3.12.4 + + test + org.apache.hadoop hadoop-common @@ -62,8 +79,8 @@ jdk.tools - servlet-api javax.servlet + servlet-api javax.servlet @@ -127,16 +144,16 @@ jsp-api - jersey-json com.sun.jersey + jersey-json - jersey-server com.sun.jersey + jersey-server - jersey-core com.sun.jersey + jersey-core @@ -154,8 +171,8 @@ slf4j-log4j12 - servlet-api javax.servlet + servlet-api org.codehaus.jackson @@ -179,37 +196,25 @@ hadoop-mapreduce-client-shuffle - jersey-client com.sun.jersey + jersey-client - jersey-core com.sun.jersey + jersey-core - jaxb-api javax.xml.bind + jaxb-api - log4j log4j + log4j - - - - org.apache.dolphinscheduler - dolphinscheduler-bom - ${project.version} - pom - import - - - - diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java index a8361fa4ed..35794cf3d0 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java @@ -17,10 +17,6 @@ package org.apache.dolphinscheduler.server.utils; -import lombok.NonNull; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.SystemUtils; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.utils.CommonUtils; import org.apache.dolphinscheduler.common.utils.FileUtils; @@ -31,10 +27,11 @@ import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; import org.apache.dolphinscheduler.remote.utils.Host; import org.apache.dolphinscheduler.service.log.LogClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import javax.annotation.Nullable; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; + import java.io.File; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -43,6 +40,13 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.annotation.Nullable; + +import lombok.NonNull; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * mainly used to get the start command line of a process. */ @@ -166,7 +170,9 @@ public class ProcessUtils { } } else { String pids = OSUtils.exeCmd(String.format("%s -p %d", Constants.PSTREE, processId)); - mat = WINDOWSATTERN.matcher(pids); + if (null != pids) { + mat = WINDOWSATTERN.matcher(pids); + } } if (null != mat) { @@ -206,10 +212,12 @@ public class ProcessUtils { taskExecutionContext.getTaskInstanceId())); } FileUtils.createWorkDirIfAbsent(taskExecutionContext.getExecutePath()); - cancelApplication(appIds, logger, taskExecutionContext.getTenantCode(), taskExecutionContext.getExecutePath()); + cancelApplication(appIds, logger, taskExecutionContext.getTenantCode(), + taskExecutionContext.getExecutePath()); return appIds; } else { - logger.info("The current appId is empty, don't need to kill the yarn job, taskInstanceId: {}", taskExecutionContext.getTaskInstanceId()); + logger.info("The current appId is empty, don't need to kill the yarn job, taskInstanceId: {}", + taskExecutionContext.getTaskInstanceId()); } } catch (Exception e) { logger.error("Kill yarn job failure, taskInstanceId: {}", taskExecutionContext.getTaskInstanceId(), e); diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java index 232f4dbaf2..7664ed7f05 100644 --- a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java +++ b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java @@ -17,31 +17,28 @@ package org.apache.dolphinscheduler.server.utils; -import static org.powermock.api.mockito.PowerMockito.when; -import org.apache.commons.lang3.SystemUtils; +import static org.mockito.ArgumentMatchers.anyString; + import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.utils.HadoopUtils; import org.apache.dolphinscheduler.common.utils.OSUtils; import org.apache.dolphinscheduler.common.utils.PropertyUtils; +import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; import java.util.ArrayList; import java.util.List; -import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@RunWith(PowerMockRunner.class) -@PrepareForTest({System.class, OSUtils.class, HadoopUtils.class, PropertyUtils.class, SystemUtils.class}) +@RunWith(MockitoJUnitRunner.class) public class ProcessUtilsTest { private static final Logger logger = LoggerFactory.getLogger(ProcessUtils.class); @@ -54,23 +51,22 @@ public class ProcessUtilsTest { @Test public void getPidsStr() throws Exception { int processId = 1; - PowerMockito.mockStatic(OSUtils.class); - Whitebox.setInternalState(SystemUtils.class, "IS_OS_MAC", true); - when(OSUtils.exeCmd(String.format("%s -p %d", Constants.PSTREE, processId))).thenReturn(null); - String pidListMac = ProcessUtils.getPidsStr(processId); - Assert.assertEquals("", pidListMac); + Mockito.mockStatic(OSUtils.class); + Mockito.when(OSUtils.exeCmd(anyString())).thenReturn(null); + String pidList = ProcessUtils.getPidsStr(processId); + Assert.assertEquals("", pidList); } @Test public void testGetKerberosInitCommand() { - PowerMockito.mockStatic(PropertyUtils.class); - PowerMockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) + Mockito.mockStatic(PropertyUtils.class); + Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) .thenReturn(true); - PowerMockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)).thenReturn("/etc/krb5.conf"); - PowerMockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)).thenReturn("/etc/krb5.keytab"); - PowerMockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)).thenReturn("test@DS.COM"); + Mockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)).thenReturn("/etc/krb5.conf"); + Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)).thenReturn("/etc/krb5.keytab"); + Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)).thenReturn("test@DS.COM"); Assert.assertNotEquals("", ProcessUtils.getKerberosInitCommand()); - PowerMockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) + Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) .thenReturn(false); Assert.assertEquals("", ProcessUtils.getKerberosInitCommand()); } @@ -84,17 +80,12 @@ public class ProcessUtilsTest { String executePath = "/ds-exec/1/1/1"; TaskExecutionStatus running = TaskExecutionStatus.RUNNING_EXECUTION; - PowerMockito.mockStatic(HadoopUtils.class); + Mockito.mockStatic(HadoopUtils.class); HadoopUtils hadoop = HadoopUtils.getInstance(); try { - PowerMockito.whenNew(HadoopUtils.class).withAnyArguments().thenReturn(hadoop); - } catch (Exception e) { - e.printStackTrace(); - } - try { - when(hadoop.getApplicationStatus("application_1585532379175_228491")).thenReturn(running); - when(hadoop.getApplicationStatus("application_1598885606600_3677")).thenReturn(running); + Mockito.when(hadoop.getApplicationStatus("application_1585532379175_228491")).thenReturn(running); + Mockito.when(hadoop.getApplicationStatus("application_1598885606600_3677")).thenReturn(running); } catch (Exception e) { e.printStackTrace(); ProcessUtils.cancelApplication(appIds, logger, tenantCode, executePath); diff --git a/dolphinscheduler-service/pom.xml b/dolphinscheduler-service/pom.xml index 8b450f9f53..b8d89c0a6a 100644 --- a/dolphinscheduler-service/pom.xml +++ b/dolphinscheduler-service/pom.xml @@ -17,17 +17,29 @@ --> + 4.0.0 - dolphinscheduler org.apache.dolphinscheduler + dolphinscheduler dev-SNAPSHOT - 4.0.0 dolphinscheduler-service dolphinscheduler-service + + + + org.apache.dolphinscheduler + dolphinscheduler-bom + ${project.version} + pom + import + + + + @@ -51,6 +63,14 @@ dolphinscheduler-task-api + + org.mockito + mockito-inline + 3.12.4 + + test + + com.cronutils cron-utils @@ -63,16 +83,4 @@ - - - - - org.apache.dolphinscheduler - dolphinscheduler-bom - ${project.version} - pom - import - - - diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java index ce6ad57035..178cd6b2cc 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java @@ -21,9 +21,9 @@ import org.apache.dolphinscheduler.remote.NettyRemotingClient; import org.apache.dolphinscheduler.remote.command.Command; import org.apache.dolphinscheduler.remote.command.alert.AlertSendRequestCommand; import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand; -import org.apache.dolphinscheduler.remote.config.NettyClientConfig; import org.apache.dolphinscheduler.remote.utils.Host; import org.apache.dolphinscheduler.remote.utils.JsonSerializer; +import org.apache.dolphinscheduler.service.factory.NettyRemotingClientFactory; import java.util.concurrent.atomic.AtomicBoolean; @@ -34,8 +34,6 @@ public class AlertClientService implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(AlertClientService.class); - private final NettyClientConfig clientConfig; - private final NettyRemotingClient client; private final AtomicBoolean isRunning; @@ -53,8 +51,7 @@ public class AlertClientService implements AutoCloseable { * alert client */ public AlertClientService() { - this.clientConfig = new NettyClientConfig(); - this.client = new NettyRemotingClient(clientConfig); + this.client = NettyRemotingClientFactory.buildNettyRemotingClient(); this.isRunning = new AtomicBoolean(true); } @@ -89,8 +86,8 @@ public class AlertClientService implements AutoCloseable { * @param content * @return */ - public AlertSendResponseCommand sendAlert(int groupId, String title, String content, int strategy) { - return this.sendAlert(this.host,this.port,groupId,title,content,strategy); + public AlertSendResponseCommand sendAlert(int groupId, String title, String content, int strategy) { + return this.sendAlert(this.host, this.port, groupId, title, content, strategy); } /** @@ -102,8 +99,10 @@ public class AlertClientService implements AutoCloseable { * @param content content * @return AlertSendResponseCommand */ - public AlertSendResponseCommand sendAlert(String host, int port, int groupId, String title, String content, int strategy) { - logger.info("sync alert send, host : {}, port : {}, groupId : {}, title : {} , strategy : {} ", host, port, groupId, title, strategy); + public AlertSendResponseCommand sendAlert(String host, int port, int groupId, String title, String content, + int strategy) { + logger.info("sync alert send, host : {}, port : {}, groupId : {}, title : {} , strategy : {} ", host, port, + groupId, title, strategy); AlertSendRequestCommand request = new AlertSendRequestCommand(groupId, title, content, strategy); final Host address = new Host(host, port); try { diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/factory/NettyRemotingClientFactory.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/factory/NettyRemotingClientFactory.java new file mode 100644 index 0000000000..450b052be4 --- /dev/null +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/factory/NettyRemotingClientFactory.java @@ -0,0 +1,38 @@ +/* + * 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.service.factory; + +import org.apache.dolphinscheduler.remote.NettyRemotingClient; +import org.apache.dolphinscheduler.remote.config.NettyClientConfig; + +import lombok.experimental.UtilityClass; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@UtilityClass +public class NettyRemotingClientFactory { + + private final Logger logger = LoggerFactory.getLogger(NettyRemotingClientFactory.class); + + public NettyRemotingClient buildNettyRemotingClient() { + NettyClientConfig nettyClientConfig = new NettyClientConfig(); + logger.info("NettyRemotingClient initialized with config: {}", nettyClientConfig); + return new NettyRemotingClient(nettyClientConfig); + } +} diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/log/LogClient.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/log/LogClient.java index 611bb49b67..d72dfeba13 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/log/LogClient.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/log/LogClient.java @@ -33,9 +33,9 @@ import org.apache.dolphinscheduler.remote.command.log.RollViewLogRequestCommand; import org.apache.dolphinscheduler.remote.command.log.RollViewLogResponseCommand; import org.apache.dolphinscheduler.remote.command.log.ViewLogRequestCommand; import org.apache.dolphinscheduler.remote.command.log.ViewLogResponseCommand; -import org.apache.dolphinscheduler.remote.config.NettyClientConfig; import org.apache.dolphinscheduler.remote.exceptions.RemotingException; import org.apache.dolphinscheduler.remote.utils.Host; +import org.apache.dolphinscheduler.service.factory.NettyRemotingClientFactory; import java.util.List; @@ -59,9 +59,7 @@ public class LogClient implements AutoCloseable { private static final long LOG_REQUEST_TIMEOUT = 10 * 1000L; public LogClient() { - NettyClientConfig nettyClientConfig = new NettyClientConfig(); - this.client = new NettyRemotingClient(nettyClientConfig); - logger.info("Initialized LogClientService with config: {}", nettyClientConfig); + client = NettyRemotingClientFactory.buildNettyRemotingClient(); } /** diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java index 5d9c952617..57e16d2250 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java @@ -169,7 +169,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.base.Joiner; import com.google.common.base.Strings; import com.google.common.collect.Lists; - import io.micrometer.core.annotation.Counted; /** @@ -939,7 +938,7 @@ public class ProcessServiceImpl implements ProcessService { throw new IllegalArgumentException("Cannot find the process definition for this workflowInstance"); } Map cmdParam = JSONUtils.toMap(command.getCommandParam()); - if(cmdParam == null){ + if (cmdParam == null) { cmdParam = new HashMap<>(); } int processInstanceId = command.getProcessInstanceId(); @@ -963,10 +962,10 @@ public class ProcessServiceImpl implements ProcessService { // Recalculate global parameters after rerun. String globalParams = curingGlobalParamsService.curingGlobalParams(processInstance.getId(), - processDefinition.getGlobalParamMap(), - processDefinition.getGlobalParamList(), - commandTypeIfComplement, - processInstance.getScheduleTime(), timezoneId); + processDefinition.getGlobalParamMap(), + processDefinition.getGlobalParamList(), + commandTypeIfComplement, + processInstance.getScheduleTime(), timezoneId); processInstance.setGlobalParams(globalParams); processInstance.setProcessDefinition(processDefinition); @@ -1014,7 +1013,7 @@ public class ProcessServiceImpl implements ProcessService { initTaskInstance(this.findTaskInstanceById(taskId)); } cmdParam.put(Constants.CMD_PARAM_RECOVERY_START_NODE_STRING, - String.join(Constants.COMMA, convertIntListToString(failedList))); + String.join(Constants.COMMA, convertIntListToString(failedList))); processInstance.setCommandParam(JSONUtils.toJsonString(cmdParam)); processInstance.setRunTimes(runTime + 1); break; @@ -1032,7 +1031,7 @@ public class ProcessServiceImpl implements ProcessService { initTaskInstance(this.findTaskInstanceById(taskId)); } cmdParam.put(Constants.CMD_PARAM_RECOVERY_START_NODE_STRING, - String.join(Constants.COMMA, convertIntListToString(stopNodeList))); + String.join(Constants.COMMA, convertIntListToString(stopNodeList))); processInstance.setCommandParam(JSONUtils.toJsonString(cmdParam)); processInstance.setRunTimes(runTime + 1); break; @@ -1817,7 +1816,7 @@ public class ProcessServiceImpl implements ProcessService { * @param res origin resource info * @return {@link ResourceInfo} */ - private ResourceInfo updateResourceInfo(ResourceInfo res) { + protected ResourceInfo updateResourceInfo(ResourceInfo res) { ResourceInfo resourceInfo = null; // only if mainJar is not null and does not contains "resourceName" field if (res != null) { diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java index 5dbd26b173..0140f6e770 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java +++ b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java @@ -23,27 +23,24 @@ import org.apache.dolphinscheduler.remote.command.Command; import org.apache.dolphinscheduler.remote.command.alert.AlertSendRequestCommand; import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand; import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult; +import org.apache.dolphinscheduler.service.factory.NettyRemotingClientFactory; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * alert client service test - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({AlertClientService.class}) +@RunWith(MockitoJUnitRunner.class) public class AlertClientServiceTest { private static final Logger logger = LoggerFactory.getLogger(AlertClientServiceTest.class); @@ -52,13 +49,22 @@ public class AlertClientServiceTest { private AlertClientService alertClient; + private MockedStatic mockedNettyRemotingClientFactory; + @Before public void before() throws Exception { - client = PowerMockito.mock(NettyRemotingClient.class); - PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(client); + client = Mockito.mock(NettyRemotingClient.class); + mockedNettyRemotingClientFactory = Mockito.mockStatic(NettyRemotingClientFactory.class); + mockedNettyRemotingClientFactory.when(NettyRemotingClientFactory::buildNettyRemotingClient) + .thenReturn(client); alertClient = new AlertClientService(); } + @After + public void after() { + mockedNettyRemotingClientFactory.close(); + } + @Test public void testSendAlert() throws Exception { String host = "127.0.0.1"; @@ -89,7 +95,7 @@ public class AlertClientServiceTest { new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); Command resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); - PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); + Mockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content, WarningType.FAILURE.getCode()); Assert.assertFalse(alertSendResponseCommand.isSuccess()); @@ -104,7 +110,7 @@ public class AlertClientServiceTest { alertResult.setMessage(message); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); - PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); + Mockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content, WarningType.FAILURE.getCode()); Assert.assertFalse(alertSendResponseCommand.isSuccess()); @@ -118,7 +124,7 @@ public class AlertClientServiceTest { alertResult.setMessage(message); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); - PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); + Mockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content, WarningType.FAILURE.getCode()); Assert.assertFalse(alertSendResponseCommand.isSuccess()); @@ -131,7 +137,7 @@ public class AlertClientServiceTest { alertResult.setMessage("Abnormal information inside the alert plug-in code"); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); - PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); + Mockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content, WarningType.FAILURE.getCode()); Assert.assertFalse(alertSendResponseCommand.isSuccess()); @@ -145,7 +151,7 @@ public class AlertClientServiceTest { alertResult.setMessage(message); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); - PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); + Mockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content, WarningType.FAILURE.getCode()); Assert.assertTrue(alertSendResponseCommand.isSuccess()); diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManagerTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManagerTest.java index ec37903c9c..e0fbc58b9a 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManagerTest.java +++ b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManagerTest.java @@ -33,14 +33,14 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ProcessAlertManager Test */ -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class ProcessAlertManagerTest { private static final Logger logger = LoggerFactory.getLogger(ProcessAlertManagerTest.class); diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/log/LogClientTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/log/LogClientTest.java index c0b6001303..449cbc5a3c 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/log/LogClientTest.java +++ b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/log/LogClientTest.java @@ -27,20 +27,18 @@ import org.apache.dolphinscheduler.remote.command.log.RemoveTaskLogResponseComma import org.apache.dolphinscheduler.remote.command.log.RollViewLogResponseCommand; import org.apache.dolphinscheduler.remote.command.log.ViewLogResponseCommand; import org.apache.dolphinscheduler.remote.utils.Host; +import org.apache.dolphinscheduler.service.factory.NettyRemotingClientFactory; import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.Test; -import org.junit.Test.None; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(PowerMockRunner.class) -@PrepareForTest({LogClient.class, NetUtils.class, LoggerUtils.class, NettyRemotingClient.class}) +@RunWith(MockitoJUnitRunner.class) public class LogClientTest { @Test @@ -49,14 +47,17 @@ public class LogClientTest { int port = 1234; String path = "/tmp/log"; - PowerMockito.mockStatic(NetUtils.class); - PowerMockito.when(NetUtils.getHost()).thenReturn(localMachine); - PowerMockito.mockStatic(LoggerUtils.class); - PowerMockito.when(LoggerUtils.readWholeFileContent(Mockito.anyString())).thenReturn("application_xx_11"); - - LogClient logClient = new LogClient(); - String log = logClient.viewLog(localMachine, port, path); - Assert.assertNotNull(log); + try ( + MockedStatic mockedNetUtils = Mockito.mockStatic(NetUtils.class); + MockedStatic mockedLoggerUtils = Mockito.mockStatic(LoggerUtils.class)) { + mockedNetUtils.when(NetUtils::getHost) + .thenReturn(localMachine); + mockedLoggerUtils.when(() -> LoggerUtils.readWholeFileContent(Mockito.anyString())) + .thenReturn("application_xx_11"); + LogClient logClient = new LogClient(); + String log = logClient.viewLog(localMachine, port, path); + Assert.assertNotNull(log); + } } @Test @@ -65,74 +66,93 @@ public class LogClientTest { int port = 1234; String path = "/tmp/log"; - PowerMockito.mockStatic(NetUtils.class); - PowerMockito.when(NetUtils.getHost()).thenReturn(localMachine + "1"); - - NettyRemotingClient remotingClient = PowerMockito.mock(NettyRemotingClient.class); - PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(remotingClient); + try (MockedStatic mockedNetUtils = Mockito.mockStatic(NetUtils.class)) { + mockedNetUtils.when(NetUtils::getHost) + .thenReturn(localMachine + "1"); + LogClient logClient = new LogClient(); + String log = logClient.viewLog(localMachine, port, path); + Assert.assertNotNull(log); + } Command command = new Command(); command.setBody(JSONUtils.toJsonString(new ViewLogResponseCommand("")).getBytes(StandardCharsets.UTF_8)); - PowerMockito.when(remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) - .thenReturn(command); LogClient logClient = new LogClient(); String log = logClient.viewLog(localMachine, port, path); Assert.assertNotNull(log); } - @Test(expected = None.class) - public void testClose() throws Exception { - NettyRemotingClient remotingClient = PowerMockito.mock(NettyRemotingClient.class); - PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(remotingClient); - PowerMockito.doNothing().when(remotingClient).close(); - - LogClient logClient = new LogClient(); - logClient.close(); + @Test + public void testClose() { + try ( + MockedStatic mockedNettyRemotingClientFactory = + Mockito.mockStatic(NettyRemotingClientFactory.class)) { + NettyRemotingClient remotingClient = Mockito.mock(NettyRemotingClient.class); + mockedNettyRemotingClientFactory.when(NettyRemotingClientFactory::buildNettyRemotingClient) + .thenReturn(remotingClient); + LogClient logClient = new LogClient(); + logClient.close(); + } } @Test public void testRollViewLog() throws Exception { - NettyRemotingClient remotingClient = PowerMockito.mock(NettyRemotingClient.class); - PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(remotingClient); - - Command command = new Command(); - command.setBody(JSONUtils.toJsonByteArray(new RollViewLogResponseCommand("success"))); - PowerMockito.when(remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) - .thenReturn(command); - - LogClient logClient = new LogClient(); - String msg = logClient.rollViewLog("localhost", 1234, "/tmp/log", 0, 10); - Assert.assertNotNull(msg); + try ( + MockedStatic mockedNettyRemotingClientFactory = + Mockito.mockStatic(NettyRemotingClientFactory.class)) { + NettyRemotingClient remotingClient = Mockito.mock(NettyRemotingClient.class); + mockedNettyRemotingClientFactory.when(NettyRemotingClientFactory::buildNettyRemotingClient) + .thenReturn(remotingClient); + Command command = new Command(); + command.setBody(JSONUtils.toJsonByteArray(new RollViewLogResponseCommand("success"))); + Mockito.when( + remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) + .thenReturn(command); + + LogClient logClient = new LogClient(); + String msg = logClient.rollViewLog("localhost", 1234, "/tmp/log", 0, 10); + Assert.assertNotNull(msg); + } } @Test public void testGetLogBytes() throws Exception { - NettyRemotingClient remotingClient = PowerMockito.mock(NettyRemotingClient.class); - PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(remotingClient); - - Command command = new Command(); - command.setBody(JSONUtils.toJsonByteArray(new GetLogBytesResponseCommand("log".getBytes(StandardCharsets.UTF_8)))); - PowerMockito.when(remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) - .thenReturn(command); - - LogClient logClient = new LogClient(); - byte[] logBytes = logClient.getLogBytes("localhost", 1234, "/tmp/log"); - Assert.assertNotNull(logBytes); + try ( + MockedStatic mockedNettyRemotingClientFactory = + Mockito.mockStatic(NettyRemotingClientFactory.class)) { + NettyRemotingClient remotingClient = Mockito.mock(NettyRemotingClient.class); + mockedNettyRemotingClientFactory.when(NettyRemotingClientFactory::buildNettyRemotingClient) + .thenReturn(remotingClient); + Command command = new Command(); + command.setBody( + JSONUtils.toJsonByteArray(new GetLogBytesResponseCommand("log".getBytes(StandardCharsets.UTF_8)))); + Mockito.when( + remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) + .thenReturn(command); + + LogClient logClient = new LogClient(); + byte[] logBytes = logClient.getLogBytes("localhost", 1234, "/tmp/log"); + Assert.assertNotNull(logBytes); + } } @Test public void testRemoveTaskLog() throws Exception { - NettyRemotingClient remotingClient = PowerMockito.mock(NettyRemotingClient.class); - PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(remotingClient); - - Command command = new Command(); - command.setBody(JSONUtils.toJsonByteArray(new RemoveTaskLogResponseCommand(true))); - PowerMockito.when(remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) - .thenReturn(command); - LogClient logClient = new LogClient(); - Boolean status = logClient.removeTaskLog("localhost", 1234, "/log/path"); - Assert.assertTrue(status); + try ( + MockedStatic mockedNettyRemotingClientFactory = + Mockito.mockStatic(NettyRemotingClientFactory.class)) { + NettyRemotingClient remotingClient = Mockito.mock(NettyRemotingClient.class); + mockedNettyRemotingClientFactory.when(NettyRemotingClientFactory::buildNettyRemotingClient) + .thenReturn(remotingClient); + Command command = new Command(); + command.setBody(JSONUtils.toJsonByteArray(new RemoveTaskLogResponseCommand(true))); + Mockito.when( + remotingClient.sendSync(Mockito.any(Host.class), Mockito.any(Command.class), Mockito.anyLong())) + .thenReturn(command); + + LogClient logClient = new LogClient(); + Boolean status = logClient.removeTaskLog("localhost", 1234, "/log/path"); + Assert.assertTrue(status); + } } - } diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java index b1d64e67c3..1cb0794875 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java +++ b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java @@ -102,7 +102,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import org.powermock.reflect.Whitebox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -823,16 +822,12 @@ public class ProcessServiceTest { public void testUpdateResourceInfo() throws Exception { // test if input is null ResourceInfo resourceInfoNull = null; - ResourceInfo updatedResourceInfo1 = Whitebox.invokeMethod(processService, - "updateResourceInfo", - resourceInfoNull); + ResourceInfo updatedResourceInfo1 = processService.updateResourceInfo(resourceInfoNull); Assert.assertNull(updatedResourceInfo1); // test if resource id less than 1 ResourceInfo resourceInfoVoid = new ResourceInfo(); - ResourceInfo updatedResourceInfo2 = Whitebox.invokeMethod(processService, - "updateResourceInfo", - resourceInfoVoid); + ResourceInfo updatedResourceInfo2 = processService.updateResourceInfo(resourceInfoVoid); Assert.assertNull(updatedResourceInfo2); // test normal situation @@ -843,9 +838,8 @@ public class ProcessServiceTest { resource.setFileName("test.txt"); resource.setFullName("/test.txt"); Mockito.when(resourceMapper.selectById(1)).thenReturn(resource); - ResourceInfo updatedResourceInfo3 = Whitebox.invokeMethod(processService, - "updateResourceInfo", - resourceInfoNormal); + + ResourceInfo updatedResourceInfo3 = processService.updateResourceInfo(resourceInfoNormal); Assert.assertEquals(1, updatedResourceInfo3.getId().intValue()); Assert.assertEquals("test.txt", updatedResourceInfo3.getRes()); @@ -911,13 +905,15 @@ public class ProcessServiceTest { DateInterval dateInterval = new DateInterval(new Date(), new Date()); int testFlag = 1; - //find test lastManualProcessInterval - ProcessInstance lastManualProcessInterval = processService.findLastManualProcessInterval(definitionCode, dateInterval, testFlag); + // find test lastManualProcessInterval + ProcessInstance lastManualProcessInterval = + processService.findLastManualProcessInterval(definitionCode, dateInterval, testFlag); Assert.assertEquals(null, lastManualProcessInterval); - //find online lastManualProcessInterval + // find online lastManualProcessInterval testFlag = 0; - lastManualProcessInterval = processService.findLastManualProcessInterval(definitionCode, dateInterval, testFlag); + lastManualProcessInterval = + processService.findLastManualProcessInterval(definitionCode, dateInterval, testFlag); Assert.assertEquals(null, lastManualProcessInterval); } @@ -925,12 +921,12 @@ public class ProcessServiceTest { public void testQueryTestDataSourceId() { Integer onlineDataSourceId = 1; - //unbound testDataSourceId + // unbound testDataSourceId Mockito.when(dataSourceMapper.queryTestDataSourceId(any(Integer.class))).thenReturn(null); Integer result = processService.queryTestDataSourceId(onlineDataSourceId); Assert.assertNull(result); - //bound testDataSourceId + // bound testDataSourceId Integer testDataSourceId = 2; Mockito.when(dataSourceMapper.queryTestDataSourceId(any(Integer.class))).thenReturn(testDataSourceId); result = processService.queryTestDataSourceId(onlineDataSourceId);