初始化 SysSocialServiceTest 单元测试类

This commit is contained in:
YunaiV 2021-10-08 22:34:32 +08:00
parent 6686ded18b
commit 8d9d678464
6 changed files with 121 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialServi
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import com.google.common.annotations.VisibleForTesting;
import com.xkcoding.justauth.AuthRequestFactory;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
@ -138,10 +139,11 @@ public class SysSocialServiceImpl implements SysSocialService {
socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SysSocialUserDO::getId));
}
private void unbindOldSocialUser(Long userId, Integer type, String newUnionId) {
@VisibleForTesting
public void unbindOldSocialUser(Long userId, Integer type, String newUnionId) {
List<Integer> types = SysSocialTypeEnum.getRelationTypes(type);
List<SysSocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId(UserTypeEnum.ADMIN.getValue(),
types, userId);
List<SysSocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId(
UserTypeEnum.ADMIN.getValue(), types, userId);
// 如果新老的 unionId 是一致的说明无需解绑
if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) {
return;

View File

@ -1,13 +0,0 @@
package cn.iocoder.yudao.adminserver;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
/**
* Mockito 的单元测试
*
* @author 芋道源码
*/
@ExtendWith(MockitoExtension.class)
public class BaseMockitoUnitTest {
}

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.adminserver.modules.system.service.sms;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.adminserver.BaseMockitoUnitTest;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
@ -14,6 +13,7 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.sms.SysSmsTemp
import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsSendMessage;
import cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms.SysSmsProducer;
import cn.iocoder.yudao.adminserver.modules.system.service.sms.impl.SysSmsServiceImpl;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;

View File

@ -0,0 +1,94 @@
package cn.iocoder.yudao.adminserver.modules.system.service.social;
import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper;
import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO;
import cn.iocoder.yudao.adminserver.modules.system.enums.user.SysSocialTypeEnum;
import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
import com.xkcoding.justauth.AuthRequestFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.hutool.core.util.RandomUtil.randomString;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* {@link SysSocialServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import({SysSocialServiceImpl.class, SysSocialAuthUserRedisDAO.class})
public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
@Resource
private SysSocialServiceImpl socialService;
@Resource
private SysSocialUserMapper socialUserMapper;
@MockBean
private AuthRequestFactory authRequestFactory;
@Test
public void testBindSocialUser_create() {
// mock 数据
// 准备参数
// mock 方法
// 调用
// 断言
}
/**
* 情况一如果新老的 unionId 是一致的无需解绑
*/
@Test
public void testUnbindOldSocialUser_no() {
// mock 数据
SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> {
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
});
socialUserMapper.insert(oldSocialUser);
// 准备参数
Long userId = oldSocialUser.getUserId();
Integer type = oldSocialUser.getType();
String newUnionId = oldSocialUser.getUnionId();
// 调用
socialService.unbindOldSocialUser(userId, type, newUnionId);
// 断言
assertEquals(1L, socialUserMapper.selectCount(null).longValue());
}
/**
* 情况二如果新老的 unionId 不一致的需解绑
*/
@Test
public void testUnbindOldSocialUser_yes() {
// mock 数据
SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> {
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
});
socialUserMapper.insert(oldSocialUser);
// 准备参数
Long userId = oldSocialUser.getUserId();
Integer type = oldSocialUser.getType();
String newUnionId = randomString(10);
// 调用
socialService.unbindOldSocialUser(userId, type, newUnionId);
// 断言
assertEquals(0L, socialUserMapper.selectCount(null).longValue());
}
}

View File

@ -23,3 +23,4 @@ DELETE FROM "sys_sms_channel";
DELETE FROM "sys_sms_template";
DELETE FROM "sys_sms_log";
DELETE FROM "sys_error_code";
DELETE FROM "sys_social_user";

View File

@ -426,3 +426,23 @@ CREATE TABLE IF NOT EXISTS "sys_error_code" (
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '错误码表';
CREATE TABLE IF NOT EXISTS "sys_social_user" (
"id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"user_id" bigint NOT NULL,
"user_type" tinyint NOT NULL DEFAULT '0',
"type" tinyint NOT NULL,
"openid" varchar(32) NOT NULL,
"token" varchar(256) DEFAULT NULL,
"union_id" varchar(32) NOT NULL,
"raw_token_info" varchar(1024) NOT NULL,
"nickname" varchar(32) NOT NULL,
"avatar" varchar(255) DEFAULT NULL,
"raw_user_info" varchar(1024) NOT NULL,
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '社交用户';