mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix: serialize Kinesis stream creation timestamps as plain decimals (#2173)
Fixes #2099 Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
This commit is contained in:
1 parent
c22652dd15
commit
c33508651e
3 files changed
+103
-3
No files matched your search
@@ -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() {
|
||||
|
||||
+59
-1
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user