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.statement.ddl.CreateIndexStatement;
23  import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.CreateIndexStatementHandler;
24  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateIndexStatement;
25  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
26  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.column.ColumnAssert;
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.CreateIndexStatementTestCase;
30  
31  import static org.hamcrest.CoreMatchers.is;
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.junit.jupiter.api.Assertions.assertNotNull;
34  import static org.junit.jupiter.api.Assertions.assertNull;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  import static org.junit.jupiter.api.Assertions.assertFalse;
37  
38  /**
39   * Create index statement assert.
40   */
41  @NoArgsConstructor(access = AccessLevel.PRIVATE)
42  public final class CreateIndexStatementAssert {
43      
44      /**
45       * Assert create index statement is correct with expected parser result.
46       * 
47       * @param assertContext assert context
48       * @param actual actual create index statement
49       * @param expected expected create index statement test case
50       */
51      public static void assertIs(final SQLCaseAssertContext assertContext, final CreateIndexStatement actual, final CreateIndexStatementTestCase expected) {
52          assertTable(assertContext, actual, expected);
53          assertIndex(assertContext, actual, expected);
54          assertColumns(assertContext, actual, expected);
55          assertLockTable(assertContext, actual, expected);
56          assertAlgorithm(assertContext, actual, expected);
57      }
58      
59      private static void assertTable(final SQLCaseAssertContext assertContext, final CreateIndexStatement actual, final CreateIndexStatementTestCase expected) {
60          if (null == expected.getTable()) {
61              assertNull(actual.getTable(), assertContext.getText("Actual table segment should not exist."));
62          } else {
63              assertNotNull(actual.getTable(), assertContext.getText("Actual table segment should exist."));
64              TableAssert.assertIs(assertContext, actual.getTable(), expected.getTable());
65          }
66      }
67      
68      private static void assertIndex(final SQLCaseAssertContext assertContext, final CreateIndexStatement actual, final CreateIndexStatementTestCase expected) {
69          // TODO should assert index for all databases(mysql and sqlserver do not parse index right now)
70          if (actual instanceof OracleCreateIndexStatement) {
71              IndexAssert.assertIs(assertContext, actual.getIndex(), expected.getIndex());
72          }
73      }
74      
75      private static void assertColumns(final SQLCaseAssertContext assertContext, final CreateIndexStatement actual, final CreateIndexStatementTestCase expected) {
76          if (null == expected.getColumns()) {
77              assertTrue(actual.getColumns().isEmpty(), assertContext.getText("Actual columns segments should not exist."));
78          } else {
79              ColumnAssert.assertIs(assertContext, actual.getColumns(), expected.getColumns());
80          }
81      }
82      
83      private static void assertLockTable(final SQLCaseAssertContext assertContext, final CreateIndexStatement actual, final CreateIndexStatementTestCase expected) {
84          if (null == expected.getLockOption()) {
85              assertFalse(CreateIndexStatementHandler.getLockTableSegment(actual).isPresent(), assertContext.getText("Actual lock table segments should not exist."));
86          } else {
87              assertTrue(CreateIndexStatementHandler.getLockTableSegment(actual).isPresent(), assertContext.getText("Actual lock table segments should exist."));
88              assertThat(assertContext.getText(String.format("`%s`'s lock table assertion error: ", actual.getClass().getSimpleName())),
89                      CreateIndexStatementHandler.getLockTableSegment(actual).get().getLockTableOption().name(), is(expected.getLockOption().getType()));
90          }
91      }
92      
93      private static void assertAlgorithm(final SQLCaseAssertContext assertContext, final CreateIndexStatement actual, final CreateIndexStatementTestCase expected) {
94          if (null == expected.getAlgorithmOption()) {
95              assertFalse(CreateIndexStatementHandler.getAlgorithmTypeSegment(actual).isPresent(), assertContext.getText("Actual algorithm segments should not exist."));
96          } else {
97              assertTrue(CreateIndexStatementHandler.getAlgorithmTypeSegment(actual).isPresent(), assertContext.getText("Actual algorithm segments should exist."));
98              assertThat(assertContext.getText(String.format("`%s`'s algorithm assertion error: ", actual.getClass().getSimpleName())),
99                      CreateIndexStatementHandler.getAlgorithmTypeSegment(actual).get().getAlgorithmOption().name(), is(expected.getAlgorithmOption().getType()));
100         }
101     }
102 }