完成 SysSocialServiceTest 的绑定用户的单测
This commit is contained in:
parent
90d5c22cb6
commit
4e93efdb9b
|
@ -102,7 +102,7 @@ public class SysSocialServiceImpl implements SysSocialService {
|
||||||
if (Objects.equals(socialUser.getUserId(), userId)) {
|
if (Objects.equals(socialUser.getUserId(), userId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
socialUserMapper.updateById(new SysSocialUserDO().setUserId(socialUser.getUserId()).setUserId(userId));
|
socialUserMapper.updateById(new SysSocialUserDO().setId(socialUser.getId()).setUserId(userId));
|
||||||
});
|
});
|
||||||
|
|
||||||
// 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新
|
// 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新
|
||||||
|
|
|
@ -4,19 +4,23 @@ 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.dataobject.social.SysSocialUserDO;
|
||||||
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper;
|
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.dal.redis.social.SysSocialAuthUserRedisDAO;
|
||||||
import cn.iocoder.yudao.adminserver.modules.system.enums.user.SysSocialTypeEnum;
|
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
|
||||||
import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl;
|
import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl;
|
||||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
|
||||||
import com.xkcoding.justauth.AuthRequestFactory;
|
import com.xkcoding.justauth.AuthRequestFactory;
|
||||||
|
import me.zhyd.oauth.model.AuthUser;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||||
import static cn.hutool.core.util.RandomUtil.randomString;
|
import static cn.hutool.core.util.RandomUtil.randomString;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,14 +40,87 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
|
||||||
@MockBean
|
@MockBean
|
||||||
private AuthRequestFactory authRequestFactory;
|
private AuthRequestFactory authRequestFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 情况一,创建 SysSocialUserDO 的情况
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testBindSocialUser_create() {
|
public void testBindSocialUser_create() {
|
||||||
// mock 数据
|
// mock 数据
|
||||||
// 准备参数
|
// 准备参数
|
||||||
|
Long userId = randomLongId();
|
||||||
|
Integer type = randomEle(SysSocialTypeEnum.values()).getType();
|
||||||
|
AuthUser authUser = randomPojo(AuthUser.class);
|
||||||
// mock 方法
|
// mock 方法
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
|
socialService.bindSocialUser(userId, type, authUser);
|
||||||
// 断言
|
// 断言
|
||||||
|
List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
|
||||||
|
assertEquals(1, socialUsers.size());
|
||||||
|
assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 情况二,更新 SysSocialUserDO 的情况
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testBindSocialUser_update() {
|
||||||
|
// mock 数据
|
||||||
|
SysSocialUserDO dbSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> {
|
||||||
|
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||||
|
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
|
||||||
|
});
|
||||||
|
socialUserMapper.insert(dbSocialUser);
|
||||||
|
// 准备参数
|
||||||
|
Long userId = dbSocialUser.getUserId();
|
||||||
|
Integer type = dbSocialUser.getType();
|
||||||
|
AuthUser authUser = randomPojo(AuthUser.class);
|
||||||
|
// mock 方法
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
socialService.bindSocialUser(userId, type, authUser);
|
||||||
|
// 断言
|
||||||
|
List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
|
||||||
|
assertEquals(1, socialUsers.size());
|
||||||
|
assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 情况一和二都存在的,逻辑二的场景
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testBindSocialUser_userId() {
|
||||||
|
// mock 数据
|
||||||
|
SysSocialUserDO dbSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> {
|
||||||
|
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||||
|
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
|
||||||
|
});
|
||||||
|
socialUserMapper.insert(dbSocialUser);
|
||||||
|
// 准备参数
|
||||||
|
Long userId = randomLongId();
|
||||||
|
Integer type = dbSocialUser.getType();
|
||||||
|
AuthUser authUser = randomPojo(AuthUser.class);
|
||||||
|
// mock 方法
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
socialService.bindSocialUser(userId, type, authUser);
|
||||||
|
// 断言
|
||||||
|
List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
|
||||||
|
assertEquals(1, socialUsers.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertBindSocialUser(SysSocialUserDO socialUser, AuthUser authUser, Long userId,
|
||||||
|
Integer type) {
|
||||||
|
assertEquals(authUser.getToken().getAccessToken(), socialUser.getToken());
|
||||||
|
assertEquals(toJsonString(authUser.getToken()), socialUser.getRawTokenInfo());
|
||||||
|
assertEquals(authUser.getNickname(), socialUser.getNickname());
|
||||||
|
assertEquals(authUser.getAvatar(), socialUser.getAvatar());
|
||||||
|
assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo());
|
||||||
|
assertEquals(userId, socialUser.getUserId());
|
||||||
|
assertEquals(UserTypeEnum.ADMIN.getValue(), socialUser.getUserType());
|
||||||
|
assertEquals(type, socialUser.getType());
|
||||||
|
assertEquals(authUser.getUuid(), socialUser.getOpenid());
|
||||||
|
assertEquals(socialService.getAuthUserUnionId(authUser), socialUser.getUnionId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,7 +129,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void testUnbindOldSocialUser_no() {
|
public void testUnbindOldSocialUser_no() {
|
||||||
// mock 数据
|
// mock 数据
|
||||||
SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> {
|
SysSocialUserDO oldSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> {
|
||||||
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||||
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
|
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
|
||||||
});
|
});
|
||||||
|
@ -75,7 +152,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void testUnbindOldSocialUser_yes() {
|
public void testUnbindOldSocialUser_yes() {
|
||||||
// mock 数据
|
// mock 数据
|
||||||
SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> {
|
SysSocialUserDO oldSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> {
|
||||||
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||||
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
|
socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType());
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -19,6 +20,10 @@ public class JsonUtils {
|
||||||
|
|
||||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
static {
|
||||||
|
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化 objectMapper 属性
|
* 初始化 objectMapper 属性
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -36,4 +36,8 @@ public interface BaseMapperX<T> extends BaseMapper<T> {
|
||||||
return selectList(new QueryWrapper<>());
|
return selectList(new QueryWrapper<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default List<T> selectList(String field, Object value) {
|
||||||
|
return selectList(new QueryWrapper<T>().eq(field, value));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue