Skip to content

Commit

Permalink
[DE-1044] Coupons tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-nedza committed Nov 15, 2024
1 parent 5bd53a1 commit 2b731a6
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class CouponsControllerArchiveTest extends CouponsControllerTestBase {
void shouldArchiveCoupon() throws IOException, ApiException {
// given
Coupon coupon = COUPONS_CONTROLLER
.createCoupon(productFamilyId, validCreateOrUpdateCouponRequest()).getCoupon();
.createCoupon(productFamilyId, validCouponRequest()).getCoupon();

// when
Coupon archiveCouponResponse = COUPONS_CONTROLLER.archiveCoupon(productFamilyId, coupon.getId()).getCoupon();
Coupon couponRead = COUPONS_CONTROLLER.readCoupon(productFamilyId, coupon.getId()).getCoupon();
Coupon couponRead = COUPONS_CONTROLLER.readCoupon(productFamilyId, coupon.getId(), null).getCoupon();

// then
assertThat(archiveCouponResponse.getArchivedAt()).isNotNull();
Expand All @@ -35,7 +35,7 @@ void shouldArchiveCoupon() throws IOException, ApiException {

@Test
void shouldNotArchiveCouponUsingWrongProductFamilyId() throws IOException, ApiException {
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCreateOrUpdateCouponRequest())
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest())
.getCoupon();
ProductFamily productFamily2 = TEST_SETUP.createProductFamily();

Expand All @@ -49,7 +49,7 @@ void shouldNotArchiveNonExistentCoupon() {

@Test
void shouldNotArchiveCouponWhenProvidingInvalidCredentials() throws IOException, ApiException {
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCreateOrUpdateCouponRequest())
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest())
.getCoupon();

assertUnauthorized(() -> TestClient.createInvalidCredentialsClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CompoundingStrategy;
import com.maxio.advancedbilling.models.Coupon;
import com.maxio.advancedbilling.models.CreateOrUpdateCoupon;
import com.maxio.advancedbilling.models.CreateOrUpdateFlatAmountCoupon;
import com.maxio.advancedbilling.models.CreateOrUpdatePercentageCoupon;
import com.maxio.advancedbilling.models.CouponPayload;
import com.maxio.advancedbilling.models.CouponRequest;
import com.maxio.advancedbilling.models.DiscountType;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.containers.CreateOrUpdateCouponCoupon;
import com.maxio.advancedbilling.models.containers.CreateOrUpdatePercentageCouponPercentage;
import com.maxio.advancedbilling.models.containers.CouponPayloadPercentage;
import com.maxio.advancedbilling.utils.assertions.CommonAssertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import java.util.Map;

import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound;
Expand All @@ -41,23 +40,21 @@ static void setupResources() throws IOException, ApiException {
@Test
void shouldCreateFlatAmountCoupon() throws IOException, ApiException {
// given
CreateOrUpdateFlatAmountCoupon flatAmountCoupon = new CreateOrUpdateFlatAmountCoupon.Builder()
CouponPayload flatAmountCoupon = new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.amountInCents(1234)
.amountInCents(1234L)
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.allowNegativeBalance(true)
.recurring(true)
.endDate(ZonedDateTime.now().plusDays(35).plusHours(13))
.endDate(LocalDate.now().plusDays(35))
.stackable(true)
.compoundingStrategy(CompoundingStrategy.COMPOUND)
.excludeMidPeriodAllocations(true)
.applyOnCancelAtEndOfPeriod(true)
.applyOnSubscriptionExpiration(true)
.build();

CreateOrUpdateCouponCoupon createCouponRequest = CreateOrUpdateCouponCoupon
.fromCreateOrUpdateFlatAmountCoupon(flatAmountCoupon);
Map<String, Boolean> restrictedProducts = Map.of(
String.valueOf(product1.getId()), true,
String.valueOf(product2.getId()), false,
Expand All @@ -71,35 +68,33 @@ void shouldCreateFlatAmountCoupon() throws IOException, ApiException {

// when
Coupon coupon = COUPONS_CONTROLLER
.createCoupon(productFamilyId, new CreateOrUpdateCoupon(
createCouponRequest, restrictedProducts, restrictedComponents
.createCoupon(productFamilyId, new CouponRequest(
flatAmountCoupon, restrictedProducts, restrictedComponents
)).getCoupon();

// then
assertResponseCoupon(flatAmountCoupon, coupon);
assertResponseCoupon(flatAmountCoupon, coupon, DiscountType.AMOUNT);
assertRestrictions(coupon, product1, component1);
}

@Test
void shouldCreatePercentageCoupon() throws IOException, ApiException {
// given
CreateOrUpdatePercentageCoupon percentageCoupon = new CreateOrUpdatePercentageCoupon.Builder()
CouponPayload percentageCoupon = new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.percentage(CreateOrUpdatePercentageCouponPercentage.fromPrecision(15.2))
.percentage(CouponPayloadPercentage.fromPrecision(15.2))
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.allowNegativeBalance(true)
.recurring(true)
.endDate(ZonedDateTime.now().plusDays(35).plusHours(13))
.endDate(LocalDate.now().plusDays(35))
.stackable(true)
.compoundingStrategy(CompoundingStrategy.FULLPRICE)
.excludeMidPeriodAllocations(true)
.applyOnCancelAtEndOfPeriod(true)
.applyOnSubscriptionExpiration(true)
.build();

CreateOrUpdateCouponCoupon createCouponRequest = CreateOrUpdateCouponCoupon
.fromCreateOrUpdatePercentageCoupon(percentageCoupon);
Map<String, Boolean> restrictedProducts = Map.of(
String.valueOf(product1.getId()), true,
String.valueOf(product2.getId()), false,
Expand All @@ -113,12 +108,12 @@ void shouldCreatePercentageCoupon() throws IOException, ApiException {

// when
Coupon coupon = COUPONS_CONTROLLER
.createCoupon(productFamilyId, new CreateOrUpdateCoupon(
createCouponRequest, restrictedProducts, restrictedComponents
.createCoupon(productFamilyId, new CouponRequest(
percentageCoupon, restrictedProducts, restrictedComponents
)).getCoupon();

// then
assertResponseCoupon(percentageCoupon, coupon, "15.2");
assertResponseCoupon(percentageCoupon, coupon, DiscountType.PERCENT, "15.2");
assertRestrictions(coupon, product1, component1);
}

Expand All @@ -127,41 +122,57 @@ void shouldReturn422WhenCreatingFlatCouponWithInvalidData() {
CommonAssertions
.assertThatErrorListResponse(() -> COUPONS_CONTROLLER.createCoupon(
productFamilyId,
new CreateOrUpdateCoupon(
CreateOrUpdateCouponCoupon.fromCreateOrUpdateFlatAmountCoupon(
new CreateOrUpdateFlatAmountCoupon.Builder()
.name("coupon" + randomNumeric(10))
.amountInCents(-10)
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.endDate(ZonedDateTime.now().minusDays(35))
.build()
), null, null
new CouponRequest(
new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.amountInCents(-10L)
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.endDate(LocalDate.now().minusDays(35))
.build(), null, null
)
))
.isUnprocessableEntity()
.hasErrors("Amount: must be greater than or equal to $0.00.", "Expiration Date cannot be in the past");
}

@Test
void shouldReturn422WhenBothPercentAndAmountInCentsSet() {
CommonAssertions
.assertThatErrorListResponse(() -> COUPONS_CONTROLLER.createCoupon(
productFamilyId,
new CouponRequest(
new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.amountInCents(10L)
.percentage(CouponPayloadPercentage.fromPrecision(5d))
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.build(), null, null
)
))
.isUnprocessableEntity()
.hasErrors("Either a Discount Percentage or Amount must be specified, but not both",
"Cannot create prices for a percentage-based coupon.");
}

@Test
void shouldReturn422WhenCreatingPercentageCouponWithInvalidDataAndExistingCode() throws IOException, ApiException {
Coupon existingCoupon = COUPONS_CONTROLLER.createCoupon(
productFamilyId,
validCreateOrUpdateCouponRequest()
validCouponRequest()
).getCoupon();

CommonAssertions
.assertThatErrorListResponse(() -> COUPONS_CONTROLLER.createCoupon(
productFamilyId,
new CreateOrUpdateCoupon(
CreateOrUpdateCouponCoupon.fromCreateOrUpdatePercentageCoupon(
new CreateOrUpdatePercentageCoupon.Builder()
.name("coupon" + randomNumeric(10))
.percentage(CreateOrUpdatePercentageCouponPercentage.fromPrecision(105))
.description("description" + randomNumeric(20))
.code(existingCoupon.getCode())
.build()
), null, null
new CouponRequest(
new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.percentage(CouponPayloadPercentage.fromPrecision(105))
.description("description" + randomNumeric(20))
.code(existingCoupon.getCode())
.build(), null, null
)
))
.isUnprocessableEntity()
Expand All @@ -170,13 +181,13 @@ void shouldReturn422WhenCreatingPercentageCouponWithInvalidDataAndExistingCode()

@Test
void shouldNotCreateCouponForNonExistentProductFamily() {
assertNotFound(() -> COUPONS_CONTROLLER.createCoupon(99999999, validCreateOrUpdateCouponRequest()));
assertNotFound(() -> COUPONS_CONTROLLER.createCoupon(99999999, validCouponRequest()));
}

@Test
void shouldNotCreateCouponWhenProvidingInvalidCredentials() {
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient()
.getCouponsController().createCoupon(productFamilyId, validCreateOrUpdateCouponRequest())
.getCouponsController().createCoupon(productFamilyId, validCouponRequest())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CompoundingStrategy;
import com.maxio.advancedbilling.models.Coupon;
import com.maxio.advancedbilling.models.CreateOrUpdateCoupon;
import com.maxio.advancedbilling.models.CreateOrUpdateFlatAmountCoupon;
import com.maxio.advancedbilling.models.CouponPayload;
import com.maxio.advancedbilling.models.CouponRequest;
import com.maxio.advancedbilling.models.DiscountType;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductFamily;
import com.maxio.advancedbilling.models.containers.CreateOrUpdateCouponCoupon;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import java.util.Map;

import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound;
Expand All @@ -36,23 +36,21 @@ static void setupResources() throws IOException, ApiException {
@Test
void shouldFindCoupon() throws IOException, ApiException {
// given
CreateOrUpdateFlatAmountCoupon flatAmountCoupon = new CreateOrUpdateFlatAmountCoupon.Builder()
CouponPayload flatAmountCoupon = new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.amountInCents(1234)
.amountInCents(1234L)
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.allowNegativeBalance(true)
.recurring(true)
.endDate(ZonedDateTime.now().plusDays(35).plusHours(13))
.endDate(LocalDate.now().plusDays(35))
.stackable(true)
.compoundingStrategy(CompoundingStrategy.COMPOUND)
.excludeMidPeriodAllocations(true)
.applyOnCancelAtEndOfPeriod(true)
.applyOnSubscriptionExpiration(true)
.build();

CreateOrUpdateCouponCoupon createCouponRequest = CreateOrUpdateCouponCoupon
.fromCreateOrUpdateFlatAmountCoupon(flatAmountCoupon);
Map<String, Boolean> restrictedProducts = Map.of(
String.valueOf(product.getId()), true
);
Expand All @@ -62,38 +60,44 @@ void shouldFindCoupon() throws IOException, ApiException {

// when
Coupon coupon = COUPONS_CONTROLLER
.createCoupon(productFamilyId, new CreateOrUpdateCoupon(
createCouponRequest, restrictedProducts, restrictedComponents
.createCoupon(productFamilyId, new CouponRequest(
flatAmountCoupon, restrictedProducts, restrictedComponents
)).getCoupon();
Coupon couponRead = COUPONS_CONTROLLER.findCoupon(productFamilyId, coupon.getCode()).getCoupon();
Coupon couponFound = COUPONS_CONTROLLER.findCoupon(productFamilyId, coupon.getCode(), null).getCoupon();
Coupon couponFoundWithCurrencyPrices = COUPONS_CONTROLLER.findCoupon(productFamilyId, coupon.getCode(), true).getCoupon();

// then
assertResponseCoupon(flatAmountCoupon, coupon);
assertResponseCoupon(flatAmountCoupon, coupon, DiscountType.AMOUNT);
assertRestrictions(coupon, product, component);
assertThat(couponRead).usingRecursiveComparison().isEqualTo(coupon);
assertThat(couponFound).usingRecursiveComparison().isEqualTo(coupon);
assertThat(couponFound.getCurrencyPrices()).isNull();

assertThat(couponFoundWithCurrencyPrices).usingRecursiveComparison()
.ignoringFields("currencyPrices").isEqualTo(coupon);
assertCurrencyPrices(couponFoundWithCurrencyPrices.getCurrencyPrices(), coupon.getId());
}

@Test
void shouldNotFindCouponUsingWrongProductFamilyId() throws IOException, ApiException {
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCreateOrUpdateCouponRequest())
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest())
.getCoupon();
ProductFamily productFamily2 = TEST_SETUP.createProductFamily();

assertNotFound(() -> COUPONS_CONTROLLER.findCoupon(productFamily2.getId(), coupon.getCode()));
assertNotFound(() -> COUPONS_CONTROLLER.findCoupon(productFamily2.getId(), coupon.getCode(), null));
}

@Test
void shouldNotFindNonExistentCoupon() {
assertNotFound(() -> COUPONS_CONTROLLER.findCoupon(productFamilyId, "non-existent-code"));
assertNotFound(() -> COUPONS_CONTROLLER.findCoupon(productFamilyId, "non-existent-code", null));
}

@Test
void shouldNotReadCouponWhenProvidingInvalidCredentials() throws IOException, ApiException {
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCreateOrUpdateCouponRequest())
Coupon coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest())
.getCoupon();

assertUnauthorized(() -> TestClient.createInvalidCredentialsClient()
.getCouponsController().findCoupon(productFamilyId, coupon.getCode())
.getCouponsController().findCoupon(productFamilyId, coupon.getCode(), null)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ static void setupResources() throws IOException, ApiException {
testStart = ZonedDateTime.now();
productFamily2 = TEST_SETUP.createProductFamily();
for (int i=0; i<3; i++) {
coupons1.add(COUPONS_CONTROLLER.createCoupon(productFamilyId, validCreateOrUpdateCouponRequest()).getCoupon());
coupons1.add(COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon());
}
for (int i=0; i<3; i++) {
coupons2.add(COUPONS_CONTROLLER.createCoupon(productFamily2.getId(), validCreateOrUpdateCouponRequest()).getCoupon());
coupons2.add(COUPONS_CONTROLLER.createCoupon(productFamily2.getId(), validCouponRequest()).getCoupon());
}
}

Expand Down Expand Up @@ -173,9 +173,12 @@ void shouldListCouponsWithCurrencyPrices() throws IOException, ApiException {
)
.stream().map(CouponResponse::getCoupon).toList();

assertThat(listedCoupons).hasSize(10);
assertThat(listedCoupons).usingRecursiveFieldByFieldElementComparator()
assertThat(listedCoupons).hasSize(3);
assertThat(listedCoupons).usingRecursiveFieldByFieldElementComparatorIgnoringFields("currencyPrices")
.containsExactlyInAnyOrderElementsOf(coupons1);
for (Coupon coupon: listedCoupons) {
assertCurrencyPrices(coupon.getCurrencyPrices(), coupon.getId());
}
}

@Test
Expand Down
Loading

0 comments on commit 2b731a6

Please sign in to comment.