-
Notifications
You must be signed in to change notification settings - Fork 67
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
} | ||
|
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; | ||
} | ||
} | ||
|
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(); | ||
} | ||
|
||
|
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
|
@@ -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)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work |
||
} |
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 |
---|---|---|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} |
There was a problem hiding this comment.
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.