-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DE-117] DE-545 Tests for "Remove Coupon from Subscription" (#62)
- Loading branch information
1 parent
618f2c9
commit cd345c9
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...io/advancedbilling/controllers/subscriptions/SubscriptionsControllerRemoveCouponTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.maxio.advancedbilling.controllers.subscriptions; | ||
|
||
import com.maxio.advancedbilling.AdvancedBillingClient; | ||
import com.maxio.advancedbilling.TestClient; | ||
import com.maxio.advancedbilling.controllers.SubscriptionsController; | ||
import com.maxio.advancedbilling.exceptions.ApiException; | ||
import com.maxio.advancedbilling.exceptions.SubscriptionRemoveCouponErrorsException; | ||
import com.maxio.advancedbilling.models.Coupon; | ||
import com.maxio.advancedbilling.models.Customer; | ||
import com.maxio.advancedbilling.models.Product; | ||
import com.maxio.advancedbilling.models.ProductFamily; | ||
import com.maxio.advancedbilling.models.Subscription; | ||
import com.maxio.advancedbilling.utils.TestSetup; | ||
import com.maxio.advancedbilling.utils.TestTeardown; | ||
import com.maxio.advancedbilling.utils.assertions.CommonAssertions; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class SubscriptionsControllerRemoveCouponTest { | ||
|
||
private static final TestSetup TEST_SETUP = new TestSetup(); | ||
private static final AdvancedBillingClient CLIENT = TestClient.createClient(); | ||
private static final SubscriptionsController SUBSCRIPTIONS_CONTROLLER = CLIENT.getSubscriptionsController(); | ||
|
||
private static Product product; | ||
private static Customer customer; | ||
private static Coupon percentageCoupon; | ||
|
||
@BeforeAll | ||
static void setUp() throws IOException, ApiException { | ||
ProductFamily productFamily = TEST_SETUP.createProductFamily(); | ||
product = TEST_SETUP.createProduct(productFamily); | ||
customer = TEST_SETUP.createCustomer(); | ||
percentageCoupon = TEST_SETUP.createPercentageCoupon(productFamily, "0.5"); | ||
} | ||
|
||
@AfterAll | ||
static void teardown() throws IOException, ApiException { | ||
new TestTeardown().deleteCustomer(customer); | ||
} | ||
|
||
@Test | ||
void shouldReturn404WhenSubscriptionNotExists() { | ||
// when - then | ||
CommonAssertions.assertNotFound(() -> SUBSCRIPTIONS_CONTROLLER | ||
.deleteCouponFromSubscription(123, "coupon") | ||
); | ||
} | ||
|
||
@Test | ||
void shouldReturn422WhenSubscriptionHasNoCoupons() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = TEST_SETUP.createSubscription(customer, product); | ||
|
||
// when - then | ||
CommonAssertions.assertUnprocessableEntity( | ||
SubscriptionRemoveCouponErrorsException.class, | ||
() -> SUBSCRIPTIONS_CONTROLLER.deleteCouponFromSubscription(subscription.getId(), "coupon"), | ||
e -> assertThat(e.getSubscription()).containsExactly("This subscription does not have any coupons.") | ||
); | ||
} | ||
|
||
@Test | ||
void shouldReturn422WhenRemovingNotExistingCoupon() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = TEST_SETUP.createSubscription(customer, product, b -> b.couponCode(percentageCoupon.getCode())); | ||
|
||
// when - then | ||
CommonAssertions.assertUnprocessableEntity( | ||
SubscriptionRemoveCouponErrorsException.class, | ||
() -> SUBSCRIPTIONS_CONTROLLER.deleteCouponFromSubscription(subscription.getId(), "not-existing-coupon"), | ||
e -> assertThat(e.getSubscription()).containsExactly("Coupon is invalid.") | ||
); | ||
} | ||
|
||
@Test | ||
void shouldRemoveCouponFromSubscription() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = TEST_SETUP.createSubscription(customer, product, b -> b.couponCode(percentageCoupon.getCode())); | ||
|
||
// when | ||
SUBSCRIPTIONS_CONTROLLER.deleteCouponFromSubscription(subscription.getId(), subscription.getCouponCode()); | ||
|
||
// then | ||
Subscription updatedSubscription = SUBSCRIPTIONS_CONTROLLER | ||
.readSubscription(subscription.getId(), null) | ||
.getSubscription(); | ||
assertThat(updatedSubscription.getCouponCode()).isNull(); | ||
} | ||
} |