View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Drop index statement assert.
41   */
42  @NoArgsConstructor(access = AccessLevel.PRIVATE)
43  public final class DropIndexStatementAssert {
44      
45      /**
46       * Assert drop index statement is correct with expected parser result.
47       * 
48       * @param assertContext assert context
49       * @param actual actual drop index statement
50       * @param expected expected drop index statement test case
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          // TODO should assert index for all databases(mysql and sqlserver do not parse index right now)
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  }