Commit 8d9d6784643375db6771ac4c908a7063fa011a74
1 parent
6686ded1
初始化 SysSocialServiceTest 单元测试类
Showing
6 changed files
with
121 additions
and
17 deletions
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java
| @@ -9,6 +9,7 @@ import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialServi | @@ -9,6 +9,7 @@ import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialServi | ||
| 9 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | 9 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 11 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; | 11 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; |
| 12 | +import com.google.common.annotations.VisibleForTesting; | ||
| 12 | import com.xkcoding.justauth.AuthRequestFactory; | 13 | import com.xkcoding.justauth.AuthRequestFactory; |
| 13 | import lombok.extern.slf4j.Slf4j; | 14 | import lombok.extern.slf4j.Slf4j; |
| 14 | import me.zhyd.oauth.model.AuthCallback; | 15 | import me.zhyd.oauth.model.AuthCallback; |
| @@ -138,10 +139,11 @@ public class SysSocialServiceImpl implements SysSocialService { | @@ -138,10 +139,11 @@ public class SysSocialServiceImpl implements SysSocialService { | ||
| 138 | socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SysSocialUserDO::getId)); | 139 | socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SysSocialUserDO::getId)); |
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | - private void unbindOldSocialUser(Long userId, Integer type, String newUnionId) { | 142 | + @VisibleForTesting |
| 143 | + public void unbindOldSocialUser(Long userId, Integer type, String newUnionId) { | ||
| 142 | List<Integer> types = SysSocialTypeEnum.getRelationTypes(type); | 144 | List<Integer> types = SysSocialTypeEnum.getRelationTypes(type); |
| 143 | - List<SysSocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId(UserTypeEnum.ADMIN.getValue(), | ||
| 144 | - types, userId); | 145 | + List<SysSocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId( |
| 146 | + UserTypeEnum.ADMIN.getValue(), types, userId); | ||
| 145 | // 如果新老的 unionId 是一致的,说明无需解绑 | 147 | // 如果新老的 unionId 是一致的,说明无需解绑 |
| 146 | if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) { | 148 | if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) { |
| 147 | return; | 149 | return; |
yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseMockitoUnitTest.java deleted
| 1 | -package cn.iocoder.yudao.adminserver; | ||
| 2 | - | ||
| 3 | -import org.junit.jupiter.api.extension.ExtendWith; | ||
| 4 | -import org.mockito.junit.jupiter.MockitoExtension; | ||
| 5 | - | ||
| 6 | -/** | ||
| 7 | - * 纯 Mockito 的单元测试 | ||
| 8 | - * | ||
| 9 | - * @author 芋道源码 | ||
| 10 | - */ | ||
| 11 | -@ExtendWith(MockitoExtension.class) | ||
| 12 | -public class BaseMockitoUnitTest { | ||
| 13 | -} |
yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsServiceTest.java
| 1 | package cn.iocoder.yudao.adminserver.modules.system.service.sms; | 1 | package cn.iocoder.yudao.adminserver.modules.system.service.sms; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.map.MapUtil; | 3 | import cn.hutool.core.map.MapUtil; |
| 4 | -import cn.iocoder.yudao.adminserver.BaseMockitoUnitTest; | ||
| 5 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 4 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | 6 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| @@ -14,6 +13,7 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.sms.SysSmsTemp | @@ -14,6 +13,7 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.sms.SysSmsTemp | ||
| 14 | import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsSendMessage; | 13 | import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsSendMessage; |
| 15 | import cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms.SysSmsProducer; | 14 | import cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms.SysSmsProducer; |
| 16 | import cn.iocoder.yudao.adminserver.modules.system.service.sms.impl.SysSmsServiceImpl; | 15 | import cn.iocoder.yudao.adminserver.modules.system.service.sms.impl.SysSmsServiceImpl; |
| 16 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | ||
| 17 | import org.assertj.core.util.Lists; | 17 | import org.assertj.core.util.Lists; |
| 18 | import org.junit.jupiter.api.Test; | 18 | import org.junit.jupiter.api.Test; |
| 19 | import org.mockito.InjectMocks; | 19 | import org.mockito.InjectMocks; |
yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.adminserver.modules.system.service.social; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest; | ||
| 4 | +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | ||
| 5 | +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; | ||
| 6 | +import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; | ||
| 7 | +import cn.iocoder.yudao.adminserver.modules.system.enums.user.SysSocialTypeEnum; | ||
| 8 | +import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl; | ||
| 9 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 10 | +import cn.iocoder.yudao.framework.test.core.util.RandomUtils; | ||
| 11 | +import com.xkcoding.justauth.AuthRequestFactory; | ||
| 12 | +import org.junit.jupiter.api.Test; | ||
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 14 | +import org.springframework.context.annotation.Import; | ||
| 15 | + | ||
| 16 | +import javax.annotation.Resource; | ||
| 17 | + | ||
| 18 | +import static cn.hutool.core.util.RandomUtil.randomEle; | ||
| 19 | +import static cn.hutool.core.util.RandomUtil.randomString; | ||
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * {@link SysSocialServiceImpl} 的单元测试类 | ||
| 24 | + * | ||
| 25 | + * @author 芋道源码 | ||
| 26 | + */ | ||
| 27 | +@Import({SysSocialServiceImpl.class, SysSocialAuthUserRedisDAO.class}) | ||
| 28 | +public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | ||
| 29 | + | ||
| 30 | + @Resource | ||
| 31 | + private SysSocialServiceImpl socialService; | ||
| 32 | + | ||
| 33 | + @Resource | ||
| 34 | + private SysSocialUserMapper socialUserMapper; | ||
| 35 | + | ||
| 36 | + @MockBean | ||
| 37 | + private AuthRequestFactory authRequestFactory; | ||
| 38 | + | ||
| 39 | + @Test | ||
| 40 | + public void testBindSocialUser_create() { | ||
| 41 | + // mock 数据 | ||
| 42 | + // 准备参数 | ||
| 43 | + // mock 方法 | ||
| 44 | + | ||
| 45 | + // 调用 | ||
| 46 | + // 断言 | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * 情况一,如果新老的 unionId 是一致的,无需解绑 | ||
| 51 | + */ | ||
| 52 | + @Test | ||
| 53 | + public void testUnbindOldSocialUser_no() { | ||
| 54 | + // mock 数据 | ||
| 55 | + SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { | ||
| 56 | + socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); | ||
| 57 | + socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); | ||
| 58 | + }); | ||
| 59 | + socialUserMapper.insert(oldSocialUser); | ||
| 60 | + // 准备参数 | ||
| 61 | + Long userId = oldSocialUser.getUserId(); | ||
| 62 | + Integer type = oldSocialUser.getType(); | ||
| 63 | + String newUnionId = oldSocialUser.getUnionId(); | ||
| 64 | + | ||
| 65 | + // 调用 | ||
| 66 | + socialService.unbindOldSocialUser(userId, type, newUnionId); | ||
| 67 | + // 断言 | ||
| 68 | + assertEquals(1L, socialUserMapper.selectCount(null).longValue()); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * 情况二,如果新老的 unionId 不一致的,需解绑 | ||
| 74 | + */ | ||
| 75 | + @Test | ||
| 76 | + public void testUnbindOldSocialUser_yes() { | ||
| 77 | + // mock 数据 | ||
| 78 | + SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { | ||
| 79 | + socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); | ||
| 80 | + socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); | ||
| 81 | + }); | ||
| 82 | + socialUserMapper.insert(oldSocialUser); | ||
| 83 | + // 准备参数 | ||
| 84 | + Long userId = oldSocialUser.getUserId(); | ||
| 85 | + Integer type = oldSocialUser.getType(); | ||
| 86 | + String newUnionId = randomString(10); | ||
| 87 | + | ||
| 88 | + // 调用 | ||
| 89 | + socialService.unbindOldSocialUser(userId, type, newUnionId); | ||
| 90 | + // 断言 | ||
| 91 | + assertEquals(0L, socialUserMapper.selectCount(null).longValue()); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | +} |
yudao-admin-server/src/test/resources/sql/clean.sql
| @@ -23,3 +23,4 @@ DELETE FROM "sys_sms_channel"; | @@ -23,3 +23,4 @@ DELETE FROM "sys_sms_channel"; | ||
| 23 | DELETE FROM "sys_sms_template"; | 23 | DELETE FROM "sys_sms_template"; |
| 24 | DELETE FROM "sys_sms_log"; | 24 | DELETE FROM "sys_sms_log"; |
| 25 | DELETE FROM "sys_error_code"; | 25 | DELETE FROM "sys_error_code"; |
| 26 | +DELETE FROM "sys_social_user"; |
yudao-admin-server/src/test/resources/sql/create_tables.sql
| @@ -426,3 +426,23 @@ CREATE TABLE IF NOT EXISTS "sys_error_code" ( | @@ -426,3 +426,23 @@ CREATE TABLE IF NOT EXISTS "sys_error_code" ( | ||
| 426 | "deleted" bit NOT NULL DEFAULT FALSE, | 426 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 427 | PRIMARY KEY ("id") | 427 | PRIMARY KEY ("id") |
| 428 | ) COMMENT '错误码表'; | 428 | ) COMMENT '错误码表'; |
| 429 | + | ||
| 430 | +CREATE TABLE IF NOT EXISTS "sys_social_user" ( | ||
| 431 | + "id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY, | ||
| 432 | + "user_id" bigint NOT NULL, | ||
| 433 | + "user_type" tinyint NOT NULL DEFAULT '0', | ||
| 434 | + "type" tinyint NOT NULL, | ||
| 435 | + "openid" varchar(32) NOT NULL, | ||
| 436 | + "token" varchar(256) DEFAULT NULL, | ||
| 437 | + "union_id" varchar(32) NOT NULL, | ||
| 438 | + "raw_token_info" varchar(1024) NOT NULL, | ||
| 439 | + "nickname" varchar(32) NOT NULL, | ||
| 440 | + "avatar" varchar(255) DEFAULT NULL, | ||
| 441 | + "raw_user_info" varchar(1024) NOT NULL, | ||
| 442 | + "creator" varchar(64) DEFAULT '', | ||
| 443 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 444 | + "updater" varchar(64) DEFAULT '', | ||
| 445 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 446 | + "deleted" bit NOT NULL DEFAULT FALSE, | ||
| 447 | + PRIMARY KEY ("id") | ||
| 448 | +) COMMENT '社交用户'; |