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.statement.core.segment.ddl.index.IndexSegment;
23  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
24  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.index.DropIndexStatement;
25  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
26  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.index.IndexAssert;
27  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.table.TableAssert;
28  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.DropIndexStatementTestCase;
29  
30  import java.util.Optional;
31  
32  import static org.hamcrest.CoreMatchers.is;
33  import static org.hamcrest.MatcherAssert.assertThat;
34  import static org.junit.jupiter.api.Assertions.assertFalse;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  /**
38   * Drop index statement assert.
39   */
40  @NoArgsConstructor(access = AccessLevel.PRIVATE)
41  public final class DropIndexStatementAssert {
42      
43      /**
44       * Assert drop index statement is correct with expected parser result.
45       *
46       * @param assertContext assert context
47       * @param actual actual drop index statement
48       * @param expected expected drop index statement test case
49       */
50      public static void assertIs(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
51          assertTables(assertContext, actual, expected);
52          assertIndex(assertContext, actual, expected);
53          assertLockTable(assertContext, actual, expected);
54          assertAlgorithm(assertContext, actual, expected);
55      }
56      
57      private static void assertTables(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
58          Optional<SimpleTableSegment> simpleTableSegment = actual.getSimpleTable();
59          if (null == expected.getTable()) {
60              assertFalse(simpleTableSegment.isPresent(), assertContext.getText("Actual table segment should not exist."));
61          } else {
62              assertTrue(simpleTableSegment.isPresent(), assertContext.getText("Actual table segment should exist."));
63              TableAssert.assertIs(assertContext, simpleTableSegment.get(), expected.getTable());
64          }
65      }
66      
67      private static void assertIndex(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
68          int count = 0;
69          for (IndexSegment each : actual.getIndexes()) {
70              IndexAssert.assertIs(assertContext, each, expected.getIndexes().get(count));
71              count++;
72          }
73      }
74      
75      private static void assertLockTable(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
76          if (null == expected.getLockOption()) {
77              assertFalse(actual.getLockTable().isPresent(), assertContext.getText("Actual lock table segments should not exist."));
78          } else {
79              assertTrue(actual.getLockTable().isPresent(), assertContext.getText("Actual lock table segments should exist."));
80              assertThat(assertContext.getText(String.format("`%s`'s lock table assertion error: ", actual.getClass().getSimpleName())),
81                      actual.getLockTable().get().getLockTableOption().name(), is(expected.getLockOption().getType()));
82          }
83      }
84      
85      private static void assertAlgorithm(final SQLCaseAssertContext assertContext, final DropIndexStatement actual, final DropIndexStatementTestCase expected) {
86          if (null == expected.getAlgorithmOption()) {
87              assertFalse(actual.getAlgorithmType().isPresent(), assertContext.getText("Actual algorithm segments should not exist."));
88          } else {
89              assertTrue(actual.getAlgorithmType().isPresent(), assertContext.getText("Actual algorithm segments should exist."));
90              assertThat(assertContext.getText(String.format("`%s`'s algorithm assertion error: ", actual.getClass().getSimpleName())),
91                      actual.getAlgorithmType().get().getAlgorithmOption().name(), is(expected.getAlgorithmOption().getType()));
92          }
93      }
94  }