mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 20:28:44 +08:00 
			
		
		
		
	【代码优化】SYSTEM:增加新阿里云 SmsClient 的单测
This commit is contained in:
		@@ -1,21 +1,30 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
 | 
			
		||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
 | 
			
		||||
import com.google.common.collect.Lists;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.mockito.InjectMocks;
 | 
			
		||||
import org.mockito.MockedStatic;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
 | 
			
		||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
import static org.mockito.ArgumentMatchers.anyMap;
 | 
			
		||||
import static org.mockito.ArgumentMatchers.anyString;
 | 
			
		||||
import static org.mockito.Mockito.mockStatic;
 | 
			
		||||
 | 
			
		||||
// TODO 芋艿:需要优化
 | 
			
		||||
/**
 | 
			
		||||
 * {@link cn.iocoder.yudao.module.system.framework.sms.core.client.impl.AliyunSmsClient_old} 的单元测试
 | 
			
		||||
 * {@link cn.iocoder.yudao.module.system.framework.sms.core.client.impl.AliyunSmsClient} 的单元测试
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 */
 | 
			
		||||
@@ -38,64 +47,54 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
 | 
			
		||||
        smsClient.doInit();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    @Test
 | 
			
		||||
//    public void tesSendSms_success() 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 方法
 | 
			
		||||
//        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);
 | 
			
		||||
//
 | 
			
		||||
//        // 调用
 | 
			
		||||
//        SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
 | 
			
		||||
//                apiTemplateId, templateParams);
 | 
			
		||||
//        // 断言
 | 
			
		||||
//        assertTrue(result.getSuccess());
 | 
			
		||||
//        assertEquals(response.getRequestId(), result.getApiRequestId());
 | 
			
		||||
//        assertEquals(response.getCode(), result.getApiCode());
 | 
			
		||||
//        assertEquals(response.getMessage(), result.getApiMsg());
 | 
			
		||||
//        assertEquals(response.getBizId(), result.getSerialNo());
 | 
			
		||||
//    }
 | 
			
		||||
    @Test
 | 
			
		||||
    public void tesSendSms_success() throws Throwable {
 | 
			
		||||
        try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
 | 
			
		||||
            // 准备参数
 | 
			
		||||
            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 方法
 | 
			
		||||
            httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
 | 
			
		||||
                    .thenReturn("{\"Message\":\"OK\",\"RequestId\":\"30067CE9-3710-5984-8881-909B21D8DB28\",\"Code\":\"OK\",\"BizId\":\"800025323183427988\"}");
 | 
			
		||||
 | 
			
		||||
//    @Test
 | 
			
		||||
//    public void tesSendSms_fail() 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 方法
 | 
			
		||||
//        SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("ERROR"));
 | 
			
		||||
//        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);
 | 
			
		||||
//
 | 
			
		||||
//        // 调用
 | 
			
		||||
//        SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
 | 
			
		||||
//        // 断言
 | 
			
		||||
//        assertFalse(result.getSuccess());
 | 
			
		||||
//        assertEquals(response.getRequestId(), result.getApiRequestId());
 | 
			
		||||
//        assertEquals(response.getCode(), result.getApiCode());
 | 
			
		||||
//        assertEquals(response.getMessage(), result.getApiMsg());
 | 
			
		||||
//        assertEquals(response.getBizId(), result.getSerialNo());
 | 
			
		||||
//    }
 | 
			
		||||
            // 调用
 | 
			
		||||
            SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
 | 
			
		||||
                    apiTemplateId, templateParams);
 | 
			
		||||
            // 断言
 | 
			
		||||
            assertTrue(result.getSuccess());
 | 
			
		||||
            assertEquals("30067CE9-3710-5984-8881-909B21D8DB28", result.getApiRequestId());
 | 
			
		||||
            assertEquals("OK", result.getApiCode());
 | 
			
		||||
            assertEquals("OK", result.getApiMsg());
 | 
			
		||||
            assertEquals("800025323183427988", result.getSerialNo());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void tesSendSms_fail() throws Throwable {
 | 
			
		||||
        try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
 | 
			
		||||
            // 准备参数
 | 
			
		||||
            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 方法
 | 
			
		||||
            httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
 | 
			
		||||
                    .thenReturn("{\"Message\":\"手机号码格式错误\",\"RequestId\":\"B7700B8E-227E-5886-9564-26036172F01F\",\"Code\":\"isv.MOBILE_NUMBER_ILLEGAL\"}");
 | 
			
		||||
 | 
			
		||||
            // 调用
 | 
			
		||||
            SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
 | 
			
		||||
            // 断言
 | 
			
		||||
            assertFalse(result.getSuccess());
 | 
			
		||||
            assertEquals("B7700B8E-227E-5886-9564-26036172F01F", result.getApiRequestId());
 | 
			
		||||
            assertEquals("isv.MOBILE_NUMBER_ILLEGAL", result.getApiCode());
 | 
			
		||||
            assertEquals("手机号码格式错误", result.getApiMsg());
 | 
			
		||||
            assertNull(result.getSerialNo());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testParseSmsReceiveStatus() {
 | 
			
		||||
@@ -129,28 +128,24 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
 | 
			
		||||
        assertEquals(67890L, statuses.get(0).getLogId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    @Test
 | 
			
		||||
//    public void testGetSmsTemplate() throws Throwable {
 | 
			
		||||
//        // 准备参数
 | 
			
		||||
//        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);
 | 
			
		||||
//
 | 
			
		||||
//        // 调用
 | 
			
		||||
//        SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId);
 | 
			
		||||
//        // 断言
 | 
			
		||||
//        assertEquals(response.getTemplateCode(), result.getId());
 | 
			
		||||
//        assertEquals(response.getTemplateContent(), result.getContent());
 | 
			
		||||
//        assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
 | 
			
		||||
//        assertEquals(response.getReason(), result.getAuditReason());
 | 
			
		||||
//    }
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testGetSmsTemplate() throws Throwable {
 | 
			
		||||
        try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
 | 
			
		||||
            // 准备参数
 | 
			
		||||
            String apiTemplateId = randomString();
 | 
			
		||||
            // mock 方法
 | 
			
		||||
            httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
 | 
			
		||||
                    .thenReturn("{\"TemplateCode\":\"SMS_207945135\",\"RequestId\":\"6F4CC077-29C8-5BA5-AB62-5FF95068A5AC\",\"Message\":\"OK\",\"TemplateContent\":\"您的验证码${code},该验证码5分钟内有效,请勿泄漏于他人!\",\"TemplateName\":\"公告通知\",\"TemplateType\":0,\"Code\":\"OK\",\"CreateDate\":\"2020-12-23 17:34:42\",\"Reason\":\"无审批备注\",\"TemplateStatus\":1}");
 | 
			
		||||
 | 
			
		||||
            // 调用
 | 
			
		||||
            SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId);
 | 
			
		||||
            // 断言
 | 
			
		||||
            assertEquals("SMS_207945135", result.getId());
 | 
			
		||||
            assertEquals("您的验证码${code},该验证码5分钟内有效,请勿泄漏于他人!", result.getContent());
 | 
			
		||||
            assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
 | 
			
		||||
            assertEquals("无审批备注", result.getAuditReason());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testConvertSmsTemplateAuditStatus() {
 | 
			
		||||
 
 | 
			
		||||
@@ -57,11 +57,12 @@ public class SmsClientTests {
 | 
			
		||||
    public void testAliyunSmsClient_sendSms() throws Throwable {
 | 
			
		||||
        SmsChannelProperties properties = new SmsChannelProperties()
 | 
			
		||||
                .setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
 | 
			
		||||
                .setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz");
 | 
			
		||||
                .setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz")
 | 
			
		||||
                .setSignature("Ballcat");
 | 
			
		||||
        AliyunSmsClient client = new AliyunSmsClient(properties);
 | 
			
		||||
        // 准备参数
 | 
			
		||||
        Long sendLogId = System.currentTimeMillis();
 | 
			
		||||
        String mobile = "17321315478";
 | 
			
		||||
        String mobile = "173213154791";
 | 
			
		||||
        String apiTemplateId = "SMS_207945135";
 | 
			
		||||
        // 调用
 | 
			
		||||
        SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, List.of(new KeyValue<>("code", "1024")));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user