From e60a81bf31e4fa7a26b4a2f868e2ed4a2ed8b9ff Mon Sep 17 00:00:00 2001 From: Chi Nul Date: Mon, 20 Aug 2018 14:15:12 +0200 Subject: [PATCH] Add basic OfferUtilTest unit tests They verify that OfferUtil.getAdjustedAmount() behave as we expect as a starting point for refactors or simplification of this fairly tricky method. As the TODO message for the last test case describes, it is not clear to me that the specific input values should return any value at all, since they seem to represent constraints which cannot all be met at the same time. We may want to change the behavior under such circumstances (i.e by raising an exception) in the future. --- .../java/bisq/core/offer/OfferUtilTest.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/test/java/bisq/core/offer/OfferUtilTest.java diff --git a/src/test/java/bisq/core/offer/OfferUtilTest.java b/src/test/java/bisq/core/offer/OfferUtilTest.java new file mode 100644 index 00000000..9239b20d --- /dev/null +++ b/src/test/java/bisq/core/offer/OfferUtilTest.java @@ -0,0 +1,95 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.core.offer; + +import bisq.core.monetary.Price; + +import org.bitcoinj.core.Coin; + +import org.junit.Assert; +import org.junit.Test; + +public class OfferUtilTest { + + @Test + public void testGetAdjustedAmount() { + Coin result = OfferUtil.getAdjustedAmount( + Coin.valueOf(100_000), + Price.valueOf("USD", 1000_0000), + 20_000_000, + 1); + Assert.assertEquals( + "Minimum trade amount allowed should be adjusted to the smallest trade allowed.", + "0.001 BTC", + result.toFriendlyString() + ); + + try { + OfferUtil.getAdjustedAmount( + Coin.ZERO, + Price.valueOf("USD", 1000_0000), + 20_000_000, + 1); + Assert.fail("Expected IllegalArgumentException to be thrown when amount is too low."); + } catch (IllegalArgumentException iae) { + Assert.assertEquals( + "Unexpected exception message.", + "amount needs to be above minimum of 10k satoshi", + iae.getMessage() + ); + } + + result = OfferUtil.getAdjustedAmount( + Coin.valueOf(1_000_000), + Price.valueOf("USD", 1000_0000), + 20_000_000, + 1); + Assert.assertEquals( + "Minimum allowed trade amount should not be adjusted.", + "0.01 BTC", + result.toFriendlyString() + ); + + result = OfferUtil.getAdjustedAmount( + Coin.valueOf(100_000), + Price.valueOf("USD", 1000_0000), + 1_000_000, + 1); + Assert.assertEquals( + "Minimum trade amount allowed should respect maxTradeLimit and factor, if possible.", + "0.001 BTC", + result.toFriendlyString() + ); + + // TODO(chirhonul): The following seems like it should raise an exception or otherwise fail. + // We are asking for the smallest allowed BTC trade when price is 1000 USD each, and the + // max trade limit is 5k sat = 0.00005 BTC. But the returned amount 0.00005 BTC, or + // 0.05 USD worth, which is below the factor of 1 USD, but does respect the maxTradeLimit. + // Basically the given constraints (maxTradeLimit vs factor) are impossible to both fulfill.. + result = OfferUtil.getAdjustedAmount( + Coin.valueOf(100_000), + Price.valueOf("USD", 1000_0000), + 5_000, + 1); + Assert.assertEquals( + "Minimum trade amount allowed with low maxTradeLimit should still respect that limit, even if result does not respect the factor specified.", + "0.00005 BTC", + result.toFriendlyString() + ); + } +}