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

Implementing Cucumber Scenario. Exercise 1 #2

Open
wants to merge 2 commits into
base: master
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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.1</version>
<scope>test</scope>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful, you have this dependency twice - you only need the second one.

</dependencies>
<build>
<plugins>
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/caffeinateme/Barrista_NB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package caffeinateme;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Barrista_NB {
List<Order_NB> receivedOrders= new ArrayList<>() ;
public void addToReceivedOrders(Order_NB order)
{
this.receivedOrders.add(order);
}

public List<Order_NB> getPendingOrders() {
return receivedOrders;
}

public List<Order_NB> getUrgentOrders() {
List<Order_NB> results = new ArrayList<>();
for(int i=0;i<receivedOrders.size();i++) {
if (receivedOrders.get(i).getOrderStatus()=="Urgent")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When comparing Strings in Java, don't forget to use the equals() method, e.g. if (receivedOrders.get(i).getOrderStatus().equals("Urgent"))

results.add(receivedOrders.get(i));
}
return results;
}


public String getCathysOrderStatus()
{
int i;
for( i=0;i<receivedOrders.size();i++) {
if(receivedOrders.get(i).getOrderedCustomerName() .equals("cathy"))
{
break;
}
}
return receivedOrders.get(i).getOrderStatus();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good but you'll learn easier ways to write this code later on in the course.

}

}

32 changes: 32 additions & 0 deletions src/main/java/caffeinateme/Customer_NB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package caffeinateme;
public class Customer_NB {

private String name;
private int distanceFromShop;

public Customer_NB (String customerName)
{
this.name=customerName;
}

public void setDistanceFromShop(int distanceFromShop) {
this.distanceFromShop = distanceFromShop;
}

public Integer getDistanceFromShop()
{
return distanceFromShop;
}

public Order_NB placeOrderFor(String orderedProduct)
{
Order_NB order = new Order_NB(this,orderedProduct);
return order;
}

public String getCustomerName()
{
return this.name;
}
}

12 changes: 12 additions & 0 deletions src/main/java/caffeinateme/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ public class Order {
private final int quantity;
private final String product;
private int etaInMinutes;
private String orderStatus;


public Order(long customerId, int quantity, String product) {

this.customerId = customerId;
this.quantity = quantity;
this.product = product;
this.orderStatus = "Pending";
}

public OrderReceipt getReceipt() {
Expand All @@ -29,6 +32,15 @@ public String getProduct() {
return product;
}

public void completeOrder()
{
this.orderStatus="Complete";
}
public String getOrderStatus()
{
return orderStatus;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/caffeinateme/Order_NB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package caffeinateme;

public class Order_NB {
private final Customer_NB customer;
private String orderedItem ;
private String orderStatus;
public Order_NB (Customer_NB customer, String orderedProduct)
{
this.customer=customer;
this.orderedItem=orderedProduct;
this.orderStatus=setOrderStatus();
}

private String setOrderStatus() {
String status;
if(this.customer.getDistanceFromShop() < 101)
{
status= "Urgent";
}
else
{
status ="Normal";
}
return status;
}

public String getOrderStatus()
{
return this.orderStatus;
}

public String getOrderedCustomerName()
{
return this.customer.getCustomerName();
}


}


22 changes: 22 additions & 0 deletions src/test/java/caffeinateme/OrderACoffeeStepDefinitions.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void sheOrdersA(String order) throws Throwable {
Serenity.setSessionVariable("orderReceipt").to(orderReceipt);
}


@Then("^Barry should receive the order$")
public void barryShouldReceiveTheOrder() throws Throwable {
assertThat(barry.pendingOrders()).contains(Order.matching(orderReceipt));
Expand Down Expand Up @@ -99,4 +100,25 @@ public ReceiptLineItem convertToReceiptLineItem(Map<String, String> receiptLineI
public void theReceiptShouldContainTheLineItems(List<ReceiptLineItem> expectedLineItems) throws Throwable {
assertThat(receipt.getLineItems()).containsExactlyElementsOf(expectedLineItems);
}

@Given("^(.*) is a barista")
public void barry_is_a_barista(String name) {
barry.isCalled(name);
}

@When("^Barry marks the order as (.*)")
public void barry_marks_the_order_as_complete(String orderStatus) {
barry.completeOrder(orderReceipt);
}

@Then("the order should no longer appear in the pending orders")
public void the_order_should_no_longer_appear_in_the_pending_orders() {
assertThat(barry.pendingOrders()).doesNotContain(Order.matching(orderReceipt));
}

@Then("Sarah should see the order in her order history")
public void sarah_should_see_the_order_in_her_order_history() {
assertThat(customer.getOrderHistoryFor(orderReceipt.getCustomerId())).contains(Order.matching(orderReceipt));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work

}
43 changes: 43 additions & 0 deletions src/test/java/caffeinateme/OrderCoffeeSteps_NB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package caffeinateme;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;


public class OrderCoffeeSteps_NB {
Customer_NB customer = new Customer_NB("cathy");
Barrista_NB barry = new Barrista_NB();
Order_NB order;


@Given("^Cathy is (\\d+) meters from the coffee shop$")
public void cathy_is_n_meters_from_the_coffee_shop(int distanceInMeters) throws Throwable {
customer.setDistanceFromShop(distanceInMeters);
assertThat(customer.getDistanceFromShop()).isEqualTo(distanceInMeters);
}

@When("^Cathy orders a (.*)$")
public void cathy_orders_a(String orderedProduct) throws Throwable {
order= customer.placeOrderFor(orderedProduct);
assertThat(order.getOrderedCustomerName()).isEqualTo("cathy");
}

@Then("^Barry should receive the order$")
public void barry_should_receive_the_order() throws Throwable {
order= customer.placeOrderFor("Large Cappuccino");
barry.addToReceivedOrders(order);
assertThat(barry.getPendingOrders(),hasItem(order));
}

@Then("^Barry should know that the coffee is (.*)$")
public void barry_should_know_that_the_coffee_is_Urgent(String orderStatus) throws Throwable {
order= customer.placeOrderFor("Large Cappuccino");
assertThat(barry.getCathysOrderStatus()).isEqualTo(orderStatus);
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class UserRegistrationStepDefinitions {
@Steps(shared = true)
Customer customer;

@Given("^(.*) has a Caffeinate-Me account")
@Given("^(.*) (?:has|is) a (?:Caffeinate-Me account|registered customer)")
public void userHasACaffeinateMeAccount(String userName) {
userRegistrations.registerUser(customer);
customer.isCalled(userName);
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/caffeinateme/steps/Barista.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package caffeinateme.steps;

import caffeinateme.Order;
import caffeinateme.OrderReceipt;
import net.serenitybdd.core.steps.ScenarioActor;
import net.thucydides.core.annotations.Steps;

import java.util.List;
import java.util.stream.Collectors;

public class Barista extends ScenarioActor {

@Steps(shared = true)
CoffeeOrdersClient coffeeOrders;

public List<Order> pendingOrders() {
return coffeeOrders.getOrders();
return coffeeOrders.getOrders().stream()
.filter(order -> order.getOrderStatus() != "Complete")
.collect(Collectors.toList());
}

public void completeOrder(OrderReceipt orderReceipt) {
coffeeOrders.getOrders().stream().filter(order -> order.getCustomerId()==orderReceipt.getCustomerId())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work if customerId was a number, but if it is a string it is better to use equals()

.forEach(order ->order.completeOrder());
// coffeeOrders.getOrders().stream().filter(order -> order.getCustomerId()==orderReceipt.getCustomerId())
//.forEach(order -> coffeeOrders.setOrderHistory(order));
//coffeeOrders.getOrders().removeIf(order -> order.getCustomerId() == orderReceipt.getCustomerId());
}
}
10 changes: 10 additions & 0 deletions src/test/java/caffeinateme/steps/CoffeeOrdersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CoffeeOrdersClient {

@Steps(shared = true)
ProductCatalog productCatalog;

List<Order> orders = new ArrayList<>();
List<Order> orderHistory = new ArrayList<>();

@Step("Place order for customer {0} for {1} x {2}")
public OrderReceipt placeOrder(long customerId, int quantity, String product) {
Expand Down Expand Up @@ -65,4 +67,12 @@ private double roundedTo2DecimalPlaces(double value) {
private double subtotalFor(Order order) {
return productCatalog.priceOf(order.getProduct()) * order.getQuantity();
}

public List<Order> getOrderHistory(long customerId){
return orders.stream().filter(order -> order.getCustomerId()==customerId)
.filter(order -> order.getOrderStatus()=="Complete")
.collect(Collectors.toList());
}


}
9 changes: 9 additions & 0 deletions src/test/java/caffeinateme/steps/Customer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package caffeinateme.steps;

import caffeinateme.Order;
import caffeinateme.OrderReceipt;
import caffeinateme.Receipt;
import net.serenitybdd.core.steps.ScenarioActor;
import net.thucydides.core.annotations.Step;
import net.thucydides.core.annotations.Steps;

import java.util.List;
import java.util.stream.Collectors;

public class Customer extends ScenarioActor {

private long customerId;
Expand Down Expand Up @@ -35,4 +39,9 @@ public void updatesHerETATo(int minutesAway) {
public Receipt requestsAReceipt() {
return coffeeOrders.getReceiptFor(customerId);
}

public List<Order> getOrderHistoryFor(long customerId)
{
return coffeeOrders.getOrderHistory(customerId);
}
}