diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerCouponCurrencyPricesTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerCouponCurrencyPricesTest.java new file mode 100644 index 00000000..c6ef2d7c --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerCouponCurrencyPricesTest.java @@ -0,0 +1,101 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponCurrency; +import com.maxio.advancedbilling.models.CouponCurrencyRequest; +import com.maxio.advancedbilling.models.UpdateCouponCurrency; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertThatErrorStringMapResponse; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerCouponCurrencyPricesTest extends CouponsControllerTestBase { + + private Coupon coupon; + + @BeforeAll + void setupResources() throws IOException, ApiException { + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + } + + @Test + void shouldCreateAndUpdateCouponCurrencyPrices() throws IOException, ApiException { + // when + List currencyPrices = COUPONS_CONTROLLER + .createOrUpdateCouponCurrencyPrices(coupon.getId(), new CouponCurrencyRequest( + List.of( + new UpdateCouponCurrency("USD", 15), + new UpdateCouponCurrency("EUR", 333) + ) + ) + ).getCurrencyPrices(); + + // then + assertThat(currencyPrices).hasSize(2); + assertThat(currencyPrices.get(0).getId()).isNotNull(); + assertThat(currencyPrices.get(0).getCurrency()).isEqualTo("USD"); + assertThat(currencyPrices.get(0).getPrice()).isEqualTo(15.0); + assertThat(currencyPrices.get(0).getCouponId()).isEqualTo(coupon.getId()); + assertThat(currencyPrices.get(1).getId()).isNotNull(); + assertThat(currencyPrices.get(1).getCurrency()).isEqualTo("EUR"); + assertThat(currencyPrices.get(1).getPrice()).isEqualTo(333.0); + assertThat(currencyPrices.get(1).getCouponId()).isEqualTo(coupon.getId()); + + // then update + List currencyPrices2 = COUPONS_CONTROLLER + .createOrUpdateCouponCurrencyPrices(coupon.getId(), new CouponCurrencyRequest( + List.of( + new UpdateCouponCurrency("USD", 1), + new UpdateCouponCurrency("EUR", 2) + ) + ) + ).getCurrencyPrices(); + + // then + assertThat(currencyPrices2.get(0).getId()).isEqualTo(currencyPrices.get(0).getId()); + assertThat(currencyPrices2.get(0).getCurrency()).isEqualTo("USD"); + assertThat(currencyPrices2.get(0).getPrice()).isEqualTo(1.0); + assertThat(currencyPrices2.get(0).getCouponId()).isEqualTo(coupon.getId()); + assertThat(currencyPrices2.get(1).getId()).isEqualTo(currencyPrices.get(1).getId()); + assertThat(currencyPrices2.get(1).getCurrency()).isEqualTo("EUR"); + assertThat(currencyPrices2.get(1).getPrice()).isEqualTo(2.0); + assertThat(currencyPrices2.get(1).getCouponId()).isEqualTo(coupon.getId()); + } + + @Test + void shouldNotCreateCurrencyPricesWithInvalidData() throws IOException, ApiException { + Coupon coupon2 = TEST_SETUP.createPercentageCoupon(productFamily, "2"); + assertThatErrorStringMapResponse(() -> COUPONS_CONTROLLER.createOrUpdateCouponCurrencyPrices(coupon2.getId(), + new CouponCurrencyRequest(List.of(new UpdateCouponCurrency("PLN", -10))))) + .hasErrorMap( + Map.of( + "PLN.currency", "Currency must be defined on the site level.", + "PLN.price", "Amount: must be greater than or equal to 0,00 zł.", + "coupon", "Cannot create prices for a percentage-based coupon." + )) + .isUnprocessableEntity(); + } + + @Test + void shouldNotCreateCurrencyPricesForNonExistentCoupon() { + assertNotFound(() -> COUPONS_CONTROLLER.createOrUpdateCouponCurrencyPrices(123, + new CouponCurrencyRequest())); + } + + @Test + void shouldNotCreateCurrencyPricesWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient() + .getCouponsController().createOrUpdateCouponCurrencyPrices(coupon.getId(), new CouponCurrencyRequest()) + ); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerCreateCouponSubcodesTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerCreateCouponSubcodesTest.java new file mode 100644 index 00000000..d4bf8446 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerCreateCouponSubcodesTest.java @@ -0,0 +1,67 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponSubcodes; +import com.maxio.advancedbilling.models.CouponSubcodesResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerCreateCouponSubcodesTest extends CouponsControllerTestBase { + + private Coupon coupon; + + @BeforeAll + void setupResources() throws IOException, ApiException { + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + } + + @Test + void shouldCreateCouponSubcodes() throws IOException, ApiException { + // when + CouponSubcodesResponse response = COUPONS_CONTROLLER + .createCouponSubcodes(coupon.getId(), new CouponSubcodes( + List.of("ABC", "CcC123", "pppppP", "DD", "25%OFF", "ALLOWED%@+-_.", + "abc", "DD", + "NOTALLOWED$#?!", " ", "abc c", "") + )); + + // then + assertThat(response.getCreatedCodes()) + .containsExactlyInAnyOrder("ABC", "CCC123", "PPPPPP", "DD", "25%OFF", "ALLOWED%@+-_."); + assertThat(response.getDuplicateCodes()).isEmpty(); + assertThat(response.getInvalidCodes()) + .containsExactlyInAnyOrder("NOTALLOWED$#?!", "ABC C"); + + // when saving some duplicates + response = COUPONS_CONTROLLER + .createCouponSubcodes(coupon.getId(), new CouponSubcodes( + List.of("NEWONE", "abc", "DD"))); + + assertThat(response.getCreatedCodes()).containsExactlyInAnyOrder("NEWONE"); + assertThat(response.getDuplicateCodes()).containsExactlyInAnyOrder("ABC", "DD"); + assertThat(response.getInvalidCodes()).isEmpty(); + } + + @Test + void shouldNotCreateSubcodesForNonExistentCoupon() { + assertNotFound(() -> COUPONS_CONTROLLER.createCouponSubcodes(123, + new CouponSubcodes(List.of("abc")))); + } + + @Test + void shouldNotCreateCouponSubcodesWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient() + .getCouponsController().createCouponSubcodes(coupon.getId(), new CouponSubcodes(List.of("abc"))) + ); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerDeleteCouponSubcodesTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerDeleteCouponSubcodesTest.java new file mode 100644 index 00000000..4afecbdb --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerDeleteCouponSubcodesTest.java @@ -0,0 +1,72 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponSubcodes; +import com.maxio.advancedbilling.models.CouponSubcodesResponse; +import com.maxio.advancedbilling.models.ListCouponSubcodesInput; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerDeleteCouponSubcodesTest extends CouponsControllerTestBase { + + private Coupon coupon; + + @BeforeAll + void setupResources() throws IOException, ApiException { + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + } + + @Test + void shouldDeleteCouponSubcodes() throws IOException, ApiException { + // create subcodes then delete one + CouponSubcodesResponse response = COUPONS_CONTROLLER + .createCouponSubcodes(coupon.getId(), new CouponSubcodes( + // Unfortunately deleting coupon with . (dot) won't work with sdk + List.of("ABC", "ALLOWED%@+-_") + )); + COUPONS_CONTROLLER.deleteCouponSubcode(coupon.getId(), "abc"); + List codes = COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput.Builder() + .couponId(coupon.getId()) + .build() + ).getCodes(); + assertThat(codes).hasSize(1); + assertThat(codes.get(0)).isEqualTo("ALLOWED%@+-_"); + + // delete second one + COUPONS_CONTROLLER.deleteCouponSubcode(coupon.getId(), "ALLOWED%@+-_"); + codes = COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput.Builder() + .couponId(coupon.getId()) + .build() + ).getCodes(); + assertThat(codes).isEmpty(); + } + + @Test + void shouldNotDeleteNonExistentSubcode() { + assertNotFound(() -> COUPONS_CONTROLLER.deleteCouponSubcode(coupon.getId(), "xxx")); + } + + @Test + void shouldNotDeleteSubcodesForNonExistentCoupon() { + assertNotFound(() -> COUPONS_CONTROLLER.deleteCouponSubcode(123, "abc")); + } + + @Test + void shouldNotDeleteSubcodeWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient() + .getCouponsController().deleteCouponSubcode(coupon.getId(), "abc") + ); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerListCouponSubcodesTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerListCouponSubcodesTest.java new file mode 100644 index 00000000..86b41461 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerListCouponSubcodesTest.java @@ -0,0 +1,96 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponSubcodes; +import com.maxio.advancedbilling.models.ListCouponSubcodesInput; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerListCouponSubcodesTest extends CouponsControllerTestBase { + + private Coupon coupon; + private List subcodes; + + @BeforeAll + void setupResources() throws IOException, ApiException { + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + + List list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.add(RandomStringUtils.randomAlphanumeric(5) + i); + } + + subcodes = COUPONS_CONTROLLER + .createCouponSubcodes(coupon.getId(), new CouponSubcodes(list)).getCreatedCodes(); + } + + @Test + void shouldListCouponSubcodes() throws IOException, ApiException { + List codes = COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput.Builder() + .couponId(coupon.getId()) + .build() + ).getCodes(); + assertThat(codes).containsExactlyInAnyOrderElementsOf(subcodes); + } + + @Test + void shouldListCouponSubcodesWithPaging() throws IOException, ApiException { + // given + List page1 = COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput.Builder() + .couponId(coupon.getId()) + .page(1) + .perPage(4) + .build() + ).getCodes(); + List page2 = COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput.Builder() + .couponId(coupon.getId()) + .page(2) + .perPage(4) + .build() + ).getCodes(); + List page3 = COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput.Builder() + .couponId(coupon.getId()) + .page(3) + .perPage(4) + .build() + ).getCodes(); + + // then + assertThat(page1).hasSize(4); + assertThat(page2).hasSize(4); + assertThat(page3).hasSize(2); + + assertThat( + Stream.concat(Stream.concat(page1.stream(), page2.stream()), page3.stream()).toList() + ).containsExactlyInAnyOrderElementsOf(subcodes); + } + + @Test + void shouldNotListSubcodesForNonExistentCoupon() { + assertNotFound(() -> COUPONS_CONTROLLER + .listCouponSubcodes(new ListCouponSubcodesInput(123, 1, 1))); + } + + @Test + void shouldNotListCouponSubcodesWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient() + .getCouponsController().listCouponSubcodes(new ListCouponSubcodesInput(coupon.getId(), 1, 1))); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerReadCouponUsageTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerReadCouponUsageTest.java new file mode 100644 index 00000000..5300da20 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerReadCouponUsageTest.java @@ -0,0 +1,87 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponPayload; +import com.maxio.advancedbilling.models.CouponRequest; +import com.maxio.advancedbilling.models.CouponUsage; +import com.maxio.advancedbilling.models.Customer; +import com.maxio.advancedbilling.models.Product; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerReadCouponUsageTest extends CouponsControllerTestBase { + + private Product product; + private Coupon coupon; + + @BeforeAll + void setupResources() throws IOException, ApiException { + Customer customer = TEST_SETUP.createCustomer(); + product = TEST_SETUP.createProduct(productFamily, p -> p.priceInCents(20000)); + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, new CouponRequest( + new CouponPayload.Builder() + .name("coupon" + randomNumeric(10)) + .description("description" + randomNumeric(20)) + .amountInCents(3000L) + .code("coupon%@+-_." + randomNumeric(10)) + .build(), null, null)).getCoupon(); + TEST_SETUP.createSubscription(customer, product, + sub -> sub.couponCode(coupon.getCode())); + } + + @Test + void shouldReadCouponUsages() throws ApiException, IOException { + List couponUsages = COUPONS_CONTROLLER.readCouponUsage(productFamilyId, coupon.getId()); + + assertThat(couponUsages).hasSize(1); + assertThat(couponUsages.get(0).getId()).isEqualTo(product.getId()); + assertThat(couponUsages.get(0).getName()).isEqualTo(product.getName()); + assertThat(couponUsages.get(0).getSignups()).isEqualTo(1); + assertThat(couponUsages.get(0).getSavings()).isEqualTo(30); + assertThat(couponUsages.get(0).getSavingsInCents()).isEqualTo(3000); + assertThat(couponUsages.get(0).getRevenue()).isEqualTo(170); + assertThat(couponUsages.get(0).getRevenueInCents()).isEqualTo(17000); + } + + @Test + void shouldReadEmptyCouponUsages() throws ApiException, IOException { + Coupon coupon2 = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + + List couponUsages = COUPONS_CONTROLLER.readCouponUsage(productFamilyId, coupon2.getId()); + assertThat(couponUsages.get(0).getId()).isEqualTo(product.getId()); + assertThat(couponUsages.get(0).getName()).isEqualTo(product.getName()); + assertThat(couponUsages.get(0).getSignups()).isEqualTo(0); + assertThat(couponUsages.get(0).getSavings()).isEqualTo(0); + assertThat(couponUsages.get(0).getSavingsInCents()).isEqualTo(0); + assertThat(couponUsages.get(0).getRevenue()).isEqualTo(0); + assertThat(couponUsages.get(0).getRevenueInCents()).isEqualTo(0); + } + + @Test + void shouldNotNotReadUsagesOfNonExistentCoupon() { + assertNotFound(() -> COUPONS_CONTROLLER.readCouponUsage(productFamilyId, 123)); + } + + @Test + void shouldNotNotReadUsagesOfCouponInNonExistentProductFamily() { + assertNotFound(() -> COUPONS_CONTROLLER.readCouponUsage(123, coupon.getId())); + } + + @Test + void shouldNotReadCouponUsagesWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient() + .getCouponsController().readCouponUsage(coupon.getId(), coupon.getId()) + ); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerUpdateCouponSubcodesTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerUpdateCouponSubcodesTest.java new file mode 100644 index 00000000..9d7fad37 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerUpdateCouponSubcodesTest.java @@ -0,0 +1,59 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponSubcodes; +import com.maxio.advancedbilling.models.CouponSubcodesResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerUpdateCouponSubcodesTest extends CouponsControllerTestBase { + + private Coupon coupon; + + @BeforeAll + void setupResources() throws IOException, ApiException { + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + } + + @Test + void shouldUpdateSubcodes() throws IOException, ApiException { + // when + COUPONS_CONTROLLER + .createCouponSubcodes(coupon.getId(), new CouponSubcodes( + List.of("ABC", "CcC123", "pppppP", "DD"))); + + + // then + CouponSubcodesResponse response = COUPONS_CONTROLLER + .updateCouponSubcodes(coupon.getId(), new CouponSubcodes( + List.of("NEWONE", "newONE2", "abc", "DD", + "%$#$#", " ", "abc c", ""))); + + assertThat(response.getCreatedCodes()).containsExactlyInAnyOrder("NEWONE", "NEWONE2", "ABC", "DD"); + assertThat(response.getDuplicateCodes()).isEmpty(); + assertThat(response.getInvalidCodes()).containsExactlyInAnyOrder("%$#$#", "ABC C"); + } + + @Test + void shouldNotUpdateSubcodesForNonExistentCoupon() { + assertNotFound(() -> COUPONS_CONTROLLER.updateCouponSubcodes(123, + new CouponSubcodes(List.of("abc")))); + } + + @Test + void shouldNotUpdateCouponSubcodesWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient() + .getCouponsController().updateCouponSubcodes(coupon.getId(), new CouponSubcodes(List.of("abc"))) + ); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerValidateCouponTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerValidateCouponTest.java new file mode 100644 index 00000000..3319d80d --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/coupons/CouponsControllerValidateCouponTest.java @@ -0,0 +1,58 @@ +package com.maxio.advancedbilling.controllers.coupons; + +import com.maxio.advancedbilling.TestClientProvider; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Coupon; +import com.maxio.advancedbilling.models.CouponResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertThatSingleStringErrorResponse; +import static org.assertj.core.api.Assertions.assertThat; + +public class CouponsControllerValidateCouponTest extends CouponsControllerTestBase { + + private Coupon coupon; + + @BeforeAll + void setupResources() throws IOException, ApiException { + coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + } + + @Test + void shouldValidateValidCoupon() throws ApiException, IOException { + CouponResponse couponResponse = COUPONS_CONTROLLER.validateCoupon(coupon.getCode(), productFamilyId); + + assertThat(couponResponse.getCoupon()).usingRecursiveComparison().isEqualTo(coupon); + } + + @Test + void shouldReturn404WhenCouponDoesNotExist() { + assertThatSingleStringErrorResponse(() -> COUPONS_CONTROLLER.validateCoupon("123", productFamilyId)) + .hasErrorCode(404) + .hasErrorMessage("Coupon code could not be found.") + .hasMessage("Not Found"); + } + + @Test + void shouldReturn404WhenCouponIsArchived() throws IOException, ApiException { + Coupon coupon2 = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon(); + COUPONS_CONTROLLER.archiveCoupon(productFamilyId, coupon2.getId()); + + assertThatSingleStringErrorResponse(() -> COUPONS_CONTROLLER.validateCoupon(coupon2.getCode(), productFamilyId)) + .hasErrorCode(404) + .hasErrorMessage("That coupon is no longer active.") + .hasMessage("Not Found"); + } + + @Test + void shouldValidateCouponWhenProvidingInvalidCredentials() throws IOException, ApiException { + CouponResponse couponResponse = TestClientProvider.createInvalidCredentialsClient().getCouponsController() + .validateCoupon(coupon.getCode(), productFamilyId); + + assertThat(couponResponse.getCoupon()).usingRecursiveComparison().isEqualTo(coupon); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java index b1f4b536..0e0a9026 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java +++ b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java @@ -34,6 +34,13 @@ public static SingleErrorResponseAssert assertThatSingleErrorResponse(ThrowingRu }); } + public static SingleStringErrorResponseAssert assertThatSingleStringErrorResponse(ThrowingRunnable throwingRunnable) { + return new SingleStringErrorResponseAssert((Callable) () -> { + throwingRunnable.run(); + return null; + }); + } + public static ErrorArrayMapResponseAssert assertThatErrorArrayMapResponse(Callable throwingCallable) { return new ErrorArrayMapResponseAssert(throwingCallable); } diff --git a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/SingleStringErrorResponseAssert.java b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/SingleStringErrorResponseAssert.java new file mode 100644 index 00000000..2006f484 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/SingleStringErrorResponseAssert.java @@ -0,0 +1,19 @@ +package com.maxio.advancedbilling.utils.assertions; + +import com.maxio.advancedbilling.exceptions.SingleStringErrorResponseException; + +import java.util.concurrent.Callable; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SingleStringErrorResponseAssert extends ApiExceptionAssert { + + SingleStringErrorResponseAssert(Callable callable) { + super(callable); + } + + public SingleStringErrorResponseAssert hasErrorMessage(String errorMessage) { + assertThat(actual.getErrors()).as("Expected error message").isEqualTo(errorMessage); + return this; + } +}