提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.oauth2; |
H |
2 |
|
|
3 |
import cn.hutool.extra.spring.SpringUtil; |
|
4 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
|
6 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
7 |
import com.iailab.module.system.controller.admin.oauth2.vo.client.OAuth2ClientPageReqVO; |
|
8 |
import com.iailab.module.system.controller.admin.oauth2.vo.client.OAuth2ClientSaveReqVO; |
|
9 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2ClientDO; |
|
10 |
import com.iailab.module.system.dal.mysql.oauth2.OAuth2ClientMapper; |
|
11 |
import org.junit.jupiter.api.Test; |
|
12 |
import org.mockito.MockedStatic; |
|
13 |
import org.springframework.context.annotation.Import; |
|
14 |
|
|
15 |
import javax.annotation.Resource; |
|
16 |
import java.util.Collections; |
|
17 |
|
|
18 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
19 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
20 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
21 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
22 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
23 |
import static org.junit.jupiter.api.Assertions.*; |
|
24 |
import static org.mockito.ArgumentMatchers.eq; |
|
25 |
import static org.mockito.Mockito.mockStatic; |
|
26 |
|
|
27 |
/** |
|
28 |
* {@link OAuth2ClientServiceImpl} 的单元测试类 |
|
29 |
* |
|
30 |
* @author iailab |
|
31 |
*/ |
|
32 |
@Import(OAuth2ClientServiceImpl.class) |
|
33 |
public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { |
|
34 |
|
|
35 |
@Resource |
|
36 |
private OAuth2ClientServiceImpl oauth2ClientService; |
|
37 |
|
|
38 |
@Resource |
|
39 |
private OAuth2ClientMapper oauth2ClientMapper; |
|
40 |
|
|
41 |
@Test |
|
42 |
public void testCreateOAuth2Client_success() { |
|
43 |
// 准备参数 |
|
44 |
OAuth2ClientSaveReqVO reqVO = randomPojo(OAuth2ClientSaveReqVO.class, |
|
45 |
o -> o.setLogo(randomString())) |
|
46 |
.setId(null); // 防止 id 被赋值 |
|
47 |
|
|
48 |
// 调用 |
|
49 |
Long oauth2ClientId = oauth2ClientService.createOAuth2Client(reqVO); |
|
50 |
// 断言 |
|
51 |
assertNotNull(oauth2ClientId); |
|
52 |
// 校验记录的属性是否正确 |
|
53 |
OAuth2ClientDO oAuth2Client = oauth2ClientMapper.selectById(oauth2ClientId); |
|
54 |
assertPojoEquals(reqVO, oAuth2Client, "id"); |
|
55 |
} |
|
56 |
|
|
57 |
@Test |
|
58 |
public void testUpdateOAuth2Client_success() { |
|
59 |
// mock 数据 |
|
60 |
OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class); |
|
61 |
oauth2ClientMapper.insert(dbOAuth2Client);// @Sql: 先插入出一条存在的数据 |
|
62 |
// 准备参数 |
|
63 |
OAuth2ClientSaveReqVO reqVO = randomPojo(OAuth2ClientSaveReqVO.class, o -> { |
|
64 |
o.setId(dbOAuth2Client.getId()); // 设置更新的 ID |
|
65 |
o.setLogo(randomString()); |
|
66 |
}); |
|
67 |
|
|
68 |
// 调用 |
|
69 |
oauth2ClientService.updateOAuth2Client(reqVO); |
|
70 |
// 校验是否更新正确 |
|
71 |
OAuth2ClientDO oAuth2Client = oauth2ClientMapper.selectById(reqVO.getId()); // 获取最新的 |
|
72 |
assertPojoEquals(reqVO, oAuth2Client); |
|
73 |
} |
|
74 |
|
|
75 |
@Test |
|
76 |
public void testUpdateOAuth2Client_notExists() { |
|
77 |
// 准备参数 |
|
78 |
OAuth2ClientSaveReqVO reqVO = randomPojo(OAuth2ClientSaveReqVO.class); |
|
79 |
|
|
80 |
// 调用, 并断言异常 |
|
81 |
assertServiceException(() -> oauth2ClientService.updateOAuth2Client(reqVO), OAUTH2_CLIENT_NOT_EXISTS); |
|
82 |
} |
|
83 |
|
|
84 |
@Test |
|
85 |
public void testDeleteOAuth2Client_success() { |
|
86 |
// mock 数据 |
|
87 |
OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class); |
|
88 |
oauth2ClientMapper.insert(dbOAuth2Client);// @Sql: 先插入出一条存在的数据 |
|
89 |
// 准备参数 |
|
90 |
Long id = dbOAuth2Client.getId(); |
|
91 |
|
|
92 |
// 调用 |
|
93 |
oauth2ClientService.deleteOAuth2Client(id); |
|
94 |
// 校验数据不存在了 |
|
95 |
assertNull(oauth2ClientMapper.selectById(id)); |
|
96 |
} |
|
97 |
|
|
98 |
@Test |
|
99 |
public void testDeleteOAuth2Client_notExists() { |
|
100 |
// 准备参数 |
|
101 |
Long id = randomLongId(); |
|
102 |
|
|
103 |
// 调用, 并断言异常 |
|
104 |
assertServiceException(() -> oauth2ClientService.deleteOAuth2Client(id), OAUTH2_CLIENT_NOT_EXISTS); |
|
105 |
} |
|
106 |
|
|
107 |
@Test |
|
108 |
public void testValidateClientIdExists_withId() { |
|
109 |
// mock 数据 |
|
110 |
OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("tudou"); |
|
111 |
oauth2ClientMapper.insert(client); |
|
112 |
// 准备参数 |
|
113 |
Long id = randomLongId(); |
|
114 |
String clientId = "tudou"; |
|
115 |
|
|
116 |
// 调用,不会报错 |
|
117 |
assertServiceException(() -> oauth2ClientService.validateClientIdExists(id, clientId), OAUTH2_CLIENT_EXISTS); |
|
118 |
} |
|
119 |
|
|
120 |
@Test |
|
121 |
public void testValidateClientIdExists_noId() { |
|
122 |
// mock 数据 |
|
123 |
OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("tudou"); |
|
124 |
oauth2ClientMapper.insert(client); |
|
125 |
// 准备参数 |
|
126 |
String clientId = "tudou"; |
|
127 |
|
|
128 |
// 调用,不会报错 |
|
129 |
assertServiceException(() -> oauth2ClientService.validateClientIdExists(null, clientId), OAUTH2_CLIENT_EXISTS); |
|
130 |
} |
|
131 |
|
|
132 |
@Test |
|
133 |
public void testGetOAuth2Client() { |
|
134 |
// mock 数据 |
|
135 |
OAuth2ClientDO clientDO = randomPojo(OAuth2ClientDO.class); |
|
136 |
oauth2ClientMapper.insert(clientDO); |
|
137 |
// 准备参数 |
|
138 |
Long id = clientDO.getId(); |
|
139 |
|
|
140 |
// 调用,并断言 |
|
141 |
OAuth2ClientDO dbClientDO = oauth2ClientService.getOAuth2Client(id); |
|
142 |
assertPojoEquals(clientDO, dbClientDO); |
|
143 |
} |
|
144 |
|
|
145 |
@Test |
|
146 |
public void testGetOAuth2ClientFromCache() { |
|
147 |
// mock 数据 |
|
148 |
OAuth2ClientDO clientDO = randomPojo(OAuth2ClientDO.class); |
|
149 |
oauth2ClientMapper.insert(clientDO); |
|
150 |
// 准备参数 |
|
151 |
String clientId = clientDO.getClientId(); |
|
152 |
|
|
153 |
// 调用,并断言 |
|
154 |
OAuth2ClientDO dbClientDO = oauth2ClientService.getOAuth2ClientFromCache(clientId); |
|
155 |
assertPojoEquals(clientDO, dbClientDO); |
|
156 |
} |
|
157 |
|
|
158 |
@Test |
|
159 |
public void testGetOAuth2ClientPage() { |
|
160 |
// mock 数据 |
|
161 |
OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到 |
|
162 |
o.setName("潜龙"); |
|
163 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
164 |
}); |
|
165 |
oauth2ClientMapper.insert(dbOAuth2Client); |
|
166 |
// 测试 name 不匹配 |
|
167 |
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setName("凤凰"))); |
|
168 |
// 测试 status 不匹配 |
|
169 |
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
170 |
// 准备参数 |
|
171 |
OAuth2ClientPageReqVO reqVO = new OAuth2ClientPageReqVO(); |
|
172 |
reqVO.setName("龙"); |
|
173 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
174 |
|
|
175 |
// 调用 |
|
176 |
PageResult<OAuth2ClientDO> pageResult = oauth2ClientService.getOAuth2ClientPage(reqVO); |
|
177 |
// 断言 |
|
178 |
assertEquals(1, pageResult.getTotal()); |
|
179 |
assertEquals(1, pageResult.getList().size()); |
|
180 |
assertPojoEquals(dbOAuth2Client, pageResult.getList().get(0)); |
|
181 |
} |
|
182 |
|
|
183 |
@Test |
|
184 |
public void testValidOAuthClientFromCache() { |
|
185 |
try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { |
|
186 |
springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(OAuth2ClientServiceImpl.class))) |
|
187 |
.thenReturn(oauth2ClientService); |
|
188 |
|
|
189 |
// mock 方法 |
|
190 |
OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("default") |
|
191 |
.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
192 |
oauth2ClientMapper.insert(client); |
|
193 |
OAuth2ClientDO client02 = randomPojo(OAuth2ClientDO.class).setClientId("disable") |
|
194 |
.setStatus(CommonStatusEnum.DISABLE.getStatus()); |
|
195 |
oauth2ClientMapper.insert(client02); |
|
196 |
|
|
197 |
// 调用,并断言 |
|
198 |
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache(randomString(), |
|
199 |
null, null, null, null), OAUTH2_CLIENT_NOT_EXISTS); |
|
200 |
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("disable", |
|
201 |
null, null, null, null), OAUTH2_CLIENT_DISABLE); |
|
202 |
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", |
|
203 |
randomString(), null, null, null), OAUTH2_CLIENT_CLIENT_SECRET_ERROR); |
|
204 |
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", |
|
205 |
null, randomString(), null, null), OAUTH2_CLIENT_AUTHORIZED_GRANT_TYPE_NOT_EXISTS); |
|
206 |
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", |
|
207 |
null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER); |
|
208 |
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", |
|
209 |
null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test"); |
|
210 |
// 成功调用(1:参数完整) |
|
211 |
OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(), |
|
212 |
client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0)); |
|
213 |
assertPojoEquals(client, result); |
|
214 |
// 成功调用(2:只有 clientId 参数) |
|
215 |
result = oauth2ClientService.validOAuthClientFromCache(client.getClientId()); |
|
216 |
assertPojoEquals(client, result); |
|
217 |
} |
|
218 |
} |
|
219 |
|
|
220 |
} |