Browse Source

[Improvement][Monitor] Add UT for montor (#15998)

3.2.2-release-bak
旺阳 6 months ago committed by GitHub
parent
commit
68a190674f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 36
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataAnalysisControllerTest.java
  2. 72
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java
  3. 14
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java
  4. 17
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java

36
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataAnalysisControllerTest.java

@ -150,4 +150,40 @@ public class DataAnalysisControllerTest extends AbstractControllerTest {
assertThat(result.getCode().intValue()).isEqualTo(Status.SUCCESS.getCode());
logger.info(mvcResult.getResponse().getContentAsString());
}
@Test
public void testListCommand() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("projectCode", "16");
paramsMap.add("pageNo", "1");
paramsMap.add("pageSize", "10");
MvcResult mvcResult = mockMvc.perform(get("/projects/analysis/listCommand")
.header("sessionId", sessionId)
.params(paramsMap))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
assertThat(result.getCode().intValue()).isEqualTo(Status.SUCCESS.getCode());
logger.info(mvcResult.getResponse().getContentAsString());
}
@Test
public void testListErrorCommand() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("projectCode", "16");
paramsMap.add("pageNo", "1");
paramsMap.add("pageSize", "10");
MvcResult mvcResult = mockMvc.perform(get("/projects/analysis/listErrorCommand")
.header("sessionId", sessionId)
.params(paramsMap))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
assertThat(result.getCode().intValue()).isEqualTo(Status.SUCCESS.getCode());
logger.info(mvcResult.getResponse().getContentAsString());
}
}

72
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java

@ -32,12 +32,15 @@ import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.exceptions.ServiceException;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.impl.DataAnalysisServiceImpl;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.vo.TaskInstanceCountVO;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.Command;
import org.apache.dolphinscheduler.dao.entity.CommandCount;
import org.apache.dolphinscheduler.dao.entity.ErrorCommand;
import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount;
import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.entity.User;
@ -70,6 +73,9 @@ import org.mockito.quality.Strictness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* data analysis service test
*/
@ -110,13 +116,17 @@ public class DataAnalysisServiceTest {
private User user;
private Project project;
@BeforeEach
public void setUp() {
user = new User();
user.setUserType(UserType.ADMIN_USER);
user.setId(1);
Project project = new Project();
project = new Project();
project.setId(1);
project.setCode(1);
project.setName("test");
resultMap = new HashMap<>();
@ -285,6 +295,66 @@ public class DataAnalysisServiceTest {
.allMatch(count -> count.equals(0));
}
@Test
public void testListPendingCommands() {
IPage<Command> page = new Page<>(1, 10);
page.setTotal(2L);
page.setRecords(getList());
when(commandMapper.queryCommandPage(any())).thenReturn(page);
PageInfo<Command> list1 = dataAnalysisServiceImpl.listPendingCommands(user, 1L, 1, 10);
assertThat(list1.getTotal()).isEqualTo(2);
user.setUserType(UserType.GENERAL_USER);
when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1,
serviceLogger))
.thenReturn(projectIds());
PageInfo<Command> list2 = dataAnalysisServiceImpl.listPendingCommands(user, 1L, 1, 10);
assertThat(list2.getTotal()).isEqualTo(0);
when(projectMapper.selectBatchIds(any())).thenReturn(Collections.singletonList(project));
when(processDefinitionMapper.queryDefinitionCodeListByProjectCodes(any()))
.thenReturn(Collections.singletonList(1L));
when(commandMapper.queryCommandPageByIds(any(), any())).thenReturn(page);
PageInfo<Command> list3 = dataAnalysisServiceImpl.listPendingCommands(user, 1L, 1, 10);
assertThat(list3.getTotal()).isEqualTo(2);
}
@Test
public void testListErrorCommand() {
IPage<ErrorCommand> page = new Page<>(1, 10);
page.setTotal(2L);
page.setRecords(getErrorList());
when(errorCommandMapper.queryErrorCommandPage(any())).thenReturn(page);
PageInfo<ErrorCommand> list1 = dataAnalysisServiceImpl.listErrorCommand(user, 1L, 1, 10);
assertThat(list1.getTotal()).isEqualTo(2);
user.setUserType(UserType.GENERAL_USER);
when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1,
serviceLogger))
.thenReturn(projectIds());
PageInfo<ErrorCommand> list2 = dataAnalysisServiceImpl.listErrorCommand(user, 1L, 1, 10);
assertThat(list2.getTotal()).isEqualTo(0);
when(projectMapper.selectBatchIds(any())).thenReturn(Collections.singletonList(project));
when(processDefinitionMapper.queryDefinitionCodeListByProjectCodes(any()))
.thenReturn(Collections.singletonList(1L));
when(errorCommandMapper.queryErrorCommandPageByIds(any(), any())).thenReturn(page);
PageInfo<ErrorCommand> list3 = dataAnalysisServiceImpl.listErrorCommand(user, 1L, 1, 10);
assertThat(list3.getTotal()).isEqualTo(2);
}
private List<Command> getList() {
List<Command> commandList = new ArrayList<>();
Command command = new Command();
command.setId(1);
commandList.add(command);
return commandList;
}
private List<ErrorCommand> getErrorList() {
List<ErrorCommand> commandList = new ArrayList<>();
ErrorCommand command = new ErrorCommand();
command.setId(1);
commandList.add(command);
return commandList;
}
private Set<Integer> projectIds() {
Set<Integer> projectIds = new HashSet<>();
projectIds.add(1);

14
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java

@ -43,6 +43,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
/**
@ -65,6 +67,18 @@ public class CommandMapperTest extends BaseDaoTest {
Assertions.assertTrue(command.getId() > 0);
}
@Test
public void testQueryCommandPageByIds() {
Command expectedCommand = createCommand();
Page<Command> page = new Page<>(1, 10);
IPage<Command> commandIPage = commandMapper.queryCommandPageByIds(page,
Lists.newArrayList(expectedCommand.getProcessDefinitionCode()));
List<Command> commandList = commandIPage.getRecords();
assertThat(commandList).isNotEmpty();
assertThat(commandIPage.getTotal()).isEqualTo(1);
assertThat(commandList.get(0).getId()).isEqualTo(expectedCommand.getId());
}
/**
* test select by id
*/

17
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java

@ -17,6 +17,8 @@
package org.apache.dolphinscheduler.dao.mapper;
import static com.google.common.truth.Truth.assertThat;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.dao.BaseDaoTest;
import org.apache.dolphinscheduler.dao.entity.CommandCount;
@ -31,6 +33,9 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
public class ErrorCommandMapperTest extends BaseDaoTest {
@Autowired
@ -54,6 +59,18 @@ public class ErrorCommandMapperTest extends BaseDaoTest {
return errorCommand;
}
@Test
public void testQueryCommandPageByIds() {
ErrorCommand expectedCommand = insertOne();
Page<ErrorCommand> page = new Page<>(1, 10);
IPage<ErrorCommand> commandIPage = errorCommandMapper.queryErrorCommandPageByIds(page,
Lists.newArrayList(expectedCommand.getProcessDefinitionCode()));
List<ErrorCommand> commandList = commandIPage.getRecords();
assertThat(commandList).isNotEmpty();
assertThat(commandIPage.getTotal()).isEqualTo(1);
assertThat(commandList.get(0).getId()).isEqualTo(expectedCommand.getId());
}
/**
* test query
*/

Loading…
Cancel
Save