Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix: serialize Kinesis stream creation timestamps as plain decimals (#2173)

Matt Van Hornc33508651ea1
diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java
index 7beb2a8edd..9db0baa347 100644
--- a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java
@@ -15,6 +15,8 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
+import java.math.BigDecimal;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
@@ -168,7 +170,7 @@ public class KinesisJsonHandler {
desc.put("StreamStatus", stream.getStreamStatus());
desc.put("HasMoreShards", false);
desc.put("RetentionPeriodHours", stream.getRetentionPeriodHours());
- desc.put("StreamCreationTimestamp", stream.getStreamCreationTimestamp().toEpochMilli() / 1000.0);
+ desc.put("StreamCreationTimestamp", epochSeconds(stream.getStreamCreationTimestamp()));
desc.put("EncryptionType", stream.getEncryptionType());
if (stream.getKeyId() != null) {
desc.put("KeyId", stream.getKeyId());
@@ -209,7 +211,7 @@ public class KinesisJsonHandler {
summary.put("StreamARN", stream.getStreamArn());
summary.put("StreamStatus", stream.getStreamStatus());
summary.put("RetentionPeriodHours", stream.getRetentionPeriodHours());
- summary.put("StreamCreationTimestamp", stream.getStreamCreationTimestamp().toEpochMilli() / 1000.0);
+ summary.put("StreamCreationTimestamp", epochSeconds(stream.getStreamCreationTimestamp()));
summary.put("OpenShardCount", (int) stream.getShards().stream().filter(s -> !s.isClosed()).count());
summary.put("EncryptionType", stream.getEncryptionType());
if (stream.getKeyId() != null) {
@@ -231,6 +233,10 @@ public class KinesisJsonHandler {
parent.putObject("StreamModeDetails").put("StreamMode", stream.getStreamMode());
}
+ private BigDecimal epochSeconds(Instant timestamp) {
+ return BigDecimal.valueOf(timestamp.toEpochMilli(), 3);
+ }
+
private Response handleRegisterStreamConsumer(JsonNode request, String region) {
String streamArn = request.path("StreamARN").asText();
String consumerName = request.path("ConsumerName").asText();
diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java
index 31c97cc483..2f9fbe215f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java
@@ -10,7 +10,9 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
+import java.math.BigDecimal;
import java.nio.ByteBuffer;
+import java.util.regex.Pattern;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@@ -21,6 +23,9 @@ import static org.junit.jupiter.api.Assertions.*;
class KinesisIntegrationTest {
private static final String KINESIS_CONTENT_TYPE = "application/x-amz-json-1.1";
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static final Pattern CREATION_TIMESTAMP_PATTERN = Pattern.compile(
+ "\\\"StreamCreationTimestamp\\\"\\s*:\\s*(-?\\d+(?:\\.\\d+)?)(?=\\s*[,}])");
@BeforeAll
static void configureRestAssured() {
@@ -89,6 +94,37 @@ class KinesisIntegrationTest {
.body("Shards.size()", equalTo(2));
}
+ @Test
+ @Order(4)
+ void describeStreamReturnsPlainDecimalCreationTimestamp() throws Exception {
+ String responseBody = given()
+ .header("X-Amz-Target", "Kinesis_20131202.DescribeStream")
+ .contentType(KINESIS_CONTENT_TYPE)
+ .body("""
+ {"StreamName": "list-shards-test"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().asString();
+
+ var matcher = CREATION_TIMESTAMP_PATTERN.matcher(responseBody);
+ assertTrue(matcher.find(), "timestamp must be serialized as a plain JSON decimal: " + responseBody);
+ String timestampLexeme = matcher.group(1);
+ BigDecimal timestamp = new BigDecimal(timestampLexeme);
+ assertTrue(timestamp.signum() > 0);
+ assertTrue(timestamp.stripTrailingZeros().scale() <= 3);
+
+ JsonNode timestampNode = MAPPER.readTree(responseBody)
+ .path("StreamDescription")
+ .path("StreamCreationTimestamp");
+ assertTrue(timestampNode.isNumber());
+ assertEquals(timestamp, timestampNode.decimalValue());
+ assertFalse(timestampLexeme.contains("E"));
+ assertFalse(timestampLexeme.contains("e"));
+ }
+
@Test
@Order(20)
void describeStreamByArn() {
diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java
index 372aacb93e..72d7885ac0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java
@@ -10,12 +10,16 @@ import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import java.math.BigDecimal;
+import java.time.Instant;
import java.util.Base64;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class KinesisJsonHandlerTest {
@@ -24,11 +28,12 @@ class KinesisJsonHandlerTest {
private static final String STREAM_ARN = "arn:aws:kinesis:us-east-1:123456789012:stream/test-stream";
private static final ObjectMapper MAPPER = new ObjectMapper();
+ private KinesisService service;
private KinesisJsonHandler handler;
@BeforeEach
void setUp() {
- KinesisService service = new KinesisService(
+ service = new KinesisService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT)
@@ -59,6 +64,59 @@ class KinesisJsonHandlerTest {
assertEquals("test-stream", desc.get("StreamName").asText());
}
+ @Test
+ void describeStreamSerializesCreationTimestampAsPlainDecimal() throws Exception {
+ createStream("test-stream");
+ service.describeStream("test-stream", REGION)
+ .setStreamCreationTimestamp(Instant.ofEpochMilli(1_785_732_980_986L));
+
+ ObjectNode req = MAPPER.createObjectNode();
+ req.put("StreamName", "test-stream");
+ ObjectNode desc = (ObjectNode) responseEntity(handler.handle("DescribeStream", req, REGION))
+ .get("StreamDescription");
+
+ assertPlainDecimalTimestamp(desc, "1785732980.986");
+ }
+
+ @Test
+ void describeStreamSummarySerializesCreationTimestampAsPlainDecimal() throws Exception {
+ createStream("test-stream");
+ service.describeStream("test-stream", REGION)
+ .setStreamCreationTimestamp(Instant.ofEpochMilli(1_785_732_980_986L));
+
+ ObjectNode req = MAPPER.createObjectNode();
+ req.put("StreamName", "test-stream");
+ ObjectNode summary = (ObjectNode) responseEntity(handler.handle("DescribeStreamSummary", req, REGION))
+ .get("StreamDescriptionSummary");
+
+ assertPlainDecimalTimestamp(summary, "1785732980.986");
+ }
+
+ @Test
+ void wholeSecondCreationTimestampRemainsNumeric() throws Exception {
+ createStream("test-stream");
+ service.describeStream("test-stream", REGION)
+ .setStreamCreationTimestamp(Instant.ofEpochMilli(1_785_732_980_000L));
+
+ ObjectNode req = MAPPER.createObjectNode();
+ req.put("StreamName", "test-stream");
+ ObjectNode desc = (ObjectNode) responseEntity(handler.handle("DescribeStream", req, REGION))
+ .get("StreamDescription");
+
+ assertPlainDecimalTimestamp(desc, "1785732980.000");
+ }
+
+ private void assertPlainDecimalTimestamp(ObjectNode description, String expected) throws Exception {
+ var timestamp = description.get("StreamCreationTimestamp");
+ assertTrue(timestamp.isNumber());
+ assertEquals(new BigDecimal(expected), timestamp.decimalValue());
+
+ String serialized = MAPPER.writeValueAsString(timestamp);
+ assertEquals(expected, serialized);
+ assertFalse(serialized.contains("E"));
+ assertFalse(serialized.contains("e"));
+ }
+
@Test
void describeStreamByArn() {
createStream("test-stream");