refactor: 单元测试补充校验方法

This commit is contained in:
fit2-zhao 2023-07-06 10:55:55 +08:00 committed by fit2-zhao
parent cf00ad8e20
commit 9dace68c35
3 changed files with 32 additions and 4 deletions

View File

@ -4,6 +4,8 @@ import com.jayway.jsonpath.JsonPath;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.OperationLogExample;
import io.metersphere.system.mapper.OperationLogMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
@ -32,6 +34,8 @@ public abstract class BaseTest {
private MockMvc mockMvc;
protected static String sessionId;
protected static String csrfToken;
@Resource
private OperationLogMapper operationLogMapper;
/**
* 可以重写该方法定义 BASE_PATH
@ -69,8 +73,8 @@ public abstract class BaseTest {
}
protected ResultActions requestPost(String url, Object param, Object... uriVariables) throws Exception {
return mockMvc.perform(getPostRequestBuilder(url, param, uriVariables))
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
return mockMvc.perform(getPostRequestBuilder(url, param, uriVariables))
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
protected MvcResult requestPostAndReturn(String url, Object... uriVariables) throws Exception {
@ -102,7 +106,7 @@ public abstract class BaseTest {
.andExpect(status().isOk());
}
protected MvcResult requestPostWithOkAndReturn(String url, Object param, Object... uriVariables) throws Exception {
protected MvcResult requestPostWithOkAndReturn(String url, Object param, Object... uriVariables) throws Exception {
return this.requestPostWithOk(url, param, uriVariables).andReturn();
}
@ -126,4 +130,16 @@ public abstract class BaseTest {
pager.setList(list);
return pager;
}
protected void checkLog(String resourceId, String type) throws Exception {
OperationLogExample example = new OperationLogExample();
example.createCriteria().andSourceIdEqualTo(resourceId).andTypeEqualTo(type);
operationLogMapper.selectByExample(example).stream()
.filter(operationLog -> operationLog.getSourceId().equals(resourceId))
.filter(operationLog -> operationLog.getType().equals(type))
.filter(operationLog -> StringUtils.isNotBlank(operationLog.getProjectId()))
.filter(operationLog -> StringUtils.isNotBlank(operationLog.getModule()))
.findFirst()
.orElseThrow(() -> new Exception("日志不存在,请补充操作日志"));
}
}

View File

@ -10,6 +10,7 @@ import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.UserRoleRelation;
@ -167,6 +168,9 @@ public class SystemProjectControllerTests extends BaseTest {
projectExample.createCriteria().andOrganizationIdEqualTo(project.getOrganizationId()).andNameEqualTo(project.getName());
List<Project> projects = projectMapper.selectByExample(projectExample);
projectId = result.getId();
// 校验日志
checkLog(projectId, OperationLogType.ADD.name());
this.compareProjectDTO(projects.get(0), result);
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());

View File

@ -6,6 +6,7 @@ import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.BasePageRequest;
import io.metersphere.sdk.dto.ExcelParseDTO;
import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
@ -90,8 +91,15 @@ public class UserControllerTests extends BaseTest {
}
//成功入库的用户保存内存中其他用例会使用到
private void addUser2List(MvcResult mvcResult) {
private void addUser2List(MvcResult mvcResult){
UserBatchCreateDTO userMaintainRequest = UserTestUtils.parseObjectFromMvcResult(mvcResult, UserBatchCreateDTO.class);
userMaintainRequest.getUserInfoList().forEach(item ->{
try {
checkLog(item.getId(), OperationLogType.ADD.name());
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//返回值不为空
Assertions.assertNotNull(userMaintainRequest);
USER_LIST.addAll(userMaintainRequest.getUserInfoList());