From 560ba612d7b24f78fb3a4171acfc1dea1b57c10b Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Tue, 25 Mar 2025 13:45:15 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/WebConfigurationDto.java | 2 - service/pom.xml | 21 +- .../ConfigurationController.java | 5 +- .../controller/DeptControllerTest.java | 26 +++ .../EmailTemplateControllerTest.java | 10 + .../ConfigurationControllerTest.java | 198 ++++++++++++++++++ 6 files changed, 250 insertions(+), 12 deletions(-) create mode 100644 service/src/test/java/cn/bunny/services/controller/DeptControllerTest.java create mode 100644 service/src/test/java/cn/bunny/services/controller/EmailTemplateControllerTest.java create mode 100644 service/src/test/java/cn/bunny/services/controller/configuration/ConfigurationControllerTest.java diff --git a/dao/src/main/java/cn/bunny/dao/dto/system/configuration/WebConfigurationDto.java b/dao/src/main/java/cn/bunny/dao/dto/system/configuration/WebConfigurationDto.java index ded5a4c..160f483 100644 --- a/dao/src/main/java/cn/bunny/dao/dto/system/configuration/WebConfigurationDto.java +++ b/dao/src/main/java/cn/bunny/dao/dto/system/configuration/WebConfigurationDto.java @@ -7,11 +7,9 @@ import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Pattern; import lombok.Getter; import lombok.Setter; -import lombok.experimental.Accessors; @Getter @Setter -@Accessors(chain = true) @Schema(name = "WebConfiguration对象", title = "前端配置选项", description = "前端配置选项") public class WebConfigurationDto { diff --git a/service/pom.xml b/service/pom.xml index 2d8c695..35e6963 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -63,13 +63,11 @@ org.springframework.security spring-security-test - - - - - - - + + org.seleniumhq.selenium + selenium-java + 4.30.0 + org.springframework.boot @@ -167,6 +165,15 @@ com.fasterxml.jackson.datatype jackson-datatype-jsr310 + + org.springframework.boot + spring-boot-starter-webflux + + + io.projectreactor + reactor-test + test + diff --git a/service/src/main/java/cn/bunny/services/controller/configuration/ConfigurationController.java b/service/src/main/java/cn/bunny/services/controller/configuration/ConfigurationController.java index 317d997..ab6a110 100644 --- a/service/src/main/java/cn/bunny/services/controller/configuration/ConfigurationController.java +++ b/service/src/main/java/cn/bunny/services/controller/configuration/ConfigurationController.java @@ -10,7 +10,6 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import reactor.core.publisher.Mono; @Tag(name = "系统配置", description = "系统配置相关接口") @RestController @@ -35,9 +34,9 @@ public class ConfigurationController { @Operation(summary = "更新web配置文件", description = "更新web配置文件") @PutMapping("updateWebConfiguration") - public Mono> updateWebConfiguration(@Valid @RequestBody WebConfigurationDto dto) { + public Result updateWebConfiguration(@Valid @RequestBody WebConfigurationDto dto) { configurationService.updateWebConfiguration(dto); - return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS)); + return Result.success(ResultCodeEnum.UPDATE_SUCCESS); } } diff --git a/service/src/test/java/cn/bunny/services/controller/DeptControllerTest.java b/service/src/test/java/cn/bunny/services/controller/DeptControllerTest.java new file mode 100644 index 0000000..d753ad7 --- /dev/null +++ b/service/src/test/java/cn/bunny/services/controller/DeptControllerTest.java @@ -0,0 +1,26 @@ +package cn.bunny.services.controller; + +import org.junit.jupiter.api.Test; + +class DeptControllerTest { + + @Test + void getDeptList() { + } + + @Test + void getAllDeptList() { + } + + @Test + void addDept() { + } + + @Test + void updateDept() { + } + + @Test + void deleteDept() { + } +} \ No newline at end of file diff --git a/service/src/test/java/cn/bunny/services/controller/EmailTemplateControllerTest.java b/service/src/test/java/cn/bunny/services/controller/EmailTemplateControllerTest.java new file mode 100644 index 0000000..63a2e24 --- /dev/null +++ b/service/src/test/java/cn/bunny/services/controller/EmailTemplateControllerTest.java @@ -0,0 +1,10 @@ +package cn.bunny.services.controller; + +import org.junit.jupiter.api.BeforeEach; + +class EmailTemplateControllerTest { + + @BeforeEach + void setUp() { + } +} \ No newline at end of file diff --git a/service/src/test/java/cn/bunny/services/controller/configuration/ConfigurationControllerTest.java b/service/src/test/java/cn/bunny/services/controller/configuration/ConfigurationControllerTest.java new file mode 100644 index 0000000..02a2e9c --- /dev/null +++ b/service/src/test/java/cn/bunny/services/controller/configuration/ConfigurationControllerTest.java @@ -0,0 +1,198 @@ +package cn.bunny.services.controller.configuration; + +import cn.bunny.dao.dto.system.configuration.WebConfigurationDto; +import cn.bunny.dao.entity.configuration.WebConfiguration; +import cn.bunny.dao.entity.system.AdminUser; +import cn.bunny.dao.vo.result.Result; +import cn.bunny.dao.vo.system.user.LoginVo; +import cn.bunny.services.mapper.UserMapper; +import cn.bunny.services.utils.UserUtil; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.TypeReference; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.chrome.ChromeDriver; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@WebAppConfiguration +// @AutoConfigureMockMvc +class ConfigurationControllerTest { + private static final String prefix = "/api/config"; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Autowired + private UserUtil userUtil; + + @Autowired + private UserMapper userMapper; + + private String token; + + private MockMvc mockMvc; + + private ChromeDriver chromeDriver; + + @BeforeEach + void setUpMockMvc() { + AdminUser adminUser = userMapper.selectOne(Wrappers.lambdaQuery().eq(AdminUser::getUsername, "Administrator")); + adminUser.setPassword("admin123"); + LoginVo loginVo = userUtil.buildLoginUserVo(adminUser, 7); + token = loginVo.getToken(); + + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .apply(SecurityMockMvcConfigurers.springSecurity()) + .build(); + + chromeDriver = new ChromeDriver(); + } + + @AfterEach + void tearDown() { + chromeDriver.quit(); + } + + @Test + // @WithMockUser(username = "Administrator", password = "admin123", roles = "admin") + // @WithUserDetails("Administrator") + void webConfig() throws Exception { + mockMvc.perform(MockMvcRequestBuilders + .get(prefix + "/noAuth/webConfig") + .header("token", token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(result -> { + MockHttpServletResponse response = result.getResponse(); + String contentAsString = response.getContentAsString(); + + WebConfiguration webConfiguration = JSON.parseObject(contentAsString, WebConfiguration.class); + + if (!webConfiguration.getTitle().equals("BunnyAdmin")) { + throw new Exception(); + } + + + System.out.println(webConfiguration); + }); + } + + @Test + void getWebConfig() throws Exception { + mockMvc.perform(MockMvcRequestBuilders + .request(HttpMethod.GET, prefix + "/getWebConfig") + .header("token", token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(result -> { + MockHttpServletResponse response = result.getResponse(); + String contentAsString = response.getContentAsString(); + + Result webConfigurationResult = JSON.parseObject(contentAsString, new TypeReference<>() { + }); + + if (!webConfigurationResult.getCode().equals(200)) { + throw new Exception(); + } + + if (!webConfigurationResult.getData().getShowModel().equals("smart")) { + throw new Exception(); + } + System.out.println(contentAsString); + }); + } + + @Test + void updateWebConfiguration() throws Exception { + AtomicReference webConfigurationDto = new AtomicReference<>(); + String testTitle = "修改的之"; + + // 获取原本的配置信息 + mockMvc.perform(MockMvcRequestBuilders + .request(HttpMethod.GET, prefix + "/getWebConfig") + .header("token", token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(result -> { + MockHttpServletResponse response = result.getResponse(); + String contentAsString = response.getContentAsString(); + + System.out.println(contentAsString); + + Result webConfigurationResult = JSON.parseObject(contentAsString, new TypeReference<>() { + }); + + WebConfigurationDto dto = new WebConfigurationDto(); + BeanUtils.copyProperties(webConfigurationResult.getData(), dto); + + webConfigurationDto.set(dto); + }); + + // 修改原本的测试内容 + webConfigurationDto.get().setTitle(testTitle); + + // 测试修改方法 + mockMvc.perform(MockMvcRequestBuilders.put(prefix + "/updateWebConfiguration") + .contentType(MediaType.APPLICATION_JSON) + .content(JSON.toJSONString(webConfigurationDto.get())) + .header("token", token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(result -> { + String contentAsString = result.getResponse().getContentAsString(); + + System.out.println(contentAsString); + + Result stringResult = JSON.parseObject(contentAsString, new TypeReference<>() { + }); + + if (!stringResult.getCode().equals(200)) { + throw new Exception(); + } + }); + + // 验证是否修改成功 + mockMvc.perform(MockMvcRequestBuilders + .request(HttpMethod.GET, prefix + "/getWebConfig") + .header("token", token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(result -> { + MockHttpServletResponse response = result.getResponse(); + String contentAsString = response.getContentAsString(); + + Result webConfigurationResult = JSON.parseObject(contentAsString, new TypeReference<>() { + }); + + if (!webConfigurationResult.getCode().equals(200)) { + throw new Exception(); + } + + if (!webConfigurationResult.getData().getTitle().equals(testTitle)) { + throw new Exception(); + } + System.out.println(contentAsString); + }); + } + + @Test + void openChrome() throws InterruptedException { + chromeDriver.get("http://localhost:7000/"); + TimeUnit.MINUTES.sleep(100); + chromeDriver.manage().timeouts().implicitlyWait(Duration.of(1000L, TimeUnit.SECONDS.toChronoUnit())); + } +} \ No newline at end of file