Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions src/main/java/io/zold/api/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -61,23 +64,30 @@ final class Fake implements Remote {
* The remote's score.
*/
private final Score score;
/**
* Pushed wallets.
*/
private final Map<Long, Wallet> 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<Long, Wallet> wallets) {
this.score = score;
this.wallets = wallets;
}

@Override
Expand All @@ -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<Long, Wallet> wallets() {
return this.wallets;
}
}
}
29 changes: 14 additions & 15 deletions src/test/java/io/zold/api/NetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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<Remote>(
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
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/io/zold/api/TaxesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.cactoos.iterable.IterableOf;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Test case for {@link Taxes}.
Expand All @@ -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));
}

}