@ -27,6 +27,7 @@ import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy ;
import static org.powermock.api.mockito.PowerMockito.when ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext ;
import org.apache.dolphinscheduler.spi.utils.JSONUtils ;
@ -44,17 +45,23 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest ;
import org.powermock.modules.junit4.PowerMockRunner ;
import java.util.HashMap ;
import java.util.Map ;
@RunWith ( PowerMockRunner . class )
@PrepareForTest ( {
ZeppelinTask . class ,
ZeppelinClient . class ,
ObjectMapper . class ,
} )
@PowerMockIgnore ( { "javax.*" } )
public class ZeppelinTaskTest {
private static final String MOCK_NOTE_ID = "2GYJR92R7" ;
private static final String MOCK_PARAGRAPH_ID = "paragraph_1648793472526_1771221396" ;
private static final String MOCK_PARAMETERS = "{\"key1\": \"value1\", \"key2\": \"value2\"}" ;
private final ObjectMapper mapper = new ObjectMapper ( ) ;
private ZeppelinClient zClient ;
private ZeppelinTask zeppelinTask ;
@ -73,7 +80,7 @@ public class ZeppelinTaskTest {
// use mocked zClient in zeppelinTask
doReturn ( this . zClient ) . when ( this . zeppelinTask , "getZeppelinClient" ) ;
when ( this . zClient . executeParagraph ( any ( ) , any ( ) ) ) . thenReturn ( this . paragraphResult ) ;
when ( this . zClient . executeParagraph ( any ( ) , any ( ) , any ( Map . class ) ) ) . thenReturn ( this . paragraphResult ) ;
when ( paragraphResult . getResultInText ( ) ) . thenReturn ( "mock-zeppelin-paragraph-execution-result" ) ;
this . zeppelinTask . init ( ) ;
}
@ -82,7 +89,9 @@ public class ZeppelinTaskTest {
public void testHandleWithParagraphExecutionSucess ( ) throws Exception {
when ( this . paragraphResult . getStatus ( ) ) . thenReturn ( Status . FINISHED ) ;
this . zeppelinTask . handle ( ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID , MOCK_PARAGRAPH_ID ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID ,
MOCK_PARAGRAPH_ID ,
( Map < String , String > ) mapper . readValue ( MOCK_PARAMETERS , Map . class ) ) ;
Mockito . verify ( this . paragraphResult ) . getResultInText ( ) ;
Mockito . verify ( this . paragraphResult ) . getStatus ( ) ;
Assert . assertEquals ( EXIT_CODE_SUCCESS , this . zeppelinTask . getExitStatusCode ( ) ) ;
@ -92,7 +101,9 @@ public class ZeppelinTaskTest {
public void testHandleWithParagraphExecutionAborted ( ) throws Exception {
when ( this . paragraphResult . getStatus ( ) ) . thenReturn ( Status . ABORT ) ;
this . zeppelinTask . handle ( ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID , MOCK_PARAGRAPH_ID ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID ,
MOCK_PARAGRAPH_ID ,
( Map < String , String > ) mapper . readValue ( MOCK_PARAMETERS , Map . class ) ) ;
Mockito . verify ( this . paragraphResult ) . getResultInText ( ) ;
Mockito . verify ( this . paragraphResult ) . getStatus ( ) ;
Assert . assertEquals ( EXIT_CODE_KILL , this . zeppelinTask . getExitStatusCode ( ) ) ;
@ -102,7 +113,9 @@ public class ZeppelinTaskTest {
public void testHandleWithParagraphExecutionError ( ) throws Exception {
when ( this . paragraphResult . getStatus ( ) ) . thenReturn ( Status . ERROR ) ;
this . zeppelinTask . handle ( ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID , MOCK_PARAGRAPH_ID ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID ,
MOCK_PARAGRAPH_ID ,
( Map < String , String > ) mapper . readValue ( MOCK_PARAMETERS , Map . class ) ) ;
Mockito . verify ( this . paragraphResult ) . getResultInText ( ) ;
Mockito . verify ( this . paragraphResult ) . getStatus ( ) ;
Assert . assertEquals ( EXIT_CODE_FAILURE , this . zeppelinTask . getExitStatusCode ( ) ) ;
@ -110,11 +123,13 @@ public class ZeppelinTaskTest {
@Test
public void testHandleWithParagraphExecutionException ( ) throws Exception {
when ( this . zClient . executeParagraph ( any ( ) , any ( ) ) ) .
when ( this . zClient . executeParagraph ( any ( ) , any ( ) , any ( Map . class ) ) ) .
thenThrow ( new Exception ( "Something wrong happens from zeppelin side" ) ) ;
// when(this.paragraphResult.getStatus()).thenReturn(Status.ERROR);
this . zeppelinTask . handle ( ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID , MOCK_PARAGRAPH_ID ) ;
Mockito . verify ( this . zClient ) . executeParagraph ( MOCK_NOTE_ID ,
MOCK_PARAGRAPH_ID ,
( Map < String , String > ) mapper . readValue ( MOCK_PARAMETERS , Map . class ) ) ;
Mockito . verify ( this . paragraphResult , Mockito . times ( 0 ) ) . getResultInText ( ) ;
Mockito . verify ( this . paragraphResult , Mockito . times ( 0 ) ) . getStatus ( ) ;
Assert . assertEquals ( EXIT_CODE_FAILURE , this . zeppelinTask . getExitStatusCode ( ) ) ;
@ -122,10 +137,9 @@ public class ZeppelinTaskTest {
private String buildZeppelinTaskParameters ( ) {
ZeppelinParameters zeppelinParameters = new ZeppelinParameters ( ) ;
String noteId = MOCK_NOTE_ID ;
String paragraphId = MOCK_PARAGRAPH_ID ;
zeppelinParameters . setNoteId ( noteId ) ;
zeppelinParameters . setParagraphId ( paragraphId ) ;
zeppelinParameters . setNoteId ( MOCK_NOTE_ID ) ;
zeppelinParameters . setParagraphId ( MOCK_PARAGRAPH_ID ) ;
zeppelinParameters . setParameters ( MOCK_PARAMETERS ) ;
return JSONUtils . toJsonString ( zeppelinParameters ) ;
}