build: user crud tests

This commit is contained in:
CaptainB 2023-06-01 09:57:43 +08:00 committed by 刘瑞斌
parent 74c20efdd2
commit c14a886873
3 changed files with 45 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.system.domain.User;
import io.metersphere.system.service.UserService;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -29,7 +30,12 @@ public class UserController {
@PostMapping("/add")
public boolean addUser(@Validated({Created.class}) @RequestBody UserDTO user) {
return userService.save(user);
return userService.add(user);
}
@PostMapping("/update")
public boolean updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) {
return userService.update(user);
}
@PostMapping("/batch-add2")

View File

@ -9,6 +9,7 @@ import io.metersphere.system.domain.UserExtend;
import io.metersphere.system.mapper.UserExtendMapper;
import io.metersphere.system.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
@ -28,7 +29,11 @@ public class UserService {
@Resource
private SqlSessionFactory sqlSessionFactory;
public boolean save(UserDTO entity) {
public boolean add(UserDTO entity) {
// todo 后台直接获取在线用户
entity.setCreateUser("admin");
entity.setCreateTime(System.currentTimeMillis());
entity.setUpdateTime(System.currentTimeMillis());
userMapper.insert(entity);
UserExtend userExtend = new UserExtend();
@ -37,6 +42,20 @@ public class UserService {
return true;
}
public boolean update(UserDTO entity) {
entity.setCreateUser(null);
entity.setCreateTime(null);
entity.setUpdateTime(System.currentTimeMillis());
userMapper.updateByPrimaryKeySelective(entity);
// 扩展属性按需更新
if (entity.getPlatformInfo() != null || StringUtils.isNotEmpty(entity.getSeleniumServer())) {
UserExtend userExtend = new UserExtend();
BeanUtils.copyBean(userExtend, entity);
userExtendMapper.updateByPrimaryKeySelective(userExtend);
}
return true;
}
public UserDTO getById(String id) {
return baseUserMapper.selectById(id);
}

View File

@ -32,12 +32,9 @@ public class UserControllerTests {
UserDTO user = new UserDTO();
user.setId("admin");
user.setName("admin");
user.setCreateUser("system");
user.setSource("LOCAL");
user.setEmail("bin@fit2cloud.com");
user.setStatus("enabled");
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
user.setSeleniumServer("http://localhost:4444");
@ -53,8 +50,6 @@ public class UserControllerTests {
public void testAddUserFailed() throws Exception {
UserDTO user = new UserDTO();
user.setId("admin2");
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
user.setSeleniumServer("http://localhost:4444");
@ -74,13 +69,30 @@ public class UserControllerTests {
.andExpect(jsonPath("$.data.id").value("admin"));
}
@Test
@Order(4)
public void testUpdateUser() throws Exception {
UserDTO user = new UserDTO();
user.setId("admin");
user.setName("Administrator");
mockMvc.perform(MockMvcRequestBuilders.post("/user/update")
.content(JSON.toJSONString(user))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
@Order(5)
public void testSelectAll() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/list-all"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.data[0].name").value("admin"));
.andExpect(jsonPath("$.data[0].id").value("admin"));
}
@ -92,12 +104,9 @@ public class UserControllerTests {
User user = new User();
user.setId("batch2_" + i);
user.setName("batch2_" + i);
user.setCreateUser("system");
user.setSource("LOCAL");
user.setEmail("bin@fit2cloud.com");
user.setStatus("enabled");
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
users.add(user);
}
@ -116,12 +125,9 @@ public class UserControllerTests {
User user = new User();
user.setId("batch3_" + i);
user.setName("batch3_" + i);
user.setCreateUser("system");
user.setSource("LOCAL");
user.setEmail("bin@fit2cloud.com");
user.setStatus("enabled");
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
users.add(user);
}