|
| 1 | +package com.wb3tech.kernel.valueobject; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.DisplayName; |
| 5 | +import org.junit.jupiter.api.Tag; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +@DisplayName("Kernel - Value Object - Money") |
| 11 | +public class MoneyTests { |
| 12 | + |
| 13 | + |
| 14 | + @Test @Tag("Small") @DisplayName("Should create a default money object with about equal to 00.00 and currency of 'USD'") |
| 15 | + public void DefaultMoney() { |
| 16 | + var money = Money.Of("USD"); |
| 17 | + assertEquals(00.00, money.getAmount()); |
| 18 | + assertEquals("USD", money.getCurrency()); |
| 19 | + } |
| 20 | + |
| 21 | + @Test @Tag("Small") @DisplayName("Should throw an IllegalArgumentException " + |
| 22 | + "with message 'You must provide a valid currency.'") |
| 23 | + public void ThrowIllegalArgumentExceptionForNullCurrency() { |
| 24 | + var exception = assertThrows(IllegalArgumentException.class, () -> Money.Of(null)); |
| 25 | + Assertions.assertEquals("You must provide a valid currency.", exception.getMessage()); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + @Test @Tag("Small") @DisplayName("Should create Money object with 15.56 as amount and 'USD' as currency.") |
| 30 | + public void CreateMoney() { |
| 31 | + |
| 32 | + var money = Money.Of(15.56, "USD"); |
| 33 | + assertEquals(15.56, money.getAmount()); |
| 34 | + assertEquals("USD", money.getCurrency()); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Test @Tag("Small") @DisplayName("Should create a money object with amount -200") |
| 39 | + public void ShouldCreateNegativeMoney() { |
| 40 | + |
| 41 | + var negTwoHundred = Money.Of(-200, "USD"); |
| 42 | + Assertions.assertEquals(-200, negTwoHundred.getAmount()); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @Test @Tag("Small") @DisplayName("Should create Money object and round 15.566 up to 15.57 as amount and 'USD' as currency.") |
| 47 | + public void CreateMoneyRoundUp() { |
| 48 | + |
| 49 | + var money = Money.Of(15.566, "USD"); |
| 50 | + assertEquals(15.57, money.getAmount()); |
| 51 | + assertEquals("USD", money.getCurrency()); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test @Tag("Small") @DisplayName("Should create Money object and round 15.544 down to 15.54 as amount and 'USD' as currency.") |
| 56 | + public void CreateMoneyRoundDown() { |
| 57 | + |
| 58 | + var money = Money.Of(15.544, "USD"); |
| 59 | + assertEquals(15.54, money.getAmount()); |
| 60 | + assertEquals("USD", money.getCurrency()); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + @Test @Tag("Small") @DisplayName("Should get a Money with amount of 13.33 when you add a money instance of 13.00 " + |
| 65 | + "and a second instance of 0.33") |
| 66 | + public void AddTwoMonies() { |
| 67 | + |
| 68 | + var thirteen = Money.Of(13, "USD"); |
| 69 | + var thirtyThreeCents = Money.Of(.33, "USD"); |
| 70 | + var total = thirteen.Plus(thirtyThreeCents); |
| 71 | + |
| 72 | + assertEquals(13.33, total.getAmount()); |
| 73 | + assertEquals("USD", total.getCurrency()); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @Test @Tag("Small") @DisplayName("Should get a Money with amount of 13.33 when you subtract a money instance of 13.66 " + |
| 78 | + "and a second instance of 0.33") |
| 79 | + public void SubtractTwoMonies() { |
| 80 | + |
| 81 | + var thirteenSixtySix = Money.Of(13.66, "USD"); |
| 82 | + var thirtyThreeCents = Money.Of(.33, "USD"); |
| 83 | + var total = thirteenSixtySix.Minus(thirtyThreeCents); |
| 84 | + |
| 85 | + assertEquals(13.33, total.getAmount()); |
| 86 | + assertEquals("USD", total.getCurrency()); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + @Test @Tag("Small") @DisplayName("Should get a Money with amount of -5 when you subtract a money instance of 10 " + |
| 91 | + "and a second instance o 15") |
| 92 | + public void ShouldGetNegativeMoney() { |
| 93 | + |
| 94 | + var ten = Money.Of(10, "USD"); |
| 95 | + var fifteen = Money.Of(15, "USD"); |
| 96 | + var total = ten.Minus(fifteen); |
| 97 | + |
| 98 | + assertEquals(-5, total.getAmount()); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + @Test @Tag("Small") @DisplayName("Should throw an ArithmeticExceptionn" + |
| 103 | + "with message 'Cannot subtract monies of two different currencies'") |
| 104 | + public void SubtractShouldThrowArithmeticExceptionForNotSameCurrency() { |
| 105 | + |
| 106 | + |
| 107 | + var USDollar = Money.Of(13, "USD"); |
| 108 | + var britishPounds = Money.Of(10, "GBP"); |
| 109 | + |
| 110 | + var exception = assertThrows(ArithmeticException.class, () -> USDollar.Minus(britishPounds)); |
| 111 | + Assertions.assertEquals("Cannot subtract monies of two different currencies.", exception.getMessage()); |
| 112 | + |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + @Test @Tag("Small") @DisplayName("Should throw an ArithmeticException " + |
| 117 | + "with message 'Cannot add monies of two different currencies'") |
| 118 | + public void AddShouldThrowArithmeticExceptionForNotSameCurrency() { |
| 119 | + |
| 120 | + var USDollar = Money.Of(13, "USD"); |
| 121 | + var britishPounds = Money.Of(10, "GBP"); |
| 122 | + |
| 123 | + var exception = assertThrows(ArithmeticException.class, () -> USDollar.Plus(britishPounds)); |
| 124 | + Assertions.assertEquals("Cannot add monies of two different currencies.", exception.getMessage()); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + @Test @Tag("Small") @DisplayName("A money instance of $1 should equal the second instance of $1") |
| 129 | + public void ShouldEqual() { |
| 130 | + |
| 131 | + var firstDollarInstance = Money.Of(1, "USD"); |
| 132 | + var secondDollarInstance = Money.Of(1, "USD"); |
| 133 | + |
| 134 | + assertTrue(firstDollarInstance.equals(secondDollarInstance)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test @Tag("Small") @DisplayName("A money instance of $1 should not an instnace of 1 GBP") |
| 138 | + public void TwoDifferentCurrenciesShouldNotEqual() { |
| 139 | + |
| 140 | + var oneDollar = Money.Of(1, "USD"); |
| 141 | + var oneGBP = Money.Of(1, "GBP"); |
| 142 | + |
| 143 | + assertFalse(oneDollar.equals(oneGBP)); |
| 144 | + } |
| 145 | + |
| 146 | + @Test @Tag("Small") @DisplayName("A money instance of $1 should equal the second instance of $1") |
| 147 | + public void TwoDifferentAmountsShouldNotEqual() { |
| 148 | + |
| 149 | + var oneDollar = Money.Of(1, "USD"); |
| 150 | + var twoDollar = Money.Of(2, "USD"); |
| 151 | + |
| 152 | + assertFalse(oneDollar.equals(twoDollar)); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments