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.dml.impl;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.hint.OptionHintSegment;
23  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
24  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
25  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OutputSegment;
26  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
27  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
28  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.SQLSegmentAssert;
29  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.limit.LimitClauseAssert;
30  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.orderby.OrderByClauseAssert;
31  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.output.OutputClauseAssert;
32  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.set.SetClauseAssert;
33  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.table.TableAssert;
34  import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.where.WhereClauseAssert;
35  import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.dml.UpdateStatementTestCase;
36  
37  import java.util.Optional;
38  
39  import static org.hamcrest.CoreMatchers.is;
40  import static org.hamcrest.MatcherAssert.assertThat;
41  import static org.junit.jupiter.api.Assertions.assertFalse;
42  import static org.junit.jupiter.api.Assertions.assertNull;
43  import static org.junit.jupiter.api.Assertions.assertTrue;
44  
45  /**
46   * Update statement assert.
47   */
48  @NoArgsConstructor(access = AccessLevel.PRIVATE)
49  public final class UpdateStatementAssert {
50      
51      /**
52       * Assert update statement is correct with expected parser result.
53       *
54       * @param assertContext assert context
55       * @param actual actual update statement
56       * @param expected expected parser result
57       */
58      public static void assertIs(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
59          assertTable(assertContext, actual, expected);
60          assertSetClause(assertContext, actual, expected);
61          assertFromClause(assertContext, actual, expected);
62          assertWhereClause(assertContext, actual, expected);
63          assertOrderByClause(assertContext, actual, expected);
64          assertLimitClause(assertContext, actual, expected);
65          assertOptionHint(assertContext, actual, expected);
66          assertOutputClause(assertContext, actual, expected);
67      }
68      
69      private static void assertTable(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
70          if (null == expected.getTable()) {
71              assertNull(actual.getTable(), assertContext.getText("Actual from should not exist."));
72          } else {
73              TableAssert.assertIs(assertContext, actual.getTable(), expected.getTable());
74          }
75      }
76      
77      private static void assertSetClause(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
78          SetClauseAssert.assertIs(assertContext, actual.getSetAssignment(), expected.getSetClause());
79      }
80      
81      private static void assertFromClause(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
82          if (null == expected.getFrom()) {
83              assertFalse(actual.getFrom().isPresent(), assertContext.getText("Actual from segment should not exist."));
84          } else {
85              assertTrue(actual.getFrom().isPresent(), assertContext.getText("Actual from segment should exist."));
86              TableAssert.assertIs(assertContext, actual.getFrom().get(), expected.getFrom());
87          }
88      }
89      
90      private static void assertWhereClause(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
91          if (null == expected.getWhereClause()) {
92              assertFalse(actual.getWhere().isPresent(), assertContext.getText("Actual where segment should not exist."));
93          } else {
94              assertTrue(actual.getWhere().isPresent(), assertContext.getText("Actual where segment should exist."));
95              WhereClauseAssert.assertIs(assertContext, actual.getWhere().get(), expected.getWhereClause());
96          }
97      }
98      
99      private static void assertOrderByClause(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
100         Optional<OrderBySegment> orderBySegment = actual.getOrderBy();
101         if (null == expected.getOrderByClause()) {
102             assertFalse(orderBySegment.isPresent(), assertContext.getText("Actual order by segment should not exist."));
103         } else {
104             assertTrue(orderBySegment.isPresent(), assertContext.getText("Actual order by segment should exist."));
105             OrderByClauseAssert.assertIs(assertContext, orderBySegment.get(), expected.getOrderByClause());
106         }
107     }
108     
109     private static void assertLimitClause(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
110         Optional<LimitSegment> limitSegment = actual.getLimit();
111         if (null == expected.getLimitClause()) {
112             assertFalse(limitSegment.isPresent(), assertContext.getText("Actual limit segment should not exist."));
113         } else {
114             assertTrue(limitSegment.isPresent(), assertContext.getText("Actual limit segment should exist."));
115             LimitClauseAssert.assertRowCount(assertContext, limitSegment.get().getRowCount().orElse(null), expected.getLimitClause().getRowCount());
116             SQLSegmentAssert.assertIs(assertContext, limitSegment.get(), expected.getLimitClause());
117         }
118     }
119     
120     private static void assertOptionHint(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
121         Optional<OptionHintSegment> optionHintSegment = actual.getOptionHint();
122         if (null == expected.getOptionHint()) {
123             assertFalse(optionHintSegment.isPresent(), assertContext.getText("Actual option hint segment should not exist."));
124         } else {
125             assertTrue(optionHintSegment.isPresent(), assertContext.getText("Actual option hint segment should exist."));
126             assertThat(assertContext.getText("Option hint text assertion error: "), optionHintSegment.get().getText(), is(expected.getOptionHint().getText()));
127             SQLSegmentAssert.assertIs(assertContext, optionHintSegment.get(), expected.getOptionHint());
128         }
129     }
130     
131     private static void assertOutputClause(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final UpdateStatementTestCase expected) {
132         Optional<OutputSegment> outputSegment = actual.getOutput();
133         if (null == expected.getOutputClause()) {
134             assertFalse(outputSegment.isPresent(), assertContext.getText("Actual output segment should not exist."));
135         } else {
136             assertTrue(outputSegment.isPresent(), assertContext.getText("Actual output segment should exist."));
137             OutputClauseAssert.assertIs(assertContext, outputSegment.get(), expected.getOutputClause());
138         }
139     }
140 }