diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java index a9b212332..291015aa5 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java @@ -102,7 +102,7 @@ public class SysSocialServiceImpl implements SysSocialService { if (Objects.equals(socialUser.getUserId(), userId)) { return; } - socialUserMapper.updateById(new SysSocialUserDO().setUserId(socialUser.getUserId()).setUserId(userId)); + socialUserMapper.updateById(new SysSocialUserDO().setId(socialUser.getId()).setUserId(userId)); }); // 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新 diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java index 21fc7ee46..badaf0e76 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java @@ -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.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.enums.social.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 me.zhyd.oauth.model.AuthUser; 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 java.util.List; import static cn.hutool.core.util.RandomUtil.randomEle; 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; /** @@ -36,14 +40,87 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { @MockBean private AuthRequestFactory authRequestFactory; + /** + * 情况一,创建 SysSocialUserDO 的情况 + */ @Test public void testBindSocialUser_create() { // mock 数据 // 准备参数 + Long userId = randomLongId(); + Integer type = randomEle(SysSocialTypeEnum.values()).getType(); + AuthUser authUser = randomPojo(AuthUser.class); // mock 方法 // 调用 + socialService.bindSocialUser(userId, type, authUser); // 断言 + List 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 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 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 public void testUnbindOldSocialUser_no() { // mock 数据 - SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { + SysSocialUserDO oldSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> { socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); }); @@ -75,7 +152,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { @Test public void testUnbindOldSocialUser_yes() { // mock 数据 - SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { + SysSocialUserDO oldSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> { socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); }); diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java index b1b641fae..fb9503182 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.io.IOException; import java.util.ArrayList; @@ -19,6 +20,10 @@ public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); + static { + objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + } + /** * 初始化 objectMapper 属性 *

diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java index 656c9d4ef..a80d21b68 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java @@ -36,4 +36,8 @@ public interface BaseMapperX extends BaseMapper { return selectList(new QueryWrapper<>()); } + default List selectList(String field, Object value) { + return selectList(new QueryWrapper().eq(field, value)); + } + }