潘志宝
2024-08-15 81c220fd9e0ea6c8ee84c9b766885b0322b4038c
提交 | 用户 | 时间
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.MapUtils;
6 import com.iailab.framework.test.core.ut.BaseMockitoUnitTest;
7 import com.iailab.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
8 import com.iailab.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
9 import com.iailab.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
10 import com.iailab.module.system.framework.sms.core.client.impl.AliyunSmsClient;
11 import com.iailab.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
12 import com.iailab.module.system.framework.sms.core.property.SmsChannelProperties;
13 import com.aliyuncs.IAcsClient;
14 import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateRequest;
15 import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse;
16 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
17 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
18 import com.google.common.collect.Lists;
19 import org.junit.jupiter.api.Test;
20 import org.mockito.ArgumentMatcher;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23
24 import java.time.LocalDateTime;
25 import java.util.List;
26
27 import static com.iailab.framework.common.util.json.JsonUtils.toJsonString;
28 import static com.iailab.framework.test.core.util.RandomUtils.*;
29 import static org.junit.jupiter.api.Assertions.*;
30 import static org.mockito.ArgumentMatchers.argThat;
31 import static org.mockito.Mockito.when;
32
33 /**
34  * {@link AliyunSmsClient} 的单元测试
35  *
36  * @author iailab
37  */
38 public class AliyunSmsClientTest extends BaseMockitoUnitTest {
39
40     private final SmsChannelProperties properties = new SmsChannelProperties()
41             .setApiKey(randomString()) // 随机一个 apiKey,避免构建报错
42             .setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错
43             .setSignature("iailab");
44
45     @InjectMocks
46     private final AliyunSmsClient smsClient = new AliyunSmsClient(properties);
47
48     @Mock
49     private IAcsClient client;
50
51     @Test
52     public void testDoInit() {
53         // 准备参数
54         // mock 方法
55
56         // 调用
57         smsClient.doInit();
58         // 断言
59         assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "acsClient"));
60     }
61
62     @Test
63     public void tesSendSms_success() throws Throwable {
64         // 准备参数
65         Long sendLogId = randomLongId();
66         String mobile = randomString();
67         String apiTemplateId = randomString();
68         List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
69                 new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
70         // mock 方法
71         SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("OK"));
72         when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> {
73             assertEquals(mobile, acsRequest.getPhoneNumbers());
74             assertEquals(properties.getSignature(), acsRequest.getSignName());
75             assertEquals(apiTemplateId, acsRequest.getTemplateCode());
76             assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam());
77             assertEquals(sendLogId.toString(), acsRequest.getOutId());
78             return true;
79         }))).thenReturn(response);
80
81         // 调用
82         SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
83                 apiTemplateId, templateParams);
84         // 断言
85         assertTrue(result.getSuccess());
86         assertEquals(response.getRequestId(), result.getApiRequestId());
87         assertEquals(response.getCode(), result.getApiCode());
88         assertEquals(response.getMessage(), result.getApiMsg());
89         assertEquals(response.getBizId(), result.getSerialNo());
90     }
91
92     @Test
93     public void tesSendSms_fail() throws Throwable {
94         // 准备参数
95         Long sendLogId = randomLongId();
96         String mobile = randomString();
97         String apiTemplateId = randomString();
98         List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
99                 new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
100         // mock 方法
101         SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("ERROR"));
102         when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> {
103             assertEquals(mobile, acsRequest.getPhoneNumbers());
104             assertEquals(properties.getSignature(), acsRequest.getSignName());
105             assertEquals(apiTemplateId, acsRequest.getTemplateCode());
106             assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam());
107             assertEquals(sendLogId.toString(), acsRequest.getOutId());
108             return true;
109         }))).thenReturn(response);
110
111         // 调用
112         SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
113         // 断言
114         assertFalse(result.getSuccess());
115         assertEquals(response.getRequestId(), result.getApiRequestId());
116         assertEquals(response.getCode(), result.getApiCode());
117         assertEquals(response.getMessage(), result.getApiMsg());
118         assertEquals(response.getBizId(), result.getSerialNo());
119     }
120
121     @Test
122     public void testParseSmsReceiveStatus() {
123         // 准备参数
124         String text = "[\n" +
125                 "  {\n" +
126                 "    \"phone_number\" : \"13900000001\",\n" +
127                 "    \"send_time\" : \"2017-01-01 11:12:13\",\n" +
128                 "    \"report_time\" : \"2017-02-02 22:23:24\",\n" +
129                 "    \"success\" : true,\n" +
130                 "    \"err_code\" : \"DELIVERED\",\n" +
131                 "    \"err_msg\" : \"用户接收成功\",\n" +
132                 "    \"sms_size\" : \"1\",\n" +
133                 "    \"biz_id\" : \"12345\",\n" +
134                 "    \"out_id\" : \"67890\"\n" +
135                 "  }\n" +
136                 "]";
137         // mock 方法
138
139         // 调用
140         List<SmsReceiveRespDTO> statuses = smsClient.parseSmsReceiveStatus(text);
141         // 断言
142         assertEquals(1, statuses.size());
143         assertTrue(statuses.get(0).getSuccess());
144         assertEquals("DELIVERED", statuses.get(0).getErrorCode());
145         assertEquals("用户接收成功", statuses.get(0).getErrorMsg());
146         assertEquals("13900000001", statuses.get(0).getMobile());
147         assertEquals(LocalDateTime.of(2017, 2, 2, 22, 23, 24),
148                 statuses.get(0).getReceiveTime());
149         assertEquals("12345", statuses.get(0).getSerialNo());
150         assertEquals(67890L, statuses.get(0).getLogId());
151     }
152
153     @Test
154     public void testGetSmsTemplate() throws Throwable {
155         // 准备参数
156         String apiTemplateId = randomString();
157         // mock 方法
158         QuerySmsTemplateResponse response = randomPojo(QuerySmsTemplateResponse.class, o -> {
159             o.setCode("OK");
160             o.setTemplateStatus(1); // 设置模板通过
161         });
162         when(client.getAcsResponse(argThat((ArgumentMatcher<QuerySmsTemplateRequest>) acsRequest -> {
163             assertEquals(apiTemplateId, acsRequest.getTemplateCode());
164             return true;
165         }))).thenReturn(response);
166
167         // 调用
168         SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId);
169         // 断言
170         assertEquals(response.getTemplateCode(), result.getId());
171         assertEquals(response.getTemplateContent(), result.getContent());
172         assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
173         assertEquals(response.getReason(), result.getAuditReason());
174     }
175
176     @Test
177     public void testConvertSmsTemplateAuditStatus() {
178         assertEquals(SmsTemplateAuditStatusEnum.CHECKING.getStatus(),
179                 smsClient.convertSmsTemplateAuditStatus(0));
180         assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(),
181                 smsClient.convertSmsTemplateAuditStatus(1));
182         assertEquals(SmsTemplateAuditStatusEnum.FAIL.getStatus(),
183                 smsClient.convertSmsTemplateAuditStatus(2));
184         assertThrows(IllegalArgumentException.class, () -> smsClient.convertSmsTemplateAuditStatus(3),
185                 "未知审核状态(3)");
186     }
187
188 }