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 dbab26b0f92 Add more test cases on OpenGaussComBatchBindPacketTest and
OpenGaussCommandPacketFactoryTest (#38196)
dbab26b0f92 is described below
commit dbab26b0f92a3e016286007dfc12b15496702b71
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 25 19:38:56 2026 +0800
Add more test cases on OpenGaussComBatchBindPacketTest and
OpenGaussCommandPacketFactoryTest (#38196)
---
.../command/OpenGaussCommandPacketFactoryTest.java | 124 ++++++++++++++++++---
.../bind/OpenGaussComBatchBindPacketTest.java | 82 +++++++++++++-
2 files changed, 190 insertions(+), 16 deletions(-)
diff --git
a/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/OpenGaussCommandPacketFactoryTest.java
b/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/OpenGaussCommandPacketFactoryTest.java
index 64b17d91c9e..da730f3bf06 100644
---
a/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/OpenGaussCommandPacketFactoryTest.java
+++
b/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/OpenGaussCommandPacketFactoryTest.java
@@ -18,35 +18,133 @@
package org.apache.shardingsphere.database.protocol.opengauss.packet.command;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import
org.apache.shardingsphere.database.protocol.opengauss.packet.command.bind.OpenGaussComBatchBindPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.admin.PostgreSQLUnsupportedCommandPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.PostgreSQLAggregatedCommandPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLComBindPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.close.PostgreSQLComClosePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.describe.PostgreSQLComDescribePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.execute.PostgreSQLComExecutePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.flush.PostgreSQLComFlushPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.parse.PostgreSQLComParsePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.sync.PostgreSQLComSyncPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.simple.PostgreSQLComQueryPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.generic.PostgreSQLComTerminationPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.MockedConstruction;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-@ExtendWith(MockitoExtension.class)
class OpenGaussCommandPacketFactoryTest {
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
- private PostgreSQLPacketPayload payload;
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("nonExtendedProtocolCases")
+ void assertNewInstanceWithNonExtendedProtocol(final String name, final
PostgreSQLCommandPacketType commandPacketType,
+ final Class<? extends
PostgreSQLCommandPacket> expectedPacketClass) {
+ PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class,
RETURNS_DEEP_STUBS);
+ when(payload.readStringNul()).thenReturn("");
+ ByteBuf byteBuf = payload.getByteBuf();
+
assertThat(OpenGaussCommandPacketFactory.newInstance(commandPacketType,
payload), isA(expectedPacketClass));
+ verify(byteBuf).skipBytes(1);
+ }
@Test
- void assertNewOpenGaussComBatchBindPacket() {
- when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
-
assertThat(OpenGaussCommandPacketFactory.newInstance(OpenGaussCommandPacketType.BATCH_BIND_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ void assertNewInstanceWithExtendedProtocolWithoutPackets() {
+ PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class);
+ PostgreSQLCommandPacket actual =
OpenGaussCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.PARSE_COMMAND,
payload);
+ assertThat(actual, isA(PostgreSQLAggregatedCommandPacket.class));
+ assertTrue(((PostgreSQLAggregatedCommandPacket)
actual).getPackets().isEmpty());
}
- @Test
- void assertNewPostgreSQLPacket() {
-
assertThat(OpenGaussCommandPacketFactory.newInstance(mock(PostgreSQLCommandPacketType.class),
payload), isA(PostgreSQLCommandPacket.class));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("extendedProtocolCases")
+ void assertNewInstanceWithExtendedProtocol(final String name, final int
commandTypeValue, final Class<? extends PostgreSQLCommandPacket>
expectedPacketClass) {
+ PostgreSQLPacketPayload payload =
createExtendedPayload(commandTypeValue);
+ try (MockedConstruction<?> ignored =
mockCommandPacketConstruction(expectedPacketClass)) {
+ PostgreSQLCommandPacket actual =
OpenGaussCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.PARSE_COMMAND,
payload);
+ assertThat(actual, isA(PostgreSQLAggregatedCommandPacket.class));
+ List<PostgreSQLCommandPacket> actualPackets =
((PostgreSQLAggregatedCommandPacket) actual).getPackets();
+ assertThat(actualPackets.size(), is(1));
+ assertThat(actualPackets.get(0), isA(expectedPacketClass));
+ }
+ }
+
+ private static MockedConstruction<?> mockCommandPacketConstruction(final
Class<? extends PostgreSQLCommandPacket> expectedPacketClass) {
+ if (OpenGaussComBatchBindPacket.class == expectedPacketClass) {
+ return mockConstruction(OpenGaussComBatchBindPacket.class);
+ }
+ if (PostgreSQLComParsePacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComParsePacket.class);
+ }
+ if (PostgreSQLComBindPacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComBindPacket.class);
+ }
+ if (PostgreSQLComDescribePacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComDescribePacket.class);
+ }
+ if (PostgreSQLComExecutePacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComExecutePacket.class);
+ }
+ if (PostgreSQLComSyncPacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComSyncPacket.class);
+ }
+ if (PostgreSQLComClosePacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComClosePacket.class);
+ }
+ if (PostgreSQLComFlushPacket.class == expectedPacketClass) {
+ return mockConstruction(PostgreSQLComFlushPacket.class);
+ }
+ throw new IllegalArgumentException(String.format("Unsupported expected
packet class: %s", expectedPacketClass.getName()));
+ }
+
+ private PostgreSQLPacketPayload createExtendedPayload(final int
commandTypeValue) {
+ PostgreSQLPacketPayload result = mock(PostgreSQLPacketPayload.class);
+ ByteBuf byteBuf = mock(ByteBuf.class);
+ when(result.hasCompletePacket()).thenReturn(true, false);
+ when(result.readInt1()).thenReturn(commandTypeValue);
+ when(result.getByteBuf()).thenReturn(byteBuf);
+ when(result.getCharset()).thenReturn(StandardCharsets.UTF_8);
+ when(byteBuf.readerIndex()).thenReturn(0);
+ when(byteBuf.getInt(0)).thenReturn(4);
+ when(byteBuf.readSlice(4)).thenReturn(Unpooled.wrappedBuffer(new
byte[]{0, 0, 0, 0}));
+ return result;
+ }
+
+ private static Stream<Arguments> nonExtendedProtocolCases() {
+ return Stream.of(
+ Arguments.of("simpleQueryType",
PostgreSQLCommandPacketType.SIMPLE_QUERY, PostgreSQLComQueryPacket.class),
+ Arguments.of("terminationType",
PostgreSQLCommandPacketType.TERMINATE, PostgreSQLComTerminationPacket.class),
+ Arguments.of("unsupportedPasswordType",
PostgreSQLCommandPacketType.PASSWORD,
PostgreSQLUnsupportedCommandPacket.class));
+ }
+
+ private static Stream<Arguments> extendedProtocolCases() {
+ return Stream.of(
+ Arguments.of("batchBindType", (int)
OpenGaussCommandPacketType.BATCH_BIND_COMMAND.getValue(),
OpenGaussComBatchBindPacket.class),
+ Arguments.of("parseType", (int)
PostgreSQLCommandPacketType.PARSE_COMMAND.getValue(),
PostgreSQLComParsePacket.class),
+ Arguments.of("bindType", (int)
PostgreSQLCommandPacketType.BIND_COMMAND.getValue(),
PostgreSQLComBindPacket.class),
+ Arguments.of("describeType", (int)
PostgreSQLCommandPacketType.DESCRIBE_COMMAND.getValue(),
PostgreSQLComDescribePacket.class),
+ Arguments.of("executeType", (int)
PostgreSQLCommandPacketType.EXECUTE_COMMAND.getValue(),
PostgreSQLComExecutePacket.class),
+ Arguments.of("syncType", (int)
PostgreSQLCommandPacketType.SYNC_COMMAND.getValue(),
PostgreSQLComSyncPacket.class),
+ Arguments.of("closeType", (int)
PostgreSQLCommandPacketType.CLOSE_COMMAND.getValue(),
PostgreSQLComClosePacket.class),
+ Arguments.of("flushType", (int)
PostgreSQLCommandPacketType.FLUSH_COMMAND.getValue(),
PostgreSQLComFlushPacket.class));
}
}
diff --git
a/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/bind/OpenGaussComBatchBindPacketTest.java
b/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/bind/OpenGaussComBatchBindPacketTest.java
index ac1668107d3..144177c3e49 100644
---
a/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/bind/OpenGaussComBatchBindPacketTest.java
+++
b/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/command/bind/OpenGaussComBatchBindPacketTest.java
@@ -17,18 +17,30 @@
package
org.apache.shardingsphere.database.protocol.opengauss.packet.command.bind;
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+import
org.apache.shardingsphere.database.protocol.opengauss.packet.command.OpenGaussCommandPacketType;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Stream;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
class OpenGaussComBatchBindPacketTest {
@@ -55,10 +67,74 @@ class OpenGaussComBatchBindPacketTest {
assertThat(actual.getEachGroupParametersCount(), is(3));
assertThat(actual.getParameterFormats(), is(Arrays.asList(0, 0, 0)));
assertTrue(actual.getResultFormats().isEmpty());
- List<List<Object>> actualParameterSets = actual.readParameterSets(
- Arrays.asList(PostgreSQLColumnType.INT4,
PostgreSQLColumnType.VARCHAR, PostgreSQLColumnType.INT4));
+ List<List<Object>> actualParameterSets =
actual.readParameterSets(Arrays.asList(PostgreSQLColumnType.INT4,
PostgreSQLColumnType.VARCHAR, PostgreSQLColumnType.INT4));
assertThat(actualParameterSets.size(), is(3));
- List<List<Object>> expectedParameterSets =
Arrays.asList(Arrays.asList(1, "Foo", 18), Arrays.asList(2, "Bar", 36),
Arrays.asList(3, "Tom", 54));
+ assertThat(actualParameterSets, is(Arrays.asList(Arrays.asList(1,
"Foo", 18), Arrays.asList(2, "Bar", 36), Arrays.asList(3, "Tom", 54))));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("readParameterSetsCases")
+ void assertReadParameterSets(final String name, final
OpenGaussComBatchBindPacket packet, final PostgreSQLPacketPayload payload,
+ final List<PostgreSQLColumnType>
parameterTypes, final List<List<Object>> expectedParameterSets, final int
expectedSkipReservedBytes) {
+ List<List<Object>> actualParameterSets =
packet.readParameterSets(parameterTypes);
assertThat(actualParameterSets, is(expectedParameterSets));
+ verify(payload).skipReserved(expectedSkipReservedBytes);
+ }
+
+ @Test
+ void assertWrite() {
+ PostgreSQLPacketPayload actual = mock(PostgreSQLPacketPayload.class);
+ assertDoesNotThrow(() -> createPacketByMessage().write(actual));
+ verifyNoInteractions(actual);
+ }
+
+ @Test
+ void assertGetIdentifier() {
+ assertThat(createPacketByMessage().getIdentifier(),
is(OpenGaussCommandPacketType.BATCH_BIND_COMMAND));
+ }
+
+ private static Stream<Arguments> readParameterSetsCases() {
+ PostgreSQLPacketPayload textPayload =
mock(PostgreSQLPacketPayload.class);
+ ByteBuf textByteBuf = mock(ByteBuf.class);
+ when(textPayload.getByteBuf()).thenReturn(textByteBuf);
+ when(textPayload.getCharset()).thenReturn(StandardCharsets.UTF_8);
+ when(textPayload.readInt4()).thenReturn(0, 1, 1, 3);
+ when(textPayload.readStringNul()).thenReturn("", "S_1");
+ when(textPayload.readInt2()).thenReturn(1, 0, 1, 0, 2);
+ when(textByteBuf.readCharSequence(1,
StandardCharsets.UTF_8)).thenReturn("7");
+ when(textByteBuf.readCharSequence(3,
StandardCharsets.UTF_8)).thenReturn("foo");
+ when(textByteBuf.readableBytes()).thenReturn(6);
+ PostgreSQLPacketPayload emptyFormatPayload =
mock(PostgreSQLPacketPayload.class);
+ ByteBuf emptyFormatByteBuf = mock(ByteBuf.class);
+ when(emptyFormatPayload.getByteBuf()).thenReturn(emptyFormatByteBuf);
+
when(emptyFormatPayload.getCharset()).thenReturn(StandardCharsets.UTF_8);
+ when(emptyFormatPayload.readInt4()).thenReturn(0, 1, 2);
+ when(emptyFormatPayload.readStringNul()).thenReturn("", "S_2");
+ when(emptyFormatPayload.readInt2()).thenReturn(0, 0, 1);
+ when(emptyFormatByteBuf.readCharSequence(2,
StandardCharsets.UTF_8)).thenReturn("42");
+ when(emptyFormatByteBuf.readableBytes()).thenReturn(0);
+ PostgreSQLPacketPayload binaryPayload =
mock(PostgreSQLPacketPayload.class);
+ ByteBuf binaryByteBuf = mock(ByteBuf.class);
+ when(binaryPayload.getByteBuf()).thenReturn(binaryByteBuf);
+ when(binaryPayload.readInt4()).thenReturn(0, 1, -1, 4, 9);
+ when(binaryPayload.readStringNul()).thenReturn("", "S_3");
+ when(binaryPayload.readInt2()).thenReturn(2, 1, 1, 0, 2);
+ when(binaryByteBuf.readableBytes()).thenReturn(3);
+ OpenGaussComBatchBindPacket textPacket = new
OpenGaussComBatchBindPacket(textPayload);
+ OpenGaussComBatchBindPacket emptyFormatPacket = new
OpenGaussComBatchBindPacket(emptyFormatPayload);
+ OpenGaussComBatchBindPacket binaryPacket = new
OpenGaussComBatchBindPacket(binaryPayload);
+ return Stream.of(
+ Arguments.of("textParametersWithSingleTextFormat", textPacket,
textPayload, Arrays.asList(PostgreSQLColumnType.INT4,
PostgreSQLColumnType.VARCHAR),
+ Collections.singletonList(Arrays.asList(7, "foo")), 6),
+ Arguments.of("textParametersWhenFormatListIsEmpty",
emptyFormatPacket, emptyFormatPayload,
Collections.singletonList(PostgreSQLColumnType.INT4),
+
Collections.singletonList(Collections.singletonList(42)), 0),
+ Arguments.of("nullAndBinaryParameters", binaryPacket,
binaryPayload, Arrays.asList(PostgreSQLColumnType.INT4,
PostgreSQLColumnType.INT4),
+ Collections.singletonList(Arrays.asList(null, 9)), 3));
+ }
+
+ private static OpenGaussComBatchBindPacket createPacketByMessage() {
+ PostgreSQLPacketPayload payload = new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(BATCH_BIND_MESSAGE_BYTES),
StandardCharsets.UTF_8);
+ payload.readInt1();
+ return new OpenGaussComBatchBindPacket(payload);
}
}