Browse Source

[TEST] increase cov of logger service (#15870)

Co-authored-by: abzymeinsjtu <abzymeinsjtu@B-54Q8MD6R-0244.local>
Co-authored-by: Eric Gao <ericgao.apache@gmail.com>
3.2.2-release-bak
Evan Sun 8 months ago committed by GitHub
parent
commit
325bfa821f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java
  2. 60
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java
  3. 18
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/ServiceTestUtil.java

3
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java

@ -237,7 +237,7 @@ public class LoggerServiceImpl extends BaseServiceImpl implements LoggerService
host, host,
Constants.SYSTEM_LINE_SEPARATOR).getBytes(StandardCharsets.UTF_8); Constants.SYSTEM_LINE_SEPARATOR).getBytes(StandardCharsets.UTF_8);
byte[] logBytes = new byte[0]; byte[] logBytes;
ILogService iLogService = ILogService iLogService =
SingletonJdkDynamicRpcClientProxyFactory.getProxyClient(taskInstance.getHost(), ILogService.class); SingletonJdkDynamicRpcClientProxyFactory.getProxyClient(taskInstance.getHost(), ILogService.class);
@ -251,6 +251,5 @@ public class LoggerServiceImpl extends BaseServiceImpl implements LoggerService
log.error("Download TaskInstance: {} Log Error", taskInstance.getName(), ex); log.error("Download TaskInstance: {} Log Error", taskInstance.getName(), ex);
throw new ServiceException(Status.DOWNLOAD_TASK_INSTANCE_LOG_FILE_ERROR); throw new ServiceException(Status.DOWNLOAD_TASK_INSTANCE_LOG_FILE_ERROR);
} }
} }
} }

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

@ -18,8 +18,10 @@
package org.apache.dolphinscheduler.api.service; package org.apache.dolphinscheduler.api.service;
import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow;
import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.DOWNLOAD_LOG; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.DOWNLOAD_LOG;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.VIEW_LOG; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.VIEW_LOG;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -109,11 +111,25 @@ public class LoggerServiceTest {
@Override @Override
public TaskInstanceLogFileDownloadResponse getTaskInstanceWholeLogFileBytes(TaskInstanceLogFileDownloadRequest taskInstanceLogFileDownloadRequest) { public TaskInstanceLogFileDownloadResponse getTaskInstanceWholeLogFileBytes(TaskInstanceLogFileDownloadRequest taskInstanceLogFileDownloadRequest) {
if (taskInstanceLogFileDownloadRequest.getTaskInstanceId() == 1) {
return new TaskInstanceLogFileDownloadResponse(new byte[0]); return new TaskInstanceLogFileDownloadResponse(new byte[0]);
} else if (taskInstanceLogFileDownloadRequest.getTaskInstanceId() == 10) {
return new TaskInstanceLogFileDownloadResponse("log content".getBytes());
}
throw new ServiceException("download error");
} }
@Override @Override
public TaskInstanceLogPageQueryResponse pageQueryTaskInstanceLog(TaskInstanceLogPageQueryRequest taskInstanceLogPageQueryRequest) { public TaskInstanceLogPageQueryResponse pageQueryTaskInstanceLog(TaskInstanceLogPageQueryRequest taskInstanceLogPageQueryRequest) {
if (taskInstanceLogPageQueryRequest.getTaskInstanceId() != null) {
if (taskInstanceLogPageQueryRequest.getTaskInstanceId() == 100) {
throw new ServiceException("query log error");
} else if (taskInstanceLogPageQueryRequest.getTaskInstanceId() == 10) {
return new TaskInstanceLogPageQueryResponse("log content");
}
}
return new TaskInstanceLogPageQueryResponse(); return new TaskInstanceLogPageQueryResponse();
} }
@ -177,6 +193,13 @@ public class LoggerServiceTest {
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); when(taskInstanceDao.queryById(1)).thenReturn(taskInstance);
result = loggerService.queryLog(loginUser, 1, 1, 1); result = loggerService.queryLog(loginUser, 1, 1, 1);
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
result = loggerService.queryLog(loginUser, 1, 0, 1);
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
taskInstance.setLogPath("");
assertThrowsServiceException(Status.QUERY_TASK_INSTANCE_LOG_ERROR,
() -> loggerService.queryLog(loginUser, 1, 1, 1));
} }
@Test @Test
@ -237,9 +260,15 @@ public class LoggerServiceTest {
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
TaskInstance taskInstance = new TaskInstance(); TaskInstance taskInstance = new TaskInstance();
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); when(taskInstanceDao.queryById(1)).thenReturn(taskInstance);
when(taskInstanceDao.queryById(10)).thenReturn(null);
assertThrowsServiceException(Status.TASK_INSTANCE_NOT_FOUND,
() -> loggerService.queryLog(loginUser, projectCode, 10, 1, 1));
TaskDefinition taskDefinition = new TaskDefinition(); TaskDefinition taskDefinition = new TaskDefinition();
taskDefinition.setProjectCode(projectCode); taskDefinition.setProjectCode(projectCode);
taskDefinition.setCode(1L); taskDefinition.setCode(1L);
// SUCCESS // SUCCESS
taskInstance.setTaskCode(1L); taskInstance.setTaskCode(1L);
taskInstance.setId(1); taskInstance.setId(1);
@ -249,13 +278,27 @@ public class LoggerServiceTest {
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); when(taskInstanceDao.queryById(1)).thenReturn(taskInstance);
when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition);
assertDoesNotThrow(() -> loggerService.queryLog(loginUser, projectCode, 1, 1, 1)); assertDoesNotThrow(() -> loggerService.queryLog(loginUser, projectCode, 1, 1, 1));
taskDefinition.setProjectCode(10);
assertThrowsServiceException(Status.TASK_INSTANCE_NOT_FOUND,
() -> loggerService.queryLog(loginUser, projectCode, 1, 1, 1));
taskDefinition.setProjectCode(1);
taskInstance.setId(10);
when(taskInstanceDao.queryById(10)).thenReturn(taskInstance);
String result = loggerService.queryLog(loginUser, projectCode, 10, 1, 1);
assertEquals("log content", result);
taskInstance.setId(100);
when(taskInstanceDao.queryById(100)).thenReturn(taskInstance);
assertThrowsServiceException(Status.QUERY_TASK_INSTANCE_LOG_ERROR,
() -> loggerService.queryLog(loginUser, projectCode, 10, 1, 1));
} }
@Test @Test
public void testGetLogBytesInSpecifiedProject() { public void testGetLogBytesInSpecifiedProject() {
long projectCode = 1L; long projectCode = 1L;
when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode)); when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
Project project = getProject(projectCode);
User loginUser = new User(); User loginUser = new User();
loginUser.setId(-1); loginUser.setId(-1);
@ -272,9 +315,24 @@ public class LoggerServiceTest {
taskInstance.setHost("127.0.0.1:" + nettyServerPort); taskInstance.setHost("127.0.0.1:" + nettyServerPort);
taskInstance.setLogPath("/temp/log"); taskInstance.setLogPath("/temp/log");
doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, DOWNLOAD_LOG); doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, DOWNLOAD_LOG);
when(taskInstanceDao.queryById(1)).thenReturn(null);
assertThrowsServiceException(
Status.INTERNAL_SERVER_ERROR_ARGS, () -> loggerService.getLogBytes(loginUser, projectCode, 1));
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); when(taskInstanceDao.queryById(1)).thenReturn(taskInstance);
when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition);
assertDoesNotThrow(() -> loggerService.getLogBytes(loginUser, projectCode, 1)); assertDoesNotThrow(() -> loggerService.getLogBytes(loginUser, projectCode, 1));
taskDefinition.setProjectCode(2L);
assertThrowsServiceException(Status.INTERNAL_SERVER_ERROR_ARGS,
() -> loggerService.getLogBytes(loginUser, projectCode, 1));
taskDefinition.setProjectCode(1L);
taskInstance.setId(100);
when(taskInstanceDao.queryById(100)).thenReturn(taskInstance);
assertThrowsServiceException(Status.DOWNLOAD_TASK_INSTANCE_LOG_FILE_ERROR,
() -> loggerService.getLogBytes(loginUser, projectCode, 100));
} }
/** /**

18
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/ServiceTestUtil.java

@ -17,6 +17,9 @@
package org.apache.dolphinscheduler.api.utils; package org.apache.dolphinscheduler.api.utils;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.User;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Random; import java.util.Random;
@ -27,4 +30,19 @@ public class ServiceTestUtil {
new Random().nextBytes(bitArray); new Random().nextBytes(bitArray);
return new String(bitArray, StandardCharsets.UTF_8); return new String(bitArray, StandardCharsets.UTF_8);
} }
private static User getUser(Integer userId, String userName, UserType userType) {
User user = new User();
user.setUserType(userType);
user.setId(userId);
user.setUserName(userName);
return user;
}
public static User getAdminUser() {
return getUser(1, "admin", UserType.ADMIN_USER);
}
public static User getGeneralUser() {
return getUser(10, "user", UserType.GENERAL_USER);
}
} }

Loading…
Cancel
Save