|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.ing.data.cassandra.jdbc.codec; |
| 15 | + |
| 16 | +import com.datastax.oss.driver.api.core.ProtocolVersion; |
| 17 | +import com.datastax.oss.driver.api.core.type.DataTypes; |
| 18 | +import com.datastax.oss.driver.api.core.type.reflect.GenericType; |
| 19 | +import org.apache.commons.lang3.StringUtils; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | + |
| 24 | +import static com.ing.data.cassandra.jdbc.utils.DriverUtil.NULL_KEYWORD; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 28 | + |
| 29 | +public class TinyintToShortCodecTest { |
| 30 | + |
| 31 | + private final TinyintToShortCodec sut = new TinyintToShortCodec(); |
| 32 | + |
| 33 | + @Test |
| 34 | + void givenCodec_whenGetJavaType_returnShort() { |
| 35 | + assertEquals(GenericType.SHORT, sut.getJavaType()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void givenCodec_whenGetCqlType_returnTinyint() { |
| 40 | + assertEquals(DataTypes.TINYINT, sut.getCqlType()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void givenNullValue_whenEncode_returnNull() { |
| 45 | + assertNull(sut.encode(null, ProtocolVersion.DEFAULT)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void givenValue_whenEncode_returnByteBuffer() { |
| 50 | + ByteBuffer bytes = sut.encode((short) 64, ProtocolVersion.DEFAULT); |
| 51 | + assertNotNull(bytes); |
| 52 | + assertEquals(64, bytes.getShort()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void givenNullValue_whenDecode_returnNull() { |
| 57 | + assertNull(sut.decode(null, ProtocolVersion.DEFAULT)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void givenValue_whenDecode_returnShort() { |
| 62 | + ByteBuffer bytes = ByteBuffer.allocate(1).put((byte) 64); |
| 63 | + bytes.position(0); |
| 64 | + assertEquals((short) 64, sut.decode(bytes, ProtocolVersion.DEFAULT)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void givenNullOrEmptyValue_whenParse_returnNull() { |
| 69 | + assertNull(sut.parse(null)); |
| 70 | + assertNull(sut.parse(NULL_KEYWORD)); |
| 71 | + assertNull(sut.parse(StringUtils.EMPTY)); |
| 72 | + assertNull(sut.parse(StringUtils.SPACE)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void givenNonNullValue_whenParse_returnExpectedValue() { |
| 77 | + assertEquals((short) 64, sut.parse("64")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void givenNullValue_whenFormat_returnNull() { |
| 82 | + assertEquals(NULL_KEYWORD, sut.format(null)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void givenNonNullValue_whenFormat_returnExpectedValue() { |
| 87 | + assertEquals("64", sut.format((short) 64)); |
| 88 | + } |
| 89 | +} |
0 commit comments