Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iot): emit retained message timestamps in milliseconds (#6365)
Aniket Shukla4093694451c2
diff --git a/docs/services/iot.md b/docs/services/iot.mdindex 875a4aeea2..146160ef9e 100644--- a/docs/services/iot.md+++ b/docs/services/iot.md@@ -13,7 +13,7 @@ Supported MVP 1 behavior:- Policy basics: `CreatePolicy`, `GetPolicy`, `ListPolicies`, `DeletePolicy`, policy version lifecycle, `AttachPolicy`, `DetachPolicy`, `ListAttachedPolicies`, and `ListTargetsForPolicy`.- Thing principal basics: `AttachThingPrincipal`, `DetachThingPrincipal`, `ListThingPrincipals`, `ListPrincipalThings`, and the paginated `ListThingPrincipalsV2` and `ListPrincipalThingsV2`. Exclusive attachment is not modelled, so the V2 lists report every attachment as `NON_EXCLUSIVE_THING`, and a `thingPrincipalType` filter of `EXCLUSIVE_THING` returns none.- Tags for things, certificates, policies, and topic rules.-- IoT Data retained messages: retained `Publish`, `GetRetainedMessage`, and paginated `ListRetainedMessages`.+- IoT Data retained messages: retained `Publish`, `GetRetainedMessage`, and paginated `ListRetainedMessages`. Both read actions report `lastModifiedTime` as integer epoch milliseconds, as required by the IoT Data API.- Shadow null-delete and version-conflict behavior for HTTP and shared service paths.- Topic rule duplicate/delete/replace semantics, plus `republish`, `sqs`, `sns`, `s3`, `dynamoDBv2`, `kinesis`, `lambda`, `firehose`, and `cloudwatchLogs` action dispatch.diff --git a/pom.xml b/pom.xmlindex 3902ae10f6..2ad1956285 100644--- a/pom.xml+++ b/pom.xml@@ -330,6 +330,12 @@</dependency><!-- Testing -->+ <dependency>+ <groupId>software.amazon.awssdk</groupId>+ <artifactId>iotdataplane</artifactId>+ <version>${aws.sdk.version}</version>+ <scope>test</scope>+ </dependency><dependency><groupId>software.amazon.awssdk</groupId><artifactId>bedrock</artifactId>diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.javaindex 54c2045789..98e051d057 100644--- a/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java+++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java@@ -21,6 +21,7 @@ import jakarta.ws.rs.core.HttpHeaders;import jakarta.ws.rs.core.MediaType;import jakarta.ws.rs.core.Response;+import java.time.Instant;import java.util.Base64;import java.util.UUID;@@ -163,7 +164,7 @@ public class IotDataController {response.put("topic", retained.getTopic());response.put("payload", retained.getPayload());response.put("qos", retained.getQos());- putEpoch(response, "lastModifiedTime", retained.getLastModifiedTime());+ putEpochMillis(response, "lastModifiedTime", retained.getLastModifiedTime());return Response.ok(response).build();}@@ -179,7 +180,7 @@ public class IotDataController {item.put("topic", retained.getTopic());item.put("payloadSize", Base64.getDecoder().decode(retained.getPayload()).length);item.put("qos", retained.getQos());- putEpoch(item, "lastModifiedTime", retained.getLastModifiedTime());+ putEpochMillis(item, "lastModifiedTime", retained.getLastModifiedTime());}if (page.nextToken() != null) {response.put("nextToken", page.nextToken());@@ -187,9 +188,9 @@ public class IotDataController {return Response.ok(response).build();}- private void putEpoch(ObjectNode node, String field, java.time.Instant instant) {+ private void putEpochMillis(ObjectNode node, String field, Instant instant) {if (instant != null) {- node.put(field, instant.toEpochMilli() / 1000.0);+ node.put(field, instant.toEpochMilli());}}}diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.javanew file mode 100644index 0000000000..a384c4d273--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.java@@ -0,0 +1,102 @@+package io.github.hectorvent.floci.services.iot;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.services.iot.model.IotRetainedMessage;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.RestAssured;+import jakarta.ws.rs.core.Response;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.ValueSource;+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;+import software.amazon.awssdk.core.SdkBytes;+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;+import software.amazon.awssdk.regions.Region;+import software.amazon.awssdk.services.iotdataplane.IotDataPlaneClient;++import java.net.URI;+import java.time.Instant;+import java.util.List;+import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++@QuarkusTest+class IotRetainedTimestampIntegrationTest {++ @ParameterizedTest+ @CsvSource({"false,0", "true,0", "false,1234", "true,1234",+ "false,1792767673821", "true,1792767673821"})+ void retainedTimestampsAreIntegralMilliseconds(boolean list, long milliseconds) {+ JsonNode timestamp = rendered(list, Instant.ofEpochMilli(milliseconds)).path("lastModifiedTime");+ assertTrue(timestamp.isIntegralNumber(), timestamp.toString());+ assertEquals(milliseconds, timestamp.longValue());+ }++ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ void missingLegacyTimestampRemainsAbsent(boolean list) {+ assertFalse(rendered(list, null).has("lastModifiedTime"));+ }++ private JsonNode rendered(boolean list, Instant instant) {+ IotRetainedMessage retained = new IotRetainedMessage();+ retained.setTopic("devices/timestamp");+ retained.setPayload("b25saW5l");+ retained.setLastModifiedTime(instant);+ IotService service = mock(IotService.class);+ when(service.getRetainedMessage(retained.getTopic())).thenReturn(retained);+ when(service.listRetainedMessages(null, null)).thenReturn(new IotService.Page<>(List.of(retained), null));+ IotDataController controller = new IotDataController(service, mock(RegionResolver.class), new ObjectMapper());+ try (Response response = list ? controller.listRetainedMessages(null, null)+ : controller.getRetainedMessage(retained.getTopic())) {+ JsonNode node = (JsonNode) response.getEntity();+ return list ? node.path("retainedTopics").get(0) : node;+ }+ }++ @ParameterizedTest+ @ValueSource(ints = {0, 1})+ void sdkAndWireReadTheStoredTimeInMilliseconds(int qos) throws Exception {+ String account = "74163610000" + qos;+ String topic = "timestamp/" + UUID.randomUUID();+ String authorization = "AWS4-HMAC-SHA256 Credential=" + account+ + "/20261023/us-east-1/iotdata/aws4_request, SignedHeaders=host, Signature=test";+ ObjectMapper mapper = new ObjectMapper();+ try (IotDataPlaneClient client = IotDataPlaneClient.builder()+ .endpointOverride(URI.create("http://localhost:" + RestAssured.port))+ .httpClientBuilder(UrlConnectionHttpClient.builder()).region(Region.US_EAST_1)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test")))+ .build()) {+ long before = System.currentTimeMillis();+ client.publish(request -> request.topic(topic).qos(qos).retain(true).payload(SdkBytes.fromUtf8String("online")));+ long after = System.currentTimeMillis();+ try {+ JsonNode fetched = mapper.readTree(given().header("Authorization", authorization)+ .get("/retainedMessage/" + topic).then().statusCode(200).extract().asString());+ JsonNode timestamp = fetched.path("lastModifiedTime");+ assertTrue(timestamp.isIntegralNumber(), timestamp.toString());+ long stored = timestamp.longValue();+ assertTrue(stored >= before && stored <= after, timestamp.toString());+ JsonNode listed = mapper.readTree(given().header("Authorization", authorization)+ .get("/retainedMessage").then().statusCode(200).extract().asString()).path("retainedTopics");+ assertEquals(1, listed.size());+ assertTrue(listed.get(0).path("lastModifiedTime").isIntegralNumber());+ assertEquals(stored, listed.get(0).path("lastModifiedTime").longValue());+ assertEquals(stored, client.getRetainedMessage(request -> request.topic(topic)).lastModifiedTime());+ assertEquals(stored, client.listRetainedMessages(request -> request.maxResults(100))+ .retainedTopics().get(0).lastModifiedTime());+ } finally {+ client.publish(request -> request.topic(topic).retain(true).payload(SdkBytes.fromByteArray(new byte[0])));+ }+ }+ }+}