Browse Source

[TEST] increase coverage of project parameter service test (#15905)

Co-authored-by: abzymeinsjtu <abzymeinsjtu@B-54Q8MD6R-0244.local>
3.2.2-release-bak
Evan Sun 6 months ago committed by GitHub
parent
commit
1d13ef09c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectParameterServiceImpl.java
  2. 198
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectParameterServiceTest.java

4
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectParameterServiceImpl.java

@ -227,11 +227,7 @@ public class ProjectParameterServiceImpl extends BaseServiceImpl implements Proj
}
for (ProjectParameter projectParameter : projectParameterList) {
try {
this.deleteProjectParametersByCode(loginUser, projectCode, projectParameter.getCode());
} catch (Exception e) {
throw new ServiceException(Status.DELETE_PROJECT_PARAMETER_ERROR, e.getMessage());
}
}
putMsg(result, Status.SUCCESS);

198
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectParameterServiceTest.java

@ -17,27 +17,40 @@
package org.apache.dolphinscheduler.api.service;
import static org.apache.dolphinscheduler.api.utils.ServiceTestUtil.getGeneralUser;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import org.apache.dolphinscheduler.api.AssertionsHelper;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.impl.ProjectParameterServiceImpl;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.utils.CodeGenerateUtils;
import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.entity.ProjectParameter;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectParameterMapper;
import org.junit.jupiter.api.Assertions;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class ProjectParameterServiceTest {
@ -60,92 +73,187 @@ public class ProjectParameterServiceTest {
public void testCreateProjectParameter() {
User loginUser = getGeneralUser();
// PROJECT_PARAMETER_ALREADY_EXISTS
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
Mockito.when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(getProjectParameter());
Mockito.when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(true);
// PERMISSION DENIED
when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(false);
Result result = projectParameterService.createProjectParameter(loginUser, projectCode, "key", "value");
Assertions.assertEquals(Status.PROJECT_PARAMETER_ALREADY_EXISTS.getCode(), result.getCode());
assertNull(result.getData());
assertNull(result.getCode());
assertNull(result.getMsg());
when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(true);
// CODE GENERATION ERROR
try (MockedStatic<CodeGenerateUtils> ignored = Mockito.mockStatic(CodeGenerateUtils.class)) {
when(CodeGenerateUtils.genCode()).thenThrow(CodeGenerateUtils.CodeGenerateException.class);
result = projectParameterService.createProjectParameter(loginUser, projectCode, "key", "value");
assertEquals(Status.CREATE_PROJECT_PARAMETER_ERROR.getCode(), result.getCode());
}
// PROJECT_PARAMETER_ALREADY_EXISTS
when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(getProjectParameter());
result = projectParameterService.createProjectParameter(loginUser, projectCode, "key", "value");
assertEquals(Status.PROJECT_PARAMETER_ALREADY_EXISTS.getCode(), result.getCode());
// INSERT DATA ERROR
when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(null);
when(projectParameterMapper.insert(Mockito.any())).thenReturn(-1);
result = projectParameterService.createProjectParameter(loginUser, projectCode, "key1", "value");
assertEquals(Status.CREATE_PROJECT_PARAMETER_ERROR.getCode(), result.getCode());
// SUCCESS
Mockito.when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(null);
Mockito.when(projectParameterMapper.insert(Mockito.any())).thenReturn(1);
when(projectParameterMapper.insert(Mockito.any())).thenReturn(1);
result = projectParameterService.createProjectParameter(loginUser, projectCode, "key1", "value");
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode());
assertEquals(Status.SUCCESS.getCode(), result.getCode());
}
@Test
public void testUpdateProjectParameter() {
User loginUser = getGeneralUser();
// NO PERMISSION
when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(false);
Result result = projectParameterService.updateProjectParameter(loginUser, projectCode, 1, "key", "value");
assertNull(result.getData());
assertNull(result.getCode());
assertNull(result.getMsg());
// PROJECT_PARAMETER_NOT_EXISTS
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
Mockito.when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(true);
Mockito.when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(null);
Result result = projectParameterService.updateProjectParameter(loginUser, projectCode, 1, "key", "value");
Assertions.assertEquals(Status.PROJECT_PARAMETER_NOT_EXISTS.getCode(), result.getCode());
when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(null);
result = projectParameterService.updateProjectParameter(loginUser, projectCode, 1, "key", "value");
assertEquals(Status.PROJECT_PARAMETER_NOT_EXISTS.getCode(), result.getCode());
// PROJECT_PARAMETER_ALREADY_EXISTS
Mockito.when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(getProjectParameter());
Mockito.when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(getProjectParameter());
when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(getProjectParameter());
when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(getProjectParameter());
result = projectParameterService.updateProjectParameter(loginUser, projectCode, 1, "key", "value");
Assertions.assertEquals(Status.PROJECT_PARAMETER_ALREADY_EXISTS.getCode(), result.getCode());
assertEquals(Status.PROJECT_PARAMETER_ALREADY_EXISTS.getCode(), result.getCode());
// PROJECT_UPDATE_ERROR
when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(null);
when(projectParameterMapper.updateById(Mockito.any())).thenReturn(-1);
result = projectParameterService.updateProjectParameter(loginUser, projectCode, 1, "key1", "value");
assertEquals(Status.UPDATE_PROJECT_PARAMETER_ERROR.getCode(), result.getCode());
// SUCCESS
Mockito.when(projectParameterMapper.selectOne(Mockito.any())).thenReturn(null);
Mockito.when(projectParameterMapper.updateById(Mockito.any())).thenReturn(1);
when(projectParameterMapper.updateById(Mockito.any())).thenReturn(1);
result = projectParameterService.updateProjectParameter(loginUser, projectCode, 1, "key1", "value");
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode());
assertEquals(Status.SUCCESS.getCode(), result.getCode());
ProjectParameter projectParameter = (ProjectParameter) result.getData();
Assertions.assertNotNull(projectParameter.getOperator());
Assertions.assertNotNull(projectParameter.getUpdateTime());
assertNotNull(projectParameter.getOperator());
assertNotNull(projectParameter.getUpdateTime());
}
@Test
public void testDeleteProjectParametersByCode() {
User loginUser = getGeneralUser();
// NO PERMISSION
when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(false);
Result result = projectParameterService.deleteProjectParametersByCode(loginUser, projectCode, 1);
assertNull(result.getData());
assertNull(result.getCode());
assertNull(result.getMsg());
// PROJECT_PARAMETER_NOT_EXISTS
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
Mockito.when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
when(projectService.hasProjectAndWritePerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class)))
.thenReturn(true);
Mockito.when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(null);
Result result = projectParameterService.deleteProjectParametersByCode(loginUser, projectCode, 1);
Assertions.assertEquals(Status.PROJECT_PARAMETER_NOT_EXISTS.getCode(), result.getCode());
when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(null);
result = projectParameterService.deleteProjectParametersByCode(loginUser, projectCode, 1);
assertEquals(Status.PROJECT_PARAMETER_NOT_EXISTS.getCode(), result.getCode());
// DATABASE OPERATION ERROR
when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(getProjectParameter());
when(projectParameterMapper.deleteById(Mockito.anyInt())).thenReturn(-1);
result = projectParameterService.deleteProjectParametersByCode(loginUser, projectCode, 1);
assertEquals(Status.DELETE_PROJECT_PARAMETER_ERROR.getCode(), result.getCode());
// SUCCESS
Mockito.when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(getProjectParameter());
Mockito.when(projectParameterMapper.deleteById(Mockito.anyInt())).thenReturn(1);
when(projectParameterMapper.deleteById(Mockito.anyInt())).thenReturn(1);
result = projectParameterService.deleteProjectParametersByCode(loginUser, projectCode, 1);
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode());
assertEquals(Status.SUCCESS.getCode(), result.getCode());
}
@Test
public void testQueryProjectParameterByCode() {
User loginUser = getGeneralUser();
// NO PERMISSION
when(projectService.hasProjectAndPerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class),
Mockito.any()))
.thenReturn(false);
Result result = projectParameterService.queryProjectParameterByCode(loginUser, projectCode, 1);
assertNull(result.getData());
assertNull(result.getCode());
assertNull(result.getMsg());
// PROJECT_PARAMETER_NOT_EXISTS
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
Mockito.when(projectService.hasProjectAndPerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class),
when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode));
when(projectService.hasProjectAndPerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class),
Mockito.any())).thenReturn(true);
Mockito.when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(null);
Result result = projectParameterService.queryProjectParameterByCode(loginUser, projectCode, 1);
Assertions.assertEquals(Status.PROJECT_PARAMETER_NOT_EXISTS.getCode(), result.getCode());
when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(null);
result = projectParameterService.queryProjectParameterByCode(loginUser, projectCode, 1);
assertEquals(Status.PROJECT_PARAMETER_NOT_EXISTS.getCode(), result.getCode());
// SUCCESS
Mockito.when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(getProjectParameter());
when(projectParameterMapper.queryByCode(Mockito.anyLong())).thenReturn(getProjectParameter());
result = projectParameterService.queryProjectParameterByCode(loginUser, projectCode, 1);
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode());
assertEquals(Status.SUCCESS.getCode(), result.getCode());
}
@Test
public void testQueryProjectParameterListPaging() {
User loginUser = getGeneralUser();
Integer pageSize = 10;
Integer pageNo = 1;
// NO PERMISSION
when(projectService.hasProjectAndPerm(Mockito.any(), Mockito.any(), Mockito.any(Result.class),
Mockito.any()))
.thenReturn(false);
Result result =
projectParameterService.queryProjectParameterListPaging(loginUser, projectCode, pageSize, pageNo, null);
assertNull(result.getData());
assertNull(result.getCode());
assertNull(result.getMsg());
// SUCCESS
when(projectService.hasProjectAndPerm(any(), any(), any(Result.class), any()))
.thenReturn(true);
Page<ProjectParameter> page = new Page<>(pageNo, pageSize);
page.setRecords(Collections.singletonList(getProjectParameter()));
when(projectParameterMapper.queryProjectParameterListPaging(any(), anyLong(), any(), any())).thenReturn(page);
result = projectParameterService.queryProjectParameterListPaging(loginUser, projectCode, pageSize, pageNo,
null);
assertEquals(Status.SUCCESS.getCode(), result.getCode());
}
private User getGeneralUser() {
User loginUser = new User();
loginUser.setUserType(UserType.GENERAL_USER);
loginUser.setUserName("userName");
loginUser.setId(1);
return loginUser;
@Test
public void testBatchDeleteProjectParametersByCodes() {
User loginUser = getGeneralUser();
Result result = projectParameterService.batchDeleteProjectParametersByCodes(loginUser, projectCode, "");
assertEquals(Status.PROJECT_PARAMETER_CODE_EMPTY.getCode(), result.getCode());
when(projectParameterMapper.queryByCodes(any())).thenReturn(Collections.singletonList(getProjectParameter()));
AssertionsHelper.assertThrowsServiceException(Status.PROJECT_PARAMETER_NOT_EXISTS,
() -> projectParameterService.batchDeleteProjectParametersByCodes(loginUser, projectCode, "1,2"));
projectParameterService.batchDeleteProjectParametersByCodes(loginUser, projectCode, "1");
}
private Project getProject(long projectCode) {

Loading…
Cancel
Save