test: 邮件模板测试
This commit is contained in:
parent
05cf98ce27
commit
9e993b2b04
|
@ -28,10 +28,6 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<!-- asp 切面 -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
|
|
|
@ -14,9 +14,7 @@ import io.swagger.v3.oas.annotations.Operation;
|
|||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -34,12 +32,15 @@ import java.util.Map;
|
|||
@RequestMapping("api/emailTemplate")
|
||||
public class EmailTemplateController {
|
||||
|
||||
@Autowired
|
||||
private EmailTemplateService emailTemplateService;
|
||||
private final EmailTemplateService emailTemplateService;
|
||||
|
||||
public EmailTemplateController(EmailTemplateService emailTemplateService) {
|
||||
this.emailTemplateService = emailTemplateService;
|
||||
}
|
||||
|
||||
@Operation(summary = "分页查询邮件模板", description = "分页查询邮件模板")
|
||||
@GetMapping("getEmailTemplateList/{page}/{limit}")
|
||||
public Mono<Result<PageResult<EmailTemplateVo>>> getEmailTemplateList(
|
||||
public Result<PageResult<EmailTemplateVo>> getEmailTemplateList(
|
||||
@Parameter(name = "page", description = "当前页", required = true)
|
||||
@PathVariable("page") Integer page,
|
||||
@Parameter(name = "limit", description = "每页记录数", required = true)
|
||||
|
@ -47,34 +48,34 @@ public class EmailTemplateController {
|
|||
EmailTemplateDto dto) {
|
||||
Page<EmailTemplate> pageParams = new Page<>(page, limit);
|
||||
PageResult<EmailTemplateVo> pageResult = emailTemplateService.getEmailTemplateList(pageParams, dto);
|
||||
return Mono.just(Result.success(pageResult));
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取邮件模板类型字段", description = "获取邮件模板类型字段")
|
||||
@GetMapping("getEmailTypes")
|
||||
public Mono<Result<List<Map<String, String>>>> getEmailTypes() {
|
||||
public Result<List<Map<String, String>>> getEmailTypes() {
|
||||
List<Map<String, String>> list = emailTemplateService.getEmailTypes();
|
||||
return Mono.just(Result.success(list));
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加邮件模板", description = "添加邮件模板")
|
||||
@PostMapping("addEmailTemplate")
|
||||
public Mono<Result<String>> addEmailTemplate(@Valid @RequestBody EmailTemplateAddDto dto) {
|
||||
public Result<String> addEmailTemplate(@Valid @RequestBody EmailTemplateAddDto dto) {
|
||||
emailTemplateService.addEmailTemplate(dto);
|
||||
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
|
||||
return Result.success(ResultCodeEnum.ADD_SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新邮件模板", description = "更新邮件模板")
|
||||
@PutMapping("updateEmailTemplate")
|
||||
public Mono<Result<String>> updateEmailTemplate(@Valid @RequestBody EmailTemplateUpdateDto dto) {
|
||||
public Result<String> updateEmailTemplate(@Valid @RequestBody EmailTemplateUpdateDto dto) {
|
||||
emailTemplateService.updateEmailTemplate(dto);
|
||||
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
|
||||
return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除邮件模板", description = "删除邮件模板")
|
||||
@DeleteMapping("deleteEmailTemplate")
|
||||
public Mono<Result<String>> deleteEmailTemplate(@RequestBody List<Long> ids) {
|
||||
public Result<String> deleteEmailTemplate(@RequestBody List<Long> ids) {
|
||||
emailTemplateService.deleteEmailTemplate(ids);
|
||||
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));
|
||||
return Result.success(ResultCodeEnum.DELETE_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,8 +46,8 @@
|
|||
<if test="dto.type != null and dto.type != ''">
|
||||
and template.type like CONCAT('%',#{dto.type},'%')
|
||||
</if>
|
||||
<if test="dto.isDefault != null and dto.isDefault != ''">
|
||||
and template.isDefault = #{dto.isDefault}
|
||||
<if test="dto.isDefault != null and dto.type != ''">
|
||||
and template.is_default = #{dto.isDefault}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package cn.bunny.services.config;
|
||||
|
||||
import cn.bunny.services.utils.TokenUtilsTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@TestConfiguration
|
||||
public class WebConfig {
|
||||
@Value("${server.port}")
|
||||
private String port;
|
||||
|
||||
@Autowired
|
||||
private TokenUtilsTest tokenUtils;
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(RestTemplateBuilder builder) {
|
||||
String token = tokenUtils.getToken();
|
||||
return builder.rootUri("http://localhost:" + port)
|
||||
.defaultHeader("token", token)
|
||||
.defaultHeader("Content-Type", "application/json")
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -1,36 +1,215 @@
|
|||
package cn.bunny.services.controller.configuration;
|
||||
|
||||
import cn.bunny.dao.dto.system.email.template.EmailTemplateAddDto;
|
||||
import cn.bunny.dao.dto.system.email.template.EmailTemplateUpdateDto;
|
||||
import cn.bunny.dao.enums.EmailTemplateEnums;
|
||||
import cn.bunny.dao.vo.result.PageResult;
|
||||
import cn.bunny.dao.vo.result.Result;
|
||||
import cn.bunny.dao.vo.system.email.EmailTemplateVo;
|
||||
import cn.bunny.services.utils.TokenUtilsTest;
|
||||
import cn.hutool.crypto.digest.MD5;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
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.client.RestTemplate;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
@WebAppConfiguration
|
||||
class EmailTemplateControllerTest {
|
||||
private static final String prefix = "/api/emailTemplate";
|
||||
|
||||
private String token;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
@Autowired
|
||||
private TokenUtilsTest tokenUtilsTest;
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
token = tokenUtilsTest.getToken();
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
|
||||
.apply(SecurityMockMvcConfigurers.springSecurity())
|
||||
.build();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getEmailTemplateList() {
|
||||
void getEmailTemplateList() throws Exception {
|
||||
String api = prefix + "/getEmailTemplateList/1/10";
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.get(api)
|
||||
.header("token", token)
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
.param("type", "code"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(result -> {
|
||||
String contentAsString = result.getResponse().getContentAsString();
|
||||
JSONObject jsonObject = JSONObject.parseObject(contentAsString);
|
||||
|
||||
if (jsonObject == null) {
|
||||
throw new Exception(contentAsString);
|
||||
}
|
||||
System.out.println(jsonObject);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void getEmailTypes() {
|
||||
void getEmailTypes() throws Exception {
|
||||
String api = prefix + "/getEmailTypes";
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get(api).header("token", token))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(result -> {
|
||||
String contentAsString = result.getResponse().getContentAsString();
|
||||
JSONObject jsonObject = JSONObject.parseObject(contentAsString);
|
||||
|
||||
if (jsonObject == null) {
|
||||
throw new Exception(contentAsString);
|
||||
}
|
||||
|
||||
System.out.println(jsonObject);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void addEmailTemplate() {
|
||||
void addEmailTemplate() throws Exception {
|
||||
String api = prefix + "/addEmailTemplate";
|
||||
|
||||
EmailTemplateAddDto dto = EmailTemplateAddDto.builder()
|
||||
.emailUser(2L)
|
||||
.body("哈哈哈")
|
||||
.templateName("测试")
|
||||
.subject("test")
|
||||
.type(EmailTemplateEnums.NOTIFICATION.getType())
|
||||
.isDefault(false)
|
||||
.build();
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.post(api)
|
||||
.header("token", token)
|
||||
.content(JSONObject.toJSONString(dto))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(result -> {
|
||||
String contentAsString = result.getResponse().getContentAsString();
|
||||
JSONObject jsonObject = JSONObject.parseObject(contentAsString);
|
||||
|
||||
System.out.println(jsonObject);
|
||||
});
|
||||
|
||||
getEmailTemplateList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateEmailTemplate() {
|
||||
void updateEmailTemplate() throws Exception {
|
||||
String api = prefix + "/updateEmailTemplate";
|
||||
String restTemplateApi = prefix + "/getEmailTemplateList/1/10";
|
||||
|
||||
String url = UriComponentsBuilder.fromUriString(restTemplateApi)
|
||||
.queryParam("subject", "test")
|
||||
.build()
|
||||
.toUriString();
|
||||
|
||||
Result<PageResult<EmailTemplateVo>> result = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
new HttpEntity<>(null),
|
||||
new ParameterizedTypeReference<Result<PageResult<EmailTemplateVo>>>() {
|
||||
}
|
||||
).getBody();
|
||||
|
||||
if (result == null) throw new Exception();
|
||||
if (!result.getCode().equals(200)) throw new Exception(result.getMessage());
|
||||
if (result.getData().getList().isEmpty()) throw new Exception("没有测试数据");
|
||||
|
||||
EmailTemplateVo emailTemplateVo = result.getData().getList().get(0);
|
||||
EmailTemplateUpdateDto emailTemplateUpdateDto = new EmailTemplateUpdateDto();
|
||||
BeanUtils.copyProperties(emailTemplateVo, emailTemplateUpdateDto);
|
||||
|
||||
System.out.println(emailTemplateUpdateDto);
|
||||
|
||||
emailTemplateUpdateDto.setBody(MD5.create().digestHex16(LocalDateTime.now().toString()));
|
||||
mockMvc.perform(MockMvcRequestBuilders.put(api)
|
||||
.header("token", token)
|
||||
.content(JSON.toJSONString(emailTemplateUpdateDto))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(result1 -> {
|
||||
String contentAsString = result1.getResponse().getContentAsString();
|
||||
var pageResultResult = JSONObject.parseObject(contentAsString, new TypeReference<Result<PageResult<EmailTemplateVo>>>() {
|
||||
});
|
||||
if (pageResultResult == null) throw new Exception(contentAsString);
|
||||
if (!pageResultResult.getCode().equals(200)) throw new Exception(pageResultResult.getMessage());
|
||||
|
||||
System.out.println(pageResultResult);
|
||||
});
|
||||
|
||||
result = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
new HttpEntity<>(null),
|
||||
new ParameterizedTypeReference<Result<PageResult<EmailTemplateVo>>>() {
|
||||
}
|
||||
).getBody();
|
||||
|
||||
if (result == null) throw new Exception();
|
||||
if (!result.getCode().equals(200)) throw new Exception(result.getMessage());
|
||||
emailTemplateVo = result.getData().getList().get(0);
|
||||
if (!emailTemplateVo.getBody().equals(emailTemplateUpdateDto.getBody())) {
|
||||
throw new Exception(emailTemplateUpdateDto.getBody());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteEmailTemplate() {
|
||||
void deleteEmailTemplate() throws Exception {
|
||||
String api = prefix + "/deleteEmailTemplate";
|
||||
List<Long> ids = new ArrayList<>();
|
||||
ids.add(1905180657917157378L);
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.delete(api)
|
||||
.header("token", token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(JSON.toJSONString(ids)))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(result -> {
|
||||
String contentAsString = result.getResponse().getContentAsString();
|
||||
JSONObject jsonObject = JSONObject.parseObject(contentAsString);
|
||||
System.out.println(jsonObject);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -18,7 +18,10 @@ import org.junit.jupiter.api.Test;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
@ -98,16 +101,11 @@ class UserLoginLogControllerTest {
|
|||
Map<String, String> params = JSON.parseObject(JSON.toJSONString(dto), new TypeReference<>() {
|
||||
});
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("token", token);
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers);
|
||||
|
||||
// 发送请求
|
||||
ResponseEntity<Result<PageResult<UserLoginLogVo>>> response = restTemplate.exchange(
|
||||
"http://localhost:7070" + prefix + "/getUserLoginLogList/1/10",
|
||||
prefix + "/getUserLoginLogList/1/10",
|
||||
HttpMethod.GET,
|
||||
request,
|
||||
new HttpEntity<>(params),
|
||||
new ParameterizedTypeReference<>() {
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue