Skip to content

Commit

Permalink
Merge branch 'main' into snyk-upgrade-dafe6fd4d1eb795d6504c32641f4b46f
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremya authored Nov 9, 2023
2 parents 84be7cb + 72dded7 commit a876016
Show file tree
Hide file tree
Showing 10 changed files with 525 additions and 2 deletions.
4 changes: 4 additions & 0 deletions PERF/cdm-v4.properties
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ spark.cdm.perfops.writeRateLimit 5000
# as Epoch milliseconds
# TIMESTAMP_STRING_FORMAT : timestamp stored in a String,
# with a custom format
# POLYGON_TYPE : PolygonType geospatial DSE type
# POINT_TYPE : PointType geospatial DSE type
# LINE_STRING : LineStringType geospatial DSE type
# DATE_RANGE : DateRange DSE type
#
# *** NOTE where there are multiple type pair options, such as with
# *** TIMESTAMP_STRING, only one can be configured at a time.
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
<artifactId>log4j-to-slf4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<scope>runtime</scope>
<groupId>com.esri.geometry</groupId>
<artifactId>esri-geometry-api</artifactId>
<version>2.2.0</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down Expand Up @@ -326,7 +332,7 @@
<counter>LINE</counter>
<value>MISSEDCOUNT</value>
<!-- <maximum>1544</maximum>-->
<maximum>3073</maximum>
<maximum>3085</maximum>
</limit>
</limits>
</rule>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/datastax/cdm/cql/codec/CodecFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.datastax.cdm.cql.codec;

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 java.util.Arrays;
Expand All @@ -15,6 +19,11 @@ public static List<TypeCodec<?>> getCodecPair(PropertyHelper propertyHelper, Cod
case DECIMAL_STRING: return Arrays.asList(new DECIMAL_StringCodec(propertyHelper), new TEXT_BigDecimalCodec(propertyHelper));
case TIMESTAMP_STRING_MILLIS: return Arrays.asList(new TIMESTAMP_StringMillisCodec(propertyHelper), new TEXTMillis_InstantCodec(propertyHelper));
case TIMESTAMP_STRING_FORMAT: return Arrays.asList(new TIMESTAMP_StringFormatCodec(propertyHelper), new TEXTFormat_InstantCodec(propertyHelper));
case POLYGON_TYPE: return Arrays.asList(new PolygonCodec());
case POINT_TYPE: return Arrays.asList(new PointCodec());
case DATE_RANGE: return Arrays.asList(new DateRangeCodec());
case LINE_STRING: return Arrays.asList(new LineStringCodec());

default:
throw new IllegalArgumentException("Unknown codec: " + codec);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/datastax/cdm/cql/codec/Codecset.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ public enum Codecset {
BIGINT_STRING,
DECIMAL_STRING,
TIMESTAMP_STRING_MILLIS,
TIMESTAMP_STRING_FORMAT
TIMESTAMP_STRING_FORMAT,
POINT_TYPE,
POLYGON_TYPE,
DATE_RANGE,
LINE_STRING
}
5 changes: 5 additions & 0 deletions src/main/java/com/datastax/cdm/data/CqlData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.cdm.data;

import com.datastax.dse.driver.api.core.type.DseDataTypes;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.type.*;

Expand Down Expand Up @@ -39,6 +40,10 @@ public enum Type {
primitiveDataTypeToJavaClassMap.put(DataTypes.TIMEUUID, java.util.UUID.class);
primitiveDataTypeToJavaClassMap.put(DataTypes.COUNTER, Long.class);
primitiveDataTypeToJavaClassMap.put(DataTypes.DURATION, com.datastax.oss.driver.api.core.data.CqlDuration.class);
primitiveDataTypeToJavaClassMap.put(DseDataTypes.POLYGON, com.datastax.dse.driver.api.core.data.geometry.Polygon.class);
primitiveDataTypeToJavaClassMap.put(DseDataTypes.POINT, com.datastax.dse.driver.api.core.data.geometry.Point.class);
primitiveDataTypeToJavaClassMap.put(DseDataTypes.LINE_STRING, com.datastax.dse.driver.api.core.data.geometry.LineString.class);
primitiveDataTypeToJavaClassMap.put(DseDataTypes.DATE_RANGE, com.datastax.dse.driver.api.core.data.time.DateRange.class);
}

public static Type toType(DataType dataType) {
Expand Down
114 changes: 114 additions & 0 deletions src/test/java/com/datastax/cdm/cql/codec/CodecFactoryTest.java
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 src/test/java/com/datastax/cdm/cql/codec/DATERANGETYPE_CodecTest.java
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);
}
}
Loading

0 comments on commit a876016

Please sign in to comment.