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