Skip to content

Commit 2a82785

Browse files
Add fluent API for order placement and verification; introduce EmptyGivenClause and ViewOrderBuilder
1 parent 00eebe2 commit 2a82785

File tree

8 files changed

+599
-8
lines changed

8 files changed

+599
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.optivem.eshop.systemtest.core.gherkin.given;
2+
3+
import com.optivem.eshop.systemtest.core.SystemDsl;
4+
import com.optivem.eshop.systemtest.core.gherkin.when.WhenClause;
5+
6+
public class EmptyGivenClause {
7+
private final SystemDsl app;
8+
9+
public EmptyGivenClause(SystemDsl app) {
10+
this.app = app;
11+
}
12+
13+
public WhenClause when() {
14+
return new WhenClause(app);
15+
}
16+
}

core/src/main/java/com/optivem/eshop/systemtest/core/gherkin/given/GivenClause.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public OrderBuilder order() {
2727
return orderBuilder;
2828
}
2929

30+
public EmptyGivenClause noProducts() {
31+
// No products to create, return clause that allows .when()
32+
return new EmptyGivenClause(app);
33+
}
34+
3035
public WhenClause when() {
3136
// Execute all product creations
3237
for (var product : products) {

core/src/main/java/com/optivem/eshop/systemtest/core/gherkin/given/ProductBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public ProductBuilder withUnitPrice(double unitPrice) {
2121
return this;
2222
}
2323

24+
public ProductBuilder withUnitPrice(String unitPrice) {
25+
this.unitPrice = Double.parseDouble(unitPrice);
26+
return this;
27+
}
28+
2429
public GivenClause and() {
2530
return givenClause;
2631
}

core/src/main/java/com/optivem/eshop/systemtest/core/gherkin/then/OrderVerificationBuilder.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,46 @@ public OrderVerificationBuilder hasSku(String expectedSku) {
2121
return this;
2222
}
2323

24+
public OrderVerificationBuilder hasQuantity(int expectedQuantity) {
25+
app.shop().viewOrder()
26+
.orderNumber(orderNumber)
27+
.execute()
28+
.shouldSucceed()
29+
.quantity(expectedQuantity);
30+
return this;
31+
}
32+
33+
public OrderVerificationBuilder hasCountry(String expectedCountry) {
34+
app.shop().viewOrder()
35+
.orderNumber(orderNumber)
36+
.execute()
37+
.shouldSucceed()
38+
.country(expectedCountry);
39+
return this;
40+
}
41+
42+
public OrderVerificationBuilder hasUnitPrice(double expectedUnitPrice) {
43+
app.shop().viewOrder()
44+
.orderNumber(orderNumber)
45+
.execute()
46+
.shouldSucceed()
47+
.unitPrice(expectedUnitPrice);
48+
return this;
49+
}
50+
51+
public OrderVerificationBuilder hasOriginalPrice(double expectedOriginalPrice) {
52+
app.shop().viewOrder()
53+
.orderNumber(orderNumber)
54+
.execute()
55+
.shouldSucceed()
56+
.originalPrice(expectedOriginalPrice);
57+
return this;
58+
}
59+
60+
public OrderVerificationBuilder hasOriginalPrice(String expectedOriginalPrice) {
61+
return hasOriginalPrice(Double.parseDouble(expectedOriginalPrice));
62+
}
63+
2464
public OrderVerificationBuilder hasTotalPrice(double expectedTotalPrice) {
2565
app.shop().viewOrder()
2666
.orderNumber(orderNumber)
@@ -38,4 +78,58 @@ public OrderVerificationBuilder hasStatus(OrderStatus expectedStatus) {
3878
.status(expectedStatus);
3979
return this;
4080
}
81+
82+
public OrderVerificationBuilder hasDiscountRateGreaterThanOrEqualToZero() {
83+
app.shop().viewOrder()
84+
.orderNumber(orderNumber)
85+
.execute()
86+
.shouldSucceed()
87+
.discountRateGreaterThanOrEqualToZero();
88+
return this;
89+
}
90+
91+
public OrderVerificationBuilder hasDiscountAmountGreaterThanOrEqualToZero() {
92+
app.shop().viewOrder()
93+
.orderNumber(orderNumber)
94+
.execute()
95+
.shouldSucceed()
96+
.discountAmountGreaterThanOrEqualToZero();
97+
return this;
98+
}
99+
100+
public OrderVerificationBuilder hasSubtotalPriceGreaterThanZero() {
101+
app.shop().viewOrder()
102+
.orderNumber(orderNumber)
103+
.execute()
104+
.shouldSucceed()
105+
.subtotalPriceGreaterThanZero();
106+
return this;
107+
}
108+
109+
public OrderVerificationBuilder hasTaxRateGreaterThanOrEqualToZero() {
110+
app.shop().viewOrder()
111+
.orderNumber(orderNumber)
112+
.execute()
113+
.shouldSucceed()
114+
.taxRateGreaterThanOrEqualToZero();
115+
return this;
116+
}
117+
118+
public OrderVerificationBuilder hasTaxAmountGreaterThanOrEqualToZero() {
119+
app.shop().viewOrder()
120+
.orderNumber(orderNumber)
121+
.execute()
122+
.shouldSucceed()
123+
.taxAmountGreaterThanOrEqualToZero();
124+
return this;
125+
}
126+
127+
public OrderVerificationBuilder hasTotalPriceGreaterThanZero() {
128+
app.shop().viewOrder()
129+
.orderNumber(orderNumber)
130+
.execute()
131+
.shouldSucceed()
132+
.totalPriceGreaterThanZero();
133+
return this;
134+
}
41135
}

core/src/main/java/com/optivem/eshop/systemtest/core/gherkin/when/PlaceOrderBuilder.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
import com.optivem.eshop.systemtest.core.gherkin.then.ThenClause;
55

66
public class PlaceOrderBuilder {
7+
private static final String DEFAULT_SKU = "DEFAULT-SKU";
8+
private static final int DEFAULT_QUANTITY = 1;
9+
private static final String DEFAULT_COUNTRY = "US";
10+
711
private final SystemDsl app;
812
private String orderNumber;
9-
private String sku;
13+
private String sku = DEFAULT_SKU;
1014
private String quantityString;
11-
private Integer quantityInt;
15+
private Integer quantityInt = DEFAULT_QUANTITY;
16+
private String country = DEFAULT_COUNTRY;
17+
private boolean skuExplicitlySet = false;
18+
private boolean quantityExplicitlySet = false;
19+
private boolean countryExplicitlySet = false;
1220

1321
public PlaceOrderBuilder(SystemDsl app) {
1422
this.app = app;
@@ -21,31 +29,65 @@ public PlaceOrderBuilder withOrderNumber(String orderNumber) {
2129

2230
public PlaceOrderBuilder withSku(String sku) {
2331
this.sku = sku;
32+
this.skuExplicitlySet = true;
2433
return this;
2534
}
2635

2736
public PlaceOrderBuilder withQuantity(int quantity) {
2837
this.quantityInt = quantity;
2938
this.quantityString = null;
39+
this.quantityExplicitlySet = true;
3040
return this;
3141
}
3242

43+
/**
44+
* Accepts a String quantity for validation tests (empty, non-integer values).
45+
* For parameterized tests with valid integer strings, use this method.
46+
*/
3347
public PlaceOrderBuilder withQuantity(String quantity) {
3448
this.quantityString = quantity;
3549
this.quantityInt = null;
50+
this.quantityExplicitlySet = true;
51+
return this;
52+
}
53+
54+
public PlaceOrderBuilder withCountry(String country) {
55+
this.country = country;
56+
this.countryExplicitlySet = true;
3657
return this;
3758
}
3859

3960
public ThenClause then() {
4061
// Execute the place order
4162
var placeOrder = app.shop().placeOrder()
42-
.orderNumber(orderNumber)
43-
.sku(sku);
63+
.orderNumber(orderNumber);
64+
65+
// Apply SKU - use explicit value if set, otherwise use default
66+
if (skuExplicitlySet) {
67+
placeOrder.sku(sku);
68+
} else {
69+
placeOrder.sku(DEFAULT_SKU);
70+
}
71+
72+
// Apply quantity
73+
if (quantityExplicitlySet) {
74+
if (quantityString != null) {
75+
placeOrder.quantity(quantityString);
76+
} else if (quantityInt != null) {
77+
placeOrder.quantity(quantityInt);
78+
} else {
79+
// null was explicitly passed as String
80+
placeOrder.quantity((String) null);
81+
}
82+
} else {
83+
placeOrder.quantity(DEFAULT_QUANTITY);
84+
}
4485

45-
if (quantityString != null) {
46-
placeOrder.quantity(quantityString);
47-
} else if (quantityInt != null) {
48-
placeOrder.quantity(quantityInt);
86+
// Apply country
87+
if (countryExplicitlySet) {
88+
placeOrder.country(country);
89+
} else {
90+
placeOrder.country(DEFAULT_COUNTRY);
4991
}
5092

5193
var result = placeOrder.execute();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.optivem.eshop.systemtest.core.gherkin.when;
2+
3+
import com.optivem.eshop.systemtest.core.SystemDsl;
4+
import com.optivem.eshop.systemtest.core.gherkin.then.ThenClause;
5+
6+
public class ViewOrderBuilder {
7+
private final SystemDsl app;
8+
private String orderNumber;
9+
10+
public ViewOrderBuilder(SystemDsl app) {
11+
this.app = app;
12+
}
13+
14+
public ViewOrderBuilder withOrderNumber(String orderNumber) {
15+
this.orderNumber = orderNumber;
16+
return this;
17+
}
18+
19+
public ThenClause then() {
20+
var result = app.shop().viewOrder()
21+
.orderNumber(orderNumber)
22+
.execute();
23+
24+
return new ThenClause(app, orderNumber, result);
25+
}
26+
}

core/src/main/java/com/optivem/eshop/systemtest/core/gherkin/when/WhenClause.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ public PlaceOrderBuilder placeOrder() {
1616
public CancelOrderBuilder cancelOrder() {
1717
return new CancelOrderBuilder(app);
1818
}
19+
20+
public ViewOrderBuilder viewOrder() {
21+
return new ViewOrderBuilder(app);
22+
}
1923
}

0 commit comments

Comments
 (0)