Browse Source

Merge pull request #1916 from samz406/dev

Modify LoggerServiceTest UT by mock
pull/2/head
Jave-Chen 5 years ago committed by GitHub
parent
commit
6a94f2d3d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java
  2. 76
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java
  3. 1
      pom.xml

5
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java

@ -84,7 +84,12 @@ public class LoggerService {
if (taskInstance == null){ if (taskInstance == null){
throw new RuntimeException("task instance is null"); throw new RuntimeException("task instance is null");
} }
String host = taskInstance.getHost(); String host = taskInstance.getHost();
if(StringUtils.isEmpty(host)){
throw new RuntimeException("task instance host is null");
}
LogClient logClient = new LogClient(host, Constants.RPC_PORT); LogClient logClient = new LogClient(host, Constants.RPC_PORT);
return logClient.getLogBytes(taskInstance.getLogPath()); return logClient.getLogBytes(taskInstance.getLogPath());
} }

76
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java

@ -16,37 +16,83 @@
*/ */
package org.apache.dolphinscheduler.api.service; package org.apache.dolphinscheduler.api.service;
import org.apache.dolphinscheduler.api.ApiApplicationServer;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.dao.ProcessDao;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.TaskInstance;
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.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = ApiApplicationServer.class) @PrepareForTest({LoggerService.class})
public class LoggerServiceTest { public class LoggerServiceTest {
private static final Logger logger = LoggerFactory.getLogger(LoggerServiceTest.class); private static final Logger logger = LoggerFactory.getLogger(LoggerServiceTest.class);
@Autowired @InjectMocks
private LoggerService loggerService; private LoggerService loggerService;
@Mock
private ProcessDao processDao;
@Test
public void queryDataSourceList(){
User loginUser = new User(); @Test
loginUser.setId(27); public void testQueryDataSourceList(){
loginUser.setUserType(UserType.GENERAL_USER);
Result result = loggerService.queryLog(-1, 0, 100); TaskInstance taskInstance = new TaskInstance();
Mockito.when(processDao.findTaskInstanceById(1)).thenReturn(taskInstance);
Result result = loggerService.queryLog(2,1,1);
//TASK_INSTANCE_NOT_FOUND
Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(),result.getCode().intValue());
//HOST NOT FOUND
result = loggerService.queryLog(1,1,1);
Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(),result.getCode().intValue()); Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(),result.getCode().intValue());
//SUCCESS
taskInstance.setHost("127.0.0.1");
taskInstance.setLogPath("/temp/log");
Mockito.when(processDao.findTaskInstanceById(1)).thenReturn(taskInstance);
result = loggerService.queryLog(1,1,1);
Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
}
@Test
public void testGetLogBytes(){
TaskInstance taskInstance = new TaskInstance();
Mockito.when(processDao.findTaskInstanceById(1)).thenReturn(taskInstance);
//task instance is null
try{
loggerService.getLogBytes(2);
}catch (RuntimeException e){
Assert.assertTrue(true);
logger.error("testGetLogBytes error: {}","task instance is null");
}
//task instance host is null
try{
loggerService.getLogBytes(1);
}catch (RuntimeException e){
Assert.assertTrue(true);
logger.error("testGetLogBytes error: {}","task instance host is null");
}
//success
taskInstance.setHost("127.0.0.1");
taskInstance.setLogPath("/temp/log");
//if use @RunWith(PowerMockRunner.class) mock object,sonarcloud will not calculate the coverage,
// so no assert will be added here
loggerService.getLogBytes(1);
} }
} }

1
pom.xml

@ -700,6 +700,7 @@
<include>**/api/service/ExecutorService2Test.java</include> <include>**/api/service/ExecutorService2Test.java</include>
<include>**/api/service/BaseServiceTest.java</include> <include>**/api/service/BaseServiceTest.java</include>
<include>**/api/service/BaseDAGServiceTest.java</include> <include>**/api/service/BaseDAGServiceTest.java</include>
<include>**/api/service/LoggerServiceTest.java</include>
<include>**/alert/utils/ExcelUtilsTest.java</include> <include>**/alert/utils/ExcelUtilsTest.java</include>
<include>**/alert/utils/FuncUtilsTest.java</include> <include>**/alert/utils/FuncUtilsTest.java</include>
<include>**/alert/utils/JSONUtilsTest.java</include> <include>**/alert/utils/JSONUtilsTest.java</include>

Loading…
Cancel
Save