This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 6c172d562c2 Add test case to MaskMergedResult (#30946)
6c172d562c2 is described below
commit 6c172d562c2a866e9e9a4b03a8872b390bed0532
Author: Frosky Lrupotkin <[email protected]>
AuthorDate: Sat Apr 20 20:44:17 2024 +0800
Add test case to MaskMergedResult (#30946)
* Add test case to MaskMergedResult
* Reformat the conde
* Reformat the code
---
.../mask/merge/dql/MaskMergedResultTest.java | 60 +++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/merge/dql/MaskMergedResultTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/merge/dql/MaskMergedResultTest.java
index 9ab9a05af3f..4f180c4d73f 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/merge/dql/MaskMergedResultTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/merge/dql/MaskMergedResultTest.java
@@ -38,9 +38,12 @@ import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@ExtendWith(MockitoExtension.class)
class MaskMergedResultTest {
@@ -59,6 +62,32 @@ class MaskMergedResultTest {
assertThat(new MaskMergedResult(mockMaskRule(),
mockSelectStatementContext(), mergedResult).getValue(1, String.class),
is("MASK_VALUE"));
}
+ @Test
+ void assertGetValueWithoutColumnProjection() throws SQLException {
+ when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ MaskRule maskRule = mock(MaskRule.class);
+ assertThat(new MaskMergedResult(maskRule,
mockSelectStatementContextWithoutColumnProjection(), mergedResult).getValue(1,
String.class), is("VALUE"));
+
+ }
+
+ @Test
+ void assertGetValueWithoutMaskTable() throws SQLException {
+ when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ assertThat(new MaskMergedResult(mockMaskRuleTableAbsent(),
mockSelectStatementContextForMaskTableAbsent(), mergedResult).getValue(1,
String.class), is("VALUE"));
+ }
+
+ @Test
+ void assertGetValueWithoutMaskAlgorithm() throws SQLException {
+ when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ assertThat(new MaskMergedResult(mockMaskAlgorithmAbsent(),
mockSelectStatementContext(), mergedResult).getValue(1, String.class),
is("VALUE"));
+ }
+
+ @Test
+ void assertGetValueWhenOriginalValueIsNull() throws SQLException {
+ when(mergedResult.getValue(1, Object.class)).thenReturn(null);
+ assertNull(new MaskMergedResult(mockMaskAlgorithmAbsent(),
mockSelectStatementContext(), mergedResult).getValue(1, Object.class));
+ }
+
@SuppressWarnings("unchecked")
private MaskRule mockMaskRule() {
MaskAlgorithm<String, String> maskAlgorithm =
mock(MaskAlgorithm.class);
@@ -79,6 +108,35 @@ class MaskMergedResultTest {
return result;
}
+ private SelectStatementContext
mockSelectStatementContextWithoutColumnProjection() {
+ SelectStatementContext result = mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS);
+
when(result.getProjectionsContext().findColumnProjection(anyInt())).thenReturn(Optional.empty());
+ return result;
+ }
+
+ private MaskRule mockMaskRuleTableAbsent() {
+ MaskRule result = mock(MaskRule.class);
+ when(result.findMaskTable(anyString())).thenReturn(Optional.empty());
+ return result;
+ }
+
+ private MaskRule mockMaskAlgorithmAbsent() {
+
+ MaskRule result = mock(MaskRule.class);
+ MaskTable maskTable = mock(MaskTable.class);
+ when(maskTable.findAlgorithm("col")).thenReturn(Optional.empty());
+ when(result.findMaskTable("tbl")).thenReturn(Optional.of(maskTable));
+ return result;
+ }
+
+ private SelectStatementContext
mockSelectStatementContextForMaskTableAbsent() {
+ ColumnProjection columnProjection = mock(ColumnProjection.class,
RETURNS_DEEP_STUBS);
+ when(columnProjection.getOriginalTable().getValue()).thenReturn("tbl");
+ SelectStatementContext result = mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS);
+
when(result.getProjectionsContext().findColumnProjection(1)).thenReturn(Optional.of(columnProjection));
+ return result;
+ }
+
@Test
void assertGetCalendarValue() throws SQLException {
Calendar calendar = Calendar.getInstance();