|
| 1 | +package io.r2dbc.postgresql.codec; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.r2dbc.postgresql.client.Parameter; |
| 5 | +import io.r2dbc.postgresql.client.ParameterAssert; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE; |
| 9 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 10 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 11 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.BOX; |
| 12 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT; |
| 13 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR; |
| 14 | +import static io.r2dbc.postgresql.util.ByteBufUtils.encode; |
| 15 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Unit tests for {@link BoxCodec}. |
| 21 | + */ |
| 22 | +public class BoxCodecUnitTest { |
| 23 | + |
| 24 | + private static final int dataType = BOX.getObjectId(); |
| 25 | + |
| 26 | + @Test |
| 27 | + void constructorNoByteBufAllocator() { |
| 28 | + assertThatIllegalArgumentException().isThrownBy(() -> new BoxCodec(null)) |
| 29 | + .withMessage("byteBufAllocator must not be null"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void decode() { |
| 34 | + Box box = Box.of(Point.of(1.9, 2.8), Point.of(3.7, 4.6)); |
| 35 | + |
| 36 | + ByteBuf boxTextFormat = encode(TEST, "((1.9, 2.8),(3.7, 4.6))"); |
| 37 | + assertThat(new BoxCodec(TEST).decode(boxTextFormat, dataType, FORMAT_TEXT, Box.class)) |
| 38 | + .isEqualTo(box); |
| 39 | + |
| 40 | + ByteBuf boxByteFormat = TEST.buffer(32) |
| 41 | + .writeDouble(1.9).writeDouble(2.8) |
| 42 | + .writeDouble(3.7).writeDouble(4.6); |
| 43 | + assertThat(new BoxCodec(TEST).decode(boxByteFormat, dataType, FORMAT_BINARY, Box.class)) |
| 44 | + .isEqualTo(box); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void decodeNoByteBuf() { |
| 49 | + assertThat(new BoxCodec(TEST).decode(null, dataType, FORMAT_TEXT, Box.class)).isNull(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void doCanDecode() { |
| 54 | + BoxCodec codec = new BoxCodec(TEST); |
| 55 | + |
| 56 | + assertThat(codec.doCanDecode(BOX, FORMAT_BINARY)).isTrue(); |
| 57 | + assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse(); |
| 58 | + assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void doCanDecodeNoFormat() { |
| 63 | + assertThatIllegalArgumentException().isThrownBy(() -> new BoxCodec(TEST).doCanDecode(VARCHAR, null)) |
| 64 | + .withMessage("format must not be null"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void doCanDecodeNoType() { |
| 69 | + assertThatIllegalArgumentException().isThrownBy(() -> new BoxCodec(TEST).doCanDecode(null, FORMAT_TEXT)) |
| 70 | + .withMessage("type must not be null"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void doEncode() { |
| 75 | + Box box = Box.of(Point.of(1.9, 2.8), Point.of(3.7, 4.6)); |
| 76 | + |
| 77 | + ParameterAssert.assertThat(new BoxCodec(TEST).doEncode(box)) |
| 78 | + .hasFormat(FORMAT_BINARY) |
| 79 | + .hasType(dataType) |
| 80 | + .hasValue(TEST.buffer(32) |
| 81 | + .writeDouble(1.9) |
| 82 | + .writeDouble(2.8) |
| 83 | + .writeDouble(3.7) |
| 84 | + .writeDouble(4.6) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void doEncodeNoValue() { |
| 90 | + assertThatIllegalArgumentException().isThrownBy(() -> new BoxCodec(TEST).doEncode(null)) |
| 91 | + .withMessage("value must not be null"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void encodeNull() { |
| 96 | + ParameterAssert.assertThat(new BoxCodec(TEST).encodeNull()) |
| 97 | + .isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE)); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments