mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(marketplace-discovery): emulate AWS Marketplace Discovery API (#3342)
This commit is contained in:
1 parent
137a39add3
commit
07d6cabd8a
10 files changed
+820
-1
No files matched your search
+95
@@ -0,0 +1,95 @@
|
||||
package io.github.hectorvent.floci.services.marketplace;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class MarketplaceDiscoveryIntegrationTest {
|
||||
@Inject
|
||||
StorageFactory storageFactory;
|
||||
|
||||
@Inject
|
||||
ObjectMapper mapper;
|
||||
|
||||
private AccountAwareStorageBackend<JsonNode> entities;
|
||||
|
||||
@BeforeAll
|
||||
static void configure() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
entities = storageFactory.create("marketplace", "marketplace-entities.json",
|
||||
new TypeReference<Map<String, JsonNode>>() {});
|
||||
entities.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedCatalogProductIsDiscoverableThroughBuyerApis() {
|
||||
String productId = createProduct("Discovery product");
|
||||
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"productId\":\"" + productId + "\"}")
|
||||
.post("/2026-02-05/getProduct").then().statusCode(200)
|
||||
.body("productId", equalTo(productId)).body("productName", equalTo("Discovery product"));
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"listingId\":\"" + productId + "\"}")
|
||||
.post("/2026-02-05/getListing").then().statusCode(200)
|
||||
.body("listingId", equalTo(productId)).body("publisher.displayName", notNullValue());
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"searchText\":\"Discovery product\"}")
|
||||
.post("/2026-02-05/searchListings").then().statusCode(200)
|
||||
.body("totalResults", greaterThanOrEqualTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedDiscoveryRegionIsRejected() {
|
||||
given().contentType("application/json").header("Authorization", auth("ap-southeast-2"))
|
||||
.body("{\"searchText\":\"anything\"}")
|
||||
.post("/2026-02-05/searchListings").then().statusCode(400);
|
||||
}
|
||||
|
||||
private String createProduct(String title) {
|
||||
String productId = "prod-discovery-local";
|
||||
ObjectNode entity = mapper.createObjectNode();
|
||||
entity.put("EntityType", "SaaSProduct@1.0");
|
||||
entity.put("EntityIdentifier", productId);
|
||||
entity.put("EntityId", productId);
|
||||
entity.put("EntityArn", "arn:aws:aws-marketplace:us-east-1:000000000000:AWSMarketplace/SaaSProduct/" + productId);
|
||||
entity.put("LastModifiedDate", Instant.now().toString());
|
||||
ObjectNode details = entity.putObject("DetailsDocument");
|
||||
details.put("ProductTitle", title);
|
||||
details.put("ShortDescription", "Local product");
|
||||
details.put("FulfillmentType", "SAAS");
|
||||
entities.put(productId, entity);
|
||||
return productId;
|
||||
}
|
||||
|
||||
private static String auth() {
|
||||
return auth("us-east-1");
|
||||
}
|
||||
|
||||
private static String auth(String region) {
|
||||
return "AWS4-HMAC-SHA256 Credential=000000000000/20260908/" + region
|
||||
+ "/aws-marketplace/aws4_request";
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
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.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.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class MarketplaceDiscoveryServiceTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private AccountAwareStorageBackend<JsonNode> entities;
|
||||
private MarketplaceDiscoveryService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
entities = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
service = new MarketplaceDiscoveryService(mapper, entities);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOfferTermsReturnsStoredCatalogTerms() throws Exception {
|
||||
entities.put("offer-1", mapper.readTree("""
|
||||
{"EntityType":"Offer@1.0","EntityId":"offer-1","DetailsDocument":{
|
||||
"OfferTerms":[{"type":"LegalTerm","id":"term-1"}]}}
|
||||
"""));
|
||||
JsonNode response = service.getOfferTerms(
|
||||
mapper.readTree("{\"offerId\":\"offer-1\"}"), "us-east-1");
|
||||
assertEquals("term-1", response.path("offerTerms").get(0).path("id").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ratingSortAndFacetsUseListingValues() throws Exception {
|
||||
entities.put("prod-low", product("prod-low", "Low", "1.0", "NOT_DEPLOYED", 2));
|
||||
entities.put("prod-high", product("prod-high", "High", "4.8", "DEPLOYED", 5));
|
||||
JsonNode sorted = service.searchListings(mapper.readTree(
|
||||
"{\"sortBy\":\"AVERAGE_CUSTOMER_RATING\",\"sortOrder\":\"DESCENDING\"}"), "us-east-1");
|
||||
assertEquals("prod-high", sorted.path("listingSummaries").get(0).path("listingId").asText());
|
||||
|
||||
JsonNode facets = service.searchFacets(mapper.readTree(
|
||||
"{\"facetTypes\":[\"DEPLOYED_ON_AWS\",\"NUMBER_OF_PRODUCTS\"]}"), "us-east-1");
|
||||
assertEquals(2, facets.path("listingFacets").path("DEPLOYED_ON_AWS").size());
|
||||
assertEquals(2, facets.path("listingFacets").path("NUMBER_OF_PRODUCTS").size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void rejectsSearchFilterValueCountAboveAwsLimit() throws Exception {
|
||||
StringBuilder values = new StringBuilder();
|
||||
for (int i = 0; i < 31; i++) {
|
||||
if (i > 0) {
|
||||
values.append(',');
|
||||
}
|
||||
values.append('\"').append("value").append(i).append('\"');
|
||||
}
|
||||
JsonNode request = mapper.readTree("{\"filters\":[{\"filterType\":\"CATEGORY\",\"filterValues\":["
|
||||
+ values + "]}]}");
|
||||
|
||||
assertThrows(RuntimeException.class, () -> service.searchListings(request, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsPurchaseOptionFilterCountAboveAwsLimit() throws Exception {
|
||||
StringBuilder filters = new StringBuilder();
|
||||
for (int i = 0; i < 11; i++) {
|
||||
if (i > 0) {
|
||||
filters.append(',');
|
||||
}
|
||||
filters.append("{\"filterType\":\"PRODUCT_ID\",\"filterValues\":[\"prod-")
|
||||
.append(i).append("\"]}");
|
||||
}
|
||||
JsonNode request = mapper.readTree("{\"filters\":[" + filters + "]}");
|
||||
|
||||
assertThrows(RuntimeException.class, () -> service.listPurchaseOptions(request, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchTextIgnoresFacetOnlyMetadata() throws Exception {
|
||||
entities.put("prod-1", product("prod-1", "Visible Product", "4.0", "DEPLOYED", 3));
|
||||
|
||||
JsonNode deployed = service.searchListings(
|
||||
mapper.readTree("{\"searchText\":\"DEPLOYED\"}"), "us-east-1");
|
||||
JsonNode count = service.searchListings(
|
||||
mapper.readTree("{\"searchText\":\"3\"}"), "us-east-1");
|
||||
JsonNode visible = service.searchListings(
|
||||
mapper.readTree("{\"searchText\":\"Visible Product\"}"), "us-east-1");
|
||||
|
||||
assertEquals(0, deployed.path("listingSummaries").size());
|
||||
assertEquals(0, count.path("listingSummaries").size());
|
||||
assertEquals(1, visible.path("listingSummaries").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchListingsDoesNotLeakFacetOnlyFields() throws Exception {
|
||||
entities.put("prod-1", product("prod-1", "Product", "4.0", "DEPLOYED", 3));
|
||||
|
||||
JsonNode listing = service.searchListings(mapper.createObjectNode(), "us-east-1")
|
||||
.path("listingSummaries").get(0);
|
||||
|
||||
assertFalse(listing.has("deployedOnAws"));
|
||||
assertFalse(listing.has("numberOfProducts"));
|
||||
assertFalse(listing.has("_deployedOnAws"));
|
||||
assertFalse(listing.has("_numberOfProducts"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void listingAssociationsWrapProductsInAwsShape() throws Exception {
|
||||
entities.put("prod-1", product("prod-1", "Product", "4.0", "DEPLOYED", 1));
|
||||
|
||||
JsonNode listing = service.getListing(
|
||||
mapper.readTree("{\"listingId\":\"prod-1\"}"), "us-east-1");
|
||||
JsonNode summary = service.searchListings(mapper.createObjectNode(), "us-east-1")
|
||||
.path("listingSummaries").get(0);
|
||||
|
||||
assertEquals("prod-1", listing.path("associatedEntities").get(0)
|
||||
.path("product").path("productId").asText());
|
||||
assertEquals("prod-1", summary.path("associatedEntities").get(0)
|
||||
.path("product").path("productId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void offerSetPurchaseOptionsIncludeAssociatedProductsAndOffers() throws Exception {
|
||||
entities.put("prod-1", product("prod-1", "Product", "4.0", "DEPLOYED", 1));
|
||||
entities.put("offer-1", mapper.readTree("""
|
||||
{"EntityType":"Offer@1.0","EntityId":"offer-1","DetailsDocument":{
|
||||
"OfferName":"Offer","ProductId":"prod-1"}}
|
||||
"""));
|
||||
entities.put("set-1", mapper.readTree("""
|
||||
{"EntityType":"OfferSet@1.0","EntityId":"set-1","DetailsDocument":{
|
||||
"OfferSetName":"Set","VisibilityScope":"PUBLIC",
|
||||
"Offers":[{"ProductId":"prod-1","OfferId":"offer-1"}]}}
|
||||
"""));
|
||||
|
||||
JsonNode response = service.listPurchaseOptions(mapper.readTree("""
|
||||
{"filters":[
|
||||
{"filterType":"VISIBILITY_SCOPE","filterValues":["PUBLIC"]},
|
||||
{"filterType":"PURCHASE_OPTION_TYPE","filterValues":["OFFERSET"]}
|
||||
]}
|
||||
"""), "us-east-1");
|
||||
JsonNode purchaseOption = response.path("purchaseOptions").get(0);
|
||||
|
||||
assertEquals(1, response.path("purchaseOptions").size());
|
||||
assertEquals("set-1", purchaseOption.path("purchaseOptionId").asText());
|
||||
assertEquals("prod-1", purchaseOption.path("associatedEntities").get(0)
|
||||
.path("product").path("productId").asText());
|
||||
assertEquals("offer-1", purchaseOption.path("associatedEntities").get(0)
|
||||
.path("offer").path("offerId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearResetsSharedMarketplaceEntityState() throws Exception {
|
||||
entities.put("prod-1", product("prod-1", "Product", "4.0", "DEPLOYED", 1));
|
||||
|
||||
service.clear();
|
||||
|
||||
assertEquals(0, entities.scan(key -> true).size());
|
||||
}
|
||||
|
||||
private JsonNode product(String id, String title, String rating, String deployed, int count) throws Exception {
|
||||
return mapper.readTree("{\"EntityType\":\"SaaSProduct@1.0\",\"EntityId\":\"" + id
|
||||
+ "\",\"DetailsDocument\":{\"ProductTitle\":\"" + title
|
||||
+ "\",\"AverageCustomerRating\":\"" + rating + "\",\"DeployedOnAws\":\""
|
||||
+ deployed + "\",\"NumberOfProducts\":" + count + "}}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user