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