fix(iot): emit retained message timestamps in milliseconds (#6365)

Match the IoT Data Long response contract instead of emitting floating-point seconds. Cover both retained read responses with fixed values, missing-timestamp controls, and SDK wire round trips.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-23 14:08:01 -04:00
1 parent 1b8548a0af
commit 4093694451
4 files changed
+114 -5

No files matched your search

@@ -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])));
}
}
}
}