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.shadow.distsql.segment.ShadowRuleSegment;
23  import org.apache.shardingsphere.shadow.distsql.statement.CreateShadowRuleStatement;
24  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
25  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.distsql.rdl.ShadowRuleAssert;
26  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ExistingAssert;
27  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.distsql.rdl.ExpectedShadowRule;
28  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.rdl.rule.shadow.CreateShadowRuleStatementTestCase;
29  
30  import java.util.Collection;
31  import java.util.List;
32  
33  import static org.hamcrest.CoreMatchers.is;
34  import static org.hamcrest.MatcherAssert.assertThat;
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  import static org.junit.jupiter.api.Assertions.assertNull;
37  
38  /**
39   * Create shadow rule statement assert.
40   */
41  @NoArgsConstructor(access = AccessLevel.PRIVATE)
42  public final class CreateShadowRuleStatementAssert {
43      
44      /**
45       * Assert create shadow rule statement is correct with expected parser result.
46       *
47       * @param assertContext assert context
48       * @param actual actual create shadow rule statement
49       * @param expected expected create shadow rule statement test case
50       */
51      public static void assertIs(final SQLCaseAssertContext assertContext, final CreateShadowRuleStatement actual, final CreateShadowRuleStatementTestCase expected) {
52          if (ExistingAssert.assertIs(assertContext, actual, expected)) {
53              assertThat(assertContext.getText("if not exists segment assertion error: "), actual.isIfNotExists(), is(expected.isIfNotExists()));
54              assertShadowRule(assertContext, actual.getRules(), expected.getRules());
55          }
56      }
57      
58      private static void assertShadowRule(final SQLCaseAssertContext assertContext, final Collection<ShadowRuleSegment> actual, final List<ExpectedShadowRule> expected) {
59          if (null == expected) {
60              assertNull(actual, assertContext.getText("Actual shadow rule should not exist."));
61          } else {
62              assertNotNull(actual, assertContext.getText("Actual shadow rule should exist."));
63              int count = 0;
64              for (ShadowRuleSegment each : actual) {
65                  ShadowRuleAssert.assertIs(assertContext, each, expected.get(count));
66                  count++;
67              }
68          }
69      }
70  }