diff --git a/src/main/java/io/floci/aws/services/appsync/AppSyncService.java b/src/main/java/io/floci/aws/services/appsync/AppSyncService.java index 4aff23ce92..3f20a4a9ab 100644 --- a/src/main/java/io/floci/aws/services/appsync/AppSyncService.java +++ b/src/main/java/io/floci/aws/services/appsync/AppSyncService.java @@ -922,8 +922,9 @@ public class AppSyncService { ns.setChannelNamespaceArn(regionResolver.buildArn("appsync", regionResolver.regionOf(api.getArn()), "apis/" + apiId + "/channelNamespace/" + name)); ns.setCodeHandlers((String) request.get("codeHandlers")); - ns.setCreated(System.currentTimeMillis()); - ns.setLastModified(System.currentTimeMillis()); + long createdSeconds = clock.instant().getEpochSecond(); + ns.setCreated(createdSeconds); + ns.setLastModified(createdSeconds); channelNamespaceStore.put(nsKey, ns); return ns; } @@ -939,7 +940,7 @@ public class AppSyncService { ChannelNamespace existing = getChannelNamespace(apiId, name); if (request.containsKey("description")) existing.setDescription((String) request.get("description")); if (request.containsKey("codeHandlers")) existing.setCodeHandlers((String) request.get("codeHandlers")); - existing.setLastModified(System.currentTimeMillis()); + existing.setLastModified(clock.instant().getEpochSecond()); channelNamespaceStore.put(apiKey(apiId, name), existing); return existing; } diff --git a/src/test/java/io/floci/aws/services/appsync/AppSyncChannelNamespaceTimestampSdkTest.java b/src/test/java/io/floci/aws/services/appsync/AppSyncChannelNamespaceTimestampSdkTest.java new file mode 100644 index 0000000000..1288420f79 --- /dev/null +++ b/src/test/java/io/floci/aws/services/appsync/AppSyncChannelNamespaceTimestampSdkTest.java @@ -0,0 +1,74 @@ +package io.floci.aws.services.appsync; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.net.httpserver.HttpServer; +import io.floci.aws.services.appsync.model.ChannelNamespace; +import io.floci.aws.services.appsync.model.GraphqlApi; +import io.floci.aws.testing.ParallelLocks; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceAccessMode; +import org.junit.jupiter.api.parallel.ResourceLock; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.appsync.AppSyncClient; +import software.amazon.awssdk.services.appsync.model.CreateChannelNamespaceResponse; +import software.amazon.awssdk.services.appsync.model.UpdateChannelNamespaceResponse; + +import java.net.InetSocketAddress; +import java.net.URI; +import java.time.Duration; +import java.time.Instant; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ResourceLock(value = ParallelLocks.PORTS, mode = ResourceAccessMode.READ) +class AppSyncChannelNamespaceTimestampSdkTest { + + @Test + void theSdkDecodesSerializedNamespaceCreationAndUpdateTimes() throws Exception { + Instant createdAt = Instant.parse("2026-01-02T03:04:05Z"); + AppSyncServiceFixture.MutableClock clock = new AppSyncServiceFixture.MutableClock(createdAt); + AppSyncService service = AppSyncServiceFixture.create(clock); + GraphqlApi api = service.createGraphqlApi( + Map.of("name", "namespace-clock", "authenticationType", "API_KEY"), "us-east-1"); + ChannelNamespace created = service.createChannelNamespace(api.getApiId(), Map.of("name", "messages")); + ObjectMapper mapper = new ObjectMapper(); + AtomicReference response = new AtomicReference<>( + mapper.writeValueAsBytes(Map.of("channelNamespace", created))); + HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/", exchange -> { + exchange.getRequestBody().readAllBytes(); + byte[] body = response.get(); + exchange.getResponseHeaders().set("Content-Type", "application/json"); + exchange.sendResponseHeaders(200, body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.start(); + try (AppSyncClient client = AppSyncClient.builder() + .endpointOverride(URI.create("http://127.0.0.1:" + server.getAddress().getPort())) + .region(Region.US_EAST_1) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))) + .build()) { + CreateChannelNamespaceResponse decodedCreated = + client.createChannelNamespace(request -> request.apiId(api.getApiId()).name("messages")); + assertEquals(createdAt, decodedCreated.channelNamespace().created()); + assertEquals(createdAt, decodedCreated.channelNamespace().lastModified()); + + Instant updatedAt = createdAt.plus(Duration.ofMinutes(5)); + clock.set(updatedAt); + ChannelNamespace updated = service.updateChannelNamespace( + api.getApiId(), "messages", Map.of("codeHandlers", "updated")); + response.set(mapper.writeValueAsBytes(Map.of("channelNamespace", updated))); + UpdateChannelNamespaceResponse decodedUpdated = + client.updateChannelNamespace(request -> request.apiId(api.getApiId()).name("messages")); + assertEquals(createdAt, decodedUpdated.channelNamespace().created()); + assertEquals(updatedAt, decodedUpdated.channelNamespace().lastModified()); + } finally { + server.stop(0); + } + } +} diff --git a/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java b/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java index 5325a3a79e..34a8f76a6a 100644 --- a/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java +++ b/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java @@ -7,6 +7,7 @@ import io.floci.aws.services.appsync.graphql.auth.LambdaAuthorizerCache; import io.floci.aws.services.appsync.graphql.auth.LambdaAuthorizerResult; import io.floci.aws.services.appsync.model.ApiKey; import io.floci.aws.services.appsync.model.AuthenticationType; +import io.floci.aws.services.appsync.model.ChannelNamespace; import io.floci.aws.services.appsync.model.DomainName; import io.floci.aws.services.appsync.model.FunctionConfiguration; import io.floci.aws.services.appsync.model.GraphqlApi; @@ -766,4 +767,21 @@ class AppSyncServiceTest { service.createType(api.getApiId(), request); assertEquals("SDL", service.getType(api.getApiId(), "Item").getFormat().name()); } + + @Test + void channelNamespaceTimestampsComeFromTheInjectedClock() { + AppSyncServiceFixture.MutableClock clock = new AppSyncServiceFixture.MutableClock(NOW); + AppSyncService svc = AppSyncServiceFixture.create(clock); + GraphqlApi api = svc.createGraphqlApi(Map.of("name", "events", "authenticationType", "API_KEY"), "us-east-1"); + + ChannelNamespace created = svc.createChannelNamespace(api.getApiId(), Map.of("name", "ns")); + // The wire shape is a timestamp, which the REST JSON protocol carries as epoch seconds. + assertEquals(NOW.getEpochSecond(), created.getCreated()); + assertEquals(NOW.getEpochSecond(), created.getLastModified()); + + clock.set(NOW.plus(Duration.ofMinutes(5))); + ChannelNamespace updated = svc.updateChannelNamespace(api.getApiId(), "ns", Map.of("description", "d")); + assertEquals(NOW.getEpochSecond(), updated.getCreated()); + assertEquals(NOW.plus(Duration.ofMinutes(5)).getEpochSecond(), updated.getLastModified()); + } }