From 81f82a64153383ebde57ef20c3491afd6690e03c Mon Sep 17 00:00:00 2001 From: evela Date: Mon, 15 Jan 2024 10:06:01 +0100 Subject: [PATCH 01/12] Update to gradle 8.5 (needed for Java 21) --- gradle/wrapper/gradle-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3a906e0..d336f3c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip From 25f2a155dc1ee28d0477e90fa5e752fef6d33de6 Mon Sep 17 00:00:00 2001 From: evela Date: Mon, 15 Jan 2024 17:22:29 +0100 Subject: [PATCH 02/12] Exercise-1 --- .../java/caffeinateme/model/CoffeeShop.java | 3 ++ .../caffeinateme/steps/OrderCoffeeSteps.java | 42 +++++++++++++++++++ .../features/orders/order_a_coffee.feature | 23 +++++++--- 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/test/java/caffeinateme/steps/OrderCoffeeSteps.java diff --git a/src/main/java/caffeinateme/model/CoffeeShop.java b/src/main/java/caffeinateme/model/CoffeeShop.java index 70d7586..be53dcd 100644 --- a/src/main/java/caffeinateme/model/CoffeeShop.java +++ b/src/main/java/caffeinateme/model/CoffeeShop.java @@ -7,6 +7,9 @@ public class CoffeeShop { private Queue orders = new LinkedList<>(); public void placeOrder(Order order, int distanceInMetres) { + if (distanceInMetres <= 200) { + order = order.withStatus(OrderStatus.Urgent); + } orders.add(order); } diff --git a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java new file mode 100644 index 0000000..d84abb8 --- /dev/null +++ b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java @@ -0,0 +1,42 @@ +package caffeinateme.steps; + +import caffeinateme.model.CoffeeShop; +import caffeinateme.model.Customer; +import caffeinateme.model.Order; +import caffeinateme.model.OrderStatus; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OrderCoffeeSteps { + + Customer cathy = Customer.named("Cathy"); + CoffeeShop coffeeShop = new CoffeeShop(); + Order order; + + @Given("Cathy is {int} metres from the coffee shop") + public void cathyIsMetresFromTheCoffeeShop(Integer arg0) { + cathy.setDistanceFromShop(arg0); + } + + @When("^Cathy orders a (.*)") + public void cathy_orders_a(String orderedProduct) { + this.order = Order.of(1, orderedProduct).forCustomer(cathy); + cathy.placesAnOrderFor(order).at(coffeeShop); + } + + @Then("Barry should receive the order") + public void barryShouldReceiveTheOrder() { + assertThat(coffeeShop.getPendingOrders()).contains(order); + } + + @And("^Barry should know that the order is (.*)") + public void barry_should_know_that_the_order_is(OrderStatus expectedStatus) { + Order cathysOrder = coffeeShop.getOrderFor(cathy) + .orElseThrow(() -> new AssertionError("No order found!")); + assertThat(cathysOrder.getStatus()).isEqualTo(expectedStatus); + } +} diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index b59c117..f127707 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -4,8 +4,21 @@ Feature: Order a coffee As a coffee love I want to be able to order my coffee in advance - Scenario: Buyer orders a coffee when they are close to the coffee shop -# Given Cathy is 100 metres from the coffee shop -# When Cathy orders a large cappuccino -# Then Barry should receive the order -# And Barry should know that the order is Urgent \ No newline at end of file + Scenario: Buyers order a coffee when they are close to the coffee shop + Given Cathy is 100 metres from the coffee shop + When Cathy orders a "large cappuccino" + Then Barry should receive the order + And Barry should know that the order is Urgent + + Scenario: Buyers order a coffee when they are very close to the coffee shop + Given Cathy is 50 metres from the coffee shop + When Cathy orders a "large cappuccino" + Then Barry should receive the order + And Barry should know that the order is Urgent + + Scenario: Buyers order a coffee when they are far to the coffee shop + Given Cathy is 300 metres from the coffee shop + When Cathy orders a "large cappuccino" + Then Barry should receive the order + And Barry should know that the order is Normal + From e7d6da7e3fe0317ae859b682c70a3da101003e29 Mon Sep 17 00:00:00 2001 From: evela Date: Mon, 15 Jan 2024 17:41:52 +0100 Subject: [PATCH 03/12] Exercise-2 --- .../java/caffeinateme/model/CoffeeShop.java | 8 ++++++++ .../caffeinateme/steps/OrderCoffeeSteps.java | 17 ++++++++++++++++- .../features/orders/order_a_coffee.feature | 14 +++++++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/java/caffeinateme/model/CoffeeShop.java b/src/main/java/caffeinateme/model/CoffeeShop.java index be53dcd..0ca8382 100644 --- a/src/main/java/caffeinateme/model/CoffeeShop.java +++ b/src/main/java/caffeinateme/model/CoffeeShop.java @@ -5,6 +5,7 @@ public class CoffeeShop { private Queue orders = new LinkedList<>(); + private List customers = new ArrayList<>(); public void placeOrder(Order order, int distanceInMetres) { if (distanceInMetres <= 200) { @@ -22,4 +23,11 @@ public Optional getOrderFor(Customer customer) { .filter( order -> order.getCustomer().equals(customer)) .findFirst(); } + + public Customer registerNewCustomer(String customerName) { + + Customer newCustomer = new Customer(customerName); + customers.add(newCustomer); + return newCustomer; + } } diff --git a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java index d84abb8..e2a666e 100644 --- a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java +++ b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java @@ -17,22 +17,37 @@ public class OrderCoffeeSteps { CoffeeShop coffeeShop = new CoffeeShop(); Order order; + @Given("{} is a CaffeinateMe customer") + public void a_caffeinate_me_customer_named(String customerName) { + cathy = coffeeShop.registerNewCustomer(customerName); + } @Given("Cathy is {int} metres from the coffee shop") public void cathyIsMetresFromTheCoffeeShop(Integer arg0) { cathy.setDistanceFromShop(arg0); } - @When("^Cathy orders a (.*)") + @When("Cathy orders a {string}") public void cathy_orders_a(String orderedProduct) { this.order = Order.of(1, orderedProduct).forCustomer(cathy); cathy.placesAnOrderFor(order).at(coffeeShop); } + @When("Cathy orders a {string} with a comment {string}") + public void cathy_orders_with_comment(String orderedProduct, String comment) { + this.order = Order.of(1, orderedProduct).forCustomer(cathy).withComment(comment); + cathy.placesAnOrderFor(order).at(coffeeShop); + } + @Then("Barry should receive the order") public void barryShouldReceiveTheOrder() { assertThat(coffeeShop.getPendingOrders()).contains(order); } + @Then("the order should have the comment {string}") + public void order_should_have_comment(String comment) { + Order order = coffeeShop.getOrderFor(cathy).get(); + assertThat(order.getComment()).isEqualTo(comment); + } @And("^Barry should know that the order is (.*)") public void barry_should_know_that_the_order_is(OrderStatus expectedStatus) { Order cathysOrder = coffeeShop.getOrderFor(cathy) diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index f127707..b6ff698 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -5,20 +5,28 @@ Feature: Order a coffee I want to be able to order my coffee in advance Scenario: Buyers order a coffee when they are close to the coffee shop - Given Cathy is 100 metres from the coffee shop + Given Cathy is a CaffeinateMe customer + And Cathy is 100 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Urgent Scenario: Buyers order a coffee when they are very close to the coffee shop - Given Cathy is 50 metres from the coffee shop + Given Cathy is a CaffeinateMe customer + And Cathy is 50 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Urgent Scenario: Buyers order a coffee when they are far to the coffee shop - Given Cathy is 300 metres from the coffee shop + Given Cathy is a CaffeinateMe customer + And Cathy is 300 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Normal + Scenario: Buyers can add a comment with their order + Given Cathy is a CaffeinateMe customer + When Cathy orders a "large cappuccino" with a comment "Double sugar" + Then Barry should receive the order + And the order should have the comment "Double sugar" From 8672be68a806a395259ce8d75790abd771f76714 Mon Sep 17 00:00:00 2001 From: evela Date: Thu, 18 Jan 2024 14:51:08 +0100 Subject: [PATCH 04/12] Exercise-3: add background --- src/test/resources/features/orders/order_a_coffee.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index b6ff698..a07fa60 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -4,22 +4,22 @@ Feature: Order a coffee As a coffee love I want to be able to order my coffee in advance - Scenario: Buyers order a coffee when they are close to the coffee shop + Background: Given Cathy is a CaffeinateMe customer + + Scenario: Buyers order a coffee when they are close to the coffee shop And Cathy is 100 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Urgent Scenario: Buyers order a coffee when they are very close to the coffee shop - Given Cathy is a CaffeinateMe customer And Cathy is 50 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Urgent Scenario: Buyers order a coffee when they are far to the coffee shop - Given Cathy is a CaffeinateMe customer And Cathy is 300 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order From 8288ffbf2b47bc29f6cfd2cc0810e87ea0e6fd8e Mon Sep 17 00:00:00 2001 From: evela Date: Thu, 18 Jan 2024 14:52:18 +0100 Subject: [PATCH 05/12] Upgrade to junit 4.13.2 --- gradle.properties | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index a837e74..f346699 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ serenityCoreVersion = 1.6.9 serenityCucumberVersion = 1.5.17 -junitVersion = 4.12 +junitVersion=4.13.2 assertjVersion = 3.6.2 slf4jVersion = 1.7.7 \ No newline at end of file diff --git a/pom.xml b/pom.xml index f02af18..a9c8eaa 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ junit junit - 4.13.1 + 4.13.2 test From 9d6a9b47b37d4c2d7146346b768b7802b373d64c Mon Sep 17 00:00:00 2001 From: evela Date: Thu, 18 Jan 2024 15:12:06 +0100 Subject: [PATCH 06/12] Revert "Upgrade to junit 4.13.2" This reverts commit 8288ffbf2b47bc29f6cfd2cc0810e87ea0e6fd8e. --- gradle.properties | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index f346699..a837e74 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ serenityCoreVersion = 1.6.9 serenityCucumberVersion = 1.5.17 -junitVersion=4.13.2 +junitVersion = 4.12 assertjVersion = 3.6.2 slf4jVersion = 1.7.7 \ No newline at end of file diff --git a/pom.xml b/pom.xml index a9c8eaa..f02af18 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ junit junit - 4.13.2 + 4.13.1 test From 547458aceb20eecac30daef6eb6d0363b29f24f0 Mon Sep 17 00:00:00 2001 From: evela Date: Thu, 18 Jan 2024 15:15:50 +0100 Subject: [PATCH 07/12] Upgrade to junit 4.13.1 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index d7090e1..6e354fc 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ ext { slf4jVersion = '1.7.7' serenityCoreVersion = '3.3.10' serenityCucumberVersion = '3.3.10' - junitVersion = '4.12' + junitVersion = '4.13.1' assertJVersion = '3.22.0' logbackVersion = '1.2.3' } From ddacd5b082cfb36d779cccf1672286a5f56a3c41 Mon Sep 17 00:00:00 2001 From: evela Date: Thu, 18 Jan 2024 15:18:06 +0100 Subject: [PATCH 08/12] Fixed Scenario First step: "Given" instead of "And" --- src/test/resources/features/orders/order_a_coffee.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index a07fa60..d69046b 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -8,19 +8,19 @@ Feature: Order a coffee Given Cathy is a CaffeinateMe customer Scenario: Buyers order a coffee when they are close to the coffee shop - And Cathy is 100 metres from the coffee shop + Given Cathy is 100 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Urgent Scenario: Buyers order a coffee when they are very close to the coffee shop - And Cathy is 50 metres from the coffee shop + Given Cathy is 50 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Urgent Scenario: Buyers order a coffee when they are far to the coffee shop - And Cathy is 300 metres from the coffee shop + Given Cathy is 300 metres from the coffee shop When Cathy orders a "large cappuccino" Then Barry should receive the order And Barry should know that the order is Normal From 22d4fb3a7b56f576dcb3afe0ca37b2a9fcf0b104 Mon Sep 17 00:00:00 2001 From: evela Date: Tue, 23 Jan 2024 09:35:20 +0100 Subject: [PATCH 09/12] Exercise 4: Rules and Examples --- .../features/orders/order_a_coffee.feature | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index d69046b..64a7ef8 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -7,26 +7,30 @@ Feature: Order a coffee Background: Given Cathy is a CaffeinateMe customer - Scenario: Buyers order a coffee when they are close to the coffee shop - Given Cathy is 100 metres from the coffee shop - When Cathy orders a "large cappuccino" - Then Barry should receive the order - And Barry should know that the order is Urgent - - Scenario: Buyers order a coffee when they are very close to the coffee shop - Given Cathy is 50 metres from the coffee shop - When Cathy orders a "large cappuccino" - Then Barry should receive the order - And Barry should know that the order is Urgent - - Scenario: Buyers order a coffee when they are far to the coffee shop - Given Cathy is 300 metres from the coffee shop - When Cathy orders a "large cappuccino" - Then Barry should receive the order - And Barry should know that the order is Normal - - Scenario: Buyers can add a comment with their order - Given Cathy is a CaffeinateMe customer - When Cathy orders a "large cappuccino" with a comment "Double sugar" - Then Barry should receive the order - And the order should have the comment "Double sugar" + Rule: Orders placed close to the store should be considered as Urgent + + Example: Buyers order a coffee when they are close to the coffee shop + Given Cathy is 100 metres from the coffee shop + When Cathy orders a "large cappuccino" + Then Barry should receive the order + And Barry should know that the order is Urgent + + Example: Buyers order a coffee when they are very close to the coffee shop + Given Cathy is 50 metres from the coffee shop + When Cathy orders a "large cappuccino" + Then Barry should receive the order + And Barry should know that the order is Urgent + + Example: Buyers order a coffee when they are far to the coffee shop + Given Cathy is 300 metres from the coffee shop + When Cathy orders a "large cappuccino" + Then Barry should receive the order + And Barry should know that the order is Normal + + Rule: Buyers can specify their preferences when they order + + Example: Buyers can add a comment with their order + Given Cathy is a CaffeinateMe customer + When Cathy orders a "large cappuccino" with a comment "Double sugar" + Then Barry should receive the order + And the order should have the comment "Double sugar" From 769cddc507487b44b13f89d68fc05b0cfc8fb872 Mon Sep 17 00:00:00 2001 From: evela Date: Tue, 23 Jan 2024 11:00:52 +0100 Subject: [PATCH 10/12] Exercise 5: Custom Cucumber Expressions --- .../caffeinateme/steps/OrderCoffeeSteps.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java index e2a666e..1de1e40 100644 --- a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java +++ b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java @@ -4,6 +4,7 @@ import caffeinateme.model.Customer; import caffeinateme.model.Order; import caffeinateme.model.OrderStatus; +import io.cucumber.java.ParameterType; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; @@ -17,6 +18,11 @@ public class OrderCoffeeSteps { CoffeeShop coffeeShop = new CoffeeShop(); Order order; + @ParameterType("\"[^\"]*\"") + public Order order(String orderedProduct) { + return Order.of(1, orderedProduct).forCustomer(cathy); + } + @Given("{} is a CaffeinateMe customer") public void a_caffeinate_me_customer_named(String customerName) { cathy = coffeeShop.registerNewCustomer(customerName); @@ -26,16 +32,16 @@ public void cathyIsMetresFromTheCoffeeShop(Integer arg0) { cathy.setDistanceFromShop(arg0); } - @When("Cathy orders a {string}") - public void cathy_orders_a(String orderedProduct) { - this.order = Order.of(1, orderedProduct).forCustomer(cathy); + @When("Cathy orders a {order}") + public void cathy_orders_a(Order order) { + this.order = order; cathy.placesAnOrderFor(order).at(coffeeShop); } - @When("Cathy orders a {string} with a comment {string}") - public void cathy_orders_with_comment(String orderedProduct, String comment) { - this.order = Order.of(1, orderedProduct).forCustomer(cathy).withComment(comment); - cathy.placesAnOrderFor(order).at(coffeeShop); + @When("Cathy orders a {order} with a comment {string}") + public void cathy_orders_with_comment(Order order, String comment) { + this.order = order.withComment(comment); + cathy.placesAnOrderFor(this.order).at(coffeeShop); } @Then("Barry should receive the order") From e1983afd1e2a854d7e384518ef83caebcdf189be Mon Sep 17 00:00:00 2001 From: evela Date: Tue, 23 Jan 2024 15:15:34 +0100 Subject: [PATCH 11/12] Exercise 6: Step Definitions for Tabular Data --- .../java/caffeinateme/model/CoffeeShop.java | 5 ++++ src/main/java/caffeinateme/model/Order.java | 18 +++++++++++++ .../java/caffeinateme/model/OrderItem.java | 27 +++++++++++++++++++ .../caffeinateme/steps/OrderCoffeeSteps.java | 25 ++++++++++++++--- .../features/orders/order_a_coffee.feature | 10 +++++++ 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/main/java/caffeinateme/model/OrderItem.java diff --git a/src/main/java/caffeinateme/model/CoffeeShop.java b/src/main/java/caffeinateme/model/CoffeeShop.java index 0ca8382..ba399d2 100644 --- a/src/main/java/caffeinateme/model/CoffeeShop.java +++ b/src/main/java/caffeinateme/model/CoffeeShop.java @@ -7,6 +7,11 @@ public class CoffeeShop { private Queue orders = new LinkedList<>(); private List customers = new ArrayList<>(); + public void placeOrder(Order order) { + int DEFAULT_DISTANCE = 300; + placeOrder(order, DEFAULT_DISTANCE); + } + public void placeOrder(Order order, int distanceInMetres) { if (distanceInMetres <= 200) { order = order.withStatus(OrderStatus.Urgent); diff --git a/src/main/java/caffeinateme/model/Order.java b/src/main/java/caffeinateme/model/Order.java index b9f94be..a625c4f 100644 --- a/src/main/java/caffeinateme/model/Order.java +++ b/src/main/java/caffeinateme/model/Order.java @@ -1,5 +1,6 @@ package caffeinateme.model; +import java.util.List; import java.util.Objects; public class Order { @@ -7,6 +8,7 @@ public class Order { private final String comment; private final String product; private final Customer customer; + private final List items; private final OrderStatus status; public Order(int quantity, String product, Customer customer) { @@ -20,11 +22,27 @@ public Order(int quantity, String product, Customer customer, String comment) { public Order(int quantity, String product, Customer customer, OrderStatus status, String comment) { this.quantity = quantity; this.product = product; + this.items = null; this.customer = customer; this.status = status; this.comment = comment; } + public Order(List items, Customer customer) { + this(items, customer, OrderStatus.Normal, ""); + } + + public Order(List items, Customer customer, OrderStatus status, String comment) { + + this.quantity = 0; + this.product = null; + this.items = items; + this.customer = customer; + this.status = status; + this.comment = comment; + + } + public Order withComment(String comment) { return new Order(quantity, product, customer, status, comment); } diff --git a/src/main/java/caffeinateme/model/OrderItem.java b/src/main/java/caffeinateme/model/OrderItem.java new file mode 100644 index 0000000..e9ef435 --- /dev/null +++ b/src/main/java/caffeinateme/model/OrderItem.java @@ -0,0 +1,27 @@ +package caffeinateme.model; + +public class OrderItem { + private final String product; + private final int quantity; + + public OrderItem(String product, int quantity) { + this.product = product; + this.quantity = quantity; + } + + public String getProduct() { + return product; + } + + public int getQuantity() { + return quantity; + } + + @Override + public String toString() { + return "OrderItem{" + + "product='" + product + '\'' + + ", quantity=" + quantity + + '}'; + } +} diff --git a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java index 1de1e40..13187f5 100644 --- a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java +++ b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java @@ -1,15 +1,15 @@ package caffeinateme.steps; -import caffeinateme.model.CoffeeShop; -import caffeinateme.model.Customer; -import caffeinateme.model.Order; -import caffeinateme.model.OrderStatus; +import caffeinateme.model.*; import io.cucumber.java.ParameterType; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; +import java.util.List; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; public class OrderCoffeeSteps { @@ -60,4 +60,21 @@ public void barry_should_know_that_the_order_is(OrderStatus expectedStatus) { .orElseThrow(() -> new AssertionError("No order found!")); assertThat(cathysOrder.getStatus()).isEqualTo(expectedStatus); } + + @When("Cathy places an order for the following items:") + public void cathyPlacesAnOrderForTheFollowingItems(List> orderItemsValues) { + List theList = orderItemsValues.stream() + .map(orderRow -> new OrderItem(orderRow.get("Product"), + Integer.parseInt(orderRow.get("Quantity")))) + .toList(); + + this.order = new Order(theList, cathy); + coffeeShop.placeOrder(this.order); + } + + @And("the order should contain {int} line items") + public void theOrderShouldContainLineItems(int arg0) { + } + + } diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index 64a7ef8..54e5fcd 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -34,3 +34,13 @@ Feature: Order a coffee When Cathy orders a "large cappuccino" with a comment "Double sugar" Then Barry should receive the order And the order should have the comment "Double sugar" + + Rule: Buyers can order many items in the same order + Example: A buyer orders two items in the same order + When Cathy places an order for the following items: + | Product | Quantity | + | Large cappuccino | 1 | + | Espresso | 2 | + Then Barry should receive the order + And the order should contain 2 line items + From 3dd9fa039a6dad02f5349d5b3421de1e7a78bf39 Mon Sep 17 00:00:00 2001 From: evela Date: Tue, 23 Jan 2024 16:54:22 +0100 Subject: [PATCH 12/12] Exercise 7-8: Parameter Types For Data Tables --- src/main/java/caffeinateme/model/Order.java | 4 ++++ .../caffeinateme/steps/OrderCoffeeSteps.java | 19 ++++++++++++++----- .../features/orders/order_a_coffee.feature | 4 +++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/caffeinateme/model/Order.java b/src/main/java/caffeinateme/model/Order.java index a625c4f..1dd5899 100644 --- a/src/main/java/caffeinateme/model/Order.java +++ b/src/main/java/caffeinateme/model/Order.java @@ -73,6 +73,10 @@ public static OrderBuilder of(int quantity, String product) { return new OrderBuilder(quantity, product); } + public List getItems() { + return items; + } + public static class OrderBuilder { private final int quantity; diff --git a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java index 13187f5..4754b67 100644 --- a/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java +++ b/src/test/java/caffeinateme/steps/OrderCoffeeSteps.java @@ -1,6 +1,7 @@ package caffeinateme.steps; import caffeinateme.model.*; +import io.cucumber.java.DataTableType; import io.cucumber.java.ParameterType; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; @@ -23,6 +24,11 @@ public Order order(String orderedProduct) { return Order.of(1, orderedProduct).forCustomer(cathy); } + @DataTableType + public OrderItem orderItem(Map row) { + return new OrderItem(row.get("Product"), Integer.parseInt(row.get("Quantity"))); + } + @Given("{} is a CaffeinateMe customer") public void a_caffeinate_me_customer_named(String customerName) { cathy = coffeeShop.registerNewCustomer(customerName); @@ -62,11 +68,7 @@ public void barry_should_know_that_the_order_is(OrderStatus expectedStatus) { } @When("Cathy places an order for the following items:") - public void cathyPlacesAnOrderForTheFollowingItems(List> orderItemsValues) { - List theList = orderItemsValues.stream() - .map(orderRow -> new OrderItem(orderRow.get("Product"), - Integer.parseInt(orderRow.get("Quantity")))) - .toList(); + public void cathyPlacesAnOrderForTheFollowingItems(List theList) { this.order = new Order(theList, cathy); coffeeShop.placeOrder(this.order); @@ -76,5 +78,12 @@ public void cathyPlacesAnOrderForTheFollowingItems(List> ord public void theOrderShouldContainLineItems(int arg0) { } + @And("the order should contain the following products:") + public void theOrderShouldContain(List expectedProducts) { + Order order = coffeeShop.getOrderFor(cathy).get(); + List productItems = order.getItems().stream().map(OrderItem::getProduct).toList(); + assertThat(productItems).hasSameElementsAs(expectedProducts); + } + } diff --git a/src/test/resources/features/orders/order_a_coffee.feature b/src/test/resources/features/orders/order_a_coffee.feature index 54e5fcd..6a3f8d3 100644 --- a/src/test/resources/features/orders/order_a_coffee.feature +++ b/src/test/resources/features/orders/order_a_coffee.feature @@ -43,4 +43,6 @@ Feature: Order a coffee | Espresso | 2 | Then Barry should receive the order And the order should contain 2 line items - + And the order should contain the following products: + | Large cappuccino | + | Espresso | \ No newline at end of file