
- 优化了多个控制器、服务和 mapper 类的实现 - 删除了不必要的代码和注释 - 改进了查询逻辑,使用 LambdaQueryWrapper替代复杂的 XML 配置- 统一了分页查询的实现方式 - 删除了未使用的 AttachmentVo 类 -调整了 Event 和 Product 相关的 mapper 配置
136 lines
5.4 KiB
Java
136 lines
5.4 KiB
Java
package com.zsc.edu.gateway.rest;
|
|
|
|
import com.zsc.edu.gateway.MockMvcConfigBase;
|
|
import com.zsc.edu.gateway.domain.RoleBuilder;
|
|
import com.zsc.edu.gateway.modules.system.controller.RoleController;
|
|
import com.zsc.edu.gateway.modules.system.dto.RoleDto;
|
|
import com.zsc.edu.gateway.modules.system.entity.Role;
|
|
import com.zsc.edu.gateway.modules.system.service.RoleService;
|
|
import org.assertj.core.util.Lists;
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
import org.springframework.http.MediaType;
|
|
|
|
import java.util.List;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
|
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.patch;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
@WebMvcTest(RoleController.class)
|
|
public class RoleControllerTest extends MockMvcConfigBase {
|
|
private static Role role1;
|
|
|
|
private static Role role2;
|
|
|
|
@MockBean
|
|
private RoleService service;
|
|
|
|
@BeforeAll
|
|
static void beforeAll() {
|
|
role1 = RoleBuilder.aRole().name("管理员").build();
|
|
role2 = RoleBuilder.aRole().name("普通用户").build();
|
|
}
|
|
|
|
@Test
|
|
void create() throws Exception{
|
|
RoleDto dto = new RoleDto();
|
|
dto.name = role1.getName();
|
|
when(service.create(any())).thenReturn(role1);
|
|
mockMvc.perform(post("/api/rest/role")
|
|
.with(csrf().asHeader())
|
|
.with(user(userDetails))
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content(objectMapper.writeValueAsString(dto))
|
|
).andExpect(status().isOk()).andDo(print());
|
|
verify(service).create(any());
|
|
}
|
|
|
|
@Test
|
|
void list() throws Exception {
|
|
List<Role> Roles = Lists.newArrayList(role1, role2);
|
|
when(service.list()).thenReturn(Roles);
|
|
mockMvc.perform(get("/api/rest/role").with(user(userDetails)))
|
|
.andExpect(status().isOk())
|
|
.andDo(print());
|
|
verify(service).list();
|
|
}
|
|
|
|
@Test
|
|
void update() throws Exception {
|
|
RoleDto dto = new RoleDto();
|
|
dto.name = role1.getName();
|
|
when(service.update(any(), any())).thenReturn(true);
|
|
mockMvc.perform(patch("/api/rest/role/{id}", role1.id)
|
|
.with(csrf().asHeader())
|
|
.with(user(userDetails))
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content(objectMapper.writeValueAsString(dto))
|
|
)
|
|
.andExpect(status().isOk()).andDo(print());
|
|
verify(service).update(any(), any());
|
|
}
|
|
// @Test
|
|
// void update() throws Exception {
|
|
// AuthorityDto dto = new AuthorityDto();
|
|
// dto.name = aut1.getName();
|
|
// when(service.update((AuthorityDto) any(), any())).thenReturn(true);
|
|
// mockMvc.perform(patch("/api/rest/auth/{id}", aut1.id)
|
|
// .with(csrf().asHeader())
|
|
// .with(user(userDetails))
|
|
// .contentType(MediaType.APPLICATION_JSON)
|
|
// .content(objectMapper.writeValueAsString(dto))
|
|
// )
|
|
// .andExpect(status().isOk()).andDo(print());
|
|
// verify(service).update((AuthorityDto) any(), any());
|
|
// }
|
|
//
|
|
// @Test
|
|
// void remove() throws Exception {
|
|
// when(service.deleteById(anyLong())).thenReturn(lampGroup1);
|
|
// mockMvc.perform(delete("/api/rest/lamp-group/{id}", lampGroup1.id)
|
|
// .with(csrf().asHeader())
|
|
// .with(user(userDetails))
|
|
// )
|
|
// .andExpect(status().isOk())
|
|
// .andDo(document("lamp-group/delete"));
|
|
// verify(service).deleteById(anyLong());
|
|
// }
|
|
//
|
|
// @Test
|
|
// void detail() throws Exception {
|
|
// when(service.detail(anyLong())).thenReturn(lampGroup1);
|
|
// mockMvc.perform(get("/api/rest/lamp-group/{id}", lampGroup1.id)
|
|
// .with(user(userDetails))
|
|
// )
|
|
// .andExpect(status().isOk())
|
|
// .andDo(document("lamp-group/detail"));
|
|
// verify(service).detail(anyLong());
|
|
// }
|
|
//
|
|
// @Test
|
|
// void batchDelete() throws Exception {
|
|
// Long[] ids = new Long[]{1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l};
|
|
// when(service.batchDelete(any())).thenReturn(ids);
|
|
// mockMvc.perform(post("/api/rest/lamp-group/batchRemove")
|
|
// .contentType(MediaType.APPLICATION_JSON)
|
|
// .content(objectMapper.writeValueAsString(ids))
|
|
// .with(csrf().asHeader())
|
|
// .with(user(userDetails))
|
|
// )
|
|
// .andExpect(status().isOk())
|
|
// .andDo(document("lamp-group/batchRemove"));
|
|
// verify(service).batchDelete(any());
|
|
// }
|
|
|
|
}
|