|
|
@ -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( |
|
|
|