Convert Subscriptions to sync DynamoDB client

This commit is contained in:
Chris Eager authored and Jon Chambers committed 2026-05-21 13:09:31 -04:00
1 parent 482a1b7bb5
commit 90b280d6a0
6 files changed
+188 -250

No files matched your search

@@ -175,10 +175,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.get(eq(Arrays.copyOfRange(subscriberUserAndKey, 0, 16)), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
when(SUBSCRIPTIONS.subscriptionCreated(any(), any(), any(), anyLong()))
.thenReturn(CompletableFuture.completedFuture(null));
.thenReturn(Subscriptions.GetResult.found(record));
}
@Test
@@ -233,7 +230,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.get(eq(Arrays.copyOfRange(subscriberUserAndKey, 0, 16)), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final String level = String.valueOf(levelId);
final String idempotencyKey = UUID.randomUUID().toString();
@@ -260,7 +257,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.get(eq(Arrays.copyOfRange(subscriberUserAndKey, 0, 16)), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/create_payment_method", subscriberId))
@@ -305,8 +302,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
Arrays.fill(subscriberUserAndKey, (byte) 1);
final String subscriberId = Base64.getEncoder().encodeToString(subscriberUserAndKey);
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(CompletableFuture.completedFuture(
Subscriptions.GetResult.NOT_STORED));
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(Subscriptions.GetResult.NOT_STORED);
final Map<String, AttributeValue> dynamoItem = Map.of(Subscriptions.KEY_PASSWORD, b(new byte[16]),
Subscriptions.KEY_CREATED_AT, n(Instant.now().getEpochSecond()),
@@ -314,7 +310,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
);
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.create(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(record));
when(SUBSCRIPTIONS.create(any(), any(), any())).thenReturn(record);
final Response createResponse = RESOURCE_EXTENSION.target(String.format("/v1/subscription/%s", subscriberId))
.request()
@@ -322,9 +318,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
assertThat(createResponse.getStatus()).isEqualTo(200);
// creating should be idempotent
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(CompletableFuture.completedFuture(
Subscriptions.GetResult.found(record)));
when(SUBSCRIPTIONS.accessedAt(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(Subscriptions.GetResult.found(record));
final Response idempotentCreateResponse = RESOURCE_EXTENSION.target(
String.format("/v1/subscription/%s", subscriberId))
@@ -334,9 +328,8 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
// when the manager returns `null`, it means there was a password mismatch from the storage layer `create`.
// this could happen if there is a race between two concurrent `create` requests for the same user ID
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(CompletableFuture.completedFuture(
Subscriptions.GetResult.NOT_STORED));
when(SUBSCRIPTIONS.create(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(null));
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(Subscriptions.GetResult.NOT_STORED);
when(SUBSCRIPTIONS.create(any(), any(), any())).thenReturn(null);
final Response managerCreateNullResponse = RESOURCE_EXTENSION.target(
String.format("/v1/subscription/%s", subscriberId))
@@ -350,8 +343,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final String mismatchedSubscriberId = Base64.getEncoder().encodeToString(subscriberUserAndMismatchedKey);
// a password mismatch for an existing record
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(CompletableFuture.completedFuture(
Subscriptions.GetResult.PASSWORD_MISMATCH));
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(Subscriptions.GetResult.PASSWORD_MISMATCH);
final Response passwordMismatchResponse = RESOURCE_EXTENSION.target(
String.format("/v1/subscription/%s", mismatchedSubscriberId))
@@ -380,8 +372,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final String subscriberId = Base64.getEncoder().encodeToString(subscriberUserAndKey);
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(CompletableFuture.completedFuture(
Subscriptions.GetResult.NOT_STORED));
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(Subscriptions.GetResult.NOT_STORED);
final Map<String, AttributeValue> dynamoItem = Map.of(Subscriptions.KEY_PASSWORD, b(new byte[16]),
Subscriptions.KEY_CREATED_AT, n(Instant.now().getEpochSecond()),
@@ -389,8 +380,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
);
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.create(any(), any(), any(Instant.class)))
.thenReturn(CompletableFuture.completedFuture(record));
when(SUBSCRIPTIONS.create(any(), any(), any(Instant.class))).thenReturn(record);
final Response createSubscriberResponse = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s", subscriberId))
@@ -400,7 +390,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
assertThat(createSubscriberResponse.getStatus()).isEqualTo(200);
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final String customerId = "some-customer-id";
final ProcessorCustomer customer = new ProcessorCustomer(
@@ -414,9 +404,8 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record recordWithCustomerId = Subscriptions.Record.from(record.user,
dynamoItemWithProcessorCustomer);
when(SUBSCRIPTIONS.setProcessorAndCustomerId(any(Subscriptions.Record.class), any(),
any(Instant.class)))
.thenReturn(CompletableFuture.completedFuture(recordWithCustomerId));
when(SUBSCRIPTIONS.setProcessorAndCustomerId(any(Subscriptions.Record.class), any(), any(Instant.class)))
.thenReturn(recordWithCustomerId);
final String clientSecret = "some-client-secret";
when(STRIPE_MANAGER.createPaymentMethodSetupToken(customerId))
@@ -447,12 +436,12 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.create(any(), any(), any(Instant.class)))
.thenReturn(CompletableFuture.completedFuture(record));
.thenReturn(record);
// set up mocks
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/level/%d/%s/%s", subscriberId, 5, "usd", "abcd"))
@@ -486,17 +475,15 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.create(any(), any(), any(Instant.class)))
.thenReturn(CompletableFuture.completedFuture(record));
.thenReturn(record);
// set up mocks
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
when(BRAINTREE_MANAGER.createSubscription(any(), any(), anyLong(), anyLong()))
.thenReturn(new CustomerAwareSubscriptionPaymentProcessor.SubscriptionId("subscription"));
when(SUBSCRIPTIONS.subscriptionCreated(any(), any(), any(), anyLong()))
.thenReturn(CompletableFuture.completedFuture(null));
final Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/level/%d/%s/%s", subscriberId, levelId, "usd", "abcd"))
@@ -536,12 +523,12 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.create(any(), any(), any(Instant.class)))
.thenReturn(CompletableFuture.completedFuture(record));
.thenReturn(record);
// set up mocks
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final Object subscriptionObj = new Object();
when(BRAINTREE_MANAGER.getSubscription(any())).thenReturn(subscriptionObj);
@@ -552,8 +539,6 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
if (expectUpdate) {
when(BRAINTREE_MANAGER.updateSubscription(any(), any(), anyLong(), anyString()))
.thenReturn(new CustomerAwareSubscriptionPaymentProcessor.SubscriptionId(updatedSubscriptionId));
when(SUBSCRIPTIONS.subscriptionLevelChanged(any(), any(), anyLong(), anyString()))
.thenReturn(CompletableFuture.completedFuture(null));
}
final String idempotencyKey = "abcd";
@@ -609,11 +594,11 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16), dynamoItem);
when(SUBSCRIPTIONS.create(any(), any(), any(Instant.class)))
.thenReturn(CompletableFuture.completedFuture(record));
.thenReturn(record);
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final Object subscriptionObj = new Object();
when(BRAINTREE_MANAGER.getSubscription(any())).thenReturn(subscriptionObj);
@@ -653,14 +638,11 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
final Subscriptions.Record record = Subscriptions.Record.from(user, dynamoItem);
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
when(APPSTORE_MANAGER.validateTransaction(eq(originalTxId)))
.thenReturn(99L);
when(SUBSCRIPTIONS.setIapPurchase(any(), any(), anyString(), anyLong(), any()))
.thenReturn(CompletableFuture.completedFuture(null));
final Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/appstore/%s", subscriberId, originalTxId))
.request()
@@ -695,15 +677,12 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
);
final Subscriptions.Record record = Subscriptions.Record.from(user, dynamoItem);
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final GooglePlayBillingManager.ValidatedToken validatedToken = mock(GooglePlayBillingManager.ValidatedToken.class);
when(validatedToken.getLevel()).thenReturn(99L);
when(PLAY_MANAGER.validateToken(eq(purchaseToken))).thenReturn(validatedToken);
when(SUBSCRIPTIONS.setIapPurchase(any(), any(), anyString(), anyLong(), any()))
.thenReturn(CompletableFuture.completedFuture(null));
final Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/playbilling/%s", subscriberId, purchaseToken))
.request()
@@ -739,16 +718,13 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
Subscriptions.KEY_PROCESSOR_ID_CUSTOMER_ID, b(oldPc.toDynamoBytes()));
final Subscriptions.Record record = Subscriptions.Record.from(user, dynamoItem);
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
final GooglePlayBillingManager.ValidatedToken validatedToken = mock(GooglePlayBillingManager.ValidatedToken.class);
when(validatedToken.getLevel()).thenReturn(99L);
when(PLAY_MANAGER.validateToken(eq(newPurchaseToken))).thenReturn(validatedToken);
when(SUBSCRIPTIONS.setIapPurchase(any(), any(), anyString(), anyLong(), any()))
.thenReturn(CompletableFuture.completedFuture(null));
final Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/playbilling/%s", subscriberId, newPurchaseToken))
.request()
@@ -776,14 +752,14 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(Subscriptions.Record.from(
.thenReturn(Subscriptions.GetResult.found(Subscriptions.Record.from(
Arrays.copyOfRange(subscriberUserAndKey, 0, 16),
Map.of(Subscriptions.KEY_PASSWORD, b(new byte[16]),
Subscriptions.KEY_CREATED_AT, n(Instant.now().getEpochSecond()),
Subscriptions.KEY_ACCESSED_AT, n(Instant.now().getEpochSecond()),
Subscriptions.KEY_PROCESSOR_ID_CUSTOMER_ID,
b(new ProcessorCustomer("customer", PaymentProvider.STRIPE).toDynamoBytes()),
Subscriptions.KEY_SUBSCRIPTION_ID, s("subscriptionId"))))));
Subscriptions.KEY_SUBSCRIPTION_ID, s("subscriptionId")))));
when(STRIPE_MANAGER.getReceiptItem(any()))
.thenThrow(new SubscriptionChargeFailurePaymentRequiredException(
PaymentProvider.STRIPE,
@@ -831,7 +807,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
when(CLOCK.instant()).thenReturn(Instant.now());
when(SUBSCRIPTIONS.get(any(), any()))
.thenReturn(CompletableFuture.completedFuture(Subscriptions.GetResult.found(record)));
.thenReturn(Subscriptions.GetResult.found(record));
when(BRAINTREE_MANAGER.getReceiptItem(subscriptionId)).thenReturn(
new CustomerAwareSubscriptionPaymentProcessor.ReceiptItem(
"itemId",
@@ -6,6 +6,7 @@
package org.whispersystems.textsecuregcm.storage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Fail.fail;
import static org.whispersystems.textsecuregcm.storage.Subscriptions.GetResult.Type.FOUND;
import static org.whispersystems.textsecuregcm.storage.Subscriptions.GetResult.Type.NOT_STORED;
@@ -16,8 +17,6 @@ import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
import org.assertj.core.api.Condition;
@@ -34,7 +33,6 @@ import org.whispersystems.textsecuregcm.util.TestRandomUtil;
class SubscriptionsTest {
private static final long NOW_EPOCH_SECONDS = 1_500_000_000L;
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(3);
@RegisterExtension
static final DynamoDbExtension DYNAMO_DB_EXTENSION = new DynamoDbExtension(Tables.SUBSCRIPTIONS);
@@ -52,7 +50,7 @@ class SubscriptionsTest {
customer = Base64.getEncoder().encodeToString(TestRandomUtil.nextBytes(16));
created = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
subscriptions = new Subscriptions(
Tables.SUBSCRIPTIONS.tableName(), DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient());
Tables.SUBSCRIPTIONS.tableName(), DYNAMO_DB_EXTENSION.getDynamoDbClient());
}
@Test
@@ -62,124 +60,112 @@ class SubscriptionsTest {
Instant created1 = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
Instant created2 = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 1);
CompletableFuture<GetResult> getFuture = subscriptions.get(user, password1);
assertThat(getFuture).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
assertThat(getResult.type).isEqualTo(NOT_STORED);
assertThat(getResult.record).isNull();
});
GetResult getResult = subscriptions.get(user, password1);
assertThat(getResult.type).isEqualTo(NOT_STORED);
assertThat(getResult.record).isNull();
getFuture = subscriptions.get(user, password2);
assertThat(getFuture).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
assertThat(getResult.type).isEqualTo(NOT_STORED);
assertThat(getResult.record).isNull();
});
getResult = subscriptions.get(user, password2);
assertThat(getResult.type).isEqualTo(NOT_STORED);
assertThat(getResult.record).isNull();
CompletableFuture<Subscriptions.Record> createFuture =
Subscriptions.Record create =
subscriptions.create(user, password1, created1);
Consumer<Record> recordRequirements = checkFreshlyCreatedRecord(user, password1, created1);
assertThat(createFuture).succeedsWithin(DEFAULT_TIMEOUT).satisfies(recordRequirements);
assertThat(create).satisfies(recordRequirements);
// password check fails so this should return null
createFuture = subscriptions.create(user, password2, created2);
assertThat(createFuture).succeedsWithin(DEFAULT_TIMEOUT).isNull();
create = subscriptions.create(user, password2, created2);
assertThat(create).isNull();
// password check matches, but the record already exists so nothing should get updated
createFuture = subscriptions.create(user, password1, created2);
assertThat(createFuture).succeedsWithin(DEFAULT_TIMEOUT).satisfies(recordRequirements);
create = subscriptions.create(user, password1, created2);
assertThat(create).satisfies(recordRequirements);
}
@Test
void testGet() {
byte[] wrongUser = TestRandomUtil.nextBytes(16);
byte[] wrongPassword = TestRandomUtil.nextBytes(16);
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password)).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
subscriptions.create(user, password, created);
assertThat(subscriptions.get(user, password)).satisfies(getResult -> {
assertThat(getResult.type).isEqualTo(FOUND);
assertThat(getResult.record).isNotNull().satisfies(checkFreshlyCreatedRecord(user, password, created));
});
assertThat(subscriptions.get(user, wrongPassword)).succeedsWithin(DEFAULT_TIMEOUT)
.satisfies(getResult -> {
assertThat(subscriptions.get(user, wrongPassword)).satisfies(getResult -> {
assertThat(getResult.type).isEqualTo(PASSWORD_MISMATCH);
assertThat(getResult.record).isNull();
});
assertThat(subscriptions.get(wrongUser, password)).succeedsWithin(DEFAULT_TIMEOUT)
.satisfies(getResult -> {
assertThat(subscriptions.get(wrongUser, password)).satisfies(getResult -> {
assertThat(getResult.type).isEqualTo(NOT_STORED);
assertThat(getResult.record).isNull();
});
}
@Test
void testSetCustomerIdAndProcessor() throws Exception {
void testSetCustomerIdAndProcessor() {
Instant subscriptionUpdated = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 1);
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.create(user, password, created)).isNotNull();
final CompletableFuture<GetResult> getUser = subscriptions.get(user, password);
assertThat(getUser).succeedsWithin(DEFAULT_TIMEOUT);
final Record userRecord = getUser.get().record;
final GetResult getUser = subscriptions.get(user, password);
final Record userRecord = getUser.record;
assertThat(subscriptions.setProcessorAndCustomerId(userRecord,
new ProcessorCustomer(customer, PaymentProvider.STRIPE),
subscriptionUpdated)).succeedsWithin(DEFAULT_TIMEOUT)
.hasFieldOrPropertyWithValue("processorCustomer",
subscriptionUpdated)).hasFieldOrPropertyWithValue("processorCustomer",
Optional.of(new ProcessorCustomer(customer, PaymentProvider.STRIPE)));
final Condition<Throwable> clientError409Condition = new Condition<>(e ->
e instanceof ClientErrorException cee && cee.getResponse().getStatus() == 409, "Client error: 409");
// changing the customer ID is not permitted
assertThat(
assertThatThrownBy(() ->
subscriptions.setProcessorAndCustomerId(userRecord,
new ProcessorCustomer(customer + "1", PaymentProvider.STRIPE),
subscriptionUpdated)).failsWithin(DEFAULT_TIMEOUT)
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(ClientErrorException.class)
.extracting(Throwable::getCause)
subscriptionUpdated))
.isInstanceOf(ClientErrorException.class)
.satisfies(clientError409Condition);
// calling setProcessorAndCustomerId() with the same customer ID is also an error
assertThat(
assertThatThrownBy(() ->
subscriptions.setProcessorAndCustomerId(userRecord,
new ProcessorCustomer(customer, PaymentProvider.STRIPE),
subscriptionUpdated)).failsWithin(DEFAULT_TIMEOUT)
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(ClientErrorException.class)
.extracting(Throwable::getCause)
subscriptionUpdated))
.isInstanceOf(ClientErrorException.class)
.satisfies(clientError409Condition);
assertThat(subscriptions.getSubscriberUserByProcessorCustomer(
new ProcessorCustomer(customer, PaymentProvider.STRIPE)))
.succeedsWithin(DEFAULT_TIMEOUT).
isEqualTo(user);
.isEqualTo(user);
}
@Test
void testLookupByCustomerId() throws Exception {
void testLookupByCustomerId() {
Instant subscriptionUpdated = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 1);
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.create(user, password, created);
final CompletableFuture<GetResult> getUser = subscriptions.get(user, password);
assertThat(getUser).succeedsWithin(DEFAULT_TIMEOUT);
final Record userRecord = getUser.get().record;
final GetResult getUser = subscriptions.get(user, password);
final Record userRecord = getUser.record;
assertThat(subscriptions.setProcessorAndCustomerId(userRecord,
subscriptions.setProcessorAndCustomerId(userRecord,
new ProcessorCustomer(customer, PaymentProvider.STRIPE),
subscriptionUpdated)).succeedsWithin(DEFAULT_TIMEOUT);
subscriptionUpdated);
assertThat(subscriptions.getSubscriberUserByProcessorCustomer(
new ProcessorCustomer(customer, PaymentProvider.STRIPE))).
succeedsWithin(DEFAULT_TIMEOUT).
isEqualTo(user);
new ProcessorCustomer(customer, PaymentProvider.STRIPE)))
.isEqualTo(user);
}
@Test
void testSetCanceledAt() {
Instant canceled = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 42);
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.setCanceledAt(user, canceled)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password)).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
subscriptions.create(user, password, created);
subscriptions.setCanceledAt(user, canceled);
assertThat(subscriptions.get(user, password)).satisfies(getResult -> {
assertThat(getResult).isNotNull();
assertThat(getResult.type).isEqualTo(FOUND);
assertThat(getResult.record).isNotNull().satisfies(record -> {
@@ -195,10 +181,9 @@ class SubscriptionsTest {
String subscriptionId = Base64.getEncoder().encodeToString(TestRandomUtil.nextBytes(16));
Instant subscriptionCreated = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 1);
long level = 42;
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.subscriptionCreated(user, subscriptionId, subscriptionCreated, level)).
succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password)).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
subscriptions.create(user, password, created);
subscriptions.subscriptionCreated(user, subscriptionId, subscriptionCreated, level);
assertThat(subscriptions.get(user, password)).satisfies(getResult -> {
assertThat(getResult).isNotNull();
assertThat(getResult.type).isEqualTo(FOUND);
assertThat(getResult.record).isNotNull().satisfies(record -> {
@@ -217,17 +202,15 @@ class SubscriptionsTest {
Instant subscriptionCreated = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 1);
Instant canceledAt = subscriptionCreated.plusSeconds(1);
long level = 42;
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.subscriptionCreated(user, subscriptionId, subscriptionCreated, level))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.create(user, password, created);
subscriptions.subscriptionCreated(user, subscriptionId, subscriptionCreated, level);
assertThat(subscriptions.setCanceledAt(user, canceledAt)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password).join().record.canceledAt).isEqualTo(canceledAt);
subscriptions.setCanceledAt(user, canceledAt);
assertThat(subscriptions.get(user, password).record.canceledAt).isEqualTo(canceledAt);
assertThat(subscriptions.subscriptionCreated(user, subscriptionId, subscriptionCreated, level))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.subscriptionCreated(user, subscriptionId, subscriptionCreated, level);
assertThat(subscriptions.get(user, password)).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
assertThat(subscriptions.get(user, password)).satisfies(getResult -> {
assertThat(getResult).isNotNull();
assertThat(getResult.type).isEqualTo(FOUND);
assertThat(getResult.record).isNotNull().satisfies(record -> {
@@ -246,12 +229,10 @@ class SubscriptionsTest {
Instant at = Instant.ofEpochSecond(NOW_EPOCH_SECONDS + 500);
long level = 1776;
String updatedSubscriptionId = "new";
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.subscriptionCreated(user, "original", created, level - 1)).succeedsWithin(
DEFAULT_TIMEOUT);
assertThat(subscriptions.subscriptionLevelChanged(user, at, level, updatedSubscriptionId)).succeedsWithin(
DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password)).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
subscriptions.create(user, password, created);
subscriptions.subscriptionCreated(user, "original", created, level - 1);
subscriptions.subscriptionLevelChanged(user, at, level, updatedSubscriptionId);
assertThat(subscriptions.get(user, password)).satisfies(getResult -> {
assertThat(getResult).isNotNull();
assertThat(getResult.type).isEqualTo(FOUND);
assertThat(getResult.record).isNotNull().satisfies(record -> {
@@ -269,16 +250,14 @@ class SubscriptionsTest {
Instant canceledAt = at.plusSeconds(100);
long level = 1776;
String updatedSubscriptionId = "new";
assertThat(subscriptions.create(user, password, created)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.subscriptionCreated(user, "original", created, level - 1))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.create(user, password, created);
subscriptions.subscriptionCreated(user, "original", created, level - 1);
assertThat(subscriptions.setCanceledAt(user, canceledAt)).succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password).join().record.canceledAt).isEqualTo(canceledAt);
subscriptions.setCanceledAt(user, canceledAt);
assertThat(subscriptions.get(user, password).record.canceledAt).isEqualTo(canceledAt);
assertThat(subscriptions.subscriptionLevelChanged(user, at, level, updatedSubscriptionId))
.succeedsWithin(DEFAULT_TIMEOUT);
assertThat(subscriptions.get(user, password)).succeedsWithin(DEFAULT_TIMEOUT).satisfies(getResult -> {
subscriptions.subscriptionLevelChanged(user, at, level, updatedSubscriptionId);
assertThat(subscriptions.get(user, password)).satisfies(getResult -> {
assertThat(getResult).isNotNull();
assertThat(getResult.type).isEqualTo(FOUND);
assertThat(getResult.record).isNotNull().satisfies(record -> {
@@ -297,13 +276,12 @@ class SubscriptionsTest {
long level = 100;
ProcessorCustomer pc = new ProcessorCustomer("customerId", PaymentProvider.GOOGLE_PLAY_BILLING);
Record record = subscriptions.create(user, password, created).join();
Record record = assertThat(subscriptions.create(user, password, created)).isNotNull().actual();
// Should be able to set a fresh subscription
assertThat(subscriptions.setIapPurchase(record, pc, "subscriptionId", level, at))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.setIapPurchase(record, pc, "subscriptionId", level, at);
record = subscriptions.get(user, password).join().record;
record = subscriptions.get(user, password).record;
assertThat(record.subscriptionLevel).isEqualTo(level);
assertThat(record.subscriptionLevelChangedAt).isEqualTo(at);
assertThat(record.subscriptionCreatedAt).isEqualTo(at);
@@ -312,31 +290,29 @@ class SubscriptionsTest {
// should be able to update the level
Instant nextAt = at.plus(Duration.ofSeconds(10));
long nextLevel = level + 1;
assertThat(subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt);
record = subscriptions.get(user, password).join().record;
record = subscriptions.get(user, password).record;
assertThat(record.subscriptionLevel).isEqualTo(nextLevel);
assertThat(record.subscriptionLevelChangedAt).isEqualTo(nextAt);
assertThat(record.subscriptionCreatedAt).isEqualTo(at);
assertThat(record.getProcessorCustomer().orElseThrow()).isEqualTo(pc);
nextAt = nextAt.plus(Duration.ofSeconds(10));
nextLevel = level + 1;
nextLevel = nextLevel + 1;
pc = new ProcessorCustomer("newCustomerId", PaymentProvider.STRIPE);
try {
subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt).join();
subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt);
fail("should not be able to change the processor for an existing subscription record");
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException _) {
}
// should be able to change the customerId of an existing record if the processor matches
pc = new ProcessorCustomer("newCustomerId", PaymentProvider.GOOGLE_PLAY_BILLING);
assertThat(subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt);
record = subscriptions.get(user, password).join().record;
record = subscriptions.get(user, password).record;
assertThat(record.subscriptionLevel).isEqualTo(nextLevel);
assertThat(record.subscriptionLevelChangedAt).isEqualTo(nextAt);
assertThat(record.subscriptionCreatedAt).isEqualTo(at);
@@ -350,26 +326,23 @@ class SubscriptionsTest {
long level = 100;
ProcessorCustomer pc = new ProcessorCustomer("customerId", PaymentProvider.GOOGLE_PLAY_BILLING);
Record record = subscriptions.create(user, password, created).join();
Record record = assertThat(subscriptions.create(user, password, created)).isNotNull().actual();
// Should be able to set a fresh subscription
assertThat(subscriptions.setIapPurchase(record, pc, "subscriptionId", level, at))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.setIapPurchase(record, pc, "subscriptionId", level, at);
assertThat(subscriptions.setCanceledAt(record.user, canceledAt))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.setCanceledAt(record.user, canceledAt);
record = subscriptions.get(user, password).join().record;
record = subscriptions.get(user, password).record;
assertThat(record.canceledAt).isEqualTo(canceledAt);
// should be able to update the level
Instant nextAt = at.plus(Duration.ofSeconds(10));
long nextLevel = level + 1;
assertThat(subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt))
.succeedsWithin(DEFAULT_TIMEOUT);
subscriptions.setIapPurchase(record, pc, "subscriptionId", nextLevel, nextAt);
// Resetting the level should clear the "canceled at" timestamp
record = subscriptions.get(user, password).join().record;
record = subscriptions.get(user, password).record;
assertThat(record.canceledAt).isNull();
}