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