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.generic.table.SimpleTableSegment;
23  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterIndexStatement;
24  import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.AlterIndexStatementHandler;
25  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleAlterIndexStatement;
26  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
27  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.index.IndexAssert;
28  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.table.TableAssert;
29  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.AlterIndexStatementTestCase;
30  
31  import java.util.Optional;
32  
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  /**
37   * Alter index statement assert.
38   */
39  @NoArgsConstructor(access = AccessLevel.PRIVATE)
40  public final class AlterIndexStatementAssert {
41      
42      /**
43       * Assert alter index statement is correct with expected parser result.
44       * 
45       * @param assertContext assert context
46       * @param actual actual alter index statement
47       * @param expected expected alter index statement test case
48       */
49      public static void assertIs(final SQLCaseAssertContext assertContext, final AlterIndexStatement actual, final AlterIndexStatementTestCase expected) {
50          assertTable(assertContext, actual, expected);
51          assertIndex(assertContext, actual, expected);
52      }
53      
54      private static void assertTable(final SQLCaseAssertContext assertContext, final AlterIndexStatement actual, final AlterIndexStatementTestCase expected) {
55          Optional<SimpleTableSegment> tableSegment = AlterIndexStatementHandler.getSimpleTableSegment(actual);
56          if (null == expected.getTable()) {
57              assertFalse(tableSegment.isPresent(), assertContext.getText("Actual table segment should not exist."));
58          } else {
59              assertTrue(tableSegment.isPresent(), assertContext.getText("Actual table segment should exist."));
60              TableAssert.assertIs(assertContext, tableSegment.get(), expected.getTable());
61          }
62      }
63      
64      private static void assertIndex(final SQLCaseAssertContext assertContext, final AlterIndexStatement actual, final AlterIndexStatementTestCase expected) {
65          // TODO should assert index for all databases(mysql and sqlserver do not parse index right now)
66          if (actual instanceof OracleAlterIndexStatement && actual.getIndex().isPresent()) {
67              IndexAssert.assertIs(assertContext, actual.getIndex().get(), expected.getIndex());
68          }
69      }
70  }