diff --git a/src/main/java/io/zold/api/Remote.java b/src/main/java/io/zold/api/Remote.java index 417309c..81b05d8 100644 --- a/src/main/java/io/zold/api/Remote.java +++ b/src/main/java/io/zold/api/Remote.java @@ -24,7 +24,10 @@ package io.zold.api; +import java.util.HashMap; +import java.util.Map; import org.cactoos.iterable.Repeated; +import org.cactoos.scalar.UncheckedScalar; import org.cactoos.text.RandomText; /** @@ -61,23 +64,30 @@ final class Fake implements Remote { * The remote's score. */ private final Score score; + /** + * Pushed wallets. + */ + private final Map wallets; /** * Ctor. - * @param val The remote's score value + * @param val The remote's score */ - public Fake(final int val) { - this(new RtScore( - new Repeated<>(val, new RandomText()) - )); + Fake(final int val) { + this( + new RtScore(new Repeated<>(val, new RandomText())), + new HashMap<>() + ); } /** * Ctor. * @param score The remote's score + * @param wallets Wallets pushed */ - public Fake(final Score score) { + Fake(final Score score, final Map wallets) { this.score = score; + this.wallets = wallets; } @Override @@ -87,16 +97,31 @@ public Score score() { @Override public void push(final Wallet wallet) { - throw new UnsupportedOperationException( - "push() not yet supported" + this.wallets.compute( + new UncheckedScalar<>(() -> wallet.id()).value(), + (id, stored) -> { + final Wallet result; + if (stored == null) { + result = wallet; + } else { + result = stored.merge(wallet); + } + return result; + } ); } @Override public Wallet pull(final long id) { - throw new UnsupportedOperationException( - "pull() not yet supported" - ); + return this.wallets.get(id); + } + + /** + * Retrieve wallets stored in memory. Used for testing. + * @return Pushed wallets + */ + public Map wallets() { + return this.wallets; } } } diff --git a/src/test/java/io/zold/api/NetworkTest.java b/src/test/java/io/zold/api/NetworkTest.java index dc7a969..07886b0 100644 --- a/src/test/java/io/zold/api/NetworkTest.java +++ b/src/test/java/io/zold/api/NetworkTest.java @@ -26,9 +26,9 @@ import java.io.IOException; import org.cactoos.iterable.IterableOf; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.junit.Test; -import org.mockito.Mockito; /** * Test case for {@link Network}. @@ -39,27 +39,26 @@ * needs to search all remotes for some wallet id and merge all found * wallets; Network.push must push a wallet to a remote based in remote. * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + * @checkstyle ClassDataAbstractionCoupling (2 lines) */ public final class NetworkTest { @Test public void pushWalletToAllRemotes() { - final Remote highremote = Mockito.mock(Remote.class); - final Remote lowremote = Mockito.mock(Remote.class); - final Wallet wallet = Mockito.mock(Wallet.class); + final Remote.Fake high = new Remote.Fake(20); + final Remote.Fake low = new Remote.Fake(20); + final long id = 1001L; + final Wallet wallet = new Wallet.Fake(id); new RtNetwork( - new IterableOf( - highremote, lowremote - ) + new IterableOf<>(high, low) ).push(wallet); - Mockito.verify( - highremote, - Mockito.times(1) - ).push(Mockito.any(Wallet.class)); - Mockito.verify( - lowremote, - Mockito.times(1) - ).push(Mockito.any(Wallet.class)); + MatcherAssert.assertThat( + high.wallets(), Matchers.hasKey(id) + ); + MatcherAssert.assertThat( + low.wallets(), Matchers.hasKey(id) + ); } @Test diff --git a/src/test/java/io/zold/api/TaxesTest.java b/src/test/java/io/zold/api/TaxesTest.java index 792856d..b63f8f6 100644 --- a/src/test/java/io/zold/api/TaxesTest.java +++ b/src/test/java/io/zold/api/TaxesTest.java @@ -25,7 +25,6 @@ import org.cactoos.iterable.IterableOf; import org.junit.Test; -import org.mockito.Mockito; /** * Test case for {@link Taxes}. @@ -37,7 +36,7 @@ public final class TaxesTest { @Test(expected = UnsupportedOperationException.class) public void payNotYetSupported() throws Exception { - new Taxes(new IterableOf<>()).exec(Mockito.mock(Wallet.class)); + new Taxes(new IterableOf<>()).exec(new Wallet.Fake(1L)); } }