提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.framework.sms.core.client.impl; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.ReflectUtil; |
|
4 |
import com.iailab.framework.common.core.KeyValue; |
|
5 |
import com.iailab.framework.common.util.collection.ArrayUtils; |
|
6 |
import com.iailab.framework.common.util.collection.MapUtils; |
|
7 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
8 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
9 |
import com.iailab.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; |
|
10 |
import com.iailab.module.system.framework.sms.core.client.dto.SmsSendRespDTO; |
|
11 |
import com.iailab.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; |
|
12 |
import com.iailab.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
|
13 |
import com.iailab.module.system.framework.sms.core.property.SmsChannelProperties; |
|
14 |
import com.google.common.collect.Lists; |
|
15 |
import com.tencentcloudapi.sms.v20210111.SmsClient; |
|
16 |
import com.tencentcloudapi.sms.v20210111.models.DescribeSmsTemplateListResponse; |
|
17 |
import com.tencentcloudapi.sms.v20210111.models.DescribeTemplateListStatus; |
|
18 |
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; |
|
19 |
import com.tencentcloudapi.sms.v20210111.models.SendStatus; |
|
20 |
import org.junit.jupiter.api.Test; |
|
21 |
import org.mockito.InjectMocks; |
|
22 |
import org.mockito.Mock; |
|
23 |
|
|
24 |
import java.time.LocalDateTime; |
|
25 |
import java.util.ArrayList; |
|
26 |
import java.util.List; |
|
27 |
|
|
28 |
import static com.iailab.framework.common.util.json.JsonUtils.toJsonString; |
|
29 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
30 |
import static org.junit.jupiter.api.Assertions.*; |
|
31 |
import static org.mockito.ArgumentMatchers.argThat; |
|
32 |
import static org.mockito.Mockito.when; |
|
33 |
|
|
34 |
/** |
|
35 |
* {@link TencentSmsClient} 的单元测试 |
|
36 |
* |
|
37 |
* @author shiwp |
|
38 |
*/ |
|
39 |
public class TencentSmsClientTest extends BaseMockitoUnitTest { |
|
40 |
|
|
41 |
private final SmsChannelProperties properties = new SmsChannelProperties() |
|
42 |
.setApiKey(randomString() + " " + randomString()) // 随机一个 apiKey,避免构建报错 |
|
43 |
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错 |
|
44 |
.setSignature("iailab"); |
|
45 |
|
|
46 |
@InjectMocks |
|
47 |
private TencentSmsClient smsClient = new TencentSmsClient(properties); |
|
48 |
|
|
49 |
@Mock |
|
50 |
private SmsClient client; |
|
51 |
|
|
52 |
@Test |
|
53 |
public void testDoInit() { |
|
54 |
// 准备参数 |
|
55 |
// mock 方法 |
|
56 |
|
|
57 |
// 调用 |
|
58 |
smsClient.doInit(); |
|
59 |
// 断言 |
|
60 |
assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "client")); |
|
61 |
} |
|
62 |
|
|
63 |
@Test |
|
64 |
public void testRefresh() { |
|
65 |
// 准备参数 |
|
66 |
SmsChannelProperties p = new SmsChannelProperties() |
|
67 |
.setApiKey(randomString() + " " + randomString()) // 随机一个 apiKey,避免构建报错 |
|
68 |
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错 |
|
69 |
.setSignature("iailab"); |
|
70 |
// 调用 |
|
71 |
smsClient.refresh(p); |
|
72 |
// 断言 |
|
73 |
assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "client")); |
|
74 |
} |
|
75 |
|
|
76 |
@Test |
|
77 |
public void testDoSendSms_success() throws Throwable { |
|
78 |
// 准备参数 |
|
79 |
Long sendLogId = randomLongId(); |
|
80 |
String mobile = randomString(); |
|
81 |
String apiTemplateId = randomString(); |
|
82 |
List<KeyValue<String, Object>> templateParams = Lists.newArrayList( |
|
83 |
new KeyValue<>("1", 1234), new KeyValue<>("2", "login")); |
|
84 |
String requestId = randomString(); |
|
85 |
String serialNo = randomString(); |
|
86 |
// mock 方法 |
|
87 |
SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> { |
|
88 |
o.setRequestId(requestId); |
|
89 |
SendStatus[] sendStatuses = new SendStatus[1]; |
|
90 |
o.setSendStatusSet(sendStatuses); |
|
91 |
SendStatus sendStatus = new SendStatus(); |
|
92 |
sendStatuses[0] = sendStatus; |
|
93 |
sendStatus.setCode(TencentSmsClient.API_CODE_SUCCESS); |
|
94 |
sendStatus.setMessage("send success"); |
|
95 |
sendStatus.setSerialNo(serialNo); |
|
96 |
}); |
|
97 |
when(client.SendSms(argThat(request -> { |
|
98 |
assertEquals(mobile, request.getPhoneNumberSet()[0]); |
|
99 |
assertEquals(properties.getSignature(), request.getSignName()); |
|
100 |
assertEquals(apiTemplateId, request.getTemplateId()); |
|
101 |
assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), |
|
102 |
toJsonString(request.getTemplateParamSet())); |
|
103 |
assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); |
|
104 |
return true; |
|
105 |
}))).thenReturn(response); |
|
106 |
|
|
107 |
// 调用 |
|
108 |
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); |
|
109 |
// 断言 |
|
110 |
assertTrue(result.getSuccess()); |
|
111 |
assertEquals(response.getRequestId(), result.getApiRequestId()); |
|
112 |
assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode()); |
|
113 |
assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg()); |
|
114 |
assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo()); |
|
115 |
} |
|
116 |
|
|
117 |
@Test |
|
118 |
public void testDoSendSms_fail() throws Throwable { |
|
119 |
// 准备参数 |
|
120 |
Long sendLogId = randomLongId(); |
|
121 |
String mobile = randomString(); |
|
122 |
String apiTemplateId = randomString(); |
|
123 |
List<KeyValue<String, Object>> templateParams = Lists.newArrayList( |
|
124 |
new KeyValue<>("1", 1234), new KeyValue<>("2", "login")); |
|
125 |
String requestId = randomString(); |
|
126 |
String serialNo = randomString(); |
|
127 |
// mock 方法 |
|
128 |
SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> { |
|
129 |
o.setRequestId(requestId); |
|
130 |
SendStatus[] sendStatuses = new SendStatus[1]; |
|
131 |
o.setSendStatusSet(sendStatuses); |
|
132 |
SendStatus sendStatus = new SendStatus(); |
|
133 |
sendStatuses[0] = sendStatus; |
|
134 |
sendStatus.setCode("ERROR"); |
|
135 |
sendStatus.setMessage("send success"); |
|
136 |
sendStatus.setSerialNo(serialNo); |
|
137 |
}); |
|
138 |
when(client.SendSms(argThat(request -> { |
|
139 |
assertEquals(mobile, request.getPhoneNumberSet()[0]); |
|
140 |
assertEquals(properties.getSignature(), request.getSignName()); |
|
141 |
assertEquals(apiTemplateId, request.getTemplateId()); |
|
142 |
assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), |
|
143 |
toJsonString(request.getTemplateParamSet())); |
|
144 |
assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); |
|
145 |
return true; |
|
146 |
}))).thenReturn(response); |
|
147 |
|
|
148 |
// 调用 |
|
149 |
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); |
|
150 |
// 断言 |
|
151 |
assertFalse(result.getSuccess()); |
|
152 |
assertEquals(response.getRequestId(), result.getApiRequestId()); |
|
153 |
assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode()); |
|
154 |
assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg()); |
|
155 |
assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo()); |
|
156 |
} |
|
157 |
|
|
158 |
@Test |
|
159 |
public void testParseSmsReceiveStatus() { |
|
160 |
// 准备参数 |
|
161 |
String text = "[\n" + |
|
162 |
" {\n" + |
|
163 |
" \"user_receive_time\": \"2015-10-17 08:03:04\",\n" + |
|
164 |
" \"nationcode\": \"86\",\n" + |
|
165 |
" \"mobile\": \"13900000001\",\n" + |
|
166 |
" \"report_status\": \"SUCCESS\",\n" + |
|
167 |
" \"errmsg\": \"DELIVRD\",\n" + |
|
168 |
" \"description\": \"用户短信送达成功\",\n" + |
|
169 |
" \"sid\": \"12345\",\n" + |
|
170 |
" \"ext\": {\"logId\":\"67890\"}\n" + |
|
171 |
" }\n" + |
|
172 |
"]"; |
|
173 |
// mock 方法 |
|
174 |
|
|
175 |
// 调用 |
|
176 |
List<SmsReceiveRespDTO> statuses = smsClient.parseSmsReceiveStatus(text); |
|
177 |
// 断言 |
|
178 |
assertEquals(1, statuses.size()); |
|
179 |
assertTrue(statuses.get(0).getSuccess()); |
|
180 |
assertEquals("DELIVRD", statuses.get(0).getErrorCode()); |
|
181 |
assertEquals("用户短信送达成功", statuses.get(0).getErrorMsg()); |
|
182 |
assertEquals("13900000001", statuses.get(0).getMobile()); |
|
183 |
assertEquals(LocalDateTime.of(2015, 10, 17, 8, 3, 4), statuses.get(0).getReceiveTime()); |
|
184 |
assertEquals("12345", statuses.get(0).getSerialNo()); |
|
185 |
assertEquals(67890L, statuses.get(0).getLogId()); |
|
186 |
} |
|
187 |
|
|
188 |
@Test |
|
189 |
public void testGetSmsTemplate() throws Throwable { |
|
190 |
// 准备参数 |
|
191 |
Long apiTemplateId = randomLongId(); |
|
192 |
String requestId = randomString(); |
|
193 |
|
|
194 |
// mock 方法 |
|
195 |
DescribeSmsTemplateListResponse response = randomPojo(DescribeSmsTemplateListResponse.class, o -> { |
|
196 |
DescribeTemplateListStatus[] describeTemplateListStatuses = new DescribeTemplateListStatus[1]; |
|
197 |
DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus(); |
|
198 |
templateStatus.setTemplateId(apiTemplateId); |
|
199 |
templateStatus.setStatusCode(0L);// 设置模板通过 |
|
200 |
describeTemplateListStatuses[0] = templateStatus; |
|
201 |
o.setDescribeTemplateStatusSet(describeTemplateListStatuses); |
|
202 |
o.setRequestId(requestId); |
|
203 |
}); |
|
204 |
when(client.DescribeSmsTemplateList(argThat(request -> { |
|
205 |
assertEquals(apiTemplateId, request.getTemplateIdSet()[0]); |
|
206 |
return true; |
|
207 |
}))).thenReturn(response); |
|
208 |
|
|
209 |
// 调用 |
|
210 |
SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId.toString()); |
|
211 |
// 断言 |
|
212 |
assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateId().toString(), result.getId()); |
|
213 |
assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateContent(), result.getContent()); |
|
214 |
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus()); |
|
215 |
assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getAuditReason()); |
|
216 |
} |
|
217 |
|
|
218 |
@Test |
|
219 |
public void testConvertSmsTemplateAuditStatus() { |
|
220 |
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), |
|
221 |
smsClient.convertSmsTemplateAuditStatus(0)); |
|
222 |
assertEquals(SmsTemplateAuditStatusEnum.CHECKING.getStatus(), |
|
223 |
smsClient.convertSmsTemplateAuditStatus(1)); |
|
224 |
assertEquals(SmsTemplateAuditStatusEnum.FAIL.getStatus(), |
|
225 |
smsClient.convertSmsTemplateAuditStatus(-1)); |
|
226 |
assertThrows(IllegalArgumentException.class, () -> smsClient.convertSmsTemplateAuditStatus(3), |
|
227 |
"未知审核状态(3)"); |
|
228 |
} |
|
229 |
|
|
230 |
} |