@ -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.RollViewLogResponseCommand ;
import org.apache.dolphinscheduler.remote.command.log.ViewLogResponseCommand ;
import org.apache.dolphinscheduler.remote.command.log.ViewLogResponseCommand ;
import org.apache.dolphinscheduler.remote.utils.Host ;
import org.apache.dolphinscheduler.remote.utils.Host ;
import org.apache.dolphinscheduler.service.factory.NettyRemotingClientFactory ;
import java.nio.charset.StandardCharsets ;
import java.nio.charset.StandardCharsets ;
import org.junit.Assert ;
import org.junit.Assert ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.Test.None ;
import org.junit.runner.RunWith ;
import org.junit.runner.RunWith ;
import org.mockito.MockedStatic ;
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.PrepareForTest ;
import org.powermock.modules.junit4.PowerMockRunner ;
@RunWith ( PowerMockRunner . class )
@RunWith ( MockitoJUnitRunner . class )
@PrepareForTest ( { LogClient . class , NetUtils . class , LoggerUtils . class , NettyRemotingClient . class } )
public class LogClientTest {
public class LogClientTest {
@Test
@Test
@ -49,15 +47,18 @@ public class LogClientTest {
int port = 1234 ;
int port = 1234 ;
String path = "/tmp/log" ;
String path = "/tmp/log" ;
PowerMockito . mockStatic ( NetUtils . class ) ;
try (
PowerMockito . when ( NetUtils . getHost ( ) ) . thenReturn ( localMachine ) ;
MockedStatic < NetUtils > mockedNetUtils = Mockito . mockStatic ( NetUtils . class ) ;
PowerMockito . mockStatic ( LoggerUtils . class ) ;
MockedStatic < LoggerUtils > mockedLoggerUtils = Mockito . mockStatic ( LoggerUtils . class ) ) {
PowerMockito . when ( LoggerUtils . readWholeFileContent ( Mockito . anyString ( ) ) ) . thenReturn ( "application_xx_11" ) ;
mockedNetUtils . when ( NetUtils : : getHost )
. thenReturn ( localMachine ) ;
mockedLoggerUtils . when ( ( ) - > LoggerUtils . readWholeFileContent ( Mockito . anyString ( ) ) )
. thenReturn ( "application_xx_11" ) ;
LogClient logClient = new LogClient ( ) ;
LogClient logClient = new LogClient ( ) ;
String log = logClient . viewLog ( localMachine , port , path ) ;
String log = logClient . viewLog ( localMachine , port , path ) ;
Assert . assertNotNull ( log ) ;
Assert . assertNotNull ( log ) ;
}
}
}
@Test
@Test
public void testViewLogFromRemote ( ) throws Exception {
public void testViewLogFromRemote ( ) throws Exception {
@ -65,74 +66,93 @@ public class LogClientTest {
int port = 1234 ;
int port = 1234 ;
String path = "/tmp/log" ;
String path = "/tmp/log" ;
PowerMockito . mockStatic ( NetUtils . class ) ;
try ( MockedStatic < NetUtils > mockedNetUtils = Mockito . mockStatic ( NetUtils . class ) ) {
PowerMockito . when ( NetUtils . getHost ( ) ) . thenReturn ( localMachine + "1" ) ;
mockedNetUtils . when ( NetUtils : : getHost )
. thenReturn ( localMachine + "1" ) ;
NettyRemotingClient remotingClient = PowerMockito . mock ( NettyRemotingClient . class ) ;
LogClient logClient = new LogClient ( ) ;
PowerMockito . whenNew ( NettyRemotingClient . class ) . withAnyArguments ( ) . thenReturn ( remotingClient ) ;
String log = logClient . viewLog ( localMachine , port , path ) ;
Assert . assertNotNull ( log ) ;
}
Command command = new Command ( ) ;
Command command = new Command ( ) ;
command . setBody ( JSONUtils . toJsonString ( new ViewLogResponseCommand ( "" ) ) . getBytes ( StandardCharsets . UTF_8 ) ) ;
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 ( ) ;
LogClient logClient = new LogClient ( ) ;
String log = logClient . viewLog ( localMachine , port , path ) ;
String log = logClient . viewLog ( localMachine , port , path ) ;
Assert . assertNotNull ( log ) ;
Assert . assertNotNull ( log ) ;
}
}
@Test ( expected = None . class )
@Test
public void testClose ( ) throws Exception {
public void testClose ( ) {
NettyRemotingClient remotingClient = PowerMockito . mock ( NettyRemotingClient . class ) ;
try (
PowerMockito . whenNew ( NettyRemotingClient . class ) . withAnyArguments ( ) . thenReturn ( remotingClient ) ;
MockedStatic < NettyRemotingClientFactory > mockedNettyRemotingClientFactory =
PowerMockito . doNothing ( ) . when ( remotingClient ) . close ( ) ;
Mockito . mockStatic ( NettyRemotingClientFactory . class ) ) {
NettyRemotingClient remotingClient = Mockito . mock ( NettyRemotingClient . class ) ;
mockedNettyRemotingClientFactory . when ( NettyRemotingClientFactory : : buildNettyRemotingClient )
. thenReturn ( remotingClient ) ;
LogClient logClient = new LogClient ( ) ;
LogClient logClient = new LogClient ( ) ;
logClient . close ( ) ;
logClient . close ( ) ;
}
}
}
@Test
@Test
public void testRollViewLog ( ) throws Exception {
public void testRollViewLog ( ) throws Exception {
NettyRemotingClient remotingClient = PowerMockito . mock ( NettyRemotingClient . class ) ;
try (
PowerMockito . whenNew ( NettyRemotingClient . class ) . withAnyArguments ( ) . thenReturn ( remotingClient ) ;
MockedStatic < NettyRemotingClientFactory > mockedNettyRemotingClientFactory =
Mockito . mockStatic ( NettyRemotingClientFactory . class ) ) {
NettyRemotingClient remotingClient = Mockito . mock ( NettyRemotingClient . class ) ;
mockedNettyRemotingClientFactory . when ( NettyRemotingClientFactory : : buildNettyRemotingClient )
. thenReturn ( remotingClient ) ;
Command command = new Command ( ) ;
Command command = new Command ( ) ;
command . setBody ( JSONUtils . toJsonByteArray ( new RollViewLogResponseCommand ( "success" ) ) ) ;
command . setBody ( JSONUtils . toJsonByteArray ( new RollViewLogResponseCommand ( "success" ) ) ) ;
PowerMockito . when ( remotingClient . sendSync ( Mockito . any ( Host . class ) , Mockito . any ( Command . class ) , Mockito . anyLong ( ) ) )
Mockito . when (
remotingClient . sendSync ( Mockito . any ( Host . class ) , Mockito . any ( Command . class ) , Mockito . anyLong ( ) ) )
. thenReturn ( command ) ;
. thenReturn ( command ) ;
LogClient logClient = new LogClient ( ) ;
LogClient logClient = new LogClient ( ) ;
String msg = logClient . rollViewLog ( "localhost" , 1234 , "/tmp/log" , 0 , 10 ) ;
String msg = logClient . rollViewLog ( "localhost" , 1234 , "/tmp/log" , 0 , 10 ) ;
Assert . assertNotNull ( msg ) ;
Assert . assertNotNull ( msg ) ;
}
}
}
@Test
@Test
public void testGetLogBytes ( ) throws Exception {
public void testGetLogBytes ( ) throws Exception {
NettyRemotingClient remotingClient = PowerMockito . mock ( NettyRemotingClient . class ) ;
try (
PowerMockito . whenNew ( NettyRemotingClient . class ) . withAnyArguments ( ) . thenReturn ( remotingClient ) ;
MockedStatic < NettyRemotingClientFactory > mockedNettyRemotingClientFactory =
Mockito . mockStatic ( NettyRemotingClientFactory . class ) ) {
NettyRemotingClient remotingClient = Mockito . mock ( NettyRemotingClient . class ) ;
mockedNettyRemotingClientFactory . when ( NettyRemotingClientFactory : : buildNettyRemotingClient )
. thenReturn ( remotingClient ) ;
Command command = new Command ( ) ;
Command command = new Command ( ) ;
command . setBody ( JSONUtils . toJsonByteArray ( new GetLogBytesResponseCommand ( "log" . getBytes ( StandardCharsets . UTF_8 ) ) ) ) ;
command . setBody (
PowerMockito . when ( remotingClient . sendSync ( Mockito . any ( Host . class ) , Mockito . any ( Command . class ) , Mockito . anyLong ( ) ) )
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 ) ;
. thenReturn ( command ) ;
LogClient logClient = new LogClient ( ) ;
LogClient logClient = new LogClient ( ) ;
byte [ ] logBytes = logClient . getLogBytes ( "localhost" , 1234 , "/tmp/log" ) ;
byte [ ] logBytes = logClient . getLogBytes ( "localhost" , 1234 , "/tmp/log" ) ;
Assert . assertNotNull ( logBytes ) ;
Assert . assertNotNull ( logBytes ) ;
}
}
}
@Test
@Test
public void testRemoveTaskLog ( ) throws Exception {
public void testRemoveTaskLog ( ) throws Exception {
NettyRemotingClient remotingClient = PowerMockito . mock ( NettyRemotingClient . class ) ;
PowerMockito . whenNew ( NettyRemotingClient . class ) . withAnyArguments ( ) . thenReturn ( remotingClient ) ;
try (
MockedStatic < NettyRemotingClientFactory > mockedNettyRemotingClientFactory =
Mockito . mockStatic ( NettyRemotingClientFactory . class ) ) {
NettyRemotingClient remotingClient = Mockito . mock ( NettyRemotingClient . class ) ;
mockedNettyRemotingClientFactory . when ( NettyRemotingClientFactory : : buildNettyRemotingClient )
. thenReturn ( remotingClient ) ;
Command command = new Command ( ) ;
Command command = new Command ( ) ;
command . setBody ( JSONUtils . toJsonByteArray ( new RemoveTaskLogResponseCommand ( true ) ) ) ;
command . setBody ( JSONUtils . toJsonByteArray ( new RemoveTaskLogResponseCommand ( true ) ) ) ;
PowerMockito . when ( remotingClient . sendSync ( Mockito . any ( Host . class ) , Mockito . any ( Command . class ) , Mockito . anyLong ( ) ) )
Mockito . when (
remotingClient . sendSync ( Mockito . any ( Host . class ) , Mockito . any ( Command . class ) , Mockito . anyLong ( ) ) )
. thenReturn ( command ) ;
. thenReturn ( command ) ;
LogClient logClient = new LogClient ( ) ;
LogClient logClient = new LogClient ( ) ;
Boolean status = logClient . removeTaskLog ( "localhost" , 1234 , "/log/path" ) ;
Boolean status = logClient . removeTaskLog ( "localhost" , 1234 , "/log/path" ) ;
Assert . assertTrue ( status ) ;
Assert . assertTrue ( status ) ;
}
}
}
}
}