-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCouponsControllerCreateCouponTest.java
More file actions
194 lines (173 loc) · 8.43 KB
/
CouponsControllerCreateCouponTest.java
File metadata and controls
194 lines (173 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.maxio.advancedbilling.controllers.coupons;
import com.maxio.advancedbilling.TestClientProvider;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CompoundingStrategy;
import com.maxio.advancedbilling.models.Coupon;
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.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.LocalDate;
import java.util.Map;
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;
public class CouponsControllerCreateCouponTest extends CouponsControllerTestBase {
protected Product product1;
protected Product product2;
protected Component component1;
protected Component component2;
@BeforeAll
void setupResources() throws IOException, ApiException {
product1 = TEST_SETUP.createProduct(productFamily);
product2 = TEST_SETUP.createProduct(productFamily);
component1 = TEST_SETUP.createQuantityBasedComponent(productFamilyId);
component2 = TEST_SETUP.createQuantityBasedComponent(productFamilyId);
}
@Test
void shouldCreateFlatAmountCoupon() throws IOException, ApiException {
// given
CouponPayload flatAmountCoupon = new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.amountInCents(1234L)
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.allowNegativeBalance(true)
.recurring(true)
.endDate(LocalDate.now().plusDays(35))
.stackable(true)
.compoundingStrategy(CompoundingStrategy.COMPOUND)
.excludeMidPeriodAllocations(true)
.applyOnCancelAtEndOfPeriod(true)
.applyOnSubscriptionExpiration(true)
.build();
Map<String, Boolean> restrictedProducts = Map.of(
String.valueOf(product1.getId()), true,
String.valueOf(product2.getId()), false,
"1234", true
);
Map<String, Boolean> restrictedComponents = Map.of(
String.valueOf(component1.getId()), true,
String.valueOf(component2.getId()), false,
"1234", true
);
// when
Coupon coupon = COUPONS_CONTROLLER
.createCoupon(productFamilyId, new CouponRequest(
flatAmountCoupon, restrictedProducts, restrictedComponents
)).getCoupon();
// then
assertResponseCoupon(flatAmountCoupon, coupon, DiscountType.AMOUNT);
assertRestrictions(coupon, product1, component1);
}
@Test
void shouldCreatePercentageCoupon() throws IOException, ApiException {
// given
CouponPayload percentageCoupon = new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.percentage(CouponPayloadPercentage.fromPrecision(15.2))
.description("description" + randomNumeric(20))
.code("coupon%@+-_." + randomNumeric(10))
.allowNegativeBalance(true)
.recurring(true)
.endDate(LocalDate.now().plusDays(35))
.stackable(true)
.compoundingStrategy(CompoundingStrategy.FULLPRICE)
.excludeMidPeriodAllocations(true)
.applyOnCancelAtEndOfPeriod(true)
.applyOnSubscriptionExpiration(true)
.build();
Map<String, Boolean> restrictedProducts = Map.of(
String.valueOf(product1.getId()), true,
String.valueOf(product2.getId()), false,
"1234", true
);
Map<String, Boolean> restrictedComponents = Map.of(
String.valueOf(component1.getId()), true,
String.valueOf(component2.getId()), false,
"1234", true
);
// when
Coupon coupon = COUPONS_CONTROLLER
.createCoupon(productFamilyId, new CouponRequest(
percentageCoupon, restrictedProducts, restrictedComponents
)).getCoupon();
// then
assertResponseCoupon(percentageCoupon, coupon, DiscountType.PERCENT, "15.2");
assertRestrictions(coupon, product1, component1);
}
@Test
void shouldReturn422WhenCreatingFlatCouponWithInvalidData() {
CommonAssertions
.assertThatErrorListResponse(() -> COUPONS_CONTROLLER.createCoupon(
productFamilyId,
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 an 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,
validCouponRequest()
).getCoupon();
CommonAssertions
.assertThatErrorListResponse(() -> COUPONS_CONTROLLER.createCoupon(
productFamilyId,
new CouponRequest(
new CouponPayload.Builder()
.name("coupon" + randomNumeric(10))
.percentage(CouponPayloadPercentage.fromPrecision(105))
.description("description" + randomNumeric(20))
.code(existingCoupon.getCode())
.build(), null, null
)
))
.isUnprocessableEntity()
.hasErrors("Percentage: must be less than or equal to 100.", "Code: must be unique - that value has been taken.");
}
@Test
void shouldNotCreateCouponForNonExistentProductFamily() {
assertNotFound(() -> COUPONS_CONTROLLER.createCoupon(99999999, validCouponRequest()));
}
@Test
void shouldNotCreateCouponWhenProvidingInvalidCredentials() {
assertUnauthorized(() -> TestClientProvider.createInvalidCredentialsClient()
.getCouponsController().createCoupon(productFamilyId, validCouponRequest())
);
}
}