From 9297bc2675c2b1e296903f1d70f59bb0b135a3ea Mon Sep 17 00:00:00 2001 From: straumat Date: Wed, 23 Feb 2022 23:39:28 +0100 Subject: [PATCH] DTO tests #881 --- .../trading/bot/dto/util/GainDTO.java | 2 +- .../bot/test/core/dto/AccountDTOTest.java | 24 +++++++++---------- .../bot/test/core/dto/EqualsBuilderTest.java | 14 ++++++++--- .../bot/test/core/dto/GainDTOTest.java | 19 +++++++-------- .../bot/test/core/dto/OrderDTOTest.java | 2 +- .../dto/PositionCreationResultDTOTest.java | 12 ++++++---- .../test/core/dto/PositionRulesDTOTest.java | 21 +++++++++++----- .../bot/test/core/dto/TickerDTOTest.java | 2 ++ .../bot/test/core/dto/TradeDTOTest.java | 13 ++-------- 9 files changed, 59 insertions(+), 50 deletions(-) diff --git a/spring-boot-starter/autoconfigure/src/main/java/tech/cassandre/trading/bot/dto/util/GainDTO.java b/spring-boot-starter/autoconfigure/src/main/java/tech/cassandre/trading/bot/dto/util/GainDTO.java index be975fa74..c2c995a65 100644 --- a/spring-boot-starter/autoconfigure/src/main/java/tech/cassandre/trading/bot/dto/util/GainDTO.java +++ b/spring-boot-starter/autoconfigure/src/main/java/tech/cassandre/trading/bot/dto/util/GainDTO.java @@ -27,7 +27,7 @@ @SuppressWarnings("checkstyle:VisibilityModifier") public class GainDTO { - /** Zero constant. */ + /** Zero gain constant. */ public static final GainDTO ZERO = GainDTO.builder() .percentage(0) .amount(CurrencyAmountDTO.ZERO) diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/AccountDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/AccountDTOTest.java index cd7b5a08c..ac57a1ee6 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/AccountDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/AccountDTOTest.java @@ -20,22 +20,22 @@ public class AccountDTOTest { @Test @DisplayName("Check equals() on account id & name") - public void checkEqualToForAccountIdAndName() { + public void checkEqualsOnAccountIdAndName() { // Account 1 (null). AccountDTO account1 = AccountDTO.builder().accountId(null).name(null).build(); // Account 2. AccountDTO account2 = AccountDTO.builder().accountId("01").name("01").build(); assertNotEquals(account2, account1); assertNotEquals(account1, account2); - // Account 3 - Same. + // Account 3 - Same as account2. AccountDTO account3 = AccountDTO.builder().accountId("01").name("01").build(); assertEquals(account2, account3); assertEquals(account3, account2); - // Account 4 - id changed. + // Account 4 - id changed compared to account 2. AccountDTO account4 = AccountDTO.builder().accountId("CHANGED").name("01").build(); assertNotEquals(account2, account4); assertNotEquals(account4, account2); - // Account 5 - Name changed. + // Account 5 - Name changed compared to account 2. AccountDTO account5 = AccountDTO.builder().accountId("01").name("CHANGED").build(); assertNotEquals(account2, account5); assertNotEquals(account5, account2); @@ -57,26 +57,26 @@ public void checkEqualToForBalancesList() { assertNotEquals(account2, account1); // Account 3 - One ETH & one BTC. - balances.add(BalanceDTO.builder().build()); - balances.add(BalanceDTO.builder().build()); + balances.add(BalanceDTO.builder().currency(ETH).build()); + balances.add(BalanceDTO.builder().currency(BTC).build()); AccountDTO account3 = AccountDTO.builder().balances(balances).build(); balances.clear(); // Account 4 - One BTC & one ETH (inverted compared to account 3). - balances.add(BalanceDTO.builder().build()); - balances.add(BalanceDTO.builder().build()); + balances.add(BalanceDTO.builder().currency(BTC).build()); + balances.add(BalanceDTO.builder().currency(ETH).build()); AccountDTO account4 = AccountDTO.builder().balances(balances).build(); balances.clear(); assertEquals(account3, account4); assertEquals(account4, account3); // Account 5 - One BTC & one USDT (inverted). - balances.add(BalanceDTO.builder().build()); - balances.add(BalanceDTO.builder().build()); + balances.add(BalanceDTO.builder().currency(BTC).build()); + balances.add(BalanceDTO.builder().currency(USDT).build()); AccountDTO account5 = AccountDTO.builder().balances(balances).build(); balances.clear(); - assertEquals(account4, account5); - assertEquals(account5, account4); + assertNotEquals(account4, account5); + assertNotEquals(account5, account4); } @Test diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/EqualsBuilderTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/EqualsBuilderTest.java index e96d1c2ed..c0bcedac8 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/EqualsBuilderTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/EqualsBuilderTest.java @@ -13,7 +13,7 @@ public class EqualsBuilderTest { @Test - @DisplayName("Check equals on strings") + @DisplayName("Check equals() on strings") public void checkStringEquals() { boolean result; @@ -21,7 +21,7 @@ public void checkStringEquals() { result = new EqualsBuilder().append(null, null).isEquals(); assertTrue(result); - // First object null and second is also not null -> Not equals. + // First object null and second is not null -> Not equals. result = new EqualsBuilder().append(null, "e").isEquals(); assertFalse(result); @@ -57,7 +57,7 @@ public void checkStringEquals() { } @Test - @DisplayName("Check equals on BigDecimal") + @DisplayName("Check equals() on BigDecimal") public void checkBigDecimalEquals() { boolean result; @@ -65,10 +65,18 @@ public void checkBigDecimalEquals() { result = new EqualsBuilder().append(new BigDecimal("1.00000"), null).isEquals(); assertFalse(result); + // BigDecimal & null. + result = new EqualsBuilder().append(null, new BigDecimal("1.00000")).isEquals(); + assertFalse(result); + // Same BigDecimal but with a different format. result = new EqualsBuilder().append(new BigDecimal("1.00000"), new BigDecimal("1")).isEquals(); assertTrue(result); + // Same BigDecimal but with a different format. + result = new EqualsBuilder().append(new BigDecimal("1.10000"), new BigDecimal("1.1")).isEquals(); + assertTrue(result); + // Two different BigDecimals. result = new EqualsBuilder().append(new BigDecimal("1.00000"), new BigDecimal("1.1")).isEquals(); assertFalse(result); diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/GainDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/GainDTOTest.java index 88e1cf425..f7609d13a 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/GainDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/GainDTOTest.java @@ -23,27 +23,23 @@ public class GainDTOTest { @Test @DisplayName("Check toString() method") public void checkToString() { + // Testing zero gain constant toString(). final GainDTO gain1 = GainDTO.ZERO; assertEquals("No gain", gain1.toString()); assertEquals(0, gain1.getOrdersFees().size()); + // We create a complete complex gain. final GainDTO gain2 = GainDTO.builder() .percentage(1) .amount(new CurrencyAmountDTO(new BigDecimal("2"), BTC)) - // Opening order fees. - // 4.6 BTC - // 0.5 ETH - // 3 KCS + // Opening order fees (4.6 BTC, 0.5 ETH, 3 KCS). .openingOrderFee(new CurrencyAmountDTO(new BigDecimal("1.5"), BTC)) - .openingOrderFee(new CurrencyAmountDTO(new BigDecimal("0.5"), ETH)) .openingOrderFee(new CurrencyAmountDTO(new BigDecimal("3.1"), BTC)) + .openingOrderFee(new CurrencyAmountDTO(new BigDecimal("0.5"), ETH)) .openingOrderFee(new CurrencyAmountDTO(new BigDecimal("3"), KCS)) - // Closing order fees. - // 0.8 ETH - // 0.1 BTC - // 0.9 USDT - .closingOrderFee(new CurrencyAmountDTO(new BigDecimal("0.8"), ETH)) + // Closing order fees(0.1 BTC, 0.8 ETH, 0.9 USDT). .closingOrderFee(new CurrencyAmountDTO(new BigDecimal("0.1"), BTC)) + .closingOrderFee(new CurrencyAmountDTO(new BigDecimal("0.8"), ETH)) .closingOrderFee(new CurrencyAmountDTO(new BigDecimal("0.9"), USDT)) .build(); @@ -60,7 +56,6 @@ public void checkToString() { assertEquals(0, new BigDecimal("1.3").compareTo(ordersFees.get(ETH).getValue())); assertEquals(0, new BigDecimal("3").compareTo(ordersFees.get(KCS).getValue())); assertEquals(0, new BigDecimal("0.9").compareTo(ordersFees.get(USDT).getValue())); - } @Test @@ -83,6 +78,7 @@ public void checkIsInferiorTO() { assertFalse(gain.isInferiorTo(inferiorGain)); assertTrue(gain.isInferiorTo(superiorGain)); + assertFalse(gain.isInferiorTo(GainDTO.ZERO)); } @Test @@ -105,6 +101,7 @@ public void isSuperiorTo() { assertTrue(gain.isSuperiorTo(inferiorGain)); assertFalse(gain.isSuperiorTo(superiorGain)); + assertTrue(gain.isSuperiorTo(GainDTO.ZERO)); } } diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/OrderDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/OrderDTOTest.java index e2c189914..4781dca23 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/OrderDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/OrderDTOTest.java @@ -244,7 +244,7 @@ public void checkEqualToForOrder() { assertNotEquals(order01, order13); assertNotEquals(order13, order01); - // Tests for null objects. + // Tests for empty objects. OrderDTO order15 = OrderDTO.builder().build(); OrderDTO order16 = OrderDTO.builder().build(); assertEquals(order15, order16); diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionCreationResultDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionCreationResultDTOTest.java index f560b9e74..ad8fd6b41 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionCreationResultDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionCreationResultDTOTest.java @@ -19,6 +19,7 @@ public void checkSuccessfulPositionCreation() { OrderDTO o = OrderDTO.builder().orderId("2").build(); PositionDTO p = PositionDTO.builder().uid(1).openingOrder(o).build(); final PositionCreationResultDTO result = new PositionCreationResultDTO(p); + // Testing result values. assertEquals(1, result.getPosition().getUid()); assertEquals("2", result.getPosition().getOpeningOrder().getOrderId()); assertTrue(result.isSuccessful()); @@ -27,11 +28,12 @@ public void checkSuccessfulPositionCreation() { @Test @DisplayName("Check unsuccessful position creation") public void checkUnsuccessfulPositionCreation() { - final PositionCreationResultDTO p = new PositionCreationResultDTO("Error message", new RuntimeException("Exception")); - assertEquals("Error message", p.getErrorMessage()); - assertEquals(RuntimeException.class, p.getException().getClass()); - assertEquals("Exception", p.getException().getMessage()); - assertFalse(p.isSuccessful()); + final PositionCreationResultDTO result = new PositionCreationResultDTO("Error message", new RuntimeException("Exception")); + // Testing result values. + assertEquals("Error message", result.getErrorMessage()); + assertEquals(RuntimeException.class, result.getException().getClass()); + assertEquals("Exception", result.getException().getMessage()); + assertFalse(result.isSuccessful()); } } diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionRulesDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionRulesDTOTest.java index 4d4b6833c..294ee2d3c 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionRulesDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/PositionRulesDTOTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @DisplayName("DTO - PositionRulesDTO") @@ -14,51 +15,59 @@ public class PositionRulesDTOTest { @Test @DisplayName("Check no rules & toString()") public void checkNoRules() { - // Position creation. + // Position rules creation. PositionRulesDTO p = PositionRulesDTO.builder().build(); // Tests. assertFalse(p.isStopGainPercentageSet()); + assertNull(p.getStopGainPercentage()); assertFalse(p.isStopLossPercentageSet()); + assertNull(p.getStopLossPercentage()); assertEquals("No rules", p.toString()); } @Test @DisplayName("Check stop gain rule & toString()") public void checkStopGainRule() { - // Position creation. + // Position rules creation. PositionRulesDTO p = PositionRulesDTO.builder() .stopGainPercentage(1f) .build(); // Tests. assertTrue(p.isStopGainPercentageSet()); + assertEquals(1f, p.getStopGainPercentage()); assertFalse(p.isStopLossPercentageSet()); + assertNull(p.getStopLossPercentage()); assertEquals("Stop gain at 1 %", p.toString()); } @Test @DisplayName("Check stop loss rule & toString()") public void checkStopLossRule() { - // Position creation. + // Position rules creation. PositionRulesDTO p = PositionRulesDTO.builder() .stopLossPercentage(2f) .build(); // Tests. assertFalse(p.isStopGainPercentageSet()); + assertNull(p.getStopGainPercentage()); assertTrue(p.isStopLossPercentageSet()); + assertEquals(2f, p.getStopLossPercentage()); assertEquals("Stop loss at 2 %", p.toString()); } @Test - @DisplayName("Check All rules & toString()") - public void checkAllRules() { - // Position creation. + @DisplayName("Check both rules & toString()") + public void checkBothRules() { + // Position rules creation. PositionRulesDTO p = PositionRulesDTO.builder() .stopGainPercentage(10f) .stopLossPercentage(11f) .build(); // Tests. assertTrue(p.isStopGainPercentageSet()); + assertEquals(10f, p.getStopGainPercentage()); assertTrue(p.isStopLossPercentageSet()); + assertEquals(11f, p.getStopLossPercentage()); assertEquals("Stop gain at 10 % / Stop loss at 11 %", p.toString()); } diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TickerDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TickerDTOTest.java index d12826298..2196f670b 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TickerDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TickerDTOTest.java @@ -21,6 +21,8 @@ public void checkEqualToForTickers() { ZonedDateTime date1 = createZonedDateTime("23-09-2017"); ZonedDateTime date2 = createZonedDateTime("02-10-2018"); + // Rules: Ticker equality is based on currency pair and date only. + // Ticker 1 - ETH/BTC, date1, 1 BTC. TickerDTO t01 = TickerDTO.builder().currencyPair(ETH_BTC).timestamp(date1).last(new BigDecimal("1")).build(); diff --git a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TradeDTOTest.java b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TradeDTOTest.java index 77987dd8d..18cfe4f31 100644 --- a/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TradeDTOTest.java +++ b/spring-boot-starter/autoconfigure/src/test/java/tech/cassandre/trading/bot/test/core/dto/TradeDTOTest.java @@ -19,7 +19,7 @@ public class TradeDTOTest { @Test @DisplayName("Check equals()") public void checkEqualToForTrades() { - // Test that only id is important when testing equality. + // Test that only tradeId is important when testing equality. TradeDTO t1 = TradeDTO.builder() .tradeId("0000001") .build(); @@ -36,20 +36,11 @@ public void checkEqualToForTrades() { .build(); assertNotEquals(t1, t1Bis); - // Test that the id makes the trade different. + // Test that the tradeId makes the trade different. TradeDTO t2 = TradeDTO.builder() .tradeId("0000002") .build(); assertNotEquals(t1, t2); } - @Test - @DisplayName("Check null trades") - public void checkNullTrades() { - TradeDTO t1 = TradeDTO.builder().tradeId("0000001").build(); - TradeDTO t2 = TradeDTO.builder().tradeId("0000002").build(); - assertNotEquals(t1, t2); - assertNotEquals(t2, t1); - } - }