Skip to content

Commit

Permalink
[Improvement][Monitor] Add UT for montor (#15998)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingwli committed May 15, 2024
1 parent 0e5cb66 commit 68a1906
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,40 @@ public void testCountQueueState() throws Exception {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
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;
Expand Down Expand Up @@ -70,6 +73,9 @@
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
*/
Expand Down Expand Up @@ -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<>();

Expand Down Expand Up @@ -285,6 +295,66 @@ public void testCountQueueState() {
.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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
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;

/**
Expand All @@ -65,6 +67,18 @@ public void testInsert() {
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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,9 @@
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
Expand All @@ -54,6 +59,18 @@ private ErrorCommand insertOne() {
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
*/
Expand Down

0 comments on commit 68a1906

Please sign in to comment.