|
1 | 1 | package group.rohlik.acceptance.steps; |
2 | 2 |
|
| 3 | +import group.rohlik.entity.Cart; |
| 4 | +import group.rohlik.entity.CartLine; |
| 5 | +import group.rohlik.entity.CartRepository; |
| 6 | +import group.rohlik.entity.Discount; |
| 7 | +import io.cucumber.gherkin.internal.com.eclipsesource.json.JsonObject; |
| 8 | +import io.cucumber.java.Before; |
3 | 9 | import io.cucumber.java.en.Given; |
4 | 10 | import io.cucumber.java.en.Then; |
5 | 11 | import io.cucumber.java.en.When; |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 15 | +import org.springframework.http.HttpEntity; |
| 16 | +import org.springframework.http.HttpHeaders; |
| 17 | +import org.springframework.http.HttpMethod; |
| 18 | +import org.springframework.http.MediaType; |
6 | 19 |
|
7 | 20 | public class CartSteps { |
| 21 | + |
| 22 | + private final CartRepository cartRepository; |
| 23 | + private final TestRestTemplate template; |
| 24 | + private Long currentCartId; |
| 25 | + |
| 26 | + @Autowired |
| 27 | + public CartSteps(CartRepository cartRepository, TestRestTemplate template) { |
| 28 | + this.cartRepository = cartRepository; |
| 29 | + this.template = template; |
| 30 | + } |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + currentCartId = null; |
| 35 | + } |
| 36 | + |
8 | 37 | @Given("I have a cart") |
9 | 38 | public void haveCart() { |
10 | | - SetupSteps.notImplemented(); |
| 39 | + Cart cart = new Cart(); |
| 40 | + cartRepository.save(cart); |
| 41 | + currentCartId = cart.getId(); |
11 | 42 | } |
12 | 43 |
|
13 | 44 | @When("I add {int} unit(s) of product {string} to my cart") |
14 | 45 | public void addProductUnitsToMyCart(int quantity, String sku) { |
15 | | - SetupSteps.notImplemented(); |
| 46 | + JsonObject body = new JsonObject(); |
| 47 | + body.add("sku", sku); |
| 48 | + body.add("quantity", quantity); |
| 49 | + HttpHeaders headers = new HttpHeaders(); |
| 50 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 51 | + |
| 52 | + template.exchange( |
| 53 | + String.format("/carts/%d/lines", currentCartId), |
| 54 | + HttpMethod.POST, |
| 55 | + new HttpEntity<>(body.toString(), headers), |
| 56 | + String.class |
| 57 | + ); |
16 | 58 | } |
17 | 59 |
|
18 | 60 | @When("I remove product {string} of my cart") |
19 | 61 | public void removeProductOfMyCart(String sku) { |
20 | | - SetupSteps.notImplemented(); |
| 62 | + addProductUnitsToMyCart(0, sku); |
21 | 63 | } |
22 | 64 |
|
23 | 65 | @Then("the cart's total cost should be {float} euro(s)") |
24 | 66 | public void cartTotalCost(float amount) { |
25 | | - SetupSteps.notImplemented(); |
| 67 | + Cart cart = currentCart(); |
| 68 | + double totalProducts = cart |
| 69 | + .getLines() |
| 70 | + .stream() |
| 71 | + .mapToDouble(currentCartLine -> currentCartLine.getQuantity() * currentCartLine.getProduct().getPrice()) |
| 72 | + .sum(); |
| 73 | + double totalDiscounts = cart.getDiscounts().stream().mapToDouble(Discount::getAmount).sum(); |
| 74 | + double totalPrice = totalProducts - totalDiscounts; |
| 75 | + |
| 76 | + Assertions.assertEquals(Math.round(totalPrice * 100f) / 100f, amount); |
26 | 77 | } |
27 | 78 |
|
28 | 79 | @Then("there should be {int} unit(s) of product {string} in my cart") |
29 | 80 | public void thereShouldBeProductUnitsInMyCart(int quantity, String sku) { |
30 | | - SetupSteps.notImplemented(); |
| 81 | + Cart cart = currentCart(); |
| 82 | + CartLine cartLine = cart.getLines() |
| 83 | + .stream() |
| 84 | + .filter(currentCartLine -> currentCartLine.getProduct().getSku().equals(sku)) |
| 85 | + .findFirst() |
| 86 | + .orElseThrow(); |
| 87 | + Assertions.assertEquals(quantity, cartLine.getQuantity()); |
31 | 88 | } |
32 | 89 |
|
33 | 90 | @Then("there shouldn't be product {string} in my cart") |
34 | 91 | public void thereShouldNotBeProductInCart(String sku) { |
35 | | - SetupSteps.notImplemented(); |
| 92 | + Cart cart = currentCart(); |
| 93 | + CartLine cartLine = cart.getLines() |
| 94 | + .stream() |
| 95 | + .filter(currentCartLine -> currentCartLine.getProduct().getSku().equals(sku)) |
| 96 | + .findFirst() |
| 97 | + .orElse(null); |
| 98 | + Assertions.assertNull(cartLine, String.format("Product %s should not be present", sku)); |
36 | 99 | } |
37 | 100 |
|
38 | 101 | @Then("there should be discount {long} in my cart") |
39 | 102 | public void thereShouldBeDiscountInCart(long discountId) { |
40 | | - SetupSteps.notImplemented(); |
| 103 | + Cart cart = currentCart(); |
| 104 | + Discount discount = cart.getDiscounts() |
| 105 | + .stream() |
| 106 | + .filter(currentDiscount -> currentDiscount.getId() == discountId) |
| 107 | + .findFirst() |
| 108 | + .orElse(null); |
| 109 | + Assertions.assertNotNull(discount, String.format("Discount %s should be present", discountId)); |
41 | 110 | } |
42 | 111 |
|
43 | 112 | @Then("there shouldn't be discounts in my cart") |
44 | 113 | public void thereShouldNotBeDiscountsInCart() { |
45 | | - SetupSteps.notImplemented(); |
| 114 | + Cart cart = currentCart(); |
| 115 | + Assertions.assertEquals(0, cart.getDiscounts().size()); |
| 116 | + } |
| 117 | + |
| 118 | + private Cart currentCart() { |
| 119 | + return cartRepository.findById(currentCartId).orElseThrow(); |
46 | 120 | } |
47 | 121 | } |
0 commit comments