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