Skip to content
Original file line number Diff line number Diff line change
@@ -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<CouponCurrency> 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<CouponCurrency> 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())
);
}

}
Original file line number Diff line number Diff line change
@@ -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")))
);
}

}
Original file line number Diff line number Diff line change
@@ -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<String> 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")
);
}

}
Original file line number Diff line number Diff line change
@@ -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<String> subcodes;

@BeforeAll
void setupResources() throws IOException, ApiException {
coupon = COUPONS_CONTROLLER.createCoupon(productFamilyId, validCouponRequest()).getCoupon();

List<String> 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<String> codes = COUPONS_CONTROLLER
.listCouponSubcodes(new ListCouponSubcodesInput.Builder()
.couponId(coupon.getId())
.build()
).getCodes();
assertThat(codes).containsExactlyInAnyOrderElementsOf(subcodes);
}

@Test
void shouldListCouponSubcodesWithPaging() throws IOException, ApiException {
// given
List<String> page1 = COUPONS_CONTROLLER
.listCouponSubcodes(new ListCouponSubcodesInput.Builder()
.couponId(coupon.getId())
.page(1)
.perPage(4)
.build()
).getCodes();
List<String> page2 = COUPONS_CONTROLLER
.listCouponSubcodes(new ListCouponSubcodesInput.Builder()
.couponId(coupon.getId())
.page(2)
.perPage(4)
.build()
).getCodes();
List<String> 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)));
}

}
Loading
Loading