mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
完成 AliyunSmsClient 的单元测试
完成 YunpianSmsClient 的单元测试
This commit is contained in:
parent
71d2f74110
commit
a432b303e6
@ -24,6 +24,7 @@ import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.aliyuncs.profile.IClientProfile;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@ -53,7 +54,7 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||
/**
|
||||
* 阿里云客户端
|
||||
*/
|
||||
private volatile IAcsClient acsClient;
|
||||
private volatile IAcsClient client;
|
||||
|
||||
public AliyunSmsClient(SmsChannelProperties properties) {
|
||||
super(properties, new AliyunSmsCodeMapping());
|
||||
@ -64,7 +65,7 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||
@Override
|
||||
protected void doInit() {
|
||||
IClientProfile profile = DefaultProfile.getProfile(ENDPOINT, properties.getApiKey(), properties.getApiSecret());
|
||||
acsClient = new DefaultAcsClient(profile);
|
||||
client = new DefaultAcsClient(profile);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -81,13 +82,6 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||
return invoke(request, response -> new SmsSendRespDTO().setSerialNo(response.getBizId()));
|
||||
}
|
||||
|
||||
private static String formatResultMsg(ClientException ex) {
|
||||
if (StrUtil.isEmpty(ex.getErrorDescription())) {
|
||||
return ex.getMessage();
|
||||
}
|
||||
return ex.getErrMsg() + " => " + ex.getErrorDescription();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SmsReceiveRespDTO> doParseSmsReceiveStatus(String text) throws Throwable {
|
||||
List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
|
||||
@ -115,7 +109,8 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||
});
|
||||
}
|
||||
|
||||
private Integer convertSmsTemplateAuditStatus(Integer templateStatus) {
|
||||
@VisibleForTesting
|
||||
Integer convertSmsTemplateAuditStatus(Integer templateStatus) {
|
||||
switch (templateStatus) {
|
||||
case 0: return SmsTemplateAuditStatusEnum.CHECKING.getStatus();
|
||||
case 1: return SmsTemplateAuditStatusEnum.SUCCESS.getStatus();
|
||||
@ -124,10 +119,11 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||
}
|
||||
}
|
||||
|
||||
private <T extends AcsResponse, R> SmsCommonResult<R> invoke(AcsRequest<T> request, Function<T, R> responseConsumer) {
|
||||
@VisibleForTesting
|
||||
<T extends AcsResponse, R> SmsCommonResult<R> invoke(AcsRequest<T> request, Function<T, R> responseConsumer) {
|
||||
try {
|
||||
// 执行发送. 由于阿里云 sms 短信没有统一的 Response,但是有统一的 code、message、requestId 属性,所以只好反射
|
||||
T sendResult = acsClient.getAcsResponse(request);
|
||||
T sendResult = client.getAcsResponse(request);
|
||||
String code = (String) ReflectUtil.getFieldValue(sendResult, "code");
|
||||
String message = (String) ReflectUtil.getFieldValue(sendResult, "message");
|
||||
String requestId = (String) ReflectUtil.getFieldValue(sendResult, "requestId");
|
||||
@ -143,6 +139,13 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatResultMsg(ClientException ex) {
|
||||
if (StrUtil.isEmpty(ex.getErrorDescription())) {
|
||||
return ex.getErrMsg();
|
||||
}
|
||||
return ex.getErrMsg() + " => " + ex.getErrorDescription();
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信接收状态
|
||||
*
|
||||
|
@ -15,6 +15,7 @@ import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
||||
import cn.iocoder.dashboard.util.json.JsonUtils;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.yunpian.sdk.YunpianClient;
|
||||
import com.yunpian.sdk.constant.YunpianConstant;
|
||||
import com.yunpian.sdk.model.Result;
|
||||
@ -113,7 +114,8 @@ public class YunpianSmsClient extends AbstractSmsClient {
|
||||
});
|
||||
}
|
||||
|
||||
private Integer convertSmsTemplateAuditStatus(String checkStatus) {
|
||||
@VisibleForTesting
|
||||
Integer convertSmsTemplateAuditStatus(String checkStatus) {
|
||||
switch (checkStatus) {
|
||||
case "CHECKING": return SmsTemplateAuditStatusEnum.CHECKING.getStatus();
|
||||
case "SUCCESS": return SmsTemplateAuditStatusEnum.SUCCESS.getStatus();
|
||||
@ -122,7 +124,8 @@ public class YunpianSmsClient extends AbstractSmsClient {
|
||||
}
|
||||
}
|
||||
|
||||
private <T, R> SmsCommonResult<R> invoke(Supplier<Result<T>> requestConsumer, Function<T, R> responseConsumer) throws Throwable {
|
||||
@VisibleForTesting
|
||||
<T, R> SmsCommonResult<R> invoke(Supplier<Result<T>> requestConsumer, Function<T, R> responseConsumer) throws Throwable {
|
||||
// 执行请求
|
||||
Result<T> result = requestConsumer.get();
|
||||
if (result.getThrowable() != null) {
|
||||
|
@ -1,29 +1,101 @@
|
||||
package cn.iocoder.dashboard.framework.sms.core.client.impl.aliyun;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.iocoder.dashboard.BaseMockitoUnitTest;
|
||||
import cn.iocoder.dashboard.common.core.KeyValue;
|
||||
import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
||||
import cn.iocoder.dashboard.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
||||
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
||||
import cn.iocoder.dashboard.util.collection.MapUtils;
|
||||
import cn.iocoder.dashboard.util.date.DateUtils;
|
||||
import com.aliyuncs.AcsRequest;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateRequest;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static cn.iocoder.dashboard.framework.sms.core.enums.SmsFrameworkErrorCodeConstants.SMS_API_PARAM_ERROR;
|
||||
import static cn.iocoder.dashboard.util.RandomUtils.*;
|
||||
import static cn.iocoder.dashboard.util.json.JsonUtils.toJsonString;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@link AliyunSmsClient} 的单元测试
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class AliyunSmsClientTest {
|
||||
public class AliyunSmsClientTest extends BaseMockitoUnitTest {
|
||||
|
||||
private final SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey(randomString()) // 随机一个 apiKey,避免构建报错
|
||||
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错
|
||||
.setSignature("芋道源码");
|
||||
|
||||
@InjectMocks
|
||||
private final AliyunSmsClient smsClient = new AliyunSmsClient(null);
|
||||
private final AliyunSmsClient smsClient = new AliyunSmsClient(properties);
|
||||
|
||||
@Mock
|
||||
private IAcsClient client;
|
||||
|
||||
@Test
|
||||
void doInit() {
|
||||
public void testDoInit() {
|
||||
// 准备参数
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
smsClient.doInit();
|
||||
// 断言
|
||||
assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "acsClient"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doSendSms() {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testDoSendSms() throws ClientException {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
|
||||
// mock 方法
|
||||
SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("OK"));
|
||||
when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> {
|
||||
assertEquals(mobile, acsRequest.getPhoneNumbers());
|
||||
assertEquals(properties.getSignature(), acsRequest.getSignName());
|
||||
assertEquals(apiTemplateId, acsRequest.getTemplateCode());
|
||||
assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam());
|
||||
assertEquals(sendLogId.toString(), acsRequest.getOutId());
|
||||
return true;
|
||||
}))).thenReturn(response);
|
||||
|
||||
// 调用
|
||||
SmsCommonResult<SmsSendRespDTO> result = smsClient.doSendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertEquals(response.getCode(), result.getApiCode());
|
||||
assertEquals(response.getMessage(), result.getApiMsg());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
||||
assertEquals(response.getRequestId(), result.getApiRequestId());
|
||||
// 断言结果
|
||||
assertEquals(response.getBizId(), result.getData().getSerialNo());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -57,4 +129,96 @@ public class AliyunSmsClientTest {
|
||||
assertEquals(67890L, statuses.get(0).getLogId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoGetSmsTemplate() throws ClientException {
|
||||
// 准备参数
|
||||
String apiTemplateId = randomString();
|
||||
// mock 方法
|
||||
QuerySmsTemplateResponse response = randomPojo(QuerySmsTemplateResponse.class, o -> {
|
||||
o.setCode("OK");
|
||||
o.setTemplateStatus(1); // 设置模板通过
|
||||
});
|
||||
when(client.getAcsResponse(argThat((ArgumentMatcher<QuerySmsTemplateRequest>) acsRequest -> {
|
||||
assertEquals(apiTemplateId, acsRequest.getTemplateCode());
|
||||
return true;
|
||||
}))).thenReturn(response);
|
||||
|
||||
// 调用
|
||||
SmsCommonResult<SmsTemplateRespDTO> result = smsClient.doGetSmsTemplate(apiTemplateId);
|
||||
// 断言
|
||||
assertEquals(response.getCode(), result.getApiCode());
|
||||
assertEquals(response.getMessage(), result.getApiMsg());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
||||
assertEquals(response.getRequestId(), result.getApiRequestId());
|
||||
// 断言结果
|
||||
assertEquals(response.getTemplateCode(), result.getData().getId());
|
||||
assertEquals(response.getTemplateContent(), result.getData().getContent());
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getData().getAuditStatus());
|
||||
assertEquals(response.getReason(), result.getData().getAuditReason());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertSmsTemplateAuditStatus() {
|
||||
assertEquals(SmsTemplateAuditStatusEnum.CHECKING.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus(0));
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus(1));
|
||||
assertEquals(SmsTemplateAuditStatusEnum.FAIL.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus(2));
|
||||
assertThrows(IllegalArgumentException.class, () -> smsClient.convertSmsTemplateAuditStatus(3),
|
||||
"未知审核状态(3)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testInvoke_throwable() throws ClientException {
|
||||
// 准备参数
|
||||
QuerySmsTemplateRequest request = new QuerySmsTemplateRequest();
|
||||
// mock 方法
|
||||
ClientException ex = new ClientException("isv.INVALID_PARAMETERS", "参数不正确", randomString());
|
||||
when(client.getAcsResponse(any(AcsRequest.class))).thenThrow(ex);
|
||||
|
||||
// 调用,并断言异常
|
||||
SmsCommonResult<?> result = smsClient.invoke(request,null);
|
||||
// 断言
|
||||
assertEquals(ex.getErrCode(), result.getApiCode());
|
||||
assertEquals(ex.getErrMsg(), result.getApiMsg());
|
||||
assertEquals(SMS_API_PARAM_ERROR.getCode(), result.getCode());
|
||||
assertEquals(SMS_API_PARAM_ERROR.getMsg(), result.getMsg());
|
||||
assertEquals(ex.getRequestId(), result.getApiRequestId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvoke_success() throws ClientException {
|
||||
// 准备参数
|
||||
QuerySmsTemplateRequest request = new QuerySmsTemplateRequest();
|
||||
Function<QuerySmsTemplateResponse, SmsTemplateRespDTO> responseConsumer = response -> {
|
||||
SmsTemplateRespDTO data = new SmsTemplateRespDTO();
|
||||
data.setId(response.getTemplateCode()).setContent(response.getTemplateContent());
|
||||
data.setAuditStatus(SmsTemplateAuditStatusEnum.SUCCESS.getStatus()).setAuditReason(response.getReason());
|
||||
return data;
|
||||
};
|
||||
// mock 方法
|
||||
QuerySmsTemplateResponse response = randomPojo(QuerySmsTemplateResponse.class, o -> {
|
||||
o.setCode("OK");
|
||||
o.setTemplateStatus(1); // 设置模板通过
|
||||
});
|
||||
when(client.getAcsResponse(any(AcsRequest.class))).thenReturn(response);
|
||||
|
||||
// 调用
|
||||
SmsCommonResult<SmsTemplateRespDTO> result = smsClient.invoke(request, responseConsumer);
|
||||
// 断言
|
||||
assertEquals(response.getCode(), result.getApiCode());
|
||||
assertEquals(response.getMessage(), result.getApiMsg());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
||||
assertEquals(response.getRequestId(), result.getApiRequestId());
|
||||
// 断言结果
|
||||
assertEquals(response.getTemplateCode(), result.getData().getId());
|
||||
assertEquals(response.getTemplateContent(), result.getData().getContent());
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getData().getAuditStatus());
|
||||
assertEquals(response.getReason(), result.getData().getAuditReason());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,32 +1,105 @@
|
||||
package cn.iocoder.dashboard.framework.sms.core.client.impl.yunpian;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.iocoder.dashboard.BaseMockitoUnitTest;
|
||||
import cn.iocoder.dashboard.common.core.KeyValue;
|
||||
import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
||||
import cn.iocoder.dashboard.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
||||
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
||||
import cn.iocoder.dashboard.util.date.DateUtils;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.yunpian.sdk.YunpianClient;
|
||||
import com.yunpian.sdk.api.SmsApi;
|
||||
import com.yunpian.sdk.api.TplApi;
|
||||
import com.yunpian.sdk.constant.YunpianConstant;
|
||||
import com.yunpian.sdk.model.Result;
|
||||
import com.yunpian.sdk.model.SmsSingleSend;
|
||||
import com.yunpian.sdk.model.Template;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static cn.iocoder.dashboard.util.RandomUtils.*;
|
||||
import static com.yunpian.sdk.constant.Code.OK;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* 对 {@link YunpianSmsClient} 的单元测试
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class YunpianSmsClientTest {
|
||||
public class YunpianSmsClientTest extends BaseMockitoUnitTest {
|
||||
|
||||
private final SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey(randomString()); // 随机一个 apiKey,避免构建报错
|
||||
|
||||
@InjectMocks
|
||||
private final YunpianSmsClient smsClient = new YunpianSmsClient(null);
|
||||
private final YunpianSmsClient smsClient = new YunpianSmsClient(properties);
|
||||
|
||||
@Mock
|
||||
private YunpianClient client;
|
||||
|
||||
@Test
|
||||
void doInit() {
|
||||
public void testDoInit() {
|
||||
// 准备参数
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
smsClient.doInit();
|
||||
// 断言
|
||||
assertNotEquals(client, ReflectUtil.getFieldValue(smsClient, "client"));
|
||||
verify(client, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void doSendSms() {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testDoSendSms() throws Throwable {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
|
||||
// mock sms 方法
|
||||
SmsApi smsApi = mock(SmsApi.class);
|
||||
when(client.sms()).thenReturn(smsApi);
|
||||
// mock tpl_single_send 方法
|
||||
Map<String, String> request = new HashMap<>();
|
||||
request.put(YunpianConstant.MOBILE, mobile);
|
||||
request.put(YunpianConstant.TPL_ID, apiTemplateId);
|
||||
request.put(YunpianConstant.TPL_VALUE, "#code#=1234&#op#=login");
|
||||
request.put(YunpianConstant.UID, String.valueOf(sendLogId));
|
||||
request.put(YunpianConstant.CALLBACK_URL, properties.getCallbackUrl());
|
||||
Result<SmsSingleSend> responseResult = randomPojo(Result.class, SmsSingleSend.class,
|
||||
o -> o.setCode(OK)); // API 发送成功的 code
|
||||
when(smsApi.tpl_single_send(eq(request))).thenReturn(responseResult);
|
||||
|
||||
// 调用
|
||||
SmsCommonResult<SmsSendRespDTO> result = smsClient.doSendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertEquals(String.valueOf(responseResult.getCode()), result.getApiCode());
|
||||
assertEquals(responseResult.getMsg() + " => " + responseResult.getDetail(), result.getApiMsg());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
||||
assertNull(result.getApiRequestId());
|
||||
// 断言结果
|
||||
assertEquals(String.valueOf(responseResult.getData().getSid()), result.getData().getSerialNo());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoParseSmsReceiveStatus() throws Throwable {
|
||||
public void testDoParseSmsReceiveStatus() throws Throwable {
|
||||
// 准备参数
|
||||
String text = "[{\"sid\":9527,\"uid\":1024,\"user_receive_time\":\"2014-03-17 22:55:21\",\"error_msg\":\"\",\"mobile\":\"15205201314\",\"report_status\":\"SUCCESS\"}]";
|
||||
// mock 方法
|
||||
@ -47,4 +120,83 @@ public class YunpianSmsClientTest {
|
||||
assertEquals(1024L, statuses.get(0).getLogId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testDoGetSmsTemplate() throws Throwable {
|
||||
// 准备参数
|
||||
String apiTemplateId = randomString();
|
||||
// mock tpl 方法
|
||||
TplApi tplApi = mock(TplApi.class);
|
||||
when(client.tpl()).thenReturn(tplApi);
|
||||
// mock get 方法
|
||||
Map<String, String> request = new HashMap<>();
|
||||
request.put(YunpianConstant.APIKEY, properties.getApiKey());
|
||||
request.put(YunpianConstant.TPL_ID, apiTemplateId);
|
||||
Result<List<Template>> responseResult = randomPojo(Result.class, List.class, o -> {
|
||||
o.setCode(OK); // API 发送成功的 code
|
||||
o.setData(randomPojoList(Template.class, t -> t.setCheck_status("SUCCESS")));
|
||||
});
|
||||
when(tplApi.get(eq(request))).thenReturn(responseResult);
|
||||
|
||||
// 调用
|
||||
SmsCommonResult<SmsTemplateRespDTO> result = smsClient.doGetSmsTemplate(apiTemplateId);
|
||||
// 断言
|
||||
assertEquals(String.valueOf(responseResult.getCode()), result.getApiCode());
|
||||
assertEquals(responseResult.getMsg() + " => " + responseResult.getDetail(), result.getApiMsg());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
||||
assertNull(result.getApiRequestId());
|
||||
// 断言结果
|
||||
Template template = responseResult.getData().get(0);
|
||||
assertEquals(template.getTpl_id().toString(), result.getData().getId());
|
||||
assertEquals(template.getTpl_content(), result.getData().getContent());
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getData().getAuditStatus());
|
||||
assertEquals(template.getReason(), result.getData().getAuditReason());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertSmsTemplateAuditStatus() {
|
||||
assertEquals(SmsTemplateAuditStatusEnum.CHECKING.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus("CHECKING"));
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus("SUCCESS"));
|
||||
assertEquals(SmsTemplateAuditStatusEnum.FAIL.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus("FAIL"));
|
||||
assertThrows(IllegalArgumentException.class, () -> smsClient.convertSmsTemplateAuditStatus("test"),
|
||||
"未知审核状态(test)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvoke_throwable() {
|
||||
// 准备参数
|
||||
Supplier<Result<Object>> requestConsumer =
|
||||
() -> new Result<>().setThrowable(new NullPointerException());
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> smsClient.invoke(requestConsumer, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testInvoke_success() throws Throwable {
|
||||
// 准备参数
|
||||
Result<SmsSingleSend> responseResult = randomPojo(Result.class, SmsSingleSend.class, o -> o.setCode(OK));
|
||||
Supplier<Result<SmsSingleSend>> requestConsumer = () -> responseResult;
|
||||
Function<SmsSingleSend, SmsSendRespDTO> responseConsumer =
|
||||
smsSingleSend -> new SmsSendRespDTO().setSerialNo(String.valueOf(responseResult.getData().getSid()));
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
SmsCommonResult<SmsSendRespDTO> result = smsClient.invoke(requestConsumer, responseConsumer);
|
||||
// 断言
|
||||
assertEquals(String.valueOf(responseResult.getCode()), result.getApiCode());
|
||||
assertEquals(responseResult.getMsg() + " => " + responseResult.getDetail(), result.getApiMsg());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
||||
assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
||||
assertNull(result.getApiRequestId());
|
||||
assertEquals(String.valueOf(responseResult.getData().getSid()), result.getData().getSerialNo());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user