From 9e0bc1b43b644aae58d027998f83adcfed53382b Mon Sep 17 00:00:00 2001 From: Jimmy Liu Jia <91264074+JimmyLiuJia@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:15:32 +0800 Subject: [PATCH] test(servicecatalog): use an advanceable clock for termination records (#7102) --- .../servicecatalog/ServiceCatalogService.java | 31 ++++++++++--------- ...ovisionedProductLifecycleConsumerTest.java | 16 +++++++++- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java b/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java index 002663c5f2..8301264e0e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java +++ b/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java @@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.organizations.model.OrganizationAccou import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import java.time.Clock; import java.time.Instant; import java.util.ArrayList; import java.util.Comparator; @@ -47,10 +48,11 @@ public class ServiceCatalogService implements PassedRoleLookup { private final StorageBackend provisionedProductStore; private final ObjectMapper objectMapper; private final OrganizationsService organizationsService; + private final Clock clock; @Inject public ServiceCatalogService(StorageFactory storageFactory, ObjectMapper objectMapper, - OrganizationsService organizationsService) { + OrganizationsService organizationsService, Clock clock) { this.portfolioStore = storageFactory.create("servicecatalog", "servicecatalog-portfolios.json", new TypeReference>() {}); this.productStore = storageFactory.create("servicecatalog", "servicecatalog-products.json", @@ -65,6 +67,7 @@ public class ServiceCatalogService implements PassedRoleLookup { new TypeReference>() {}); this.objectMapper = objectMapper; this.organizationsService = organizationsService; + this.clock = clock; } public ObjectNode createPortfolio(JsonNode request, String region, String accountId) { @@ -74,7 +77,7 @@ public class ServiceCatalogService implements PassedRoleLookup { ObjectNode portfolio = copy(request); portfolio.put("Id", id); portfolio.put("ARN", AwsArnUtils.Arn.of("catalog", region, accountId, "portfolio/" + id).toString()); - portfolio.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + portfolio.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); portfolioStore.put(id, portfolio); return portfolio.deepCopy(); } @@ -107,7 +110,7 @@ public class ServiceCatalogService implements PassedRoleLookup { portfolio.put("DisplayName", CONTROL_TOWER_PORTFOLIO_NAME); portfolio.put("ProviderName", CONTROL_TOWER_PROVIDER_NAME); portfolio.put("Description", "AWS Control Tower Account Factory Portfolio"); - portfolio.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + portfolio.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); portfolioStore.put(CONTROL_TOWER_PORTFOLIO_ID, portfolio); } if (productStore.get(CONTROL_TOWER_PRODUCT_ID).isEmpty()) { @@ -118,7 +121,7 @@ public class ServiceCatalogService implements PassedRoleLookup { product.put("Name", CONTROL_TOWER_PRODUCT_NAME); product.put("Owner", CONTROL_TOWER_PROVIDER_NAME); product.put("Type", "CLOUD_FORMATION_TEMPLATE"); - product.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + product.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); product.putArray("ProvisioningArtifactIds").add(CONTROL_TOWER_ARTIFACT_ID); product.putArray("ProvisioningArtifactNames").add("AWS Control Tower Account Factory"); productStore.put(CONTROL_TOWER_PRODUCT_ID, product); @@ -159,7 +162,7 @@ public class ServiceCatalogService implements PassedRoleLookup { ObjectNode product = copy(request); product.put("Id", id); product.put("ARN", AwsArnUtils.Arn.of("catalog", region, accountId, "product/" + id).toString()); - product.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + product.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); ArrayNode artifactIds = product.putArray("ProvisioningArtifactIds"); ArrayNode artifactNames = product.putArray("ProvisioningArtifactNames"); JsonNode artifacts = request.path("ProvisioningArtifactParameters"); @@ -231,7 +234,7 @@ public class ServiceCatalogService implements PassedRoleLookup { provisioned.put("Status", "AVAILABLE"); provisioned.put("ProductId", productId); provisioned.put("ProvisioningArtifactId", artifactId); - provisioned.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + provisioned.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); provisionedProductStore.put(provisionedId, provisioned); return provisionedProductWithRecord(provisioned); } @@ -701,7 +704,7 @@ public class ServiceCatalogService implements PassedRoleLookup { ObjectNode product = source.deepCopy(); product.put("Id", newId); product.put("ARN", AwsArnUtils.Arn.of("catalog", region, accountId, "product/" + newId).toString()); - product.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + product.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); String targetName = text(request, "TargetProductName"); if (targetName != null && !targetName.isBlank()) { product.put("Name", targetName); @@ -955,7 +958,7 @@ public class ServiceCatalogService implements PassedRoleLookup { plan.put("ProvisionProductId", productId); plan.put("ProvisionedProductName", text(request, "ProvisionedProductName")); plan.put("ProvisioningArtifactId", artifactId); - plan.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + plan.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); associationStore.put(planId, plan); return plan.deepCopy(); } @@ -1162,7 +1165,7 @@ public class ServiceCatalogService implements PassedRoleLookup { String recordId = id("rec"); ObjectNode record = newProvisionedProductRecord(product, recordId, - "UPDATE_PROVISIONED_PRODUCT", Instant.now().toEpochMilli() / 1000.0); + "UPDATE_PROVISIONED_PRODUCT", Instant.now(clock).toEpochMilli() / 1000.0); record.put("ProvisionedProductType", text(product, "Type")); associationStore.put(recordId, record); @@ -1206,7 +1209,7 @@ public class ServiceCatalogService implements PassedRoleLookup { provisioned.put("PhysicalId", physicalId); provisioned.put("ProductId", productId); provisioned.put("ProvisioningArtifactId", provisioningArtifactId); - provisioned.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + provisioned.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); provisionedProductStore.put(provisionedId, provisioned); String recordId = id("rec"); @@ -1454,7 +1457,7 @@ public class ServiceCatalogService implements PassedRoleLookup { String recordId = id("rec"); ObjectNode record = newProvisionedProductRecord(product, recordId, - "UPDATE_PROVISIONED_PRODUCT", Instant.now().toEpochMilli() / 1000.0); + "UPDATE_PROVISIONED_PRODUCT", Instant.now(clock).toEpochMilli() / 1000.0); associationStore.put(recordId, record); ObjectNode result = product.deepCopy(); @@ -1477,7 +1480,7 @@ public class ServiceCatalogService implements PassedRoleLookup { String recordId = id("rec"); ObjectNode record = newProvisionedProductRecord(product, recordId, - "UPDATE_PROVISIONED_PRODUCT", Instant.now().toEpochMilli() / 1000.0); + "UPDATE_PROVISIONED_PRODUCT", Instant.now(clock).toEpochMilli() / 1000.0); associationStore.put(recordId, record); ObjectNode result = product.deepCopy(); @@ -1528,7 +1531,7 @@ public class ServiceCatalogService implements PassedRoleLookup { detail.put("Id", artifactId); detail.put("Name", name); detail.put("Type", type); - detail.put("CreatedTime", Instant.now().toEpochMilli() / 1000.0); + detail.put("CreatedTime", Instant.now(clock).toEpochMilli() / 1000.0); detail.put("Active", true); String description = text(parameters, "Description"); if (description != null && !description.isBlank()) { @@ -1569,7 +1572,7 @@ public class ServiceCatalogService implements PassedRoleLookup { } String id = text(product, "Id"); product.put("Status", "TERMINATED"); - product.put("UpdatedTime", Instant.now().toEpochMilli() / 1000.0); + product.put("UpdatedTime", Instant.now(clock).toEpochMilli() / 1000.0); provisionedProductStore.put(id, product); String recordId = id("rec"); diff --git a/src/test/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogProvisionedProductLifecycleConsumerTest.java b/src/test/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogProvisionedProductLifecycleConsumerTest.java index 3140eb1fcb..9f6530912e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogProvisionedProductLifecycleConsumerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogProvisionedProductLifecycleConsumerTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.servicecatalog; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.testing.MutableClock; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; @@ -9,10 +10,13 @@ import jakarta.inject.Inject; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.time.Duration; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Wire-level tests for {@code DescribeProvisionedProduct}, @@ -40,6 +44,9 @@ class ServiceCatalogProvisionedProductLifecycleConsumerTest { @Inject ObjectMapper objectMapper; + @Inject + MutableClock clock; + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -241,7 +248,8 @@ class ServiceCatalogProvisionedProductLifecycleConsumerTest { double originalCreatedTime = objectMapper.readTree(originalProduct.asString()) .path("ProvisionedProductDetail").path("CreatedTime").asDouble(); - Thread.sleep(25); + clock.advance(Duration.ofSeconds(1)); + double beforeTermination = clock.instant().toEpochMilli() / 1000.0; Response terminated = call("TerminateProvisionedProduct", "{\"ProvisionedProductId\":\"" + provisionedId + "\",\"TerminateToken\":\"tok-terminate-pp\"}") .then() @@ -250,6 +258,7 @@ class ServiceCatalogProvisionedProductLifecycleConsumerTest { .body("RecordDetail.Status", equalTo("SUCCEEDED")) .body("RecordDetail.RecordType", equalTo("TERMINATE_PROVISIONED_PRODUCT")) .extract().response(); + double afterTermination = clock.instant().toEpochMilli() / 1000.0; JsonNode terminationRecord = objectMapper.readTree(terminated.asString()).path("RecordDetail"); String recordId = terminationRecord.path("RecordId").asText(); @@ -260,6 +269,11 @@ class ServiceCatalogProvisionedProductLifecycleConsumerTest { .extract().response(); assertEquals(originalCreatedTime, objectMapper.readTree(terminatedProduct.asString()) .path("ProvisionedProductDetail").path("CreatedTime").asDouble()); + double updatedTime = objectMapper.readTree(terminatedProduct.asString()) + .path("ProvisionedProductDetail").path("UpdatedTime").asDouble(); + assertTrue(updatedTime > originalCreatedTime); + assertTrue(updatedTime >= beforeTermination && updatedTime <= afterTermination); + assertEquals(updatedTime, terminationRecord.path("CreatedTime").asDouble()); Response described = call("DescribeRecord", "{\"Id\":\"" + recordId + "\"}") .then()