Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.

Commit ca6a468

Browse files
author
Félix Carpena
committed
implementing feature steps (step1)
1 parent ef3afa8 commit ca6a468

4 files changed

Lines changed: 114 additions & 14 deletions

File tree

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,121 @@
11
package group.rohlik.acceptance.steps;
22

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;
39
import io.cucumber.java.en.Given;
410
import io.cucumber.java.en.Then;
511
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;
619

720
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+
837
@Given("I have a cart")
938
public void haveCart() {
10-
SetupSteps.notImplemented();
39+
Cart cart = new Cart();
40+
cartRepository.save(cart);
41+
currentCartId = cart.getId();
1142
}
1243

1344
@When("I add {int} unit(s) of product {string} to my cart")
1445
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+
);
1658
}
1759

1860
@When("I remove product {string} of my cart")
1961
public void removeProductOfMyCart(String sku) {
20-
SetupSteps.notImplemented();
62+
addProductUnitsToMyCart(0, sku);
2163
}
2264

2365
@Then("the cart's total cost should be {float} euro(s)")
2466
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);
2677
}
2778

2879
@Then("there should be {int} unit(s) of product {string} in my cart")
2980
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());
3188
}
3289

3390
@Then("there shouldn't be product {string} in my cart")
3491
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));
3699
}
37100

38101
@Then("there should be discount {long} in my cart")
39102
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));
41110
}
42111

43112
@Then("there shouldn't be discounts in my cart")
44113
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();
46120
}
47121
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package group.rohlik.acceptance.steps;
22

3+
import group.rohlik.entity.Discount;
4+
import group.rohlik.entity.DiscountRepository;
5+
import io.cucumber.java.en.And;
36
import io.cucumber.java.en.Given;
47
import lombok.AllArgsConstructor;
8+
import org.springframework.transaction.annotation.Transactional;
59

610
@AllArgsConstructor
711
public class DiscountSteps {
12+
private final DiscountRepository discountRepository;
13+
814
@Given("there is a cart discount {string} for {float} euros with a minimum total price of {float} euros")
15+
@Transactional
916
public void thereIsACartDiscountWithAMinimumTotalPrice(String name, float amount, float minPrice) {
10-
SetupSteps.notImplemented();
17+
Discount discount = new Discount();
18+
discount.setName(name);
19+
discount.setAmount(amount);
20+
discount.setMinPrice(minPrice);
21+
discountRepository.save(discount);
1122
}
1223
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
package group.rohlik.acceptance.steps;
22

3+
import group.rohlik.entity.Product;
4+
import group.rohlik.entity.ProductRepository;
35
import io.cucumber.datatable.DataTable;
46
import io.cucumber.java.en.Given;
7+
import lombok.AllArgsConstructor;
8+
import org.springframework.transaction.annotation.Transactional;
59

10+
import java.util.Map;
11+
12+
@AllArgsConstructor
613
public class ProductSteps {
14+
private final ProductRepository productRepository;
15+
716
@Given("the following products exist:")
17+
@Transactional
818
public void theFollowingProductsExists(DataTable table) {
9-
SetupSteps.notImplemented();
19+
table
20+
.asMaps(String.class, String.class)
21+
.forEach((Map<String, String> row) -> {
22+
Product product = new Product();
23+
product.setSku(row.get("sku"));
24+
product.setName(row.get("name"));
25+
product.setPrice(Float.parseFloat(row.get("price")));
26+
productRepository.save(product);
27+
});
1028
}
1129
}

src/test/java/group/rohlik/acceptance/steps/SetupSteps.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
@AllArgsConstructor
1212
public class SetupSteps {
13+
1314
@PersistenceContext
1415
private EntityManager em;
1516

@@ -28,8 +29,4 @@ public void truncateDB() {
2829
private void executeUpdate(String sql) {
2930
em.createNativeQuery(sql).executeUpdate();
3031
}
31-
32-
public static void notImplemented() {
33-
throw new RuntimeException("Step method not implemented");
34-
}
3532
}

0 commit comments

Comments
 (0)