diff --git a/src/integrationTest/java/com/trynoice/api/subscription/SubscriptionControllerTest.java b/src/integrationTest/java/com/trynoice/api/subscription/SubscriptionControllerTest.java index 07ca477..22a441a 100644 --- a/src/integrationTest/java/com/trynoice/api/subscription/SubscriptionControllerTest.java +++ b/src/integrationTest/java/com/trynoice/api/subscription/SubscriptionControllerTest.java @@ -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; @@ -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"); } @@ -419,8 +423,10 @@ static Stream 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()) ); } diff --git a/src/main/java/com/trynoice/api/subscription/SubscriptionService.java b/src/main/java/com/trynoice/api/subscription/SubscriptionService.java index aca9a4d..f19092c 100644 --- a/src/main/java/com/trynoice/api/subscription/SubscriptionService.java +++ b/src/main/java/com/trynoice/api/subscription/SubscriptionService.java @@ -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"); diff --git a/src/test/java/com/trynoice/api/subscription/SubscriptionServiceTest.java b/src/test/java/com/trynoice/api/subscription/SubscriptionServiceTest.java index 9c182de..896c88c 100644 --- a/src/test/java/com/trynoice/api/subscription/SubscriptionServiceTest.java +++ b/src/test/java/com/trynoice/api/subscription/SubscriptionServiceTest.java @@ -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; @@ -323,6 +324,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"); } @@ -332,17 +336,21 @@ void cancelSubscription( static Stream 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) ); }