提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.datapermission.core.rule; |
H |
2 |
|
|
3 |
import com.iailab.framework.datapermission.core.annotation.DataPermission; |
|
4 |
import com.iailab.framework.datapermission.core.aop.DataPermissionContextHolder; |
|
5 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
6 |
import net.sf.jsqlparser.expression.Alias; |
|
7 |
import net.sf.jsqlparser.expression.Expression; |
|
8 |
import org.junit.jupiter.api.BeforeEach; |
|
9 |
import org.junit.jupiter.api.Test; |
|
10 |
import org.mockito.InjectMocks; |
|
11 |
import org.mockito.Spy; |
|
12 |
import org.springframework.core.annotation.AnnotationUtils; |
|
13 |
|
|
14 |
import java.util.Arrays; |
|
15 |
import java.util.List; |
|
16 |
import java.util.Set; |
|
17 |
|
|
18 |
import static com.iailab.framework.test.core.util.RandomUtils.randomString; |
|
19 |
import static org.junit.jupiter.api.Assertions.*; |
|
20 |
|
|
21 |
/** |
|
22 |
* {@link DataPermissionRuleFactoryImpl} 单元测试 |
|
23 |
* |
|
24 |
* @author iailab |
|
25 |
*/ |
|
26 |
class DataPermissionRuleFactoryImplTest extends BaseMockitoUnitTest { |
|
27 |
|
|
28 |
@InjectMocks |
|
29 |
private DataPermissionRuleFactoryImpl dataPermissionRuleFactory; |
|
30 |
|
|
31 |
@Spy |
|
32 |
private List<DataPermissionRule> rules = Arrays.asList(new DataPermissionRule01(), |
|
33 |
new DataPermissionRule02()); |
|
34 |
|
|
35 |
@BeforeEach |
|
36 |
public void setUp() { |
|
37 |
DataPermissionContextHolder.clear(); |
|
38 |
} |
|
39 |
|
|
40 |
@Test |
|
41 |
public void testGetDataPermissionRule_02() { |
|
42 |
// 准备参数 |
|
43 |
String mappedStatementId = randomString(); |
|
44 |
|
|
45 |
// 调用 |
|
46 |
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|
47 |
// 断言 |
|
48 |
assertSame(rules, result); |
|
49 |
} |
|
50 |
|
|
51 |
@Test |
|
52 |
public void testGetDataPermissionRule_03() { |
|
53 |
// 准备参数 |
|
54 |
String mappedStatementId = randomString(); |
|
55 |
// mock 方法 |
|
56 |
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass03.class, DataPermission.class)); |
|
57 |
|
|
58 |
// 调用 |
|
59 |
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|
60 |
// 断言 |
|
61 |
assertTrue(result.isEmpty()); |
|
62 |
} |
|
63 |
|
|
64 |
@Test |
|
65 |
public void testGetDataPermissionRule_04() { |
|
66 |
// 准备参数 |
|
67 |
String mappedStatementId = randomString(); |
|
68 |
// mock 方法 |
|
69 |
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass04.class, DataPermission.class)); |
|
70 |
|
|
71 |
// 调用 |
|
72 |
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|
73 |
// 断言 |
|
74 |
assertEquals(1, result.size()); |
|
75 |
assertEquals(DataPermissionRule01.class, result.get(0).getClass()); |
|
76 |
} |
|
77 |
|
|
78 |
@Test |
|
79 |
public void testGetDataPermissionRule_05() { |
|
80 |
// 准备参数 |
|
81 |
String mappedStatementId = randomString(); |
|
82 |
// mock 方法 |
|
83 |
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass05.class, DataPermission.class)); |
|
84 |
|
|
85 |
// 调用 |
|
86 |
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|
87 |
// 断言 |
|
88 |
assertEquals(1, result.size()); |
|
89 |
assertEquals(DataPermissionRule02.class, result.get(0).getClass()); |
|
90 |
} |
|
91 |
|
|
92 |
@Test |
|
93 |
public void testGetDataPermissionRule_06() { |
|
94 |
// 准备参数 |
|
95 |
String mappedStatementId = randomString(); |
|
96 |
// mock 方法 |
|
97 |
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass06.class, DataPermission.class)); |
|
98 |
|
|
99 |
// 调用 |
|
100 |
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|
101 |
// 断言 |
|
102 |
assertSame(rules, result); |
|
103 |
} |
|
104 |
|
|
105 |
@DataPermission(enable = false) |
|
106 |
static class TestClass03 {} |
|
107 |
|
|
108 |
@DataPermission(includeRules = DataPermissionRule01.class) |
|
109 |
static class TestClass04 {} |
|
110 |
|
|
111 |
@DataPermission(excludeRules = DataPermissionRule01.class) |
|
112 |
static class TestClass05 {} |
|
113 |
|
|
114 |
@DataPermission |
|
115 |
static class TestClass06 {} |
|
116 |
|
|
117 |
static class DataPermissionRule01 implements DataPermissionRule { |
|
118 |
|
|
119 |
@Override |
|
120 |
public Set<String> getTableNames() { |
|
121 |
return null; |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public Expression getExpression(String tableName, Alias tableAlias) { |
|
126 |
return null; |
|
127 |
} |
|
128 |
|
|
129 |
} |
|
130 |
|
|
131 |
static class DataPermissionRule02 implements DataPermissionRule { |
|
132 |
|
|
133 |
@Override |
|
134 |
public Set<String> getTableNames() { |
|
135 |
return null; |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public Expression getExpression(String tableName, Alias tableAlias) { |
|
140 |
return null; |
|
141 |
} |
|
142 |
|
|
143 |
} |
|
144 |
|
|
145 |
} |