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

Commit 76810f6

Browse files
author
Félix Carpena
committed
cover untested feature (step3)
1 parent cb9a272 commit 76810f6

4 files changed

Lines changed: 88 additions & 18 deletions

File tree

src/main/java/group/rohlik/application/CartDiscountAdder.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import lombok.AllArgsConstructor;
88
import org.springframework.stereotype.Service;
99
import org.springframework.transaction.annotation.Transactional;
10-
import org.springframework.util.Assert;
1110

1211
@AllArgsConstructor
1312
@Service
@@ -20,15 +19,8 @@ public class CartDiscountAdder {
2019
public void add(long cartId, String code) {
2120
Cart cart = cartRepository.findById(cartId).orElseThrow();
2221
Discount discount = discountRepository.findByCode(code).orElseThrow();
23-
applyDiscount(cart, discount);
22+
cart.applyDiscount(discount);
2423

2524
cartRepository.save(cart);
2625
}
27-
28-
public void applyDiscount(Cart cart, Discount discount) {
29-
//@todo suspicious code - should we move this logic to cart entity?
30-
Assert.isTrue(!cart.getDiscounts().contains(discount), "Discount already applied");
31-
32-
cart.getDiscounts().add(discount);
33-
}
3426
}

src/main/java/group/rohlik/entity/Cart.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ private double totalDiscountsPrice() {
8282
}
8383

8484
public double totalPrice() {
85-
return BigDecimal
86-
.valueOf(totalLinesPrice() - totalDiscountsPrice())
87-
.setScale(2, RoundingMode.CEILING)
88-
.doubleValue();
85+
return Math.max(
86+
0,
87+
BigDecimal
88+
.valueOf(totalLinesPrice() - totalDiscountsPrice())
89+
.setScale(2, RoundingMode.CEILING)
90+
.doubleValue()
91+
);
8992
}
9093

9194
public Integer quantityOfProduct(String sku) {
@@ -96,4 +99,16 @@ public Integer quantityOfProduct(String sku) {
9699
.map(CartLine::getQuantity)
97100
.orElse(0);
98101
}
102+
103+
public boolean hasDiscount(String name) {
104+
return discounts
105+
.stream()
106+
.anyMatch(line -> line.getName().equals(name));
107+
}
108+
109+
public void applyDiscount(Discount discount) {
110+
Assert.isTrue(!discounts.contains(discount), "Discount already applied");
111+
112+
discounts.add(discount);
113+
}
99114
}

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package group.rohlik.acceptance.steps;
22

33
import group.rohlik.entity.Cart;
4-
import group.rohlik.entity.CartLine;
54
import group.rohlik.entity.CartRepository;
6-
import group.rohlik.entity.Discount;
75
import io.cucumber.gherkin.internal.com.eclipsesource.json.JsonObject;
86
import io.cucumber.java.Before;
97
import io.cucumber.java.en.Given;
@@ -17,9 +15,6 @@
1715
import org.springframework.http.HttpMethod;
1816
import org.springframework.http.MediaType;
1917

20-
import java.math.BigDecimal;
21-
import java.math.RoundingMode;
22-
2318

2419
@RequiredArgsConstructor
2520
public class CartSteps {
@@ -83,6 +78,35 @@ public void thereShouldNotBeProductInCart(String sku) {
8378
Assertions.assertTrue(cart.quantityOfProduct(sku) == 0, String.format("Product %s should not be present", sku));
8479
}
8580

81+
@Then("there should be discount {string} in my cart")
82+
public void thereShouldBeDiscountInCart(String name) {
83+
Cart cart = currentCart();
84+
85+
Assertions.assertTrue(cart.hasDiscount(name), String.format("Discount %s should be present", name));
86+
}
87+
88+
@Then("there shouldn't be discounts in my cart")
89+
public void thereShouldNotBeDiscountsInCart() {
90+
Cart cart = currentCart();
91+
92+
Assertions.assertEquals(0, cart.getDiscounts().size());
93+
}
94+
95+
@When("I apply {string} discount to my cart")
96+
public void iApplyDiscountToMyCart(String code) {
97+
JsonObject body = new JsonObject();
98+
body.add("code", code);
99+
HttpHeaders headers = new HttpHeaders();
100+
headers.setContentType(MediaType.APPLICATION_JSON);
101+
102+
template.exchange(
103+
String.format("/carts/%d/discounts", currentCartId),
104+
HttpMethod.POST,
105+
new HttpEntity<>(body.toString(), headers),
106+
String.class
107+
);
108+
}
109+
86110
private Cart currentCart() {
87111
return cartRepository.findById(currentCartId).orElseThrow();
88112
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Feature: Add discount to cart
2+
As a Rohlik customer
3+
I want to apply discounts to my cart
4+
so that I can save some money
5+
6+
Background:
7+
Given the following products exist:
8+
| sku | name | price |
9+
| 001 | potatoes | 2.5 |
10+
| 002 | water | 0.95 |
11+
And there is a cart discount "free shipping" for 1 euros with code "FREE-SHIPPING"
12+
And there is a cart discount "special offer" for 10 euros with code "SPECIAL-OFFER"
13+
And I have a cart
14+
15+
Scenario: No discounts
16+
When I add 2 units of product "001" to my cart
17+
Then the cart's total cost should be 5.0 euros
18+
But there shouldn't be discounts in my cart
19+
20+
Scenario: Add discount
21+
Given I add 2 units of product "001" to my cart
22+
When I apply "FREE-SHIPPING" discount to my cart
23+
Then the cart's total cost should be 4.0 euros
24+
And there should be discount "free shipping" in my cart
25+
26+
Scenario: Add discount only apply once
27+
Given I add 2 units of product "001" to my cart
28+
And I apply "FREE-SHIPPING" discount to my cart
29+
When I apply "FREE-SHIPPING" discount to my cart
30+
Then the cart's total cost should be 4.0 euros
31+
And there should be discount "free shipping" in my cart
32+
33+
Scenario: Cart total should be always positive
34+
Given I add 2 units of product "001" to my cart
35+
And I apply "FREE-SHIPPING" discount to my cart
36+
When I apply "SPECIAL-OFFER" discount to my cart
37+
Then the cart's total cost should be 0.0 euros
38+
And there should be discount "free shipping" in my cart
39+
And there should be discount "special offer" in my cart

0 commit comments

Comments
 (0)