From 68a190674fe7f6d0e957fca1efa085b067224cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=BA=E9=98=B3?= Date: Wed, 15 May 2024 14:44:04 +0800 Subject: [PATCH] [Improvement][Monitor] Add UT for montor (#15998) --- .../DataAnalysisControllerTest.java | 36 ++++++++++ .../api/service/DataAnalysisServiceTest.java | 72 ++++++++++++++++++- .../dao/mapper/CommandMapperTest.java | 14 ++++ .../dao/mapper/ErrorCommandMapperTest.java | 17 +++++ 4 files changed, 138 insertions(+), 1 deletion(-) diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataAnalysisControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataAnalysisControllerTest.java index 2e9961b5d2..36622b8b98 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataAnalysisControllerTest.java +++ b/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 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 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()); + } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java index 32033cc2af..a2d655f47f 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java +++ b/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 page = new Page<>(1, 10); + page.setTotal(2L); + page.setRecords(getList()); + when(commandMapper.queryCommandPage(any())).thenReturn(page); + PageInfo 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 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 list3 = dataAnalysisServiceImpl.listPendingCommands(user, 1L, 1, 10); + assertThat(list3.getTotal()).isEqualTo(2); + } + + @Test + public void testListErrorCommand() { + IPage page = new Page<>(1, 10); + page.setTotal(2L); + page.setRecords(getErrorList()); + when(errorCommandMapper.queryErrorCommandPage(any())).thenReturn(page); + PageInfo 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 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 list3 = dataAnalysisServiceImpl.listErrorCommand(user, 1L, 1, 10); + assertThat(list3.getTotal()).isEqualTo(2); + } + + private List getList() { + List commandList = new ArrayList<>(); + Command command = new Command(); + command.setId(1); + commandList.add(command); + return commandList; + } + + private List getErrorList() { + List commandList = new ArrayList<>(); + ErrorCommand command = new ErrorCommand(); + command.setId(1); + commandList.add(command); + return commandList; + } + private Set projectIds() { Set projectIds = new HashSet<>(); projectIds.add(1); diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java index 5623969b08..2105885fa3 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java +++ b/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 page = new Page<>(1, 10); + IPage commandIPage = commandMapper.queryCommandPageByIds(page, + Lists.newArrayList(expectedCommand.getProcessDefinitionCode())); + List commandList = commandIPage.getRecords(); + assertThat(commandList).isNotEmpty(); + assertThat(commandIPage.getTotal()).isEqualTo(1); + assertThat(commandList.get(0).getId()).isEqualTo(expectedCommand.getId()); + } + /** * test select by id */ diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java index 098c001f2d..07409cf0e5 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java +++ b/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 page = new Page<>(1, 10); + IPage commandIPage = errorCommandMapper.queryErrorCommandPageByIds(page, + Lists.newArrayList(expectedCommand.getProcessDefinitionCode())); + List commandList = commandIPage.getRecords(); + assertThat(commandList).isNotEmpty(); + assertThat(commandIPage.getTotal()).isEqualTo(1); + assertThat(commandList.get(0).getId()).isEqualTo(expectedCommand.getId()); + } + /** * test query */