Commit 4e93efdb9b229c4e50d3b4c2932841bb2100e42f
1 parent
90d5c22c
完成 SysSocialServiceTest 的绑定用户的单测
Showing
4 changed files
with
91 additions
and
5 deletions
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 { | @@ -102,7 +102,7 @@ public class SysSocialServiceImpl implements SysSocialService { | ||
| 102 | if (Objects.equals(socialUser.getUserId(), userId)) { | 102 | if (Objects.equals(socialUser.getUserId(), userId)) { |
| 103 | return; | 103 | return; |
| 104 | } | 104 | } |
| 105 | - socialUserMapper.updateById(new SysSocialUserDO().setUserId(socialUser.getUserId()).setUserId(userId)); | 105 | + socialUserMapper.updateById(new SysSocialUserDO().setId(socialUser.getId()).setUserId(userId)); |
| 106 | }); | 106 | }); |
| 107 | 107 | ||
| 108 | // 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新 | 108 | // 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新 |
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; | @@ -4,19 +4,23 @@ import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest; | ||
| 4 | import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | 4 | import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; |
| 5 | import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; | 5 | import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; |
| 6 | import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; | 6 | import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; |
| 7 | -import cn.iocoder.yudao.adminserver.modules.system.enums.user.SysSocialTypeEnum; | 7 | +import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; |
| 8 | import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl; | 8 | import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl; |
| 9 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | 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; | 10 | import com.xkcoding.justauth.AuthRequestFactory; |
| 11 | +import me.zhyd.oauth.model.AuthUser; | ||
| 12 | import org.junit.jupiter.api.Test; | 12 | import org.junit.jupiter.api.Test; |
| 13 | import org.springframework.boot.test.mock.mockito.MockBean; | 13 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | import org.springframework.context.annotation.Import; | 14 | import org.springframework.context.annotation.Import; |
| 15 | 15 | ||
| 16 | import javax.annotation.Resource; | 16 | import javax.annotation.Resource; |
| 17 | +import java.util.List; | ||
| 17 | 18 | ||
| 18 | import static cn.hutool.core.util.RandomUtil.randomEle; | 19 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 19 | import static cn.hutool.core.util.RandomUtil.randomString; | 20 | import static cn.hutool.core.util.RandomUtil.randomString; |
| 21 | +import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; | ||
| 22 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | ||
| 23 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | ||
| 20 | import static org.junit.jupiter.api.Assertions.assertEquals; | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | 25 | ||
| 22 | /** | 26 | /** |
| @@ -36,14 +40,87 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | @@ -36,14 +40,87 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | ||
| 36 | @MockBean | 40 | @MockBean |
| 37 | private AuthRequestFactory authRequestFactory; | 41 | private AuthRequestFactory authRequestFactory; |
| 38 | 42 | ||
| 43 | + /** | ||
| 44 | + * 情况一,创建 SysSocialUserDO 的情况 | ||
| 45 | + */ | ||
| 39 | @Test | 46 | @Test |
| 40 | public void testBindSocialUser_create() { | 47 | public void testBindSocialUser_create() { |
| 41 | // mock 数据 | 48 | // mock 数据 |
| 42 | // 准备参数 | 49 | // 准备参数 |
| 50 | + Long userId = randomLongId(); | ||
| 51 | + Integer type = randomEle(SysSocialTypeEnum.values()).getType(); | ||
| 52 | + AuthUser authUser = randomPojo(AuthUser.class); | ||
| 53 | + // mock 方法 | ||
| 54 | + | ||
| 55 | + // 调用 | ||
| 56 | + socialService.bindSocialUser(userId, type, authUser); | ||
| 57 | + // 断言 | ||
| 58 | + List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId); | ||
| 59 | + assertEquals(1, socialUsers.size()); | ||
| 60 | + assertBindSocialUser(socialUsers.get(0), authUser, userId, type); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * 情况二,更新 SysSocialUserDO 的情况 | ||
| 65 | + */ | ||
| 66 | + @Test | ||
| 67 | + public void testBindSocialUser_update() { | ||
| 68 | + // mock 数据 | ||
| 69 | + SysSocialUserDO dbSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> { | ||
| 70 | + socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); | ||
| 71 | + socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); | ||
| 72 | + }); | ||
| 73 | + socialUserMapper.insert(dbSocialUser); | ||
| 74 | + // 准备参数 | ||
| 75 | + Long userId = dbSocialUser.getUserId(); | ||
| 76 | + Integer type = dbSocialUser.getType(); | ||
| 77 | + AuthUser authUser = randomPojo(AuthUser.class); | ||
| 78 | + // mock 方法 | ||
| 79 | + | ||
| 80 | + // 调用 | ||
| 81 | + socialService.bindSocialUser(userId, type, authUser); | ||
| 82 | + // 断言 | ||
| 83 | + List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId); | ||
| 84 | + assertEquals(1, socialUsers.size()); | ||
| 85 | + assertBindSocialUser(socialUsers.get(0), authUser, userId, type); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * 情况一和二都存在的,逻辑二的场景 | ||
| 90 | + */ | ||
| 91 | + @Test | ||
| 92 | + public void testBindSocialUser_userId() { | ||
| 93 | + // mock 数据 | ||
| 94 | + SysSocialUserDO dbSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> { | ||
| 95 | + socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); | ||
| 96 | + socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); | ||
| 97 | + }); | ||
| 98 | + socialUserMapper.insert(dbSocialUser); | ||
| 99 | + // 准备参数 | ||
| 100 | + Long userId = randomLongId(); | ||
| 101 | + Integer type = dbSocialUser.getType(); | ||
| 102 | + AuthUser authUser = randomPojo(AuthUser.class); | ||
| 43 | // mock 方法 | 103 | // mock 方法 |
| 44 | 104 | ||
| 45 | // 调用 | 105 | // 调用 |
| 106 | + socialService.bindSocialUser(userId, type, authUser); | ||
| 46 | // 断言 | 107 | // 断言 |
| 108 | + List<SysSocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId); | ||
| 109 | + assertEquals(1, socialUsers.size()); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + private void assertBindSocialUser(SysSocialUserDO socialUser, AuthUser authUser, Long userId, | ||
| 113 | + Integer type) { | ||
| 114 | + assertEquals(authUser.getToken().getAccessToken(), socialUser.getToken()); | ||
| 115 | + assertEquals(toJsonString(authUser.getToken()), socialUser.getRawTokenInfo()); | ||
| 116 | + assertEquals(authUser.getNickname(), socialUser.getNickname()); | ||
| 117 | + assertEquals(authUser.getAvatar(), socialUser.getAvatar()); | ||
| 118 | + assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo()); | ||
| 119 | + assertEquals(userId, socialUser.getUserId()); | ||
| 120 | + assertEquals(UserTypeEnum.ADMIN.getValue(), socialUser.getUserType()); | ||
| 121 | + assertEquals(type, socialUser.getType()); | ||
| 122 | + assertEquals(authUser.getUuid(), socialUser.getOpenid()); | ||
| 123 | + assertEquals(socialService.getAuthUserUnionId(authUser), socialUser.getUnionId()); | ||
| 47 | } | 124 | } |
| 48 | 125 | ||
| 49 | /** | 126 | /** |
| @@ -52,7 +129,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | @@ -52,7 +129,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | ||
| 52 | @Test | 129 | @Test |
| 53 | public void testUnbindOldSocialUser_no() { | 130 | public void testUnbindOldSocialUser_no() { |
| 54 | // mock 数据 | 131 | // mock 数据 |
| 55 | - SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { | 132 | + SysSocialUserDO oldSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> { |
| 56 | socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); | 133 | socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); |
| 57 | socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); | 134 | socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); |
| 58 | }); | 135 | }); |
| @@ -75,7 +152,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | @@ -75,7 +152,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { | ||
| 75 | @Test | 152 | @Test |
| 76 | public void testUnbindOldSocialUser_yes() { | 153 | public void testUnbindOldSocialUser_yes() { |
| 77 | // mock 数据 | 154 | // mock 数据 |
| 78 | - SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { | 155 | + SysSocialUserDO oldSocialUser = randomPojo(SysSocialUserDO.class, socialUserDO -> { |
| 79 | socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); | 156 | socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); |
| 80 | socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); | 157 | socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); |
| 81 | }); | 158 | }); |
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; | @@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil; | ||
| 5 | import com.fasterxml.jackson.core.JsonProcessingException; | 5 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 6 | import com.fasterxml.jackson.core.type.TypeReference; | 6 | import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | import com.fasterxml.jackson.databind.ObjectMapper; | 7 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.SerializationFeature; | ||
| 8 | 9 | ||
| 9 | import java.io.IOException; | 10 | import java.io.IOException; |
| 10 | import java.util.ArrayList; | 11 | import java.util.ArrayList; |
| @@ -19,6 +20,10 @@ public class JsonUtils { | @@ -19,6 +20,10 @@ public class JsonUtils { | ||
| 19 | 20 | ||
| 20 | private static ObjectMapper objectMapper = new ObjectMapper(); | 21 | private static ObjectMapper objectMapper = new ObjectMapper(); |
| 21 | 22 | ||
| 23 | + static { | ||
| 24 | + objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | ||
| 25 | + } | ||
| 26 | + | ||
| 22 | /** | 27 | /** |
| 23 | * 初始化 objectMapper 属性 | 28 | * 初始化 objectMapper 属性 |
| 24 | * <p> | 29 | * <p> |
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<T> extends BaseMapper<T> { | @@ -36,4 +36,8 @@ public interface BaseMapperX<T> extends BaseMapper<T> { | ||
| 36 | return selectList(new QueryWrapper<>()); | 36 | return selectList(new QueryWrapper<>()); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | + default List<T> selectList(String field, Object value) { | ||
| 40 | + return selectList(new QueryWrapper<T>().eq(field, value)); | ||
| 41 | + } | ||
| 42 | + | ||
| 39 | } | 43 | } |