Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ernestomv - Exercise 1 #14

Open
wants to merge 12 commits into
base: start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions src/main/java/caffeinateme/model/CoffeeShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
public class CoffeeShop {

private Queue<Order> orders = new LinkedList<>();
private List<Customer> 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);
}
orders.add(order);
}

Expand All @@ -19,4 +28,11 @@ public Optional<Order> 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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/caffeinateme/model/Order.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package caffeinateme.model;

import java.util.List;
import java.util.Objects;

public class Order {
private final int quantity;
private final String comment;
private final String product;
private final Customer customer;
private final List<OrderItem> items;
private final OrderStatus status;

public Order(int quantity, String product, Customer customer) {
Expand All @@ -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<OrderItem> items, Customer customer) {
this(items, customer, OrderStatus.Normal, "");
}

public Order(List<OrderItem> 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);
}
Expand Down Expand Up @@ -55,6 +73,10 @@ public static OrderBuilder of(int quantity, String product) {
return new OrderBuilder(quantity, product);
}

public List<OrderItem> getItems() {
return items;
}

public static class OrderBuilder {

private final int quantity;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/caffeinateme/model/OrderItem.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
89 changes: 89 additions & 0 deletions src/test/java/caffeinateme/steps/OrderCoffeeSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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;
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 {

Customer cathy = Customer.named("Cathy");
CoffeeShop coffeeShop = new CoffeeShop();
Order order;

@ParameterType("\"[^\"]*\"")
public Order order(String orderedProduct) {
return Order.of(1, orderedProduct).forCustomer(cathy);
}

@DataTableType
public OrderItem orderItem(Map<String, String> 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);
}
@Given("Cathy is {int} metres from the coffee shop")
public void cathyIsMetresFromTheCoffeeShop(Integer arg0) {
cathy.setDistanceFromShop(arg0);
}

@When("Cathy orders a {order}")
public void cathy_orders_a(Order order) {
this.order = order;
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")
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)
.orElseThrow(() -> new AssertionError("No order found!"));
assertThat(cathysOrder.getStatus()).isEqualTo(expectedStatus);
}

@When("Cathy places an order for the following items:")
public void cathyPlacesAnOrderForTheFollowingItems(List<OrderItem> theList) {

this.order = new Order(theList, cathy);
coffeeShop.placeOrder(this.order);
}

@And("the order should contain {int} line items")
public void theOrderShouldContainLineItems(int arg0) {
}

@And("the order should contain the following products:")
public void theOrderShouldContain(List<String> expectedProducts) {
Order order = coffeeShop.getOrderFor(cathy).get();
List<String> productItems = order.getItems().stream().map(OrderItem::getProduct).toList();
assertThat(productItems).hasSameElementsAs(expectedProducts);
}


}
47 changes: 42 additions & 5 deletions src/test/resources/features/orders/order_a_coffee.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,45 @@ 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
Background:
Given Cathy is a CaffeinateMe customer

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"

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
And the order should contain the following products:
| Large cappuccino |
| Espresso |