mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(marketplace-agreement): emulate AWS Marketplace Agreement API (#3339)
This commit is contained in:
1 parent
570f975829
commit
c99ecea0da
13 files changed
+1227
-4
No files matched your search
+106
@@ -0,0 +1,106 @@
|
||||
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.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class MarketplaceAgreementControllerIntegrationTest {
|
||||
@BeforeAll
|
||||
static void configure() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsAcceptsDescribesAndSearchesAgreement() {
|
||||
String requestId = rpc("CreateAgreementRequest",
|
||||
"{\"intent\":\"NEW\",\"agreementProposalIdentifier\":\"ap-Example123\","
|
||||
+ "\"requestedTerms\":[{\"id\":\"term-1\"}]}")
|
||||
.statusCode(200)
|
||||
.body("agreementRequestId", notNullValue())
|
||||
.extract().path("agreementRequestId");
|
||||
String agreementId = rpc("AcceptAgreementRequest",
|
||||
"{\"agreementRequestId\":\"" + requestId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("agreementId", notNullValue())
|
||||
.extract().path("agreementId");
|
||||
|
||||
rpc("DescribeAgreement", "{\"agreementId\":\"" + agreementId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("agreementId", equalTo(agreementId))
|
||||
.body("status", equalTo("ACTIVE"));
|
||||
rpc("GetAgreementTerms", "{\"agreementId\":\"" + agreementId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("acceptedTerms[0].id", equalTo("term-1"));
|
||||
rpc("SearchAgreements", "{\"filters\":[{\"name\":\"AgreementType\",\"values\":[\"PurchaseAgreement\"]},{\"name\":\"Status\",\"values\":[\"ACTIVE\"]}]}")
|
||||
.statusCode(200)
|
||||
.body("agreementViewSummaries[0].agreementId", equalTo(agreementId));
|
||||
rpc("SearchAgreements",
|
||||
"{\"filters\":[{\"name\":\"AgreementType\",\"values\":[\"PurchaseAgreement\"]},{\"name\":\"ResourceIdentifier\",\"values\":[\"does-not-exist\"]}]}")
|
||||
.statusCode(200)
|
||||
.body("agreementViewSummaries.size()", equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellationRequestTransitionsAgreement() {
|
||||
String requestId = rpc("CreateAgreementRequest",
|
||||
"{\"intent\":\"NEW\",\"agreementProposalIdentifier\":\"ap-Cancel123\","
|
||||
+ "\"requestedTerms\":[{\"id\":\"term-2\"}]}")
|
||||
.statusCode(200)
|
||||
.extract().path("agreementRequestId");
|
||||
String agreementId = rpc("AcceptAgreementRequest",
|
||||
"{\"agreementRequestId\":\"" + requestId + "\"}")
|
||||
.statusCode(200)
|
||||
.extract().path("agreementId");
|
||||
String cancellationId = rpc("SendAgreementCancellationRequest",
|
||||
"{\"agreementId\":\"" + agreementId + "\",\"reasonCode\":\"OTHER\"}")
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("PENDING_APPROVAL"))
|
||||
.extract().path("agreementCancellationRequestId");
|
||||
|
||||
rpc("AcceptAgreementCancellationRequest",
|
||||
"{\"agreementId\":\"" + agreementId + "\",\"agreementCancellationRequestId\":\""
|
||||
+ cancellationId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("APPROVED"));
|
||||
rpc("DescribeAgreement", "{\"agreementId\":\"" + agreementId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("CANCELLED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedRegionIsRejected() {
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("Authorization", auth("us-west-2"))
|
||||
.header("X-Amz-Target", "AWSMPCommerceService_v20200301.SearchAgreements")
|
||||
.body("{}")
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
private static io.restassured.response.ValidatableResponse rpc(String action, String body) {
|
||||
return given()
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("Authorization", auth())
|
||||
.header("X-Amz-Target", "AWSMPCommerceService_v20200301." + action)
|
||||
.body(body)
|
||||
.post("/")
|
||||
.then();
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
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.assertThrows;
|
||||
|
||||
class MarketplaceAgreementServiceTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private AccountAwareStorageBackend<JsonNode> agreements;
|
||||
private AccountAwareStorageBackend<JsonNode> cancellations;
|
||||
private AccountAwareStorageBackend<JsonNode> payments;
|
||||
private AccountAwareStorageBackend<JsonNode> billing;
|
||||
private AccountAwareStorageBackend<JsonNode> catalog;
|
||||
private MarketplaceAgreementService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
agreements = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
cancellations = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
payments = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
billing = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
catalog = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
service = new MarketplaceAgreementService(
|
||||
mapper,
|
||||
agreements,
|
||||
AccountAwareStorageBackend.inMemory("000000000000"),
|
||||
cancellations,
|
||||
payments,
|
||||
billing,
|
||||
catalog,
|
||||
null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceIdentifierSearchUsesCatalogProposalMetadata() throws Exception {
|
||||
catalog.put("offer-1", mapper.readTree("""
|
||||
{"EntityType":"Offer@1.0","EntityId":"offer-1","DetailsDocument":{
|
||||
"AgreementProposalId":"ap-Example123","ProductId":"prod-1","ProductType":"SaaSProduct"}}
|
||||
"""));
|
||||
String requestId = service.handle("CreateAgreementRequest", mapper.readTree("""
|
||||
{"intent":"NEW","agreementProposalIdentifier":"ap-Example123","requestedTerms":[{"id":"term-1"}]}
|
||||
"""), "us-east-1").path("agreementRequestId").asText();
|
||||
String agreementId = service.handle("AcceptAgreementRequest",
|
||||
mapper.readTree("{\"agreementRequestId\":\"" + requestId + "\"}"), "us-east-1")
|
||||
.path("agreementId").asText();
|
||||
|
||||
JsonNode response = service.handle("SearchAgreements", mapper.readTree("""
|
||||
{"filters":[{"name":"AgreementType","values":["PurchaseAgreement"]},{"name":"ResourceIdentifier","values":["prod-1"]}]}
|
||||
"""), "us-east-1");
|
||||
assertEquals(1, response.path("agreementViewSummaries").size());
|
||||
assertEquals(agreementId, response.path("agreementViewSummaries").get(0).path("agreementId").asText());
|
||||
assertEquals("offer-1", response.path("agreementViewSummaries").get(0)
|
||||
.path("proposalSummary").path("offerId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPaymentRequestsAppliesAgreementAndStatusFilters() throws Exception {
|
||||
payments.put("pr-1", mapper.readTree("""
|
||||
{"paymentRequestId":"pr-1","agreementId":"agr-1","agreementType":"PurchaseAgreement",
|
||||
"catalog":"AWSMarketplace","status":"APPROVED","createdAt":2}
|
||||
"""));
|
||||
payments.put("pr-2", mapper.readTree("""
|
||||
{"paymentRequestId":"pr-2","agreementId":"agr-2","agreementType":"PurchaseAgreement",
|
||||
"catalog":"AWSMarketplace","status":"REJECTED","createdAt":1}
|
||||
"""));
|
||||
JsonNode response = service.handle("ListAgreementPaymentRequests", mapper.readTree("""
|
||||
{"partyType":"Acceptor","agreementId":"agr-1","status":"APPROVED"}
|
||||
"""), "us-east-1");
|
||||
assertEquals(1, response.path("items").size());
|
||||
assertEquals("pr-1", response.path("items").get(0).path("paymentRequestId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAgreementChargesUsesAwsFieldNames() throws Exception {
|
||||
payments.put("pr-1", mapper.readTree("""
|
||||
{"paymentRequestId":"pr-1","chargeId":"ch-1","agreementId":"agr-1",
|
||||
"agreementType":"PurchaseAgreement","catalog":"AWSMarketplace","status":"APPROVED",
|
||||
"chargeAmount":"12.50","currencyCode":"USD","createdAt":1,"updatedAt":2}
|
||||
"""));
|
||||
JsonNode response = service.handle("ListAgreementCharges",
|
||||
mapper.readTree("{\"agreementId\":\"agr-1\"}"), "us-east-1");
|
||||
JsonNode charge = response.path("items").get(0);
|
||||
assertEquals("ch-1", charge.path("id").asText());
|
||||
assertEquals("12.50", charge.path("amount").asText());
|
||||
assertEquals(true, charge.path("chargeId").isMissingNode());
|
||||
assertEquals(true, charge.path("chargeAmount").isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchAgreementsDefaultsToEndTimeDescending() throws Exception {
|
||||
agreements.put("agr-early", mapper.readTree("""
|
||||
{"agreementId":"agr-early","agreementType":"PurchaseAgreement","status":"ACTIVE",
|
||||
"startTime":1,"endTime":10,"lastUpdateTime":2}
|
||||
"""));
|
||||
agreements.put("agr-late", mapper.readTree("""
|
||||
{"agreementId":"agr-late","agreementType":"PurchaseAgreement","status":"ACTIVE",
|
||||
"startTime":2,"endTime":20,"lastUpdateTime":3}
|
||||
"""));
|
||||
JsonNode response = service.handle("SearchAgreements", mapper.readTree("{\"filters\":[{\"name\":\"AgreementType\",\"values\":[\"PurchaseAgreement\"]}]}"), "us-east-1");
|
||||
assertEquals("agr-late", response.path("agreementViewSummaries").get(0).path("agreementId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchAgreementsRequiresAgreementTypeAndEnforcesProposerOnlySorts() throws Exception {
|
||||
AwsException missing = assertThrows(AwsException.class,
|
||||
() -> service.handle("SearchAgreements", mapper.createObjectNode(), "us-east-1"));
|
||||
assertEquals("ValidationException", missing.getErrorCode());
|
||||
|
||||
AwsException invalidSort = assertThrows(AwsException.class, () -> service.handle("SearchAgreements",
|
||||
mapper.readTree("""
|
||||
{"filters":[{"name":"AgreementType","values":["PurchaseAgreement"]},
|
||||
{"name":"PartyType","values":["Acceptor"]}],
|
||||
"sort":{"sortBy":"StartTime","sortOrder":"ASCENDING"}}
|
||||
"""), "us-east-1"));
|
||||
assertEquals("ValidationException", invalidSort.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchAgreementsSupportsDocumentedTimeChainBehaviorAndLicenseFilters() throws Exception {
|
||||
agreements.put("agr-1", mapper.readTree("""
|
||||
{"agreementId":"agr-1","agreementType":"PurchaseAgreement","status":"ACTIVE",
|
||||
"startTime":100,"endTime":300,"lastUpdateTime":200,"initialAgreementId":"agr-root",
|
||||
"endTimeBehaviorType":"EXPIRE","endTimeBehaviorReasonCode":"NO_RENEWAL_TERM",
|
||||
"entitlements":[{"licenseArn":"arn:aws:license-manager::000000000000:license/lic-1"}]}
|
||||
"""));
|
||||
JsonNode response = service.handle("SearchAgreements", mapper.readTree("""
|
||||
{"filters":[
|
||||
{"name":"AgreementType","values":["PurchaseAgreement"]},
|
||||
{"name":"InitialAgreementId","values":["agr-root"]},
|
||||
{"name":"EndTimeBehaviorType","values":["EXPIRE"]},
|
||||
{"name":"EndTimeBehaviorReasonCode","values":["NO_RENEWAL_TERM"]},
|
||||
{"name":"LicenseArn","values":["arn:aws:license-manager::000000000000:license/lic-1"]}]}
|
||||
"""), "us-east-1");
|
||||
assertEquals(1, response.path("agreementViewSummaries").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void regionOutsideUsEastOneIsRejected() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.handle("SearchAgreements", mapper.readTree("{\"filters\":[{\"name\":\"AgreementType\",\"values\":[\"PurchaseAgreement\"]}]}"), "us-west-2"));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user