mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(marketplace-catalog): emulate AWS Marketplace Catalog API (#3338)
This commit is contained in:
1 parent
9a2a89399a
commit
50fa334dc5
17 files changed
+1295
-2
No files matched your search
+88
@@ -0,0 +1,88 @@
|
||||
package io.github.hectorvent.floci.services.marketplace;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class MarketplaceCatalogControllerIntegrationTest {
|
||||
@BeforeAll
|
||||
static void configure() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeSetCreatesAndDescribesEntity() {
|
||||
String body = "{\"Catalog\":\"AWSMarketplace\",\"ChangeSet\":[{\"ChangeType\":\"CreateProduct\",\"Entity\":{\"Type\":\"SaaSProduct@1.0\",\"Identifier\":\"@1\"},\"DetailsDocument\":{\"ProductTitle\":\"Local product\"}}]}";
|
||||
String id = given().contentType("application/json").header("Authorization", auth())
|
||||
.body(body).post("/StartChangeSet").then().statusCode(200)
|
||||
.body("ChangeSetId", notNullValue())
|
||||
.body("ChangeSetArn", containsString(":000000000000:AWSMarketplace/ChangeSet/"))
|
||||
.extract().path("ChangeSetId");
|
||||
var described = given().header("Authorization", auth())
|
||||
.get("/DescribeChangeSet?catalog=AWSMarketplace&changeSetId=" + id)
|
||||
.then().statusCode(200).body("Status", equalTo("SUCCEEDED")).extract().response();
|
||||
String entityId = described.path("ChangeSet[0].Entity.Identifier");
|
||||
given().header("Authorization", auth())
|
||||
.get("/DescribeEntity?catalog=AWSMarketplace&entityId=" + entityId)
|
||||
.then().statusCode(200)
|
||||
.body("EntityIdentifier", equalTo(entityId))
|
||||
.body("EntityArn", containsString(":000000000000:AWSMarketplace/SaaSProduct/"))
|
||||
.body("DetailsDocument.ProductTitle", equalTo("Local product"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelAndValidationMatchAwsErrorShapes() {
|
||||
String id = given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"Catalog\":\"AWSMarketplace\",\"ChangeSet\":[{\"ChangeType\":\"CreateOffer\",\"Entity\":{\"Type\":\"Offer@1.0\",\"Identifier\":\"@1\"},\"DetailsDocument\":{}}]}")
|
||||
.post("/StartChangeSet").then().statusCode(200).extract().path("ChangeSetId");
|
||||
given().header("Authorization", auth())
|
||||
.patch("/CancelChangeSet?catalog=AWSMarketplace&changeSetId=" + id)
|
||||
.then().statusCode(200).body("ChangeSetId", equalTo(id));
|
||||
given().contentType("application/json").header("Authorization", auth()).body("{}")
|
||||
.post("/ListEntities").then().statusCode(422)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnsupportedChangeTypeBeforeMutatingCatalogState() {
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"Catalog\":\"AWSMarketplace\",\"ChangeSet\":[{\"ChangeType\":\"CreatProduct\",\"Entity\":{\"Type\":\"SaaSProduct@1.0\"},\"DetailsDocument\":{}}]}")
|
||||
.post("/StartChangeSet").then().statusCode(422)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsAndPoliciesPersistOnEntity() {
|
||||
String cs = given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"Catalog\":\"AWSMarketplace\",\"ChangeSet\":[{\"ChangeType\":\"CreateProduct\",\"Entity\":{\"Type\":\"SaaSProduct@1.0\",\"Identifier\":\"@1\"},\"DetailsDocument\":{}}]}")
|
||||
.post("/StartChangeSet").then().statusCode(200).extract().path("ChangeSetId");
|
||||
var response = given().header("Authorization", auth())
|
||||
.get("/DescribeChangeSet?catalog=AWSMarketplace&changeSetId=" + cs)
|
||||
.then().extract().response();
|
||||
String arn = response.path("ChangeSet[0].EntityArn");
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"ResourceArn\":\"" + arn + "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"test\"}]}")
|
||||
.post("/TagResource").then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.post("/ListTagsForResource").then().statusCode(200)
|
||||
.body("Tags[0].Key", equalTo("env"));
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"ResourceArn\":\"" + arn + "\",\"Policy\":\"{\\\"Version\\\":\\\"2012-10-17\\\"}\"}")
|
||||
.post("/PutResourcePolicy").then().statusCode(200);
|
||||
given().header("Authorization", auth())
|
||||
.get("/GetResourcePolicy?resourceArn=" + arn).then().statusCode(200)
|
||||
.body("Policy", containsString("2012-10-17"));
|
||||
}
|
||||
|
||||
private static String auth() {
|
||||
return "AWS4-HMAC-SHA256 Credential=000000000000/20260908/us-east-1/aws-marketplace/aws4_request";
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package io.github.hectorvent.floci.services.marketplace;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class MarketplaceServiceTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private MarketplaceService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new MarketplaceService(
|
||||
mapper,
|
||||
AccountAwareStorageBackend.inMemory("000000000000"),
|
||||
AccountAwareStorageBackend.inMemory("000000000000"),
|
||||
AccountAwareStorageBackend.inMemory("000000000000"),
|
||||
AccountAwareStorageBackend.inMemory("000000000000"),
|
||||
AccountAwareStorageBackend.inMemory("000000000000"),
|
||||
null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void startChangeSetRejectsUnsupportedRegion() throws Exception {
|
||||
JsonNode request = mapper.readTree("""
|
||||
{"Catalog":"AWSMarketplace","ChangeSet":[{"ChangeType":"CreateProduct",
|
||||
"Entity":{"Type":"SaaSProduct@1.0","Identifier":"@1"},"DetailsDocument":{}}]}
|
||||
""");
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.startChangeSet(request, "us-west-2"));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void idempotentRetryRequiresSameParameters() throws Exception {
|
||||
JsonNode first = mapper.readTree("""
|
||||
{"Catalog":"AWSMarketplace","ClientRequestToken":"token-1",
|
||||
"ChangeSet":[{"ChangeType":"CreateProduct","Entity":{"Type":"SaaSProduct@1.0","Identifier":"@1"},
|
||||
"DetailsDocument":{"ProductTitle":"first"}}]}
|
||||
""");
|
||||
JsonNode different = mapper.readTree("""
|
||||
{"Catalog":"AWSMarketplace","ClientRequestToken":"token-1",
|
||||
"ChangeSet":[{"ChangeType":"CreateProduct","Entity":{"Type":"SaaSProduct@1.0","Identifier":"@1"},
|
||||
"DetailsDocument":{"ProductTitle":"different"}}]}
|
||||
""");
|
||||
String firstId = service.startChangeSet(first, "us-east-1").path("ChangeSetId").asText();
|
||||
assertEquals(firstId, service.startChangeSet(first, "us-east-1").path("ChangeSetId").asText());
|
||||
assertThrows(AwsException.class, () -> service.startChangeSet(different, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEntitiesAppliesIdFilterAndAwsDefaultSort() throws Exception {
|
||||
String first = createProduct("Zeta");
|
||||
String second = createProduct("Alpha");
|
||||
JsonNode filtered = mapper.readTree("""
|
||||
{"Catalog":"AWSMarketplace","EntityType":"SaaSProduct",
|
||||
"FilterList":[{"Name":"EntityId","ValueList":["%s"]}]}
|
||||
""".formatted(first));
|
||||
JsonNode response = service.listEntities(filtered, "us-east-1");
|
||||
assertEquals(1, response.path("EntitySummaryList").size());
|
||||
assertEquals(first, response.path("EntitySummaryList").get(0).path("EntityId").asText());
|
||||
assertNotEquals(first, second);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listChangeSetsAppliesStatusFilter() throws Exception {
|
||||
createProduct("One");
|
||||
JsonNode request = mapper.readTree("""
|
||||
{"Catalog":"AWSMarketplace","FilterList":[{"Name":"Status","ValueList":["SUCCEEDED"]}]}
|
||||
""");
|
||||
JsonNode response = service.listChangeSets(request, "us-east-1");
|
||||
assertTrue(response.path("ChangeSetSummaryList").size() >= 1);
|
||||
assertEquals("SUCCEEDED", response.path("ChangeSetSummaryList").get(0).path("Status").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedChangeSetEntryReturnsValidationException() throws Exception {
|
||||
JsonNode request = mapper.readTree("{\"Catalog\":\"AWSMarketplace\",\"ChangeSet\":[\"bad\"]}");
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.startChangeSet(request, "us-east-1"));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
}
|
||||
|
||||
private String createProduct(String title) throws Exception {
|
||||
JsonNode request = mapper.readTree("""
|
||||
{"Catalog":"AWSMarketplace","ChangeSet":[{"ChangeType":"CreateProduct",
|
||||
"Entity":{"Type":"SaaSProduct@1.0","Identifier":"@1"},
|
||||
"DetailsDocument":{"ProductTitle":"%s"}}]}
|
||||
""".formatted(title));
|
||||
String changeSetId = service.startChangeSet(request, "us-east-1").path("ChangeSetId").asText();
|
||||
return service.describeChangeSet("AWSMarketplace", changeSetId, "us-east-1")
|
||||
.path("ChangeSet").get(0).path("Entity").path("Identifier").asText();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user