|
| 1 | +/* |
| 2 | + * Copyright 2017-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.r2dbc.postgresql.codec; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.r2dbc.postgresql.client.Parameter; |
| 21 | +import io.r2dbc.postgresql.client.ParameterAssert; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE; |
| 25 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 26 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 27 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.CIRCLE; |
| 28 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR; |
| 29 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 32 | + |
| 33 | +/** |
| 34 | + * Unit tests for {@link CircleCodec}. |
| 35 | + */ |
| 36 | +final class CircleCodecUnitTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + void constructorNoByteBufAllocator() { |
| 40 | + assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(null)) |
| 41 | + .withMessage("byteBufAllocator must not be null"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void doCanDecodeNoType() { |
| 46 | + assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doCanDecode(null, FORMAT_BINARY)) |
| 47 | + .withMessage("type must not be null"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void doCanDecode() { |
| 52 | + CircleCodec codec = new CircleCodec(TEST); |
| 53 | + |
| 54 | + assertThat(codec.doCanDecode(VARCHAR, FORMAT_BINARY)).isFalse(); |
| 55 | + assertThat(codec.doCanDecode(CIRCLE, FORMAT_TEXT)).isTrue(); |
| 56 | + assertThat(codec.doCanDecode(CIRCLE, FORMAT_BINARY)).isTrue(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void doDecodeNoByteBuf() { |
| 61 | + assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doDecode(null, CIRCLE, FORMAT_BINARY, Circle.class)) |
| 62 | + .withMessage("byteBuf must not be null"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void doDecodeNoType() { |
| 67 | + assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doDecode(TEST.buffer(), CIRCLE, FORMAT_BINARY, null)) |
| 68 | + .withMessage("type must not be null"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void doDecodeNoFormat() { |
| 73 | + assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doDecode(TEST.buffer(), CIRCLE, null, Circle.class)) |
| 74 | + .withMessage("format must not be null"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void doDecode() { |
| 79 | + CircleCodec codec = new CircleCodec(TEST); |
| 80 | + Point point = Point.of(1.12, 2.12); |
| 81 | + Circle circle = new Circle(point, 3.12); |
| 82 | + ByteBuf circleAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12).writeDouble(3.12); |
| 83 | + assertThat(codec.doDecode(circleAsBinary, CIRCLE, FORMAT_BINARY, Circle.class)).isEqualTo(circle); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void doEncodeNoValue() { |
| 88 | + assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doEncode(null)) |
| 89 | + .withMessage("value must not be null"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void doEncode() { |
| 94 | + CircleCodec codec = new CircleCodec(TEST); |
| 95 | + ByteBuf circleAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12).writeDouble(3.12); |
| 96 | + |
| 97 | + ParameterAssert.assertThat(codec.doEncode(new Circle(Point.of(1.12, 2.12), 3.12))) |
| 98 | + .hasFormat(FORMAT_BINARY) |
| 99 | + .hasType(CIRCLE.getObjectId()) |
| 100 | + .hasValue(circleAsBinary); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void encodeNull() { |
| 105 | + ParameterAssert.assertThat(new CircleCodec(TEST).encodeNull()) |
| 106 | + .isEqualTo(new Parameter(FORMAT_BINARY, CIRCLE.getObjectId(), NULL_VALUE)); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments