提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.datapermission.core.aop; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import com.iailab.framework.datapermission.core.annotation.DataPermission; |
|
5 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
6 |
import org.aopalliance.intercept.MethodInvocation; |
|
7 |
import org.junit.jupiter.api.BeforeEach; |
|
8 |
import org.junit.jupiter.api.Test; |
|
9 |
import org.mockito.InjectMocks; |
|
10 |
import org.mockito.Mock; |
|
11 |
|
|
12 |
import java.lang.reflect.Method; |
|
13 |
|
|
14 |
import static org.junit.jupiter.api.Assertions.*; |
|
15 |
import static org.mockito.Mockito.when; |
|
16 |
|
|
17 |
/** |
|
18 |
* {@link DataPermissionAnnotationInterceptor} 的单元测试 |
|
19 |
* |
|
20 |
* @author iailab |
|
21 |
*/ |
|
22 |
public class DataPermissionAnnotationInterceptorTest extends BaseMockitoUnitTest { |
|
23 |
|
|
24 |
@InjectMocks |
|
25 |
private DataPermissionAnnotationInterceptor interceptor; |
|
26 |
|
|
27 |
@Mock |
|
28 |
private MethodInvocation methodInvocation; |
|
29 |
|
|
30 |
@BeforeEach |
|
31 |
public void setUp() { |
|
32 |
interceptor.getDataPermissionCache().clear(); |
|
33 |
} |
|
34 |
|
|
35 |
@Test // 无 @DataPermission 注解 |
|
36 |
public void testInvoke_none() throws Throwable { |
|
37 |
// 参数 |
|
38 |
mockMethodInvocation(TestNone.class); |
|
39 |
|
|
40 |
// 调用 |
|
41 |
Object result = interceptor.invoke(methodInvocation); |
|
42 |
// 断言 |
|
43 |
assertEquals("none", result); |
|
44 |
assertEquals(1, interceptor.getDataPermissionCache().size()); |
|
45 |
assertTrue(CollUtil.getFirst(interceptor.getDataPermissionCache().values()).enable()); |
|
46 |
} |
|
47 |
|
|
48 |
@Test // 在 Method 上有 @DataPermission 注解 |
|
49 |
public void testInvoke_method() throws Throwable { |
|
50 |
// 参数 |
|
51 |
mockMethodInvocation(TestMethod.class); |
|
52 |
|
|
53 |
// 调用 |
|
54 |
Object result = interceptor.invoke(methodInvocation); |
|
55 |
// 断言 |
|
56 |
assertEquals("method", result); |
|
57 |
assertEquals(1, interceptor.getDataPermissionCache().size()); |
|
58 |
assertFalse(CollUtil.getFirst(interceptor.getDataPermissionCache().values()).enable()); |
|
59 |
} |
|
60 |
|
|
61 |
@Test // 在 Class 上有 @DataPermission 注解 |
|
62 |
public void testInvoke_class() throws Throwable { |
|
63 |
// 参数 |
|
64 |
mockMethodInvocation(TestClass.class); |
|
65 |
|
|
66 |
// 调用 |
|
67 |
Object result = interceptor.invoke(methodInvocation); |
|
68 |
// 断言 |
|
69 |
assertEquals("class", result); |
|
70 |
assertEquals(1, interceptor.getDataPermissionCache().size()); |
|
71 |
assertFalse(CollUtil.getFirst(interceptor.getDataPermissionCache().values()).enable()); |
|
72 |
} |
|
73 |
|
|
74 |
private void mockMethodInvocation(Class<?> clazz) throws Throwable { |
|
75 |
Object targetObject = clazz.newInstance(); |
|
76 |
Method method = targetObject.getClass().getMethod("echo"); |
|
77 |
when(methodInvocation.getThis()).thenReturn(targetObject); |
|
78 |
when(methodInvocation.getMethod()).thenReturn(method); |
|
79 |
when(methodInvocation.proceed()).then(invocationOnMock -> method.invoke(targetObject)); |
|
80 |
} |
|
81 |
|
|
82 |
static class TestMethod { |
|
83 |
|
|
84 |
@DataPermission(enable = false) |
|
85 |
public String echo() { |
|
86 |
return "method"; |
|
87 |
} |
|
88 |
|
|
89 |
} |
|
90 |
|
|
91 |
@DataPermission(enable = false) |
|
92 |
static class TestClass { |
|
93 |
|
|
94 |
public String echo() { |
|
95 |
return "class"; |
|
96 |
} |
|
97 |
|
|
98 |
} |
|
99 |
|
|
100 |
static class TestNone { |
|
101 |
|
|
102 |
public String echo() { |
|
103 |
return "none"; |
|
104 |
} |
|
105 |
|
|
106 |
} |
|
107 |
|
|
108 |
} |