From 57fafe42567b434d5a8ef4673ca5d9796cc4dd05 Mon Sep 17 00:00:00 2001
From: rickchengx <38122586+rickchengx@users.noreply.github.com>
Date: Thu, 29 Sep 2022 12:00:41 +0800
Subject: [PATCH] [Improvement-12144] Remove powermock in dolphinschduler-api
(#12169)
---
dolphinscheduler-api/pom.xml | 9 +
.../DataAnalysisControllerTest.java | 21 +-
.../ProcessInstanceControllerTest.java | 37 +-
.../ProcessTaskRelationControllerTest.java | 18 +-
.../controller/ResourcesControllerTest.java | 54 +-
.../controller/WorkerGroupControllerTest.java | 23 +-
.../interceptor/RateLimitInterceptorTest.java | 9 +-
.../api/service/BaseServiceTest.java | 24 +-
.../api/service/DataAnalysisServiceTest.java | 42 +-
.../api/service/DataSourceServiceTest.java | 202 ++++----
.../api/service/LoggerServiceTest.java | 2 -
.../api/service/ResourcesServiceTest.java | 490 +++++++++---------
.../api/service/SchedulerServiceTest.java | 15 +-
.../api/service/TenantServiceTest.java | 77 +--
.../api/service/UdfFuncServiceTest.java | 202 +++++---
15 files changed, 621 insertions(+), 604 deletions(-)
diff --git a/dolphinscheduler-api/pom.xml b/dolphinscheduler-api/pom.xml
index fee8b25056..66df53785b 100644
--- a/dolphinscheduler-api/pom.xml
+++ b/dolphinscheduler-api/pom.xml
@@ -200,6 +200,15 @@
h2
test
+
+
+ org.mockito
+ mockito-inline
+ 3.12.4
+
+ test
+
+
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 636a1ec0a4..66e9b9ce0b 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
@@ -28,9 +28,9 @@ import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
+import java.util.Date;
+
import org.junit.Test;
-import org.mockito.Mockito;
-import org.powermock.api.mockito.PowerMockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,12 +39,11 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
-import java.util.Date;
-
/**
* data analysis controller test
*/
public class DataAnalysisControllerTest extends AbstractControllerTest {
+
private static final Logger logger = LoggerFactory.getLogger(DataAnalysisControllerTest.class);
@Autowired
@@ -65,9 +64,9 @@ public class DataAnalysisControllerTest extends AbstractControllerTest {
int projectId = createProject();
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
- paramsMap.add("startDate","2019-12-01 00:00:00");
- paramsMap.add("endDate","2019-12-28 00:00:00");
- paramsMap.add("projectCode","16");
+ paramsMap.add("startDate", "2019-12-01 00:00:00");
+ paramsMap.add("endDate", "2019-12-28 00:00:00");
+ paramsMap.add("projectCode", "16");
MvcResult mvcResult = mockMvc.perform(get("/projects/analysis/task-state-count")
.header("sessionId", sessionId)
.params(paramsMap))
@@ -86,9 +85,9 @@ public class DataAnalysisControllerTest extends AbstractControllerTest {
int projectId = createProject();
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
- paramsMap.add("startDate","2019-12-01 00:00:00");
- paramsMap.add("endDate","2019-12-28 00:00:00");
- paramsMap.add("projectCode","16");
+ paramsMap.add("startDate", "2019-12-01 00:00:00");
+ paramsMap.add("endDate", "2019-12-28 00:00:00");
+ paramsMap.add("projectCode", "16");
MvcResult mvcResult = mockMvc.perform(get("/projects/analysis/process-state-count")
.header("sessionId", sessionId)
@@ -107,7 +106,7 @@ public class DataAnalysisControllerTest extends AbstractControllerTest {
public void testCountDefinitionByUser() throws Exception {
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
- paramsMap.add("projectId","16");
+ paramsMap.add("projectId", "16");
MvcResult mvcResult = mockMvc.perform(get("/projects/analysis/define-user-count")
.header("sessionId", sessionId)
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java
index 8f4fd9902d..56cbee73dc 100644
--- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java
@@ -17,8 +17,12 @@
package org.apache.dolphinscheduler.api.controller;
-import java.util.HashMap;
-import java.util.Map;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.ProcessInstanceService;
@@ -26,21 +30,18 @@ import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
-import org.powermock.api.mockito.PowerMockito;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* process instance controller test
@@ -54,7 +55,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testQueryProcessInstanceList() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.SUCCESS.getCode());
- PowerMockito.when(processInstanceService
+ Mockito.when(processInstanceService
.queryProcessInstanceList(Mockito.any(), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any()))
@@ -85,7 +86,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testQueryTaskListByProcessId() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.PROJECT_NOT_FOUND);
- PowerMockito
+ Mockito
.when(processInstanceService.queryTaskListByProcessId(Mockito.any(), Mockito.anyLong(), Mockito.any()))
.thenReturn(mockResult);
@@ -104,7 +105,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testUpdateProcessInstance() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(processInstanceService
+ Mockito.when(processInstanceService
.updateProcessInstance(Mockito.any(), Mockito.anyLong(), Mockito.anyInt(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()))
@@ -141,7 +142,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testQueryProcessInstanceById() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(
+ Mockito.when(
processInstanceService.queryProcessInstanceById(Mockito.any(), Mockito.anyLong(), Mockito.anyInt()))
.thenReturn(mockResult);
MvcResult mvcResult = mockMvc.perform(get("/projects/{projectCode}/process-instances/{id}", "1113", "123")
@@ -159,7 +160,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testQuerySubProcessInstanceByTaskId() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.TASK_INSTANCE_NOT_EXISTS);
- PowerMockito.when(processInstanceService.querySubProcessInstanceByTaskId(Mockito.any(), Mockito.anyLong(),
+ Mockito.when(processInstanceService.querySubProcessInstanceByTaskId(Mockito.any(), Mockito.anyLong(),
Mockito.anyInt())).thenReturn(mockResult);
MvcResult mvcResult = mockMvc
@@ -179,7 +180,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testQueryParentInstanceBySubId() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.PROCESS_INSTANCE_NOT_SUB_PROCESS_INSTANCE);
- PowerMockito.when(
+ Mockito.when(
processInstanceService.queryParentInstanceBySubId(Mockito.any(), Mockito.anyLong(), Mockito.anyInt()))
.thenReturn(mockResult);
@@ -200,7 +201,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testViewVariables() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(processInstanceService.viewVariables(1113L, 123)).thenReturn(mockResult);
+ Mockito.when(processInstanceService.viewVariables(1113L, 123)).thenReturn(mockResult);
MvcResult mvcResult = mockMvc
.perform(get("/projects/{projectCode}/process-instances/{id}/view-variables", "1113", "123")
.header(SESSION_ID, sessionId))
@@ -216,7 +217,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
public void testDeleteProcessInstanceById() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(
+ Mockito.when(
processInstanceService.deleteProcessInstanceById(Mockito.any(), Mockito.anyLong(), Mockito.anyInt()))
.thenReturn(mockResult);
@@ -236,7 +237,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.PROCESS_INSTANCE_NOT_EXIST);
- PowerMockito.when(
+ Mockito.when(
processInstanceService.deleteProcessInstanceById(Mockito.any(), Mockito.anyLong(), Mockito.anyInt()))
.thenReturn(mockResult);
MvcResult mvcResult = mockMvc.perform(post("/projects/{projectCode}/process-instances/batch-delete", "1113")
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessTaskRelationControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessTaskRelationControllerTest.java
index 8649d32cbd..ca675812e0 100644
--- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessTaskRelationControllerTest.java
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessTaskRelationControllerTest.java
@@ -33,7 +33,6 @@ import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
-import org.powermock.api.mockito.PowerMockito;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
@@ -42,6 +41,7 @@ import org.springframework.test.web.servlet.MvcResult;
* process task relation controller test
*/
public class ProcessTaskRelationControllerTest extends AbstractControllerTest {
+
@MockBean(name = "processTaskRelationServiceImpl")
private ProcessTaskRelationService processTaskRelationService;
@@ -49,11 +49,13 @@ public class ProcessTaskRelationControllerTest extends AbstractControllerTest {
public void testQueryDownstreamRelation() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(processTaskRelationService.queryDownstreamRelation(Mockito.any(), Mockito.anyLong(), Mockito.anyLong()))
+ Mockito.when(
+ processTaskRelationService.queryDownstreamRelation(Mockito.any(), Mockito.anyLong(), Mockito.anyLong()))
.thenReturn(mockResult);
- MvcResult mvcResult = mockMvc.perform(get("/projects/{projectCode}/process-task-relation/{taskCode}/downstream", "1113", "123")
- .header(SESSION_ID, sessionId))
+ MvcResult mvcResult = mockMvc
+ .perform(get("/projects/{projectCode}/process-task-relation/{taskCode}/downstream", "1113", "123")
+ .header(SESSION_ID, sessionId))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
@@ -67,11 +69,13 @@ public class ProcessTaskRelationControllerTest extends AbstractControllerTest {
public void testQueryUpstreamRelation() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(processTaskRelationService.queryUpstreamRelation(Mockito.any(), Mockito.anyLong(), Mockito.anyLong()))
+ Mockito.when(
+ processTaskRelationService.queryUpstreamRelation(Mockito.any(), Mockito.anyLong(), Mockito.anyLong()))
.thenReturn(mockResult);
- MvcResult mvcResult = mockMvc.perform(get("/projects/{projectCode}/process-task-relation/{taskCode}/upstream", "1113", "123")
- .header(SESSION_ID, sessionId))
+ MvcResult mvcResult = mockMvc
+ .perform(get("/projects/{projectCode}/process-task-relation/{taskCode}/upstream", "1113", "123")
+ .header(SESSION_ID, sessionId))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ResourcesControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ResourcesControllerTest.java
index 3be552f878..d499a35125 100644
--- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ResourcesControllerTest.java
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ResourcesControllerTest.java
@@ -39,7 +39,6 @@ import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
-import org.powermock.api.mockito.PowerMockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -53,6 +52,7 @@ import org.springframework.util.MultiValueMap;
* resources controller test
*/
public class ResourcesControllerTest extends AbstractControllerTest {
+
private static final Logger logger = LoggerFactory.getLogger(ResourcesControllerTest.class);
@MockBean(name = "resourcesServiceImpl")
@@ -65,7 +65,7 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testQuerytResourceList() throws Exception {
Map mockResult = new HashMap<>();
mockResult.put(Constants.STATUS, Status.SUCCESS);
- PowerMockito.when(resourcesService.queryResourceList(Mockito.any(), Mockito.any())).thenReturn(mockResult);
+ Mockito.when(resourcesService.queryResourceList(Mockito.any(), Mockito.any())).thenReturn(mockResult);
MvcResult mvcResult = mockMvc.perform(get("/resources/list")
.header(SESSION_ID, sessionId)
@@ -84,8 +84,9 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testQueryResourceListPaging() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.SUCCESS.getCode());
- PowerMockito.when(resourcesService.queryResourceListPaging(
- Mockito.any(), Mockito.anyInt(), Mockito.any(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt()))
+ Mockito.when(resourcesService.queryResourceListPaging(
+ Mockito.any(), Mockito.anyInt(), Mockito.any(), Mockito.anyString(), Mockito.anyInt(),
+ Mockito.anyInt()))
.thenReturn(mockResult);
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
@@ -112,7 +113,8 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testVerifyResourceName() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.TENANT_NOT_EXIST.getCode());
- PowerMockito.when(resourcesService.verifyResourceName(Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(mockResult);
+ Mockito.when(resourcesService.verifyResourceName(Mockito.anyString(), Mockito.any(), Mockito.any()))
+ .thenReturn(mockResult);
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("fullName", "list_resources_1.sh");
@@ -135,7 +137,8 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testViewResource() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.HDFS_NOT_STARTUP.getCode());
- PowerMockito.when(resourcesService.readResource(Mockito.any(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(mockResult);
+ Mockito.when(resourcesService.readResource(Mockito.any(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt()))
+ .thenReturn(mockResult);
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("skipLineNum", "2");
@@ -158,8 +161,9 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testOnlineCreateResource() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.TENANT_NOT_EXIST.getCode());
- PowerMockito.when(resourcesService
- .onlineCreateResource(Mockito.any(), Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()))
+ Mockito.when(resourcesService
+ .onlineCreateResource(Mockito.any(), Mockito.any(), Mockito.anyString(), Mockito.anyString(),
+ Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()))
.thenReturn(mockResult);
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
@@ -188,7 +192,8 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testUpdateResourceContent() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.TENANT_NOT_EXIST.getCode());
- PowerMockito.when(resourcesService.updateResourceContent(Mockito.any(), Mockito.anyInt(), Mockito.anyString())).thenReturn(mockResult);
+ Mockito.when(resourcesService.updateResourceContent(Mockito.any(), Mockito.anyInt(), Mockito.anyString()))
+ .thenReturn(mockResult);
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("id", "1");
@@ -210,7 +215,7 @@ public class ResourcesControllerTest extends AbstractControllerTest {
@Test
public void testDownloadResource() throws Exception {
- PowerMockito.when(resourcesService.downloadResource(Mockito.any(), Mockito.anyInt())).thenReturn(null);
+ Mockito.when(resourcesService.downloadResource(Mockito.any(), Mockito.anyInt())).thenReturn(null);
MvcResult mvcResult = mockMvc.perform(get("/resources/{id}/download", 5)
.header(SESSION_ID, sessionId))
@@ -224,8 +229,9 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testCreateUdfFunc() throws Exception {
Result mockResult = new Result<>();
mockResult.setCode(Status.TENANT_NOT_EXIST.getCode());
- PowerMockito.when(udfFuncService
- .createUdfFunction(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.anyInt()))
+ Mockito.when(udfFuncService
+ .createUdfFunction(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
+ Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.anyInt()))
.thenReturn(mockResult);
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
@@ -254,7 +260,7 @@ public class ResourcesControllerTest extends AbstractControllerTest {
public void testViewUIUdfFunction() throws Exception {
Result