Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.
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
28 changes: 25 additions & 3 deletions src/test/java/group/rohlik/acceptance/steps/CartSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import group.rohlik.entity.Cart;
import group.rohlik.entity.CartLine;
import group.rohlik.entity.CartRepository;
import group.rohlik.entity.Discount;
import io.cucumber.gherkin.internal.com.eclipsesource.json.JsonObject;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
Expand All @@ -16,6 +17,9 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;

import java.math.BigDecimal;
import java.math.RoundingMode;


@RequiredArgsConstructor
public class CartSteps {
Expand Down Expand Up @@ -54,17 +58,35 @@ public void addProductUnitsToMyCart(int quantity, String sku) {

@When("I remove product {string} of my cart")
public void removeProductOfMyCart(String sku) {
SetupSteps.notImplemented();
addProductUnitsToMyCart(0, sku);
}

@Then("the cart's total cost should be {double} euro(s)")
public void cartTotalCost(double amount) {
SetupSteps.notImplemented();
Cart cart = currentCart();
double totalProducts = cart
.getLines()
.stream()
.mapToDouble(currentCartLine -> currentCartLine.getQuantity() * currentCartLine.getProduct().getPrice())
.sum();
double totalDiscounts = cart.getDiscounts().stream().mapToDouble(Discount::getValue).sum();
double totalPrice = totalProducts - totalDiscounts;

Assertions.assertEquals(amount, BigDecimal.valueOf(totalPrice).setScale(2, RoundingMode.CEILING).doubleValue());

}

@Then("there should be {int} unit(s) of product {string} in my cart")
public void thereShouldBeProductUnitsInMyCart(int quantity, String sku) {
SetupSteps.notImplemented();
Cart cart = currentCart();
CartLine cartLine = cart.getLines()
.stream()
.filter(currentCartLine -> currentCartLine.getProduct().getSku().equals(sku))
.findFirst()
.orElse(null);

Assertions.assertNotNull(cartLine, String.format("Product %s not found", sku));
Assertions.assertEquals(quantity, cartLine.getQuantity());
}

@Then("there shouldn't be product {string} in my cart")
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/group/rohlik/acceptance/steps/ProductSteps.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package group.rohlik.acceptance.steps;

import group.rohlik.entity.Product;
import group.rohlik.entity.ProductRepository;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Given;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@AllArgsConstructor
public class ProductSteps {

Expand All @@ -14,6 +17,15 @@ public class ProductSteps {
@Given("the following products exist:")
@Transactional
public void theFollowingProductsExists(DataTable table) {
SetupSteps.notImplemented();
table
.asMaps(String.class, String.class)
.forEach((Map<String, String> row) -> {
Product product = new Product(
row.get("sku"),
row.get("name"),
Double.parseDouble(row.get("price"))
);
productRepository.save(product);
});
}
}