From bde6c43bcd1c9baedbd363d74c75906fa00b0a0e Mon Sep 17 00:00:00 2001 From: Joep de Jong Date: Sat, 4 May 2024 19:11:26 +0200 Subject: [PATCH 1/3] Add payment method tests --- .../core/model/order/PaymentMethodTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/ch/wisv/events/core/model/order/PaymentMethodTest.java diff --git a/src/test/java/ch/wisv/events/core/model/order/PaymentMethodTest.java b/src/test/java/ch/wisv/events/core/model/order/PaymentMethodTest.java new file mode 100644 index 00000000..8bbe0fee --- /dev/null +++ b/src/test/java/ch/wisv/events/core/model/order/PaymentMethodTest.java @@ -0,0 +1,71 @@ +package ch.wisv.events.core.model.order; + +import ch.wisv.events.core.model.customer.Customer; +import ch.wisv.events.core.model.product.Product; +import ch.wisv.events.core.util.VatRate; +import com.google.common.collect.ImmutableList; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +@RunWith(MockitoJUnitRunner.class) +public class PaymentMethodTest { + + @Test + public void testCalculateCostIncludingTransaction_Cash() { + double cost = 50.0; + PaymentMethod paymentMethod = PaymentMethod.CASH; + + double expected = 50.0; + double actual = paymentMethod.calculateCostIncludingTransaction(cost); + + assertEquals(expected, actual, 0.001); + } + + @Test + public void testCalculateCostIncludingTransaction_Card() { + double cost = 50.0; + PaymentMethod paymentMethod = PaymentMethod.CARD; + + double expected = 50.0; + double actual = paymentMethod.calculateCostIncludingTransaction(cost); + + assertEquals(expected, actual, 0.001); + } + @Test + public void testCalculateCostIncludingTransaction_IDEAL() { + double cost = 50.0; + PaymentMethod paymentMethod = PaymentMethod.IDEAL; + + double expected = 50.35; + double actual = paymentMethod.calculateCostIncludingTransaction(cost); + + assertEquals(expected, actual, 0.001); + } + + @Test + public void testCalculateCostIncludingTransaction_SOFORT() { + double cost = 50.0; + PaymentMethod paymentMethod = PaymentMethod.SOFORT; + + double expected = 50.85; // Rounded 50.847 + double actual = paymentMethod.calculateCostIncludingTransaction(cost); + + assertEquals(expected, actual, 0.001); + } + + @Test + public void testCalculateCostIncludingTransaction_OTHER() { + double cost = 50.0; + PaymentMethod paymentMethod = PaymentMethod.OTHER; + + double expected = 50.0; // Since OTHER method has no transaction cost, the expected value should be same as the cost + double actual = paymentMethod.calculateCostIncludingTransaction(cost); + + assertEquals(expected, actual, 0.001); + } +} From 3b46be034be4a293dad1bed5344b4bb1c684a53b Mon Sep 17 00:00:00 2001 From: Joep de Jong Date: Sat, 4 May 2024 19:14:47 +0200 Subject: [PATCH 2/3] Switch to using lambda functions in PaymentMethod --- .../core/model/order/PaymentMethod.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/main/java/ch/wisv/events/core/model/order/PaymentMethod.java b/src/main/java/ch/wisv/events/core/model/order/PaymentMethod.java index edb464df..882b9118 100644 --- a/src/main/java/ch/wisv/events/core/model/order/PaymentMethod.java +++ b/src/main/java/ch/wisv/events/core/model/order/PaymentMethod.java @@ -1,8 +1,8 @@ package ch.wisv.events.core.model.order; import lombok.Getter; -import net.objecthunter.exp4j.Expression; -import net.objecthunter.exp4j.ExpressionBuilder; + +import java.util.function.Function; /** * Payment method enum. @@ -12,37 +12,37 @@ public enum PaymentMethod { /** * User paid his order with cash. */ - CASH("cash", new ExpressionBuilder("x")), + CASH("cash", cost -> cost), /** * User paid his order by card. */ - CARD("card", new ExpressionBuilder("x")), + CARD("card", cost -> cost), /** * User paid his order via Mollie iDeal. */ - IDEAL("ideal", new ExpressionBuilder("x + 0.35")), + IDEAL("ideal", cost -> cost + 0.35), /** * User paid his order via Mollie SOFORT. */ - SOFORT("sofort", new ExpressionBuilder("1.01089 * x + 0.3025")), + SOFORT("sofort", cost -> 1.01089 * cost + 0.3025), /** * User paid his order via another method. */ - OTHER("other", new ExpressionBuilder("x")); + OTHER("other", cost -> cost); /** gets the name. */ @Getter private final String name; - /** gets the expressionbuilder. */ + /** gets the transaction cost function. */ @Getter - private final ExpressionBuilder transactionCost; + private final Function transactionCost; - PaymentMethod(String name, ExpressionBuilder transactionCost) { + PaymentMethod(String name, Function transactionCost) { this.name = name; this.transactionCost = transactionCost; } @@ -55,11 +55,7 @@ public enum PaymentMethod { * @return double */ public double calculateCostIncludingTransaction(double cost) { - Expression e = this.getTransactionCost().variables("x") - .build() - .setVariable("x", cost); - - return Math.round(e.evaluate() * 100.d) / 100.d; + return Math.round(transactionCost.apply(cost) * 100.d) / 100.d; } /** From 6ccb96ca6cd1837390ecb38699576a3f5081437f Mon Sep 17 00:00:00 2001 From: Joep de Jong Date: Sat, 4 May 2024 19:15:40 +0200 Subject: [PATCH 3/3] Remove exp4j dependency --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index fc073a44..c9fc9ec0 100644 --- a/build.gradle +++ b/build.gradle @@ -60,7 +60,6 @@ dependencies { implementation 'javax.el:javax.el-api:2.2.5' implementation "be.woutschoovaerts:mollie:3.6.1" - implementation"net.objecthunter:exp4j:0.4.8" implementation 'org.thymeleaf:thymeleaf-spring5'