1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl;
19
20 import lombok.AccessLevel;
21 import lombok.NoArgsConstructor;
22 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
23 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
24 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropIndexStatement;
25 import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.DropIndexStatementHandler;
26 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleDropIndexStatement;
27 import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
28 import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.index.IndexAssert;
29 import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.table.TableAssert;
30 import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.DropIndexStatementTestCase;
31
32 import java.util.Optional;
33
34 import static org.hamcrest.CoreMatchers.is;
35 import static org.hamcrest.MatcherAssert.assertThat;
36 import static org.junit.jupiter.api.Assertions.assertFalse;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38
39
40
41
42 @NoArgsConstructor(access = AccessLevel.PRIVATE)
43 public final class DropIndexStatementAssert {
44
45
46
47
48
49
50
51
52 public static void assertIs(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
53 assertTables(assertContext, actual, expected);
54 assertIndex(assertContext, actual, expected);
55 assertLockTable(assertContext, actual, expected);
56 assertAlgorithm(assertContext, actual, expected);
57 }
58
59 private static void assertTables(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
60 Optional<SimpleTableSegment> simpleTableSegment = DropIndexStatementHandler.getSimpleTableSegment(actual);
61 if (null == expected.getTable()) {
62 assertFalse(simpleTableSegment.isPresent(), assertContext.getText("Actual table segment should not exist."));
63 } else {
64 assertTrue(simpleTableSegment.isPresent(), assertContext.getText("Actual table segment should exist."));
65 TableAssert.assertIs(assertContext, simpleTableSegment.get(), expected.getTable());
66 }
67 }
68
69 private static void assertIndex(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
70
71 if (actual instanceof OracleDropIndexStatement) {
72 int count = 0;
73 for (IndexSegment each : actual.getIndexes()) {
74 IndexAssert.assertIs(assertContext, each, expected.getIndexes().get(count));
75 count++;
76 }
77 }
78 }
79
80 private static void assertLockTable(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
81 if (null == expected.getLockOption()) {
82 assertFalse(DropIndexStatementHandler.getLockTableSegment(actual).isPresent(), assertContext.getText("Actual lock table segments should not exist."));
83 } else {
84 assertTrue(DropIndexStatementHandler.getLockTableSegment(actual).isPresent(), assertContext.getText("Actual lock table segments should exist."));
85 assertThat(assertContext.getText(String.format("`%s`'s lock table assertion error: ", actual.getClass().getSimpleName())),
86 DropIndexStatementHandler.getLockTableSegment(actual).get().getLockTableOption().name(), is(expected.getLockOption().getType()));
87 }
88 }
89
90 private static void assertAlgorithm(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
91 if (null == expected.getAlgorithmOption()) {
92 assertFalse(DropIndexStatementHandler.getAlgorithmTypeSegment(actual).isPresent(), assertContext.getText("Actual algorithm segments should not exist."));
93 } else {
94 assertTrue(DropIndexStatementHandler.getAlgorithmTypeSegment(actual).isPresent(), assertContext.getText("Actual algorithm segments should exist."));
95 assertThat(assertContext.getText(String.format("`%s`'s algorithm assertion error: ", actual.getClass().getSimpleName())),
96 DropIndexStatementHandler.getAlgorithmTypeSegment(actual).get().getAlgorithmOption().name(), is(expected.getAlgorithmOption().getType()));
97 }
98 }
99 }