Add Order tests

main
CPlaiz 2025-12-13 17:59:55 +01:00
parent cf8244ae17
commit 84feb089c0
1 changed files with 92 additions and 0 deletions

View File

@ -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<Product> 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<Product> 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);
}
}