Browse Source

[Improvement][Test] Give an example on replacing Powermock with Mockito (#11588)

* Give an example on replacing powermock with mockito

* Remove redundant comments

* Refactoring related UTs for better readability.
3.2.0-release
Eric Gao 2 years ago committed by GitHub
parent
commit
e1b55db72a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/main/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterParameters.java
  2. 40
      dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/main/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTask.java
  3. 109
      dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java
  4. 7
      dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/pom.xml
  5. 2
      dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/main/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTask.java
  6. 65
      dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java
  7. 10
      dolphinscheduler-task-plugin/pom.xml

52
dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/main/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterParameters.java

@ -17,70 +17,38 @@
package org.apache.dolphinscheduler.plugin.task.jupyter; package org.apache.dolphinscheduler.plugin.task.jupyter;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo; import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters; import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
import org.apache.dolphinscheduler.spi.utils.StringUtils;
import java.util.List; import java.util.List;
/** import lombok.Data;
* jupyter parameters
*/ @Data
@Getter
@Setter
@ToString
public class JupyterParameters extends AbstractParameters { public class JupyterParameters extends AbstractParameters {
/**
* conda env name
*/
private String condaEnvName; private String condaEnvName;
/**
* input note path
*/
private String inputNotePath; private String inputNotePath;
/**
* output note path
*/
private String outputNotePath; private String outputNotePath;
/** // parameters to pass into jupyter note cells
* parameters to pass into jupyter note cells
*/
private String parameters; private String parameters;
/**
* jupyter kernel
*/
private String kernel; private String kernel;
/** // the execution engine name to use in evaluating the notebook
* the execution engine name to use in evaluating the notebook
*/
private String engine; private String engine;
/** // time in seconds to wait for each cell before failing execution (default: forever)
* time in seconds to wait for each cell before failing execution (default: forever)
*/
private String executionTimeout; private String executionTimeout;
/** // time in seconds to wait for kernel to start
* time in seconds to wait for kernel to start
*/
private String startTimeout; private String startTimeout;
/**
* other arguments
*/
private String others; private String others;
/**
* resource list
*/
private List<ResourceInfo> resourceList; private List<ResourceInfo> resourceList;
@Override @Override
@ -90,7 +58,9 @@ public class JupyterParameters extends AbstractParameters {
@Override @Override
public boolean checkParameters() { public boolean checkParameters() {
return condaEnvName != null && inputNotePath != null && outputNotePath != null; return StringUtils.isNotEmpty(condaEnvName) &&
StringUtils.isNotEmpty(inputNotePath) &&
StringUtils.isNotEmpty(outputNotePath);
} }
} }

40
dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/main/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTask.java

@ -43,14 +43,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public class JupyterTask extends AbstractRemoteTask { public class JupyterTask extends AbstractRemoteTask {
/**
* jupyter parameters
*/
private JupyterParameters jupyterParameters; private JupyterParameters jupyterParameters;
/**
* taskExecutionContext
*/
private TaskExecutionContext taskExecutionContext; private TaskExecutionContext taskExecutionContext;
private ShellCommandExecutor shellCommandExecutor; private ShellCommandExecutor shellCommandExecutor;
@ -88,7 +82,6 @@ public class JupyterTask extends AbstractRemoteTask {
@Override @Override
public void handle(TaskCallBack taskCallBack) throws TaskException { public void handle(TaskCallBack taskCallBack) throws TaskException {
try { try {
// SHELL task exit code
TaskResponse response = shellCommandExecutor.run(buildCommand()); TaskResponse response = shellCommandExecutor.run(buildCommand());
setExitStatusCode(response.getExitStatusCode()); setExitStatusCode(response.getExitStatusCode());
setAppIds(String.join(TaskConstants.COMMA, getApplicationIds())); setAppIds(String.join(TaskConstants.COMMA, getApplicationIds()));
@ -116,16 +109,12 @@ public class JupyterTask extends AbstractRemoteTask {
} }
/** /**
* create command * command will be like: papermill [OPTIONS] NOTEBOOK_PATH [OUTPUT_PATH]
*
* @return command
*/ */
protected String buildCommand() throws IOException { protected String buildCommand() throws IOException {
/**
* papermill [OPTIONS] NOTEBOOK_PATH [OUTPUT_PATH]
*/
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
final String condaPath = PropertyUtils.getString(TaskConstants.CONDA_PATH); final String condaPath = readCondaPath();
final String timestamp = DateUtils.getTimestampString(); final String timestamp = DateUtils.getTimestampString();
String condaEnvName = jupyterParameters.getCondaEnvName(); String condaEnvName = jupyterParameters.getCondaEnvName();
if (condaEnvName.endsWith(JupyterConstants.TXT_SUFFIX)) { if (condaEnvName.endsWith(JupyterConstants.TXT_SUFFIX)) {
@ -149,11 +138,7 @@ public class JupyterTask extends AbstractRemoteTask {
args.add(JupyterConstants.PAPERMILL); args.add(JupyterConstants.PAPERMILL);
args.add(jupyterParameters.getInputNotePath()); args.add(jupyterParameters.getInputNotePath());
args.add(jupyterParameters.getOutputNotePath()); args.add(jupyterParameters.getOutputNotePath());
// populate jupyter parameterization
args.addAll(populateJupyterParameterization()); args.addAll(populateJupyterParameterization());
// populate jupyter options
args.addAll(populateJupyterOptions()); args.addAll(populateJupyterOptions());
// remove tmp conda env, if created from requirements.txt // remove tmp conda env, if created from requirements.txt
@ -164,7 +149,6 @@ public class JupyterTask extends AbstractRemoteTask {
// replace placeholder, and combining local and global parameters // replace placeholder, and combining local and global parameters
Map<String, Property> paramsMap = taskExecutionContext.getPrepareParamsMap(); Map<String, Property> paramsMap = taskExecutionContext.getPrepareParamsMap();
String command = ParameterUtils String command = ParameterUtils
.convertParameterPlaceholders(String.join(" ", args), ParamUtils.convert(paramsMap)); .convertParameterPlaceholders(String.join(" ", args), ParamUtils.convert(paramsMap));
@ -173,12 +157,11 @@ public class JupyterTask extends AbstractRemoteTask {
return command; return command;
} }
/** protected String readCondaPath() {
* build jupyter parameterization return PropertyUtils.getString(TaskConstants.CONDA_PATH);
* }
* @return argument list
*/ protected List<String> populateJupyterParameterization() throws IOException {
private List<String> populateJupyterParameterization() throws IOException {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
String parameters = jupyterParameters.getParameters(); String parameters = jupyterParameters.getParameters();
if (StringUtils.isNotEmpty(parameters)) { if (StringUtils.isNotEmpty(parameters)) {
@ -200,12 +183,7 @@ public class JupyterTask extends AbstractRemoteTask {
return args; return args;
} }
/** protected List<String> populateJupyterOptions() {
* build jupyter options
*
* @return argument list
*/
private List<String> populateJupyterOptions() {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
String kernel = jupyterParameters.getKernel(); String kernel = jupyterParameters.getKernel();
if (StringUtils.isNotEmpty(kernel)) { if (StringUtils.isNotEmpty(kernel)) {

109
dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java

@ -17,43 +17,24 @@
package org.apache.dolphinscheduler.plugin.task.jupyter; package org.apache.dolphinscheduler.plugin.task.jupyter;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
import org.apache.dolphinscheduler.spi.utils.DateUtils; import org.apache.dolphinscheduler.spi.utils.DateUtils;
import org.apache.dolphinscheduler.spi.utils.JSONUtils; import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.dolphinscheduler.spi.utils.PropertyUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito; import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; @RunWith(MockitoJUnitRunner.class)
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({
JSONUtils.class,
PropertyUtils.class,
DateUtils.class
})
@PowerMockIgnore({"javax.*"})
@SuppressStaticInitializationFor("org.apache.dolphinscheduler.spi.utils.PropertyUtils")
public class JupyterTaskTest { public class JupyterTaskTest {
@Test private static final String EXPECTED_JUPYTER_TASK_COMMAND_USE_LOCAL_CONDA_ENV =
public void testBuildJupyterCommandWithLocalEnv() throws Exception {
String parameters = buildJupyterCommandWithLocalEnv();
TaskExecutionContext taskExecutionContext = PowerMockito.mock(TaskExecutionContext.class);
when(taskExecutionContext.getTaskParams()).thenReturn(parameters);
PowerMockito.mockStatic(PropertyUtils.class);
when(PropertyUtils.getString(any())).thenReturn("/opt/anaconda3/etc/profile.d/conda.sh");
JupyterTask jupyterTask = spy(new JupyterTask(taskExecutionContext));
jupyterTask.init();
Assert.assertEquals(jupyterTask.buildCommand(),
"source /opt/anaconda3/etc/profile.d/conda.sh && " + "source /opt/anaconda3/etc/profile.d/conda.sh && " +
"conda activate jupyter-lab && " + "conda activate jupyter-lab && " +
"papermill " + "papermill " +
@ -67,19 +48,9 @@ public class JupyterTaskTest {
"--start-timeout 3 " + "--start-timeout 3 " +
"--version " + "--version " +
"--inject-paths " + "--inject-paths " +
"--progress-bar"); "--progress-bar";
}
@Test private static final String EXPECTED_JUPYTER_TASK_COMMAND_USE_PACKED_CONDA_ENV =
public void testBuildJupyterCommandWithPackedEnv() throws Exception {
String parameters = buildJupyterCommandWithPackedEnv();
TaskExecutionContext taskExecutionContext = PowerMockito.mock(TaskExecutionContext.class);
when(taskExecutionContext.getTaskParams()).thenReturn(parameters);
PowerMockito.mockStatic(PropertyUtils.class);
when(PropertyUtils.getString(any())).thenReturn("/opt/anaconda3/etc/profile.d/conda.sh");
JupyterTask jupyterTask = spy(new JupyterTask(taskExecutionContext));
jupyterTask.init();
Assert.assertEquals(jupyterTask.buildCommand(),
"source /opt/anaconda3/etc/profile.d/conda.sh && " + "source /opt/anaconda3/etc/profile.d/conda.sh && " +
"mkdir jupyter_env && " + "mkdir jupyter_env && " +
"tar -xzf jupyter.tar.gz -C jupyter_env && " + "tar -xzf jupyter.tar.gz -C jupyter_env && " +
@ -95,21 +66,9 @@ public class JupyterTaskTest {
"--start-timeout 3 " + "--start-timeout 3 " +
"--version " + "--version " +
"--inject-paths " + "--inject-paths " +
"--progress-bar"); "--progress-bar";
}
@Test private static final String EXPECTED_JUPYTER_TASK_COMMAND_USE_PIP_REQUIREMENTS =
public void testBuildJupyterCommandWithRequirements() throws Exception {
String parameters = buildJupyterCommandWithRequirements();
TaskExecutionContext taskExecutionContext = PowerMockito.mock(TaskExecutionContext.class);
when(taskExecutionContext.getTaskParams()).thenReturn(parameters);
PowerMockito.mockStatic(PropertyUtils.class);
when(PropertyUtils.getString(any())).thenReturn("/opt/anaconda3/etc/profile.d/conda.sh");
PowerMockito.mockStatic(DateUtils.class);
when(DateUtils.getTimestampString()).thenReturn("123456789");
JupyterTask jupyterTask = spy(new JupyterTask(taskExecutionContext));
jupyterTask.init();
Assert.assertEquals(jupyterTask.buildCommand(),
"set +e \n " + "set +e \n " +
"source /opt/anaconda3/etc/profile.d/conda.sh && " + "source /opt/anaconda3/etc/profile.d/conda.sh && " +
"conda create -n jupyter-tmp-env-123456789 -y && " + "conda create -n jupyter-tmp-env-123456789 -y && " +
@ -127,11 +86,43 @@ public class JupyterTaskTest {
"--version " + "--version " +
"--inject-paths " + "--inject-paths " +
"--progress-bar \n " + "--progress-bar \n " +
"conda deactivate && conda remove --name jupyter-tmp-env-123456789 --all -y" "conda deactivate && conda remove --name jupyter-tmp-env-123456789 --all -y";
);
@Test
public void jupyterTaskUseLocalCondaEnv() throws Exception {
String jupyterTaskParameters = buildJupyterTaskUseLocalCondaEnvCommand();
JupyterTask jupyterTask = prepareJupyterTaskForTest(jupyterTaskParameters);
jupyterTask.init();
Assert.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_LOCAL_CONDA_ENV);
}
@Test
public void jupyterTaskUsePackedCondaEnv() throws Exception {
String jupyterTaskParameters = buildJupyterTaskUsePackedCondaEnvCommand();
JupyterTask jupyterTask = prepareJupyterTaskForTest(jupyterTaskParameters);
jupyterTask.init();
Assert.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_PACKED_CONDA_ENV);
}
@Test
public void jupyterTaskUsePipRequirements() throws Exception {
String jupyterTaskParameters = buildJupyterTaskUsePipRequirementsCommand();
JupyterTask jupyterTask = prepareJupyterTaskForTest(jupyterTaskParameters);
Mockito.mockStatic(DateUtils.class);
when(DateUtils.getTimestampString()).thenReturn("123456789");
jupyterTask.init();
Assert.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_PIP_REQUIREMENTS);
}
private JupyterTask prepareJupyterTaskForTest(final String jupyterTaskParameters) {
TaskExecutionContext taskExecutionContext = Mockito.mock(TaskExecutionContext.class);
when(taskExecutionContext.getTaskParams()).thenReturn(jupyterTaskParameters);
JupyterTask jupyterTask = spy(new JupyterTask(taskExecutionContext));
doReturn("/opt/anaconda3/etc/profile.d/conda.sh").when(jupyterTask).readCondaPath();
return jupyterTask;
} }
private String buildJupyterCommandWithLocalEnv() { private String buildJupyterTaskUseLocalCondaEnvCommand() {
JupyterParameters jupyterParameters = new JupyterParameters(); JupyterParameters jupyterParameters = new JupyterParameters();
jupyterParameters.setCondaEnvName("jupyter-lab"); jupyterParameters.setCondaEnvName("jupyter-lab");
jupyterParameters.setInputNotePath("/test/input_note.ipynb"); jupyterParameters.setInputNotePath("/test/input_note.ipynb");
@ -145,7 +136,7 @@ public class JupyterTaskTest {
return JSONUtils.toJsonString(jupyterParameters); return JSONUtils.toJsonString(jupyterParameters);
} }
private String buildJupyterCommandWithPackedEnv() { private String buildJupyterTaskUsePackedCondaEnvCommand() {
JupyterParameters jupyterParameters = new JupyterParameters(); JupyterParameters jupyterParameters = new JupyterParameters();
jupyterParameters.setCondaEnvName("jupyter.tar.gz"); jupyterParameters.setCondaEnvName("jupyter.tar.gz");
jupyterParameters.setInputNotePath("/test/input_note.ipynb"); jupyterParameters.setInputNotePath("/test/input_note.ipynb");
@ -159,7 +150,7 @@ public class JupyterTaskTest {
return JSONUtils.toJsonString(jupyterParameters); return JSONUtils.toJsonString(jupyterParameters);
} }
private String buildJupyterCommandWithRequirements() { private String buildJupyterTaskUsePipRequirementsCommand() {
JupyterParameters jupyterParameters = new JupyterParameters(); JupyterParameters jupyterParameters = new JupyterParameters();
jupyterParameters.setCondaEnvName("requirements.txt"); jupyterParameters.setCondaEnvName("requirements.txt");
jupyterParameters.setInputNotePath("/test/input_note.ipynb"); jupyterParameters.setInputNotePath("/test/input_note.ipynb");

7
dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/pom.xml

@ -15,15 +15,14 @@
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>dolphinscheduler-task-plugin</artifactId>
<groupId>org.apache.dolphinscheduler</groupId> <groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-task-plugin</artifactId>
<version>dev-SNAPSHOT</version> <version>dev-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dolphinscheduler-task-zeppelin</artifactId> <artifactId>dolphinscheduler-task-zeppelin</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>

2
dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/main/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTask.java

@ -165,7 +165,7 @@ public class ZeppelinTask extends AbstractRemoteTask {
* *
* @return ZeppelinClient * @return ZeppelinClient
*/ */
private ZeppelinClient getZeppelinClient() { protected ZeppelinClient getZeppelinClient() {
final String restEndpoint = zeppelinParameters.getRestEndpoint(); final String restEndpoint = zeppelinParameters.getRestEndpoint();
final ClientConfig clientConfig = new ClientConfig(restEndpoint); final ClientConfig clientConfig = new ClientConfig(restEndpoint);
ZeppelinClient zClient = null; ZeppelinClient zClient = null;

65
dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java

@ -20,14 +20,11 @@ package org.apache.dolphinscheduler.plugin.task.zeppelin;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE; import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_KILL; import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_KILL;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_SUCCESS; import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_SUCCESS;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock; import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy; import static org.mockito.Mockito.spy;
import static org.powermock.api.mockito.PowerMockito.when; import static org.mockito.Mockito.when;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.dolphinscheduler.plugin.task.api.TaskCallBack; import org.apache.dolphinscheduler.plugin.task.api.TaskCallBack;
import org.apache.dolphinscheduler.plugin.task.api.TaskException; import org.apache.dolphinscheduler.plugin.task.api.TaskException;
@ -35,32 +32,23 @@ import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
import org.apache.dolphinscheduler.spi.utils.DateUtils; import org.apache.dolphinscheduler.spi.utils.DateUtils;
import org.apache.dolphinscheduler.spi.utils.JSONUtils; import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.zeppelin.client.ParagraphResult;
import org.apache.zeppelin.client.NoteResult; import org.apache.zeppelin.client.NoteResult;
import org.apache.zeppelin.client.ParagraphResult;
import org.apache.zeppelin.client.Status; import org.apache.zeppelin.client.Status;
import org.apache.zeppelin.client.ZeppelinClient; import org.apache.zeppelin.client.ZeppelinClient;
import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(PowerMockRunner.class) @RunWith(MockitoJUnitRunner.class)
@PrepareForTest({
ZeppelinTask.class,
ZeppelinClient.class,
ObjectMapper.class,
DateUtils.class
})
@PowerMockIgnore({"javax.*"})
public class ZeppelinTaskTest { public class ZeppelinTaskTest {
private static final String MOCK_NOTE_ID = "2GYJR92R7"; private static final String MOCK_NOTE_ID = "2GYJR92R7";
@ -82,16 +70,14 @@ public class ZeppelinTaskTest {
@Before @Before
public void before() throws Exception { public void before() throws Exception {
String zeppelinParameters = buildZeppelinTaskParameters(); String zeppelinParameters = buildZeppelinTaskParameters();
TaskExecutionContext taskExecutionContext = PowerMockito.mock(TaskExecutionContext.class); TaskExecutionContext taskExecutionContext = mock(TaskExecutionContext.class);
when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParameters); when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParameters);
this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext)); this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext));
// mock zClient and paragraph result
this.zClient = mock(ZeppelinClient.class); this.zClient = mock(ZeppelinClient.class);
this.paragraphResult = mock(ParagraphResult.class); this.paragraphResult = mock(ParagraphResult.class);
// use mocked zClient in zeppelinTask doReturn(this.zClient).when(this.zeppelinTask).getZeppelinClient();
doReturn(this.zClient).when(this.zeppelinTask, "getZeppelinClient");
when(this.zClient.executeParagraph(any(), any(), any(Map.class))).thenReturn(this.paragraphResult); when(this.zClient.executeParagraph(any(), any(), any(Map.class))).thenReturn(this.paragraphResult);
when(paragraphResult.getResultInText()).thenReturn("mock-zeppelin-paragraph-execution-result"); when(paragraphResult.getResultInText()).thenReturn("mock-zeppelin-paragraph-execution-result");
this.zeppelinTask.init(); this.zeppelinTask.init();
@ -135,10 +121,10 @@ public class ZeppelinTaskTest {
@Test(expected = TaskException.class) @Test(expected = TaskException.class)
public void testHandleWithParagraphExecutionException() throws Exception { public void testHandleWithParagraphExecutionException() throws Exception {
when(this.zClient.executeParagraph(any(), any(), any(Map.class))). when(this.zClient.executeParagraph(any(), any(), any(Map.class)))
thenThrow(new TaskException("Something wrong happens from zeppelin side")); .thenThrow(new TaskException("Something wrong happens from zeppelin side"));
// when(this.paragraphResult.getStatus()).thenReturn(Status.ERROR);
this.zeppelinTask.handle(taskCallBack); this.zeppelinTask.handle(taskCallBack);
Mockito.verify(this.zClient).executeParagraph(MOCK_NOTE_ID, Mockito.verify(this.zClient).executeParagraph(MOCK_NOTE_ID,
MOCK_PARAGRAPH_ID, MOCK_PARAGRAPH_ID,
(Map<String, String>) mapper.readValue(MOCK_PARAMETERS, Map.class)); (Map<String, String>) mapper.readValue(MOCK_PARAMETERS, Map.class));
@ -150,21 +136,18 @@ public class ZeppelinTaskTest {
@Test @Test
public void testHandleWithNoteExecutionSuccess() throws Exception { public void testHandleWithNoteExecutionSuccess() throws Exception {
String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithNoParagraphId(); String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithNoParagraphId();
TaskExecutionContext taskExecutionContext= PowerMockito.mock(TaskExecutionContext.class); TaskExecutionContext taskExecutionContext = mock(TaskExecutionContext.class);
when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId); when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId);
this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext)); this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext));
// mock zClient and note result
this.zClient = mock(ZeppelinClient.class); this.zClient = mock(ZeppelinClient.class);
this.noteResult = mock(NoteResult.class); this.noteResult = mock(NoteResult.class);
// use mocked zClient in zeppelinTask doReturn(this.zClient).when(this.zeppelinTask).getZeppelinClient();
doReturn(this.zClient).when(this.zeppelinTask, "getZeppelinClient");
when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult); when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult);
when(paragraphResult.getResultInText()).thenReturn("mock-zeppelin-paragraph-execution-result");
this.zeppelinTask.init(); this.zeppelinTask.init();
when(this.paragraphResult.getStatus()).thenReturn(Status.FINISHED);
this.zeppelinTask.handle(taskCallBack); this.zeppelinTask.handle(taskCallBack);
Mockito.verify(this.zClient).executeNote(MOCK_NOTE_ID, Mockito.verify(this.zClient).executeNote(MOCK_NOTE_ID,
(Map<String, String>) mapper.readValue(MOCK_PARAMETERS, Map.class)); (Map<String, String>) mapper.readValue(MOCK_PARAMETERS, Map.class));
Mockito.verify(this.noteResult).getParagraphResultList(); Mockito.verify(this.noteResult).getParagraphResultList();
@ -174,22 +157,18 @@ public class ZeppelinTaskTest {
@Test @Test
public void testHandleWithNoteExecutionSuccessWithProductionSetting() throws Exception { public void testHandleWithNoteExecutionSuccessWithProductionSetting() throws Exception {
String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithProductionSetting(); String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithProductionSetting();
TaskExecutionContext taskExecutionContext = PowerMockito.mock(TaskExecutionContext.class); TaskExecutionContext taskExecutionContext = mock(TaskExecutionContext.class);
PowerMockito.mockStatic(DateUtils.class); Mockito.mockStatic(DateUtils.class);
when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId); when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId);
this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext)); this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext));
// mock zClient and note result
this.zClient = mock(ZeppelinClient.class); this.zClient = mock(ZeppelinClient.class);
this.noteResult = mock(NoteResult.class); this.noteResult = mock(NoteResult.class);
// use mocked zClient in zeppelinTask doReturn(this.zClient).when(this.zeppelinTask).getZeppelinClient();
doReturn(this.zClient).when(this.zeppelinTask, "getZeppelinClient");
when(this.zClient.cloneNote(any(String.class), any(String.class))).thenReturn(MOCK_CLONE_NOTE_ID); when(this.zClient.cloneNote(any(String.class), any(String.class))).thenReturn(MOCK_CLONE_NOTE_ID);
when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult); when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult);
when(paragraphResult.getResultInText()).thenReturn("mock-zeppelin-paragraph-execution-result");
this.zeppelinTask.init(); this.zeppelinTask.init();
when(this.paragraphResult.getStatus()).thenReturn(Status.FINISHED);
when(DateUtils.getTimestampString()).thenReturn("123456789"); when(DateUtils.getTimestampString()).thenReturn("123456789");
this.zeppelinTask.handle(taskCallBack); this.zeppelinTask.handle(taskCallBack);
Mockito.verify(this.zClient).cloneNote( Mockito.verify(this.zClient).cloneNote(

10
dolphinscheduler-task-plugin/pom.xml

@ -75,4 +75,14 @@
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.12.4</version>
<!-- TODO: move this dependency to root pom after removing powermock in the whole project -->
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

Loading…
Cancel
Save