This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 4303ecb6377 Add more converter test cases (#37351)
4303ecb6377 is described below
commit 4303ecb6377a60e1926428d49c42de59c7298e29
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Dec 11 19:48:25 2025 +0800
Add more converter test cases (#37351)
---
.../segment/orderby/OrderByConverterTest.java | 61 ++++++++++++++
.../item/ColumnOrderByItemConverterTest.java | 92 +++++++++++++++++++++
.../item/ExpressionOrderByItemConverterTest.java | 94 ++++++++++++++++++++++
.../item/IndexOrderByItemConverterTest.java | 65 +++++++++++++++
.../item/OrderByItemConverterUtilsTest.java | 89 ++++++++++++++++++++
5 files changed, 401 insertions(+)
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/OrderByConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/OrderByConverterTest.java
new file mode 100644
index 00000000000..37c39efbe87
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/OrderByConverterTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.orderby;
+
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.OrderByItemSegment;
+import
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.orderby.item.OrderByItemConverterUtils;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(OrderByItemConverterUtils.class)
+class OrderByConverterTest {
+
+ @Test
+ void assertConvertReturnsEmptyForNullSegment() {
+ assertFalse(OrderByConverter.convert(null).isPresent());
+ }
+
+ @Test
+ void assertConvertReturnsSqlNodeList() {
+ Collection<OrderByItemSegment> orderByItems = new
ArrayList<>(Collections.singletonList(mock(OrderByItemSegment.class)));
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(OrderByItemConverterUtils.convert(orderByItems)).thenReturn(Collections.singleton(expectedNode));
+ Optional<SqlNodeList> actual = OrderByConverter.convert(new
OrderBySegment(0, 0, orderByItems));
+ assertTrue(actual.isPresent());
+ assertThat(actual.get().size(), is(1));
+ assertThat(actual.get().get(0), is(expectedNode));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/ColumnOrderByItemConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/ColumnOrderByItemConverterTest.java
new file mode 100644
index 00000000000..ac4b629f5f0
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/ColumnOrderByItemConverterTest.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.orderby.item;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.expression.impl.ColumnConverter;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ColumnConverter.class)
+class ColumnOrderByItemConverterTest {
+
+ @Test
+ void assertConvertReturnsEmptyWhenColumnConverterReturnsEmpty() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("col"));
+
when(ColumnConverter.convert(columnSegment)).thenReturn(Optional.empty());
+ assertFalse(ColumnOrderByItemConverter.convert(new
ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC, null)).isPresent());
+ }
+
+ @Test
+ void assertConvertReturnsOriginalNodeWhenAscAndNullsAbsent() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("col"));
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(ColumnConverter.convert(columnSegment)).thenReturn(Optional.of(expectedNode));
+ Optional<SqlNode> actual = ColumnOrderByItemConverter.convert(new
ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC, null));
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is(expectedNode));
+ }
+
+ @Test
+ void assertConvertWrapsDescAndNullsFirst() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("col"));
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(ColumnConverter.convert(columnSegment)).thenReturn(Optional.of(expectedNode));
+ Optional<SqlNode> actual = ColumnOrderByItemConverter.convert(new
ColumnOrderByItemSegment(columnSegment, OrderDirection.DESC,
NullsOrderType.FIRST));
+ assertTrue(actual.isPresent());
+ SqlBasicCall nullsFirstCall = (SqlBasicCall) actual.get();
+ assertThat(nullsFirstCall.getOperator(),
is(SqlStdOperatorTable.NULLS_FIRST));
+ SqlBasicCall descCall = (SqlBasicCall)
nullsFirstCall.getOperandList().get(0);
+ assertThat(descCall.getOperator(), is(SqlStdOperatorTable.DESC));
+ assertThat(descCall.getOperandList().get(0), is(expectedNode));
+ }
+
+ @Test
+ void assertConvertWrapsNullsLastWithoutDesc() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("col"));
+ ColumnOrderByItemSegment segment = new
ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC,
NullsOrderType.LAST);
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(ColumnConverter.convert(columnSegment)).thenReturn(Optional.of(expectedNode));
+ Optional<SqlNode> actual = ColumnOrderByItemConverter.convert(segment);
+ assertTrue(actual.isPresent());
+ SqlBasicCall nullsLastCall = (SqlBasicCall) actual.get();
+ assertThat(nullsLastCall, isA(SqlBasicCall.class));
+ assertThat(nullsLastCall.getOperator(),
is(SqlStdOperatorTable.NULLS_LAST));
+ assertThat(nullsLastCall.getOperandList().get(0), is(expectedNode));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/ExpressionOrderByItemConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/ExpressionOrderByItemConverterTest.java
new file mode 100644
index 00000000000..18fc4b6ebb1
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/ExpressionOrderByItemConverterTest.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.orderby.item;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ExpressionOrderByItemSegment;
+import
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.expression.ExpressionConverter;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ExpressionConverter.class)
+class ExpressionOrderByItemConverterTest {
+
+ @Test
+ void assertConvertReturnsEmptyForNullSegment() {
+ assertFalse(ExpressionOrderByItemConverter.convert(null).isPresent());
+ }
+
+ @Test
+ void assertConvertReturnsEmptyWhenExpressionConverterReturnsEmpty() {
+ ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.empty());
+ assertFalse(ExpressionOrderByItemConverter.convert(new
ExpressionOrderByItemSegment(0, 0, "expr", OrderDirection.ASC, null,
expressionSegment)).isPresent());
+ }
+
+ @Test
+ void assertConvertReturnsExpressionNodeWithoutNulls() {
+ ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.of(expectedNode));
+ Optional<SqlNode> actual = ExpressionOrderByItemConverter.convert(new
ExpressionOrderByItemSegment(0, 0, "expr", OrderDirection.ASC, null,
expressionSegment));
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is(expectedNode));
+ }
+
+ @Test
+ void assertConvertWrapsNullsFirst() {
+ ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.of(expectedNode));
+ Optional<SqlNode> actual = ExpressionOrderByItemConverter.convert(new
ExpressionOrderByItemSegment(0, 0, "expr", OrderDirection.ASC,
NullsOrderType.FIRST, expressionSegment));
+ assertTrue(actual.isPresent());
+ SqlBasicCall nullsFirstCall = (SqlBasicCall) actual.get();
+ assertThat(nullsFirstCall, isA(SqlBasicCall.class));
+ assertThat(nullsFirstCall.getOperator(),
is(SqlStdOperatorTable.NULLS_FIRST));
+ assertThat(nullsFirstCall.getOperandList().get(0), is(expectedNode));
+ }
+
+ @Test
+ void assertConvertWrapsNullsLast() {
+ ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+ SqlNode expectedNode = mock(SqlNode.class);
+
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.of(expectedNode));
+ Optional<SqlNode> actual = ExpressionOrderByItemConverter.convert(new
ExpressionOrderByItemSegment(0, 0, "expr", OrderDirection.ASC,
NullsOrderType.LAST, expressionSegment));
+ assertTrue(actual.isPresent());
+ SqlBasicCall nullsLastCall = (SqlBasicCall) actual.get();
+ assertThat(nullsLastCall, isA(SqlBasicCall.class));
+ assertThat(nullsLastCall.getOperator(),
is(SqlStdOperatorTable.NULLS_LAST));
+ assertThat(nullsLastCall.getOperandList().get(0), is(expectedNode));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/IndexOrderByItemConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/IndexOrderByItemConverterTest.java
new file mode 100644
index 00000000000..a448c648d92
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/IndexOrderByItemConverterTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.orderby.item;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.IndexOrderByItemSegment;
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class IndexOrderByItemConverterTest {
+
+ @Test
+ void assertConvertReturnsLiteralForAscWithoutNulls() {
+ Optional<SqlNode> actual = IndexOrderByItemConverter.convert(new
IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, null));
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), isA(SqlLiteral.class));
+ assertThat(((SqlLiteral) actual.get()).toValue(), is("2"));
+ }
+
+ @Test
+ void assertConvertWrapsDescWithNullsFirst() {
+ Optional<SqlNode> actual = IndexOrderByItemConverter.convert(new
IndexOrderByItemSegment(0, 0, 3, OrderDirection.DESC, NullsOrderType.FIRST));
+ assertTrue(actual.isPresent());
+ SqlBasicCall nullsFirstCall = (SqlBasicCall) actual.get();
+ assertThat(nullsFirstCall.getOperator(),
is(SqlStdOperatorTable.NULLS_FIRST));
+ SqlBasicCall descCall = (SqlBasicCall)
nullsFirstCall.getOperandList().get(0);
+ assertThat(descCall.getOperator(), is(SqlStdOperatorTable.DESC));
+ assertThat(((SqlLiteral) descCall.getOperandList().get(0)).toValue(),
is("3"));
+ }
+
+ @Test
+ void assertConvertWrapsNullsLastWithoutDesc() {
+ Optional<SqlNode> actual = IndexOrderByItemConverter.convert(new
IndexOrderByItemSegment(0, 0, 4, OrderDirection.ASC, NullsOrderType.LAST));
+ assertTrue(actual.isPresent());
+ SqlBasicCall nullsLastCall = (SqlBasicCall) actual.get();
+ assertThat(nullsLastCall.getOperator(),
is(SqlStdOperatorTable.NULLS_LAST));
+ assertThat(((SqlLiteral)
nullsLastCall.getOperandList().get(0)).toValue(), is("4"));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/OrderByItemConverterUtilsTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/OrderByItemConverterUtilsTest.java
new file mode 100644
index 00000000000..7f21cc80e24
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/orderby/item/OrderByItemConverterUtilsTest.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.orderby.item;
+
+import org.apache.calcite.sql.SqlNode;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ExpressionOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.IndexOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.OrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.TextOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings({ColumnOrderByItemConverter.class,
ExpressionOrderByItemConverter.class, IndexOrderByItemConverter.class})
+class OrderByItemConverterUtilsTest {
+
+ @Test
+ void assertConvertCoversSupportedBranches() {
+ ColumnOrderByItemSegment columnItem = new ColumnOrderByItemSegment(new
ColumnSegment(0, 0, new IdentifierValue("col")), OrderDirection.ASC, null);
+
when(ColumnOrderByItemConverter.convert(columnItem)).thenReturn(Optional.empty());
+ ExpressionOrderByItemSegment expressionItem = new
ExpressionOrderByItemSegment(0, 0, "expr", OrderDirection.ASC, null,
mock(ExpressionSegment.class));
+ SqlNode expectedExpressionNode = mock(SqlNode.class);
+
when(ExpressionOrderByItemConverter.convert(expressionItem)).thenReturn(Optional.of(expectedExpressionNode));
+ IndexOrderByItemSegment indexItem = new IndexOrderByItemSegment(0, 0,
1, OrderDirection.ASC, null);
+ SqlNode expectedIndexNode = mock(SqlNode.class);
+
when(IndexOrderByItemConverter.convert(indexItem)).thenReturn(Optional.of(expectedIndexNode));
+ OrderByItemSegment unsupportedItem = new OrderByItemSegment(0, 0,
OrderDirection.ASC, null) {
+ };
+ Collection<OrderByItemSegment> orderByItems = new ArrayList<>(4);
+ orderByItems.add(columnItem);
+ orderByItems.add(expressionItem);
+ orderByItems.add(indexItem);
+ orderByItems.add(unsupportedItem);
+ Collection<SqlNode> actual =
OrderByItemConverterUtils.convert(orderByItems);
+ assertThat(actual.size(), is(2));
+ Iterator<SqlNode> iterator = actual.iterator();
+ assertThat(iterator.next(), is(expectedExpressionNode));
+ assertThat(iterator.next(), is(expectedIndexNode));
+ }
+
+ @Test
+ void assertConvertThrowsForTextOrderByItem() {
+ TextOrderByItemSegment textOrderByItemSegment = new
TextOrderByItemSegment(0, 0, OrderDirection.ASC, NullsOrderType.FIRST) {
+
+ @Override
+ public String getText() {
+ return "text";
+ }
+ };
+ UnsupportedSQLOperationException ex =
assertThrows(UnsupportedSQLOperationException.class, () ->
OrderByItemConverterUtils.convert(Collections.singleton(textOrderByItemSegment)));
+ assertThat(ex.getMessage(), is("Unsupported SQL operation: unsupported
TextOrderByItemSegment."));
+ }
+}