From 122939c71f121f2fe953276b009b09ea1c84aef0 Mon Sep 17 00:00:00 2001 From: Dinu John <86094133+dinujoh@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:53:09 -0500 Subject: [PATCH] Add unit test for MySQL DataType handler Signed-off-by: Dinu John <86094133+dinujoh@users.noreply.github.com> --- .../source/rds/datatype/MySQLDataType.java | 2 + .../rds/datatype/impl/NumericTypeHandler.java | 35 ++++- .../rds/datatype/impl/SpatialTypeHandler.java | 5 +- .../datatype/impl/TemporalTypeHandler.java | 22 +--- .../datatype/impl/BinaryTypeHandlerTest.java | 31 +++++ .../datatype/impl/JsonTypeHandlerTest.java | 31 +++++ .../datatype/impl/NumericTypeHandlerTest.java | 123 ++++++++++++++++++ .../datatype/impl/SpatialTypeHandlerTest.java | 30 +++++ .../datatype/impl/StringTypeHandlerTest.java | 83 ++++++++++++ .../impl/TemporalTypeHandlerTest.java | 30 +++++ 10 files changed, 364 insertions(+), 28 deletions(-) create mode 100644 data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/BinaryTypeHandlerTest.java create mode 100644 data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/JsonTypeHandlerTest.java create mode 100644 data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandlerTest.java create mode 100644 data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandlerTest.java create mode 100644 data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/StringTypeHandlerTest.java create mode 100644 data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java diff --git a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/MySQLDataType.java b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/MySQLDataType.java index 2f0b2835ad..e1d54dd6b9 100644 --- a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/MySQLDataType.java +++ b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/MySQLDataType.java @@ -167,6 +167,8 @@ public long getUnsignedMask() { return 0xFFFFFFL; case INT_UNSIGNED: return 0xFFFFFFFFL; + case BIGINT_UNSIGNED: + return 0xFFFFFFFFFFFFFFFFL; default: throw new UnsupportedOperationException("No mask for non-unsigned type: " + this); } diff --git a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandler.java b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandler.java index 654dc95ac0..3835654c11 100644 --- a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandler.java +++ b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandler.java @@ -4,17 +4,35 @@ import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import java.math.BigInteger; + public class NumericTypeHandler implements DataTypeHandler { @Override public String handle(final MySQLDataType columnType, final String columnName, final Object value, final TableMetadata metadata) { - return handleNumericType(columnType, value); + if (value == null) { + return null; + } + + if (!columnType.isNumeric()) { + throw new IllegalArgumentException("ColumnType is not numeric: " + columnType); + } + + if (!(value instanceof Number)) { + throw new IllegalArgumentException("Value is not a number: " + value); + } + + return handleNumericType(columnType, (Number) value); } - private String handleNumericType(final MySQLDataType columnType, final Object value) { + private String handleNumericType(final MySQLDataType columnType, final Number value) { if (columnType.isUnsigned()) { - return handleUnsignedNumber((Number) value, columnType.getUnsignedMask()); + if(columnType == MySQLDataType.BIGINT_UNSIGNED) { + return handleUnsignedDouble(value); + } else { + return handleUnsignedNumber(value, columnType.getUnsignedMask()); + } } return value.toString(); } @@ -23,4 +41,15 @@ private String handleUnsignedNumber(final Number value, final long mask) { final long longVal = value.longValue(); return String.valueOf(longVal < 0 ? longVal & mask : longVal); } + + private String handleUnsignedDouble(final Number value) { + long longVal = value.longValue(); + if (longVal < 0) { + return BigInteger.valueOf(longVal & Long.MAX_VALUE) + .add(BigInteger.valueOf(Long.MAX_VALUE)) + .add(BigInteger.ONE) + .toString(); + } + return String.valueOf(longVal); + } } diff --git a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandler.java b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandler.java index 9f73eb5d4d..023b1613e5 100644 --- a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandler.java +++ b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandler.java @@ -9,10 +9,7 @@ public class SpatialTypeHandler implements DataTypeHandler { @Override public String handle(final MySQLDataType columnType, final String columnName, final Object value, final TableMetadata metadata) { - // Geometry types are typically returned as WKB (Well-Known Binary) - // Convert to WKT (Well-Known Text) or handle according to your needs - //return Base64.getEncoder().encodeToString((byte[]) value); - //return value.toString(); + // TODO: Implement the transformation return new String((byte[]) value); } } diff --git a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java index 6a123e0068..5511a36975 100644 --- a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java +++ b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java @@ -4,10 +4,6 @@ import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; -import java.time.LocalDate; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; - public class TemporalTypeHandler implements DataTypeHandler { @Override @@ -15,31 +11,15 @@ public String handle(final MySQLDataType columnType, final String columnName, fi final TableMetadata metadata) { // Date and Time types switch (columnType) { - // Date and Time types + // TODO: Implement the transformation case DATE: - return value instanceof LocalDate ? - ((LocalDate) value).format(DateTimeFormatter.ISO_LOCAL_DATE) : - value.toString(); - case TIME: - return value instanceof LocalTime ? - ((LocalTime) value).format(DateTimeFormatter.ISO_LOCAL_TIME) : - value.toString(); - case TIMESTAMP: case DATETIME: - /*if (value instanceof Timestamp) { - return ((LocalDateTime) value).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); - } else if (value instanceof LocalDateTime) { - return ((LocalDateTime) value).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); - }*/ - return value.toString(); - case YEAR: return value.toString(); default: throw new IllegalArgumentException("Unsupported temporal data type: " + columnType); } - } } diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/BinaryTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/BinaryTypeHandlerTest.java new file mode 100644 index 0000000000..680edc816e --- /dev/null +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/BinaryTypeHandlerTest.java @@ -0,0 +1,31 @@ +package org.opensearch.dataprepper.plugins.source.rds.datatype.impl; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.source.rds.datatype.DataTypeHandler; +import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Base64; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +public class BinaryTypeHandlerTest { + + @Test + public void test_handle() { + final DataTypeHandler handler = new BinaryTypeHandler(); + final MySQLDataType columnType = MySQLDataType.BINARY; + final String columnName = "binaryColumn"; + final byte[] testData = "Test binary data".getBytes(); + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + String result = handler.handle(columnType, columnName, testData, metadata); + + assertThat(result, is(Base64.getEncoder().encodeToString(testData))); + } +} diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/JsonTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/JsonTypeHandlerTest.java new file mode 100644 index 0000000000..b6505f50df --- /dev/null +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/JsonTypeHandlerTest.java @@ -0,0 +1,31 @@ +package org.opensearch.dataprepper.plugins.source.rds.datatype.impl; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.source.rds.datatype.DataTypeHandler; +import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class JsonTypeHandlerTest { + + @Test + public void test_handle() { + final DataTypeHandler handler = new JsonTypeHandler(); + final MySQLDataType columnType = MySQLDataType.JSON; + final String columnName = "jsonColumn"; + final String jsonValue = "{\"key\":\"value\"}"; + final byte[] testData = jsonValue.getBytes(); + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + String result = handler.handle(columnType, columnName, testData, metadata); + + assertThat(result, is(jsonValue)); + } +} diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandlerTest.java new file mode 100644 index 0000000000..f2352559b7 --- /dev/null +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/NumericTypeHandlerTest.java @@ -0,0 +1,123 @@ +package org.opensearch.dataprepper.plugins.source.rds.datatype.impl; + +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 org.opensearch.dataprepper.plugins.source.rds.datatype.DataTypeHandler; +import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import java.util.stream.Stream; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class NumericTypeHandlerTest { + @ParameterizedTest + @MethodSource("provideNumericTypeData") + public void test_handle(final MySQLDataType mySQLDataType, final String columnName, final Object value, final Object expectedValue) { + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + final DataTypeHandler numericTypeHandler = new NumericTypeHandler(); + String result = numericTypeHandler.handle(mySQLDataType, columnName, value, metadata); + + assertThat(result, is(expectedValue)); + } + + private static Stream provideNumericTypeData() { + return Stream.of( + // TINYINT tests (signed: -128 to 127) + Arguments.of(MySQLDataType.TINYINT, "tinyint_col", (byte)1, "1"), + Arguments.of(MySQLDataType.TINYINT, "tinyint_col", (byte)-128, "-128"), + Arguments.of(MySQLDataType.TINYINT, "tinyint_col", (byte)127, "127"), + Arguments.of(MySQLDataType.TINYINT, "tinyint_col", null, null), + + // TINYINT UNSIGNED tests (0 to 255) + Arguments.of(MySQLDataType.TINYINT_UNSIGNED, "tinyint_unsigned_col", (short)0, "0"), + Arguments.of(MySQLDataType.TINYINT_UNSIGNED, "tinyint_unsigned_col", (short)255, "255"), + Arguments.of(MySQLDataType.TINYINT_UNSIGNED, "tinyint_unsigned_col", (short)128, "128"), + + // SMALLINT tests (signed: -32,768 to 32,767) + Arguments.of(MySQLDataType.SMALLINT, "smallint_col", (short)32767, "32767"), + Arguments.of(MySQLDataType.SMALLINT, "smallint_col", (short)-32768, "-32768"), + Arguments.of(MySQLDataType.SMALLINT, "smallint_col", (short)0, "0"), + + // SMALLINT UNSIGNED tests (0 to 65,535) + Arguments.of(MySQLDataType.SMALLINT_UNSIGNED, "smallint_unsigned_col", 0, "0"), + Arguments.of(MySQLDataType.SMALLINT_UNSIGNED, "smallint_unsigned_col", 65535, "65535"), + Arguments.of(MySQLDataType.SMALLINT_UNSIGNED, "smallint_unsigned_col", 32768, "32768"), + + // INTEGER/INT tests (signed: -2,147,483,648 to 2,147,483,647) + Arguments.of(MySQLDataType.INT, "int_col", 2147483647, "2147483647"), + Arguments.of(MySQLDataType.INT, "int_col", -2147483648, "-2147483648"), + Arguments.of(MySQLDataType.INT, "int_col", 0, "0"), + + // INTEGER/INT UNSIGNED tests (0 to 4,294,967,295) + Arguments.of(MySQLDataType.INT_UNSIGNED, "int_unsigned_col", 4294967295L, "4294967295"), + Arguments.of(MySQLDataType.INT_UNSIGNED, "int_unsigned_col", 0L, "0"), + Arguments.of(MySQLDataType.INT_UNSIGNED, "int_unsigned_col", 2147483648L, "2147483648"), + + // BIGINT tests (signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) + Arguments.of(MySQLDataType.BIGINT, "bigint_col", 9223372036854775807L, "9223372036854775807"), + Arguments.of(MySQLDataType.BIGINT, "bigint_col", -9223372036854775808L, "-9223372036854775808"), + Arguments.of(MySQLDataType.BIGINT, "bigint_col", 0L, "0"), + + // BIGINT UNSIGNED tests (0 to 18,446,744,073,709,551,615) + Arguments.of(MySQLDataType.BIGINT_UNSIGNED, "bigint_unsigned_col", new BigInteger("18446744073709551615"), "18446744073709551615"), + Arguments.of(MySQLDataType.BIGINT_UNSIGNED, "bigint_unsigned_col", BigInteger.ZERO, "0"), + Arguments.of(MySQLDataType.BIGINT_UNSIGNED, "bigint_unsigned_col", new BigInteger("9223372036854775808"), "9223372036854775808"), + + // DECIMAL/NUMERIC tests + Arguments.of(MySQLDataType.DECIMAL, "decimal_col", new BigDecimal("123.45"), "123.45"), + Arguments.of(MySQLDataType.DECIMAL, "decimal_col", new BigDecimal("-123.45"), "-123.45"), + Arguments.of(MySQLDataType.DECIMAL, "decimal_col", new BigDecimal("0.0"), "0.0"), + Arguments.of(MySQLDataType.DECIMAL, "decimal_col", new BigDecimal("999999.99"), "999999.99"), + + // FLOAT tests + Arguments.of(MySQLDataType.FLOAT, "float_col", 123.45f, "123.45"), + Arguments.of(MySQLDataType.FLOAT, "float_col", -123.45f, "-123.45"), + Arguments.of(MySQLDataType.FLOAT, "float_col", 0.0f, "0.0"), + Arguments.of(MySQLDataType.FLOAT, "float_col", Float.MAX_VALUE, String.valueOf(Float.MAX_VALUE)), + + // DOUBLE tests + Arguments.of(MySQLDataType.DOUBLE, "double_col", 123.45678901234, "123.45678901234"), + Arguments.of(MySQLDataType.DOUBLE, "double_col", -123.45678901234, "-123.45678901234"), + Arguments.of(MySQLDataType.DOUBLE, "double_col", 0.0, "0.0"), + Arguments.of(MySQLDataType.DOUBLE, "double_col", Double.MAX_VALUE, String.valueOf(Double.MAX_VALUE)) + ); + } + + @Test + public void test_handleInvalidType() { + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), + List.of("invalid_col"), List.of("invalid_col"), + Collections.emptyMap(), Collections.emptyMap()); + final DataTypeHandler numericTypeHandler = new NumericTypeHandler(); + + assertThrows(IllegalArgumentException.class, () -> { + numericTypeHandler.handle(MySQLDataType.INT_UNSIGNED, "invalid_col", "not_a_number", metadata); + }); + } + + @Test + public void test_handleInvalidValue() { + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), + List.of("int_col"), List.of("int_col"), + Collections.emptyMap(), Collections.emptyMap()); + final DataTypeHandler numericTypeHandler = new NumericTypeHandler(); + + assertThrows(IllegalArgumentException.class, () -> { + numericTypeHandler.handle(MySQLDataType.INT, "int_col", "not_a_number", metadata); + }); + } +} diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandlerTest.java new file mode 100644 index 0000000000..2206ef4dca --- /dev/null +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/SpatialTypeHandlerTest.java @@ -0,0 +1,30 @@ +package org.opensearch.dataprepper.plugins.source.rds.datatype.impl; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.source.rds.datatype.DataTypeHandler; +import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class SpatialTypeHandlerTest { + + @Test + public void test_handle() { + final DataTypeHandler handler = new SpatialTypeHandler(); + final MySQLDataType columnType = MySQLDataType.GEOMETRY; + final String columnName = "geometryColumn"; + final String value = UUID.randomUUID().toString(); + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + String result = handler.handle(columnType, columnName, value.getBytes(), metadata); + + assertThat(result, is(value)); + } +} diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/StringTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/StringTypeHandlerTest.java new file mode 100644 index 0000000000..ab7acca0eb --- /dev/null +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/StringTypeHandlerTest.java @@ -0,0 +1,83 @@ +package org.opensearch.dataprepper.plugins.source.rds.datatype.impl; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.source.rds.datatype.DataTypeHandler; +import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StringTypeHandlerTest { + @Test + public void test_handle_char_string() { + DataTypeHandler handler = new StringTypeHandler(); + String columnName = "testColumn"; + MySQLDataType columnType = MySQLDataType.VARCHAR; + final String value = "Hello, World!"; + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + + String result = handler.handle(columnType, columnName, value, metadata); + + assertThat(result, is(value)); + } + + @Test + public void test_handle_byte_string() { + DataTypeHandler handler = new StringTypeHandler(); + String columnName = "testColumn"; + MySQLDataType columnType = MySQLDataType.TEXT; + final String value = "Hello, World!"; + byte[] testBytes = value.getBytes(); + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + + String result = handler.handle(columnType, columnName, testBytes, metadata); + + assertThat(result, is(value)); + } + + @Test + public void test_handle_enum_string() { + StringTypeHandler handler = new StringTypeHandler(); + String columnName = "testColumn"; + Integer value = 2; + String[] enumValues = { "ENUM1", "ENUM2", "ENUM3" }; + MySQLDataType columnType = MySQLDataType.ENUM; + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Map.of(columnName, enumValues)); + + String result = handler.handle(columnType, columnName, value, metadata); + + assertThat(result, is("ENUM2")); + } + + @Test + public void test_handle_set_string() { + StringTypeHandler handler = new StringTypeHandler(); + String columnName = "testColumn"; + Long value = 3L; + String[] setStrValues = { "Value1", "Value2", "Value3" }; + Map setStrValuesMap = new HashMap<>(); + setStrValuesMap.put(columnName, setStrValues); + MySQLDataType columnType = MySQLDataType.SET; + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + setStrValuesMap, Collections.emptyMap()); + + String result = handler.handle(columnType, columnName, value, metadata); + + assertEquals("[Value1, Value2]", result); + } +} diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java new file mode 100644 index 0000000000..0f94ae4422 --- /dev/null +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java @@ -0,0 +1,30 @@ +package org.opensearch.dataprepper.plugins.source.rds.datatype.impl; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.source.rds.datatype.DataTypeHandler; +import org.opensearch.dataprepper.plugins.source.rds.datatype.MySQLDataType; +import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TemporalTypeHandlerTest { + + @Test + public void test_handle() { + final DataTypeHandler handler = new TemporalTypeHandler(); + final MySQLDataType columnType = MySQLDataType.TIME; + final String columnName = "jsonColumn"; + final String value = UUID.randomUUID().toString(); + final TableMetadata metadata = new TableMetadata( + UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(columnName), List.of(columnName), + Collections.emptyMap(), Collections.emptyMap()); + String result = handler.handle(columnType, columnName, value, metadata); + + assertThat(result, is(value)); + } +}