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.rdl.alter.impl;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.sharding.distsql.segment.table.AutoTableRuleSegment;
23  import org.apache.shardingsphere.sharding.distsql.segment.table.TableRuleSegment;
24  import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableRuleStatement;
25  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
26  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.distsql.AutoTableRuleAssert;
27  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.distsql.TableRuleAssert;
28  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ExistingAssert;
29  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.distsql.ExpectedAutoTableRule;
30  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.distsql.ExpectedTableRule;
31  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase;
32  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.rdl.rule.sharding.AlterShardingAutoTableRuleStatementTestCase;
33  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.rdl.rule.sharding.AlterShardingTableRuleStatementTestCase;
34  
35  import java.util.Collection;
36  import java.util.List;
37  import java.util.stream.Collectors;
38  
39  import static org.junit.jupiter.api.Assertions.assertNotNull;
40  import static org.junit.jupiter.api.Assertions.assertNull;
41  
42  /**
43   * Alter sharding table rule statement assert.
44   */
45  @NoArgsConstructor(access = AccessLevel.PRIVATE)
46  public final class AlterShardingTableRuleStatementAssert {
47      
48      /**
49       * Assert alter sharding table rule statement is correct with expected parser result.
50       *
51       * @param assertContext assert context
52       * @param actual actual alter sharding table rule statement
53       * @param expected expected alter sharding table rule statement test case
54       */
55      public static void assertIs(final SQLCaseAssertContext assertContext, final AlterShardingTableRuleStatement actual, final SQLParserTestCase expected) {
56          if (ExistingAssert.assertIs(assertContext, actual, expected)) {
57              if (expected instanceof AlterShardingAutoTableRuleStatementTestCase) {
58                  AlterShardingAutoTableRuleStatementTestCase autoTableRuleStatementTestCase = (AlterShardingAutoTableRuleStatementTestCase) expected;
59                  Collection<AutoTableRuleSegment> actualAutoTableRules = actual.getRules().stream().map(AutoTableRuleSegment.class::cast).collect(Collectors.toList());
60                  assertShardingAutoTableRules(assertContext, actualAutoTableRules, autoTableRuleStatementTestCase.getRules());
61              } else {
62                  AlterShardingTableRuleStatementTestCase tableRuleStatementTestCase = (AlterShardingTableRuleStatementTestCase) expected;
63                  Collection<TableRuleSegment> actualTableRules = actual.getRules().stream().map(TableRuleSegment.class::cast).collect(Collectors.toList());
64                  assertShardingTableRules(assertContext, actualTableRules, tableRuleStatementTestCase.getRules());
65              }
66          }
67      }
68      
69      private static void assertShardingAutoTableRules(final SQLCaseAssertContext assertContext, final Collection<AutoTableRuleSegment> actual, final List<ExpectedAutoTableRule> expected) {
70          if (null == expected) {
71              assertNull(actual, assertContext.getText("Actual sharding auto table rule should not exist."));
72          } else {
73              assertNotNull(actual, assertContext.getText("Actual sharding auto table rule should exist."));
74              int count = 0;
75              for (AutoTableRuleSegment each : actual) {
76                  AutoTableRuleAssert.assertIs(assertContext, each, expected.get(count));
77                  count++;
78              }
79          }
80      }
81      
82      private static void assertShardingTableRules(final SQLCaseAssertContext assertContext, final Collection<TableRuleSegment> actual, final List<ExpectedTableRule> expected) {
83          if (null == expected) {
84              assertNull(actual, assertContext.getText("Actual sharding table rule should not exist."));
85          } else {
86              assertNotNull(actual, assertContext.getText("Actual sharding table rule should exist."));
87              int count = 0;
88              for (TableRuleSegment each : actual) {
89                  TableRuleAssert.assertIs(assertContext, each, expected.get(count));
90                  count++;
91              }
92          }
93      }
94  }