From 9d653b7958b840aeb654ea52881cae5dd06bdeda Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Mon, 24 Aug 2020 21:04:02 +0800 Subject: [PATCH] Add test case for pg codesc and constants (#7021) * add test case for pg codesc and constants * fix check style error check * fix mockito strict check * remove un finish test case --- .../PostgreSQLPacketCodecEngineTest.java | 102 ++++++++++++++++++ .../constant/PostgreSQLColumnTypeTest.java | 56 ++++++++++ .../constant/PostgreSQLErrorCodeTest.java | 36 +++++++ 3 files changed, 194 insertions(+) create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLColumnTypeTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCodeTest.java diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java new file mode 100644 index 0000000000..e78b10ed89 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java @@ -0,0 +1,102 @@ +/* + * 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.db.protocol.postgresql.codec; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket; +import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType; +import org.hamcrest.CoreMatchers; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.LinkedList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class PostgreSQLPacketCodecEngineTest { + + @Mock + private ChannelHandlerContext context; + + @Mock + private ByteBuf byteBuf; + + @Test + public void assertIsValidHeader() { + assertTrue(new PostgreSQLPacketCodecEngine().isValidHeader(50)); + } + + @Test + public void assertIsInvalidHeader() { + assertFalse(new PostgreSQLPacketCodecEngine().isValidHeader(PostgreSQLPacket.PAYLOAD_LENGTH)); + } + + @Test + public void assertDecode() { + when(byteBuf.markReaderIndex()).thenReturn(byteBuf); + when(byteBuf.readInt()).thenReturn(50); + List out = new LinkedList<>(); + new PostgreSQLPacketCodecEngine().decode(context, byteBuf, out, 54); + assertThat(out.size(), CoreMatchers.is(1)); + } + + @Test + public void assertDecodeWithStickyPacket() { + when(byteBuf.markReaderIndex()).thenReturn(byteBuf); + when(byteBuf.readInt()).thenReturn(50); + List out = new LinkedList<>(); + new PostgreSQLPacketCodecEngine().decode(context, byteBuf, out, 40); + assertTrue(out.isEmpty()); + } + + @Test + public void assertEncode() { + ByteBufAllocator byteBufAllocator = mock(ByteBufAllocator.class); + when(context.alloc()).thenReturn(byteBufAllocator); + ByteBuf payloadByteBuf = mock(ByteBuf.class); + when(byteBufAllocator.buffer()).thenReturn(payloadByteBuf); + when(payloadByteBuf.readableBytes()).thenReturn(50); + + PostgreSQLPacket actualMessage = mock(PostgreSQLPacket.class); + when(actualMessage.getMessageType()).thenReturn(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue()); + new PostgreSQLPacketCodecEngine().encode(context, actualMessage, byteBuf); + verify(actualMessage).write(ArgumentMatchers.any()); + verify(byteBuf).writeByte(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue()); + verify(byteBuf).writeInt(50 + PostgreSQLPacket.PAYLOAD_LENGTH); + verify(byteBuf).writeBytes(payloadByteBuf); + } + + @Test + public void assertCreatePacketPayload() { + assertThat(new PostgreSQLPacketCodecEngine().createPacketPayload(byteBuf).getByteBuf(), is(byteBuf)); + } + +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLColumnTypeTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLColumnTypeTest.java new file mode 100644 index 0000000000..fe21b023c6 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLColumnTypeTest.java @@ -0,0 +1,56 @@ +/* + * 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.db.protocol.postgresql.constant; + +import org.junit.Test; + +import java.sql.Types; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class PostgreSQLColumnTypeTest { + + @Test + public void assertValueOfJDBCType() { + PostgreSQLColumnType sqlColumnType = PostgreSQLColumnType.valueOfJDBCType(Types.BIGINT); + assertThat(sqlColumnType, is(PostgreSQLColumnType.POSTGRESQL_TYPE_INT8)); + } + + @Test(expected = IllegalArgumentException.class) + public void assertValueOfJDBCTypeExThrown() { + PostgreSQLColumnType.valueOfJDBCType(Types.REF_CURSOR); + } + + @Test + public void assertValueOf() { + PostgreSQLColumnType sqlColumnType = PostgreSQLColumnType.valueOf(PostgreSQLColumnType.POSTGRESQL_TYPE_INT8.getValue()); + assertThat(sqlColumnType, is(PostgreSQLColumnType.POSTGRESQL_TYPE_INT8)); + } + + @Test(expected = IllegalArgumentException.class) + public void assertValueOfExThrown() { + PostgreSQLColumnType.valueOf(9999); + } + + @Test + public void assertGetValue() { + assertThat(PostgreSQLColumnType.POSTGRESQL_TYPE_INT8.getValue(), is(20)); + } + +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCodeTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCodeTest.java new file mode 100644 index 0000000000..fc1115f359 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCodeTest.java @@ -0,0 +1,36 @@ +/* + * 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.db.protocol.postgresql.constant; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PostgreSQLErrorCodeTest { + + @Test + public void assertGetErrorCode() { + assertEquals(PostgreSQLErrorCode.PROTOCOL_VIOLATION.getErrorCode(), "08P01"); + } + + @Test + public void assertGetConditionName() { + assertEquals(PostgreSQLErrorCode.PROTOCOL_VIOLATION.getConditionName(), "protocol_violation"); + } + +} -- GitLab