From 84feb089c0a4635eacfacdcaec47e3c68c42ba29 Mon Sep 17 00:00:00 2001 From: CPlaiz Date: Sat, 13 Dec 2025 17:59:55 +0100 Subject: [PATCH] Add Order tests --- src/test/java/OrderTest.java | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/test/java/OrderTest.java diff --git a/src/test/java/OrderTest.java b/src/test/java/OrderTest.java new file mode 100644 index 0000000..b010bc2 --- /dev/null +++ b/src/test/java/OrderTest.java @@ -0,0 +1,92 @@ +import org.example.Constants; +import org.example.Order; +import org.example.Product; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class OrderTest { + + private static final float DELTA = 0.0001f; + + @Test + void calculateTotalPrice() { + Product product = new Product(1, "Produkt 1", 0.5f, 100f, Constants.lowVat); + Order order = new Order(List.of(product)); + + assertEquals(100f, order.calculateTotalPrice(), DELTA); + } + + @Test + void calculateTotalVat() { + Product product = new Product(1, "Produkt 1", 0.5f, 100f, Constants.lowVat); + Order order = new Order(List.of(product)); + + float expectedVat = 100f * Constants.lowVat + order.calculateShippingVat(List.of(product)); + assertEquals(expectedVat, order.calculateTotalVat(), DELTA); + } + + @Test + void calculateShippingCost() { + Product product = new Product(1, "Produkt 1", 0.05f, 60f, Constants.highVat); + Order order = new Order(List.of(product)); + + assertEquals(3.95f, order.calculateShippingCost(), DELTA); + } + + @Test + void calculateShippingCostForExpensiveOrder() { + Product product = new Product(1, "Produkt 1", 2.69f, 5000f, Constants.highVat); + Order order = new Order(List.of(product)); + + assertEquals(0f, order.calculateShippingCost(), DELTA); + } + + @Test + void calculateTotalPriceWithVat() { + Product product1 = new Product(1, "Produkt 1", 0.5f, 200f, Constants.highVat); + Product product2 = new Product(1, "Produkt 2", 0.5f, 200f, Constants.lowVat); + List products = List.of(product1, product2); + Order order = new Order(products); + + float expectedTotal = 200f + (200f * Constants.highVat) + + 200f + (200f * Constants.lowVat) + + 4.95f + order.calculateShippingVat(products); + + assertEquals(expectedTotal, order.calculateTotalPriceWithVat(), DELTA); + } + + @Test + void calculateShippingVat() { + Product lowVatProduct = new Product(1, "Produkt 1", 1.0f, 100f, Constants.lowVat); + Product highVatProduct = new Product(2, "Produkt 2", 1.0f, 300f, Constants.highVat); + + List products = List.of(lowVatProduct, highVatProduct); + + Order order = new Order(List.of(lowVatProduct, highVatProduct)); + + float shippingCost = order.calculateShippingCost(); + float totalPrice = 400f; + + float lowVatPortion = 100f / totalPrice; + float highVatPortion = 300f / totalPrice; + + float expectedShippingVat = shippingCost + - (shippingCost * lowVatPortion / (1 + Constants.lowVat) + + shippingCost * highVatPortion / (1 + Constants.highVat)); + + assertEquals(expectedShippingVat, order.calculateShippingVat(products), DELTA); + } + + @Test + void calculateTotalPriceForMultipleProducts() { + Product product1 = new Product(1, "Produkt 1", 1f, 100f, Constants.lowVat); + Product product2 = new Product(2, "Produkt 2", 2f, 200f, Constants.highVat); + + Order order = new Order(List.of(product1, product2)); + + assertEquals(300f, order.calculateTotalPrice(), DELTA); + } +}