From b06685759f123895160d657d7bc8038fcd54490c Mon Sep 17 00:00:00 2001 From: pawankashyapollion Date: Mon, 13 Jan 2025 10:40:15 +0530 Subject: [PATCH 1/5] Added Mapping fixes --- .../teleport/v2/templates/dbutils/dao/source/CassandraDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java index 8ad0cfb972..b6c35cd42a 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java @@ -51,7 +51,7 @@ public void write(DMLGeneratorResponse dmlGeneratorResponse) throws Exception { BoundStatement boundStatement = preparedStatement.bind( preparedStatementGeneratedResponse.getValues().stream() - .map(v -> CassandraTypeHandler.castToExpectedType(v.dataType(), v.value())) + .map(v -> CassandraTypeHandler.castToExpectedType(v.dataType(), v.value())) .toArray()); session.execute(boundStatement); } From 3075874cee6ff098b85f53dd7e913d513dae3c28 Mon Sep 17 00:00:00 2001 From: pawankashyapollion Date: Mon, 13 Jan 2025 10:40:45 +0530 Subject: [PATCH 2/5] Added Spoltles fixes --- .../teleport/v2/templates/dbutils/dao/source/CassandraDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java index b6c35cd42a..8ad0cfb972 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java @@ -51,7 +51,7 @@ public void write(DMLGeneratorResponse dmlGeneratorResponse) throws Exception { BoundStatement boundStatement = preparedStatement.bind( preparedStatementGeneratedResponse.getValues().stream() - .map(v -> CassandraTypeHandler.castToExpectedType(v.dataType(), v.value())) + .map(v -> CassandraTypeHandler.castToExpectedType(v.dataType(), v.value())) .toArray()); session.execute(boundStatement); } From 536037e7ccf9153e4c5620bccc5cfb502d3f0d12 Mon Sep 17 00:00:00 2001 From: pawankashyapollion Date: Wed, 15 Jan 2025 13:05:32 +0530 Subject: [PATCH 3/5] Address multiple Session --- .../dbutils/dao/source/CassandraDao.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java index 8ad0cfb972..74f81f5965 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dao/source/CassandraDao.java @@ -38,22 +38,19 @@ public CassandraDao( @Override public void write(DMLGeneratorResponse dmlGeneratorResponse) throws Exception { - try (CqlSession session = - (CqlSession) - connectionHelper.getConnection(this.cassandraUrl)) { // Ensure connection is obtained - if (session == null) { - throw new ConnectionException("Connection is null"); - } - PreparedStatementGeneratedResponse preparedStatementGeneratedResponse = - (PreparedStatementGeneratedResponse) dmlGeneratorResponse; - String dmlStatement = preparedStatementGeneratedResponse.getDmlStatement(); - PreparedStatement preparedStatement = session.prepare(dmlStatement); - BoundStatement boundStatement = - preparedStatement.bind( - preparedStatementGeneratedResponse.getValues().stream() - .map(v -> CassandraTypeHandler.castToExpectedType(v.dataType(), v.value())) - .toArray()); - session.execute(boundStatement); + CqlSession session = (CqlSession) connectionHelper.getConnection(this.cassandraUrl); + if (session == null) { + throw new ConnectionException("Connection is null"); } + PreparedStatementGeneratedResponse preparedStatementGeneratedResponse = + (PreparedStatementGeneratedResponse) dmlGeneratorResponse; + String dmlStatement = preparedStatementGeneratedResponse.getDmlStatement(); + PreparedStatement preparedStatement = session.prepare(dmlStatement); + BoundStatement boundStatement = + preparedStatement.bind( + preparedStatementGeneratedResponse.getValues().stream() + .map(v -> CassandraTypeHandler.castToExpectedType(v.dataType(), v.value())) + .toArray()); + session.execute(boundStatement); } } From e897c4f856cc421781b9fb0df77af48836878965 Mon Sep 17 00:00:00 2001 From: pawankashyapollion Date: Wed, 15 Jan 2025 18:18:18 +0530 Subject: [PATCH 4/5] Handle Type Handler --- .../dbutils/dml/CassandraTypeHandler.java | 44 +++++++++++-------- .../templates/transforms/AssignShardIdFn.java | 4 +- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java index ac0711aa45..e25f738a82 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java @@ -43,9 +43,9 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import org.apache.commons.lang3.BooleanUtils; import org.eclipse.jetty.util.StringUtil; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -242,11 +242,11 @@ private static Boolean handleCassandraBoolType(String colName, JSONObject values * @return a {@link Float} object containing the value represented in cassandra type. */ private static Float handleCassandraFloatType(String colName, JSONObject valuesJson) { - try { - return valuesJson.getBigDecimal(colName).floatValue(); - } catch (JSONException e) { + BigDecimal colValue = valuesJson.optBigDecimal(colName, null); + if (colValue == null) { return null; } + return colValue.floatValue(); } /** @@ -257,11 +257,11 @@ private static Float handleCassandraFloatType(String colName, JSONObject valuesJ * @return a {@link Double} object containing the value represented in cassandra type. */ private static Double handleCassandraDoubleType(String colName, JSONObject valuesJson) { - try { - return valuesJson.getBigDecimal(colName).doubleValue(); - } catch (JSONException e) { + BigDecimal colValue = valuesJson.optBigDecimal(colName, null); + if (colValue == null) { return null; } + return colValue.doubleValue(); } /** @@ -399,11 +399,11 @@ private static UUID handleCassandraUuidType(String colName, JSONObject valuesJso * @return a {@link Long} object containing Long as value represented in cassandra type. */ private static Long handleCassandraBigintType(String colName, JSONObject valuesJson) { - try { - return valuesJson.getBigInteger(colName).longValue(); - } catch (JSONException e) { + BigInteger colValue = valuesJson.optBigInteger(colName, null); + if (colValue == null) { return null; } + return colValue.longValue(); } /** @@ -414,11 +414,11 @@ private static Long handleCassandraBigintType(String colName, JSONObject valuesJ * @return a {@link Integer} object containing Integer as value represented in cassandra type. */ private static Integer handleCassandraIntType(String colName, JSONObject valuesJson) { - try { - return valuesJson.getBigInteger(colName).intValue(); - } catch (JSONException e) { + BigInteger colValue = valuesJson.optBigInteger(colName, null); + if (colValue == null) { return null; } + return colValue.intValue(); } /** @@ -611,9 +611,9 @@ private static Object handleSpannerColumnType( return CassandraTypeHandler.handleCassandraTimestampType(columnName, valuesJson); } else if ("boolean".equals(spannerType)) { return CassandraTypeHandler.handleCassandraBoolType(columnName, valuesJson); - } else if (spannerType.matches("numeric|float")) { + } else if (spannerType.matches("float")) { return CassandraTypeHandler.handleCassandraFloatType(columnName, valuesJson); - } else if (spannerType.contains("float")) { + } else if (spannerType.contains("float") || spannerType.contains("numeric")) { return CassandraTypeHandler.handleCassandraDoubleType(columnName, valuesJson); } else if (spannerType.contains("bytes") || spannerType.contains("blob")) { return CassandraTypeHandler.handleCassandraBlobType(columnName, valuesJson); @@ -786,7 +786,10 @@ private static Object parseNumericType(String columnType, Object colValue) { */ private static Boolean parseBoolean(Object colValue) { if (colValue instanceof String) { - return Boolean.parseBoolean((String) colValue); + if (Arrays.asList("0", "1").contains((String) colValue)) { + return colValue.equals("1"); + } + return BooleanUtils.toBoolean((String) colValue); } return (Boolean) colValue; } @@ -804,8 +807,10 @@ private static Boolean parseBoolean(Object colValue) { private static BigDecimal parseDecimal(Object colValue) { if (colValue instanceof String) { return new BigDecimal((String) colValue); - } else if (colValue instanceof Number) { - return BigDecimal.valueOf(((Number) colValue).doubleValue()); + } else if (colValue instanceof Float) { + return BigDecimal.valueOf((Float) colValue); + } else if (colValue instanceof Double) { + return BigDecimal.valueOf((Double) colValue); } return (BigDecimal) colValue; } @@ -992,7 +997,8 @@ public static Object castToExpectedType(String cassandraType, Object columnValue return parseAndCastToCassandraType(cassandraType, columnValue).value(); } catch (ClassCastException | IllegalArgumentException e) { LOG.error("Error converting value for column: {}, type: {}", cassandraType, e.getMessage()); - throw e; + throw new IllegalArgumentException( + "Error converting value for cassandraType: " + cassandraType); } } } diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/transforms/AssignShardIdFn.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/transforms/AssignShardIdFn.java index 0afdf3bf8b..76b78d4a4e 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/transforms/AssignShardIdFn.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/transforms/AssignShardIdFn.java @@ -231,7 +231,9 @@ public void processElement(ProcessContext c) throws Exception { record.setShard(qualifiedShard); String finalKeyString = tableName + "_" + keysJsonStr + "_" + qualifiedShard; - Long finalKey = finalKeyString.hashCode() % maxConnectionsAcrossAllShards; + Long finalKey = + finalKeyString.hashCode() % maxConnectionsAcrossAllShards; // The total parallelism is + // maxConnectionsAcrossAllShards c.output(KV.of(finalKey, record)); } catch (Exception e) { From e04f2b8c03a91f1290049021c1c147e13d6c82a6 Mon Sep 17 00:00:00 2001 From: pawankashyapollion Date: Thu, 16 Jan 2025 09:58:42 +0530 Subject: [PATCH 5/5] Added UT and Value Object Bug Fixes --- .../dbutils/dml/CassandraDMLGenerator.java | 7 +- .../dbutils/dml/CassandraTypeHandler.java | 6 +- .../dml/CassandraDMLGeneratorTest.java | 2 +- .../dbutils/dml/CassandraTypeHandlerTest.java | 1054 +++++++++++------ 4 files changed, 696 insertions(+), 373 deletions(-) diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGenerator.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGenerator.java index 192202f55e..a32e554f43 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGenerator.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGenerator.java @@ -296,12 +296,7 @@ private static DMLGeneratorResponse getDeleteStatementCQL( deleteConditions.setLength(deleteConditions.length() - 5); } - String preparedStatement = - "DELETE FROM " + tableName + " WHERE " + deleteConditions + " USING TIMESTAMP ?;"; - - PreparedStatementValueObject timestampObj = - PreparedStatementValueObject.create("USING_TIMESTAMP", timestamp); - values.add(timestampObj); + String preparedStatement = "DELETE FROM " + tableName + " WHERE " + deleteConditions + ";"; return new PreparedStatementGeneratedResponse(preparedStatement, values); } diff --git a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java index e25f738a82..73c4d8f8a6 100644 --- a/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java +++ b/v2/spanner-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandler.java @@ -458,8 +458,7 @@ private static Instant convertToCassandraTimestamp(String timestampValue) { return LocalDate.from(temporal).atStartOfDay(ZoneOffset.UTC).toInstant(); } } catch (DateTimeParseException ignored) { - throw new IllegalArgumentException( - "Failed to parse timestamp value" + timestampValue, ignored); + LOG.info("Exception found from different formatter " + ignored.getMessage()); } } throw new IllegalArgumentException("Failed to parse timestamp value: " + timestampValue); @@ -647,7 +646,8 @@ private static Object handleStringType(String colName, JSONObject valuesJson) { return new JSONArray(inputValue); } else if (isValidJSONObject(inputValue)) { return new JSONObject(inputValue); - } else if (StringUtil.isHex(inputValue, 0, inputValue.length())) { + } else if (StringUtil.isHex(inputValue, 0, inputValue.length()) + && inputValue.startsWith("0x")) { return CassandraTypeHandler.handleCassandraBlobType(colName, valuesJson); } else if (isAscii(inputValue)) { return CassandraTypeHandler.handleCassandraAsciiType(colName, valuesJson); diff --git a/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGeneratorTest.java b/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGeneratorTest.java index 6dff8c230d..d3a685824f 100644 --- a/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGeneratorTest.java +++ b/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraDMLGeneratorTest.java @@ -311,7 +311,7 @@ public void deleteMultiplePKColumns() { .setSchema(schema) .build()); String sql = dmlGeneratorResponse.getDmlStatement(); - assertEquals(2, ((PreparedStatementGeneratedResponse) dmlGeneratorResponse).getValues().size()); + assertEquals(1, ((PreparedStatementGeneratedResponse) dmlGeneratorResponse).getValues().size()); } @Test diff --git a/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandlerTest.java b/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandlerTest.java index f1af3a6c67..1e69954a05 100644 --- a/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandlerTest.java +++ b/v2/spanner-to-sourcedb/src/test/java/com/google/cloud/teleport/v2/templates/dbutils/dml/CassandraTypeHandlerTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import com.google.cloud.teleport.v2.spanner.migrations.schema.SourceColumnDefinition; import com.google.cloud.teleport.v2.spanner.migrations.schema.SourceColumnType; @@ -33,6 +32,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.net.InetAddress; +import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.time.Duration; import java.time.Instant; @@ -49,606 +49,949 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.mockito.Mockito; -import org.slf4j.Logger; @RunWith(JUnit4.class) public class CassandraTypeHandlerTest { - private SpannerColumnDefinition spannerColDef; - - private SourceColumnDefinition sourceColDef; - - private JSONObject valuesJson; - - private static final Logger LOG = mock(Logger.class); - - private void mockLogging(ClassCastException e) { - Mockito.doNothing().when(LOG).error(Mockito.anyString(), Mockito.any(), Mockito.any()); - } - @Test public void testGetColumnValueByTypeForString() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColumnType = new SourceColumnType("varchar", null, null); String columnName = "test_column"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); - - Object result = + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "test_value"); + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByType() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); - SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "é"; + String spannerColumnType = "string"; + String sourceType = "varchar"; + SpannerColumnType spannerType = new SpannerColumnType(spannerColumnType, true); + SourceColumnType sourceColumnType = + new SourceColumnType(sourceType, new Long[] {10L, 20L}, new Long[] {10L, 20L}); + String columnValue = "é"; + String columnName = "LastName"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForNonString() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); - SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "DEL"; + String spannerColumnName = "NUMERIC"; + String sourceColumnName = "int"; + SpannerColumnType spannerType = new SpannerColumnType(spannerColumnName, true); + SourceColumnType sourceColumnType = + new SourceColumnType(sourceColumnName, new Long[] {10L, 20L}, new Long[] {10L, 20L}); + String columnName = "Salary"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - Object result = + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, 12345); + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForStringUUID() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "123e4567-e89b-12d3-a456-426614174000"; + SourceColumnType sourceColumnType = new SourceColumnType("uuid", null, null); + String columnName = "id"; + String columnValue = "123e4567-e89b-12d3-a456-426614174000"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForStringIpAddress() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "192.168.1.1"; + SourceColumnType sourceColumnType = new SourceColumnType("inet", null, null); + String columnValue = "192.168.1.1"; + String columnName = "ipAddress"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForStringJsonArray() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "[\"apple\", \"banana\", \"cherry\"]"; + SourceColumnType sourceColumnType = new SourceColumnType("set", null, null); + String columnValue = "[\"apple\", \"banana\", \"cherry\"]"; + String columnName = "fruits"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForStringJsonObject() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "{\"name\": \"John\", \"age\": 30}"; + SourceColumnType sourceColumnType = new SourceColumnType("map", null, null); + String columnName = "user"; + String columnValue = "{\"name\": \"John\", \"age\": \"30\"}"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForStringHex() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); - SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "a3f5b7"; + SourceColumnType sourceColumnType = new SourceColumnType("blob", null, null); + String columnName = "lastName"; + String columnValue = "a3f5b7"; String sourceDbTimezoneOffset = "UTC"; - when(valuesJson.optString(columnName, null)) - .thenReturn(columnName); // Mock string value for column - when(valuesJson.get(columnName)).thenReturn(columnName); // Mock getting column value + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); // Spanner column type - when(spannerColDef.getName()).thenReturn(columnName); // Column name in Spanner - when(sourceColDef.getType()) - .thenReturn(new SourceColumnType("sourceType", null, null)); // Source column type + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - PreparedStatementValueObject preparedStatementValueObject = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertEquals(CassandraTypeHandler.NullClass.INSTANCE, preparedStatementValueObject.value()); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForStringDuration() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("string", true); - String columnName = "P4DT1H"; + SourceColumnType sourceColumnType = new SourceColumnType("varchar", null, null); + String columnValue = "P4DT1H"; + String columnName = "total_time"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn(columnName); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForDates() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("date", true); - String columnName = "timestampColumn"; + SourceColumnType sourceColumnType = new SourceColumnType("timestamp", null, null); + String columnValue = "2025-01-01T00:00:00Z"; + String columnName = "created_on"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.optString(columnName, null)).thenReturn("2025-01-01T00:00:00Z"); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, columnValue); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test public void testGetColumnValueByTypeForBigInt() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); SpannerColumnType spannerType = new SpannerColumnType("bigint", true); - String columnName = "test_column"; + SourceColumnType sourceColumnType = new SourceColumnType("bigint", null, null); + String columnName = "Salary"; String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.getBigInteger(columnName)).thenReturn(BigInteger.valueOf(5)); - when(valuesJson.get(columnName)).thenReturn(columnName); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, BigInteger.valueOf(123456789L)); - Object result = + PreparedStatementValueObject result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test - public void testGetColumnValueByTypeForInteger() { - SpannerColumnDefinition spannerColDef = mock(SpannerColumnDefinition.class); - SourceColumnDefinition sourceColDef = mock(SourceColumnDefinition.class); - JSONObject valuesJson = mock(JSONObject.class); + public void testGetColumnValueByTypeForBytesForHexString() { + SpannerColumnType spannerType = new SpannerColumnType("String", true); + SourceColumnType sourceColumnType = new SourceColumnType("bytes", null, null); + String columnName = "Name"; + String sourceDbTimezoneOffset = "UTC"; - String columnName = "test_column"; - SpannerColumnType spannerType = new SpannerColumnType("integer", true); - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); - when(valuesJson.getBigInteger(columnName)).thenReturn(BigInteger.valueOf(5)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "48656c6c6f20576f726c64"); - when(valuesJson.getInt(columnName)).thenReturn(5); + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); - Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test - public void testGetColumnValueByTypeForValidBigInteger() { - SpannerColumnDefinition spannerColDef = mock(SpannerColumnDefinition.class); - SourceColumnDefinition sourceColDef = mock(SourceColumnDefinition.class); - JSONObject valuesJson = mock(JSONObject.class); + public void testGetColumnValueByTypeForBigIntForString() { + SpannerColumnType spannerType = new SpannerColumnType("String", true); + SourceColumnType sourceColumnType = new SourceColumnType("bigint", null, null); + String columnName = "Salary"; + String sourceDbTimezoneOffset = "UTC"; + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "123456789"); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); + } + + @Test + public void testGetColumnValueByTypeForBoolentForString() { + SpannerColumnType spannerType = new SpannerColumnType("String", true); + SourceColumnType sourceColumnType = new SourceColumnType("boolean", null, null); + String columnName = "Male"; + String sourceDbTimezoneOffset = "UTC"; + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "1"); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); + } + + @Test + public void testGetColumnValueByTypeForBoolent() { + SpannerColumnType spannerType = new SpannerColumnType("Boolean", true); + SourceColumnType sourceColumnType = new SourceColumnType("boolean", null, null); + String columnName = "Male"; + String sourceDbTimezoneOffset = "UTC"; + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, true); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); + } + + @Test + public void testGetColumnValueByTypeForIntegerValue() { + SpannerColumnType spannerType = new SpannerColumnType("Integer", true); + SourceColumnType sourceColumnType = new SourceColumnType("bigint", null, null); + String columnName = "Salary"; + String sourceDbTimezoneOffset = "UTC"; + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, 225000); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); + } + + @Test + public void testGetColumnValueByTypeForBoolentSamllCaseForString() { + SpannerColumnType spannerType = new SpannerColumnType("String", true); + SourceColumnType sourceColumnType = new SourceColumnType("boolean", null, null); + String columnName = "Male"; + String sourceDbTimezoneOffset = "UTC"; + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColumnType); + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "f"); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, sourceDbTimezoneOffset); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); + } + + // Revised and Improved Tests + + @Test + public void testGetColumnValueByTypeForInteger() { + SpannerColumnType spannerType = new SpannerColumnType("NUMERIC", true); + SourceColumnType sourceColType = new SourceColumnType("integer", new Long[] {10L, 20L}, null); String columnName = "test_column"; - SpannerColumnType spannerType = new SpannerColumnType("boolean", true); - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.getBigInteger(columnName)).thenReturn(BigInteger.valueOf(5)); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, BigInteger.valueOf(5)); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); - Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test - public void testGetColumnValueByTypeFor() { - spannerColDef = mock(SpannerColumnDefinition.class); - sourceColDef = mock(SourceColumnDefinition.class); - valuesJson = mock(JSONObject.class); - SpannerColumnType spannerType = new SpannerColumnType("float", true); + public void testGetColumnValueByTypeForValidBigInteger() { + SpannerColumnType spannerType = new SpannerColumnType("integer", true); + SourceColumnType sourceColType = new SourceColumnType("int64", new Long[] {10L, 20L}, null); String columnName = "test_column"; - String sourceDbTimezoneOffset = "UTC"; - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.getBigDecimal(columnName)).thenReturn(new BigDecimal("5.5")); - when(valuesJson.get(columnName)).thenReturn(columnName); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, BigInteger.valueOf(5)); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); - Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertTrue(result instanceof PreparedStatementValueObject); + assertNotNull(castResult); + } - Object actualValue = ((PreparedStatementValueObject) result).value(); + @Test + public void testConvertToCassandraTimestampWithISOInstant() { + String timestamp = "2025-01-15T10:15:30Z"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - assertEquals(5.5f, actualValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test - public void testGetColumnValueByTypeForFloat64() { - SpannerColumnDefinition spannerColDef = mock(SpannerColumnDefinition.class); - SourceColumnDefinition sourceColDef = mock(SourceColumnDefinition.class); - JSONObject valuesJson = mock(JSONObject.class); + public void testConvertToCassandraTimestampWithISODateTime() { + String timestamp = "2025-01-15T10:15:30"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("datetime", new Long[] {10L, 20L}, null); + String columnName = "test_column"; + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + assertNotNull(castResult); + } + + @Test + public void testConvertToCassandraTimestampWithISODate() { + String timestamp = "2025-01-15"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); String columnName = "test_column"; - SpannerColumnType spannerType = new SpannerColumnType("float64", true); - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - when(valuesJson.getBigDecimal(columnName)).thenReturn(new BigDecimal("5.5")); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); - Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertTrue(result instanceof PreparedStatementValueObject); + assertNotNull(castResult); + } - Object actualValue = ((PreparedStatementValueObject) result).value(); + @Test + public void testConvertToCassandraTimestampWithCustomFormat1() { + String timestamp = "01/15/2025"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals(5.5, actualValue); + assertNotNull(castResult); } @Test - public void testGetColumnValueByTypeForBytes() { - SpannerColumnDefinition spannerColDef = mock(SpannerColumnDefinition.class); - SourceColumnDefinition sourceColDef = mock(SourceColumnDefinition.class); - JSONObject valuesJson = mock(JSONObject.class); + public void testConvertToCassandraTimestampWithCustomFormat2() { + String timestamp = "2025/01/15"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; + + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); + } + + @Test + public void testConvertToCassandraTimestampWithCustomFormat3() { + String timestamp = "15-01-2025"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); String columnName = "test_column"; - SpannerColumnType spannerType = new SpannerColumnType("bytes", true); - Long[] myArray = new Long[5]; - myArray[0] = 10L; - myArray[1] = 20L; - byte[] expectedBytes = new byte[] {1, 2, 3, 4, 5}; - when(valuesJson.opt(columnName)).thenReturn(expectedBytes); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); - when(spannerColDef.getType()).thenReturn(spannerType); - when(spannerColDef.getName()).thenReturn(columnName); - when(sourceColDef.getType()).thenReturn(new SourceColumnType("sourceType", myArray, myArray)); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); - Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); - assertNotNull(result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertTrue(result instanceof PreparedStatementValueObject); + assertNotNull(castResult); + } - Object actualValue = ((PreparedStatementValueObject) result).value(); + @Test + public void testConvertToCassandraTimestampWithCustomFormat4() { + String timestamp = "15/01/2025"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - byte[] actualBytes = ((ByteBuffer) actualValue).array(); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); - assertArrayEquals(expectedBytes, actualBytes); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test - public void testCastToExpectedTypeForString() { - String cassandraType = "text"; - String columnValue = "Test String"; + public void testConvertToCassandraTimestampWithCustomFormat5() { + String timestamp = "2025-01-15 10:15:30"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null); - assertEquals(columnValue, result); + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertNotNull(castResult); } @Test - public void testCastToExpectedTypeForBigInt() { - String cassandraType = "bigint"; - Long columnValue = 123L; + public void testConvertToCassandraTimestampWithInvalidFormat() { + String timestamp = "invalid-timestamp"; + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, timestamp); - assertEquals(columnValue, result); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null)); + assertTrue(exception.getMessage().contains("Failed to parse timestamp value")); } @Test - public void testCastToExpectedTypeForBoolean() { - String cassandraType = "boolean"; - Boolean columnValue = true; + public void testConvertToCassandraTimestampWithNull() { + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, " "); - assertEquals(columnValue, result); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null)); + assertEquals("Timestamp value cannot be null or empty", exception.getMessage()); } @Test - public void testCastToExpectedTypeForDecimal() { - String cassandraType = "decimal"; - BigDecimal columnValue = new BigDecimal("123.456"); + public void testConvertToCassandraTimestampWithWhitespaceString() { + SpannerColumnType spannerType = new SpannerColumnType("timestamp", true); + SourceColumnType sourceColType = new SourceColumnType("date", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, " "); - assertEquals(columnValue, result); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> getColumnValueByType(spannerColDef, sourceColDef, valuesJson, null)); + assertEquals("Timestamp value cannot be null or empty", exception.getMessage()); } @Test - public void testCastToExpectedTypeForDouble() { - String cassandraType = "double"; - Double columnValue = 123.456; + public void testGetColumnValueByTypeForFloat() { + SpannerColumnType spannerType = new SpannerColumnType("float", true); + SourceColumnType sourceColType = new SourceColumnType("float", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, new BigDecimal("5.5")); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); - assertEquals(columnValue, result); + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object actualValue = ((PreparedStatementValueObject) result).value(); + assertEquals(5.5f, actualValue); } @Test - public void testCastToExpectedTypeForFloat() { - String cassandraType = "float"; - Float columnValue = 123.45f; + public void testGetColumnValueByTypeForFloat64() { + SpannerColumnType spannerType = new SpannerColumnType("float64", true); + SourceColumnType sourceColType = new SourceColumnType("double", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, new BigDecimal("5.5")); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals(columnValue, ((Double) result).floatValue(), 0.00001); + assertEquals(5.5, castResult); } @Test - public void testCastToExpectedTypeForInet() throws Exception { - String cassandraType = "inet"; - InetAddress columnValue = InetAddress.getByName("127.0.0.1"); + public void testGetColumnValueByTypeForFloat64FromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("double", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5.5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); - assertEquals(columnValue, result); + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertEquals(5.5, castResult); } @Test - public void testCastToExpectedTypeForInt() { - String cassandraType = "int"; - Integer columnValue = 123; + public void testGetColumnValueByTypeForDecimalFromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("decimal", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5.5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); - assertEquals(columnValue, result); + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertEquals(BigDecimal.valueOf(5.5), castResult); } @Test - public void testCastToExpectedTypeForSmallInt() { - String cassandraType = "smallint"; - Integer columnValue = 123; + public void testGetColumnValueByTypeForDecimalFromFloat() { + SpannerColumnType spannerType = new SpannerColumnType("float", true); + SourceColumnType sourceColType = new SourceColumnType("decimal", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, 5.5); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals((short) 123, result); + assertEquals(BigDecimal.valueOf(5.5), castResult); } @Test - public void testCastToExpectedTypeForTimestamp() { - String cassandraType = "timestamp"; - Instant columnValue = Instant.now(); + public void testGetColumnValueByTypeForDecimalFromFloat64() { + SpannerColumnType spannerType = new SpannerColumnType("float64", true); + SourceColumnType sourceColType = new SourceColumnType("decimal", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, 5.5); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals(columnValue, result); + assertEquals(BigDecimal.valueOf(5.5), castResult); } @Test - public void testCastToExpectedTypeForDate() { - String cassandraType = "date"; - LocalDate columnValue = LocalDate.now(); + public void testGetColumnValueByTypeForFloatFromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("float", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - assertThrows( - IllegalArgumentException.class, - () -> { - castToExpectedType(cassandraType, columnValue); - }); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5.5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertEquals(5.5, castResult); } @Test - public void testCastToExpectedTypeForUUID() { - String cassandraType = "uuid"; - UUID columnValue = UUID.randomUUID(); + public void testGetColumnValueByTypeForBigIntFromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("bigint", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals(columnValue, result); + assertEquals(Long.valueOf("5"), castResult); } @Test - public void testCastToExpectedTypeForTinyInt() { - String cassandraType = "tinyint"; - Integer columnValue = 100; + public void testGetColumnValueByTypeForIntFromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("int", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); - assertEquals((byte) 100, result); + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); + + assertEquals(Integer.valueOf("5"), castResult); } @Test - public void testCastToExpectedTypeForVarint() { - String cassandraType = "varint"; - ByteBuffer columnValue = ByteBuffer.wrap(new byte[] {1, 2, 3, 4}); + public void testGetColumnValueByTypeForSmallIntFromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("smallint", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals(new BigInteger(columnValue.array()), result); + assertEquals(Integer.valueOf("5").shortValue(), castResult); } @Test - public void testCastToExpectedTypeForDuration() { - String cassandraType = "duration"; - Duration columnValue = Duration.ofHours(5); + public void testGetColumnValueByTypeForTinyIntFromString() { + SpannerColumnType spannerType = new SpannerColumnType("string", true); + SourceColumnType sourceColType = new SourceColumnType("tinyint", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, "5"); + + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + PreparedStatementValueObject result = + getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object castResult = CassandraTypeHandler.castToExpectedType(result.dataType(), result.value()); - assertEquals(columnValue, result); + assertEquals(Byte.valueOf("5"), castResult); } @Test - public void testCastToExpectedTypeForJSONArrayToList() { - String cassandraType = "list"; - JSONArray columnValue = new JSONArray(Arrays.asList(1, 2, 3)); + public void testGetColumnValueByTypeForBytes() { + SpannerColumnType spannerType = new SpannerColumnType("bytes", true); + SourceColumnType sourceColType = new SourceColumnType("bytes", new Long[] {10L, 20L}, null); + String columnName = "test_column"; - Object result = castToExpectedType(cassandraType, columnValue); + byte[] expectedBytes = new byte[] {1, 2, 3, 4, 5}; + JSONObject valuesJson = new JSONObject(); + valuesJson.put(columnName, expectedBytes); - assertTrue(result instanceof List); + SpannerColumnDefinition spannerColDef = new SpannerColumnDefinition(columnName, spannerType); + SourceColumnDefinition sourceColDef = new SourceColumnDefinition(columnName, sourceColType); + + Object result = getColumnValueByType(spannerColDef, sourceColDef, valuesJson, "UTC"); + + assertNotNull(result); + assertTrue(result instanceof PreparedStatementValueObject); + + Object actualValue = ((PreparedStatementValueObject) result).value(); + byte[] actualBytes = ((ByteBuffer) actualValue).array(); + assertArrayEquals(expectedBytes, actualBytes); + } + + @Test + public void testCastToExpectedTypeForVariousTypes() throws UnknownHostException { + assertEquals("Test String", castToExpectedType("text", "Test String")); + assertEquals(123L, castToExpectedType("bigint", 123L)); + assertEquals(true, castToExpectedType("boolean", true)); + assertEquals( + new BigDecimal("123.456"), castToExpectedType("decimal", new BigDecimal("123.456"))); + assertEquals(123.456, castToExpectedType("double", 123.456)); + assertEquals(123.45f, ((Double) castToExpectedType("float", 123.45f)).floatValue(), 0.00001); + assertEquals( + InetAddress.getByName("127.0.0.1"), + castToExpectedType("inet", InetAddress.getByName("127.0.0.1"))); + assertEquals(123, castToExpectedType("int", 123)); + assertEquals((short) 123, castToExpectedType("smallint", 123)); + assertEquals( + UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), + castToExpectedType("uuid", UUID.fromString("123e4567-e89b-12d3-a456-426614174000"))); + assertEquals((byte) 100, castToExpectedType("tinyint", 100)); + assertEquals( + new BigInteger("12345"), + castToExpectedType("varint", ByteBuffer.wrap(new byte[] {0, 0, 48, 57}))); + assertEquals(Duration.ofHours(5), castToExpectedType("duration", Duration.ofHours(5))); } @Test public void testCastToExpectedTypeForJSONArrayToSet() { String cassandraType = "set"; JSONArray columnValue = new JSONArray(Arrays.asList(1, 2, 3)); - Object result = castToExpectedType(cassandraType, columnValue); - assertTrue(result instanceof Set); + assertEquals(3, ((Set) result).size()); } @Test @@ -657,7 +1000,6 @@ public void testCastToExpectedTypeForJSONObjectToMap() { JSONObject columnValue = new JSONObject(); columnValue.put("2024-12-12", "One"); columnValue.put(String.valueOf(2), "Two"); - assertThrows( IllegalArgumentException.class, () -> { @@ -669,9 +1011,6 @@ public void testCastToExpectedTypeForJSONObjectToMap() { public void testCastToExpectedTypeForExceptionScenario() { String cassandraType = "int"; String columnValue = "InvalidInt"; - - mockLogging(new ClassCastException("Invalid cast")); - assertThrows( IllegalArgumentException.class, () -> { @@ -683,7 +1022,6 @@ public void testCastToExpectedTypeForExceptionScenario() { public void testGetColumnValueByTypeForNullBothColumnDefs() { JSONObject valuesJson = mock(JSONObject.class); String sourceDbTimezoneOffset = "UTC"; - assertThrows( IllegalArgumentException.class, () -> { @@ -707,9 +1045,7 @@ public void testCastToExpectedTypeForVarchar() { @Test public void testCastToExpectedTypeForList() { - JSONArray listValue = new JSONArray(); - listValue.put("value1"); - listValue.put("value2"); + JSONArray listValue = new JSONArray(Arrays.asList("value1", "value2")); Object result = CassandraTypeHandler.castToExpectedType("list", listValue); assertTrue(result instanceof List); assertEquals(2, ((List) result).size()); @@ -717,9 +1053,7 @@ public void testCastToExpectedTypeForList() { @Test public void testCastToExpectedTypeForSet() { - JSONArray setValue = new JSONArray(); - setValue.put("value1"); - setValue.put("value2"); + JSONArray setValue = new JSONArray(Arrays.asList("value1", "value2")); Object result = CassandraTypeHandler.castToExpectedType("set", setValue); assertTrue(result instanceof Set); assertEquals(2, ((Set) result).size()); @@ -783,9 +1117,7 @@ public void testCastToExpectedTypeForDate_InvalidString() { () -> { CassandraTypeHandler.castToExpectedType("date", invalidDateString); }); - assertEquals( - "Error handling type: Text 'invalid-date' could not be parsed at index 0", - exception.getMessage()); + assertEquals("Error converting value for cassandraType: date", exception.getMessage()); } @Test @@ -797,8 +1129,7 @@ public void testCastToExpectedTypeForDate_UnsupportedType() { () -> { CassandraTypeHandler.castToExpectedType("date", unsupportedType); }); - assertEquals( - "Error handling type: Unsupported value for date conversion: 123", exception.getMessage()); + assertEquals("Error converting value for cassandraType: date", exception.getMessage()); } @Test @@ -818,8 +1149,7 @@ public void testHandleCassandraVarintType_InvalidString() { () -> { CassandraTypeHandler.castToExpectedType("varint", invalidString); }); - assertEquals( - "Invalid varint format (string) for value: invalid-number", exception.getMessage()); + assertEquals("Error converting value for cassandraType: varint", exception.getMessage()); } @Test @@ -847,8 +1177,6 @@ public void testHandleCassandraVarintType_UnsupportedType() { () -> { CassandraTypeHandler.castToExpectedType("varint", unsupportedType); }); - assertEquals( - "Invalid value type for varint conversion: class java.lang.Integer", - exception.getMessage()); + assertEquals("Error converting value for cassandraType: varint", exception.getMessage()); } }