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.segment.distsql.rdl;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment;
23  import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment;
24  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
25  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.distsql.rdl.ExpectedMaskColumn;
26  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.distsql.rdl.ExpectedMaskRule;
27  
28  import java.util.Collection;
29  import java.util.List;
30  
31  import static org.hamcrest.CoreMatchers.is;
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertNotNull;
35  import static org.junit.jupiter.api.Assertions.assertNull;
36  
37  /**
38   * Mask rule assert.
39   */
40  @NoArgsConstructor(access = AccessLevel.PRIVATE)
41  public final class MaskRuleAssert {
42      
43      /**
44       * Assert mask rule is correct with expected parser result.
45       *
46       * @param assertContext assert context
47       * @param actual actual mask rule
48       * @param expected expected mask rule test case
49       */
50      public static void assertIs(final SQLCaseAssertContext assertContext, final MaskRuleSegment actual, final ExpectedMaskRule expected) {
51          if (null == expected) {
52              assertNull(actual, assertContext.getText("Actual mask rule should not exist."));
53          } else {
54              assertNotNull(actual, assertContext.getText("Actual mask rule should exist."));
55              assertThat(assertContext.getText("mask rule assertion error: "), actual.getTableName(), is(expected.getName()));
56              assertMaskColumns(assertContext, actual.getColumns(), expected.getColumns());
57          }
58      }
59      
60      private static void assertMaskColumns(final SQLCaseAssertContext assertContext, final Collection<MaskColumnSegment> actual, final List<ExpectedMaskColumn> expected) {
61          if (expected.isEmpty()) {
62              assertNull(actual, assertContext.getText("Actual mask column should not exist."));
63          } else {
64              assertFalse(actual.isEmpty(), assertContext.getText("Actual mask column should exist."));
65              assertThat(assertContext.getText(String.format("Actual mask column size should be %s, but it was %s.", expected.size(), actual.size())), actual.size(), is(expected.size()));
66              int count = 0;
67              for (MaskColumnSegment each : actual) {
68                  ExpectedMaskColumn expectedMaskColumn = expected.get(count);
69                  MaskColumnAssert.assertIs(assertContext, each, expectedMaskColumn);
70                  count++;
71              }
72          }
73      }
74  }