Skip to content

Commit

Permalink
allow gift card subscriptions to be cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed Aug 1, 2022
1 parent fc543cd commit 1e1342b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -408,6 +409,9 @@ void cancelSubscription(
verify(stripeApi, times(1))
.cancelSubscription(subscription.getProvidedId());
break;
case GIFT_CARD:
assertTrue(subscription.getEndAt().isBefore(OffsetDateTime.now()));
break;
default:
throw new RuntimeException("unknown provider");
}
Expand All @@ -419,8 +423,10 @@ static Stream<Arguments> cancelSubscriptionTestCases() {
// subscription provider, is subscription active, expected response code
arguments(SubscriptionPlan.Provider.GOOGLE_PLAY, true, HttpStatus.NO_CONTENT.value()),
arguments(SubscriptionPlan.Provider.STRIPE, true, HttpStatus.NO_CONTENT.value()),
arguments(SubscriptionPlan.Provider.GIFT_CARD, true, HttpStatus.NO_CONTENT.value()),
arguments(SubscriptionPlan.Provider.GOOGLE_PLAY, false, HttpStatus.NOT_FOUND.value()),
arguments(SubscriptionPlan.Provider.STRIPE, false, HttpStatus.NOT_FOUND.value())
arguments(SubscriptionPlan.Provider.STRIPE, false, HttpStatus.NOT_FOUND.value()),
arguments(SubscriptionPlan.Provider.GIFT_CARD, false, HttpStatus.NOT_FOUND.value())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ public void cancelSubscription(@NonNull Long customerId, @NonNull Long subscript
throw new RuntimeException("stripe api error", e);
}

break;
case GIFT_CARD:
subscription.setEndAt(OffsetDateTime.now());
break;
default:
throw new IllegalStateException("unsupported provider used in subscription plan");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -323,6 +324,9 @@ <T extends Throwable> void cancelSubscription(
verify(stripeApi, times(1))
.cancelSubscription(subscription.getProvidedId());
break;
case GIFT_CARD:
assertTrue(subscription.getEndAt().isBefore(OffsetDateTime.now()));
break;
default:
throw new RuntimeException("unknown provider");
}
Expand All @@ -332,17 +336,21 @@ <T extends Throwable> void cancelSubscription(
static Stream<Arguments> cancelSubscriptionTestCases() {
val userId1 = 1L;
val userId2 = 2L;
val googlePlayPlan = buildSubscriptionPlan(SubscriptionPlan.Provider.GOOGLE_PLAY, "test-provider-id");
val stripePlan = buildSubscriptionPlan(SubscriptionPlan.Provider.STRIPE, "test-provider-id");
val googlePlayPlan = buildSubscriptionPlan(SubscriptionPlan.Provider.GOOGLE_PLAY, "test-provided-id");
val stripePlan = buildSubscriptionPlan(SubscriptionPlan.Provider.STRIPE, "test-provided-id");
val giftCardPlan = buildSubscriptionPlan(SubscriptionPlan.Provider.GIFT_CARD, "test-provided-id");

return Stream.of(
// subscription, principalId, expected exception
arguments(buildSubscription(userId1, googlePlayPlan, true, false), userId1, null),
arguments(buildSubscription(userId1, stripePlan, true, false), userId1, null),
arguments(buildSubscription(userId1, giftCardPlan, true, false), userId1, null),
arguments(buildSubscription(userId2, googlePlayPlan, true, false), userId1, SubscriptionNotFoundException.class),
arguments(buildSubscription(userId1, stripePlan, true, false), userId2, SubscriptionNotFoundException.class),
arguments(buildSubscription(userId1, giftCardPlan, true, false), userId2, SubscriptionNotFoundException.class),
arguments(buildSubscription(userId1, googlePlayPlan, false, false), userId1, SubscriptionNotFoundException.class),
arguments(buildSubscription(userId1, stripePlan, false, false), userId1, SubscriptionNotFoundException.class)
arguments(buildSubscription(userId1, stripePlan, false, false), userId1, SubscriptionNotFoundException.class),
arguments(buildSubscription(userId1, giftCardPlan, false, false), userId1, SubscriptionNotFoundException.class)
);
}

Expand Down

0 comments on commit 1e1342b

Please sign in to comment.