-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into snyk-upgrade-dafe6fd4d1eb795d6504c32641f4b46f
- Loading branch information
Showing
10 changed files
with
525 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/test/java/com/datastax/cdm/cql/codec/CodecFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.datastax.cdm.cql.codec; | ||
|
||
import com.datastax.cdm.data.MockitoExtension; | ||
import com.datastax.cdm.properties.PropertyHelper; | ||
import com.datastax.dse.driver.internal.core.type.codec.geometry.LineStringCodec; | ||
import com.datastax.dse.driver.internal.core.type.codec.geometry.PointCodec; | ||
import com.datastax.dse.driver.internal.core.type.codec.geometry.PolygonCodec; | ||
import com.datastax.dse.driver.internal.core.type.codec.time.DateRangeCodec; | ||
import com.datastax.oss.driver.api.core.type.codec.TypeCodec; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CodecFactoryTest { | ||
@Mock | ||
private PropertyHelper propertyHelper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
//Mockito.when(propertyHelper.getString("timestamp.format")).thenReturn("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForPolygonType() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.POLYGON_TYPE); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof PolygonCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForIntString() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.INT_STRING); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof INT_StringCodec); | ||
assertTrue(codecs.get(1) instanceof TEXT_IntegerCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForDoubleString() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.DOUBLE_STRING); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof DOUBLE_StringCodec); | ||
assertTrue(codecs.get(1) instanceof TEXT_DoubleCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForBigintString() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.BIGINT_STRING); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof BIGINT_StringCodec); | ||
assertTrue(codecs.get(1) instanceof TEXT_LongCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForDecimalString() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.DECIMAL_STRING); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof DECIMAL_StringCodec); | ||
assertTrue(codecs.get(1) instanceof TEXT_BigDecimalCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForTimestampStringMillis() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.TIMESTAMP_STRING_MILLIS); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof TIMESTAMP_StringMillisCodec); | ||
assertTrue(codecs.get(1) instanceof TEXTMillis_InstantCodec); | ||
} | ||
|
||
// @Test | ||
// void getCodecPair_ShouldReturnCorrectCodecsForTimestampStringFormat() { | ||
// Mockito.when(propertyHelper.getString("timestamp.format")).thenReturn("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); | ||
// List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.TIMESTAMP_STRING_FORMAT); | ||
// assertFalse(codecs.isEmpty()); | ||
// assertTrue(codecs.get(0) instanceof TIMESTAMP_StringFormatCodec); | ||
// assertTrue(codecs.get(1) instanceof TEXTFormat_InstantCodec); | ||
// } | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForPointType() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.POINT_TYPE); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof PointCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForDateRange() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.DATE_RANGE); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof DateRangeCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldReturnCorrectCodecsForLineString() { | ||
List<TypeCodec<?>> codecs = CodecFactory.getCodecPair(propertyHelper, Codecset.LINE_STRING); | ||
assertFalse(codecs.isEmpty()); | ||
assertTrue(codecs.get(0) instanceof LineStringCodec); | ||
} | ||
|
||
@Test | ||
void getCodecPair_ShouldThrowExceptionForUnknownCodec() { | ||
assertThrows(NullPointerException.class, () -> { | ||
CodecFactory.getCodecPair(propertyHelper, null); | ||
}); | ||
} | ||
|
||
} |
127 changes: 127 additions & 0 deletions
127
src/test/java/com/datastax/cdm/cql/codec/DATERANGETYPE_CodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.datastax.cdm.cql.codec; | ||
|
||
import com.datastax.dse.driver.api.core.data.time.DateRange; | ||
import com.datastax.dse.driver.internal.core.type.codec.time.DateRangeCodec; | ||
import com.datastax.oss.driver.api.core.ProtocolVersion; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import java.nio.ByteBuffer; | ||
import java.text.ParseException; | ||
import java.time.Instant; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DATERANGETYPE_CodecTest { | ||
|
||
private DateRangeCodec codec; | ||
private final ProtocolVersion protocolVersion = ProtocolVersion.DEFAULT; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
codec = new DateRangeCodec(); | ||
} | ||
|
||
@Test | ||
void encode_ShouldEncodeDateRangeToByteBuffer() throws ParseException { | ||
String dateRangeString = "2001-01-01"; | ||
DateRange dateRange; | ||
try { | ||
dateRange = DateRange.parse(dateRangeString); | ||
} catch (ParseException e) { | ||
fail("Failed to parse the date range: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
// Encode the DateRange object | ||
ByteBuffer encoded = codec.encode(dateRange, protocolVersion); | ||
|
||
// Assertions | ||
assertNotNull(encoded); | ||
assertTrue(encoded.remaining() > 0); | ||
|
||
// Decode the ByteBuffer back to a DateRange and compare | ||
DateRange decoded = codec.decode(encoded, protocolVersion); | ||
assertEquals(dateRange, decoded); | ||
} | ||
|
||
@Test | ||
void decode_ShouldDecodeByteBufferToDateRange() throws ParseException { | ||
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.DAYS); | ||
String expectedFormattedDate = DateTimeFormatter.ISO_LOCAL_DATE.format(zonedDateTime); | ||
|
||
// Create a DateRange object using a string in the expected format | ||
DateRange dateRange = DateRange.parse(expectedFormattedDate); | ||
String formatted = codec.format(dateRange); | ||
|
||
// The formatted string should be surrounded by single quotes, remove them for the comparison | ||
String unquotedFormatted = formatted.replace("'", ""); | ||
assertEquals(expectedFormattedDate, unquotedFormatted); | ||
} | ||
|
||
@Test | ||
void format_ShouldFormatDateRangeToString() throws ParseException { | ||
String dateRangeString = "2001-01-01"; // Adjust this string to the correct format | ||
DateRange dateRange; | ||
try { | ||
dateRange = DateRange.parse(dateRangeString); | ||
} catch (ParseException e) { | ||
fail("Failed to parse the date range for setup: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
// Format the date range using the codec | ||
String formatted = codec.format(dateRange); | ||
assertNotNull(formatted); | ||
|
||
// Remove single quotes for parsing | ||
String unquotedFormatted = formatted.replace("'", ""); | ||
DateRange parsedDateRange; | ||
try { | ||
parsedDateRange = DateRange.parse(unquotedFormatted); | ||
} catch (ParseException e) { | ||
fail("Failed to parse the formatted date range: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
// The parsed DateRange should equal the original DateRange | ||
assertEquals(dateRange, parsedDateRange); | ||
} | ||
|
||
@Test | ||
void parse_ShouldParseStringToDateRange() throws ParseException { | ||
String formattedDateTime = ZonedDateTime.now() | ||
.withZoneSameInstant(ZoneOffset.UTC) | ||
.truncatedTo(ChronoUnit.MILLIS) | ||
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
|
||
// Enclose in single quotes as per the error message | ||
String dateRangeLiteral = "'" + formattedDateTime + "'"; | ||
|
||
// Attempt to parse it using the codec | ||
DateRange parsedDateRange; | ||
try { | ||
parsedDateRange = codec.parse(dateRangeLiteral); | ||
} catch (Exception e) { | ||
fail("Parsing failed with exception: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
assertNotNull(parsedDateRange); | ||
} | ||
|
||
@Test | ||
void encode_ShouldHandleNullValues() { | ||
ByteBuffer result = codec.encode(null, protocolVersion); | ||
assertNull(result); | ||
} | ||
|
||
@Test | ||
void decode_ShouldHandleNullByteBuffer() { | ||
DateRange result = codec.decode(null, protocolVersion); | ||
assertNull(result); | ||
} | ||
} |
Oops, something went wrong.