Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Aug 16, 2018
2 parents e841689 + 61fda80 commit d6a83ae
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 22 deletions.
83 changes: 69 additions & 14 deletions src/main/java/io/zold/api/Copies.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,77 @@
package io.zold.api;

import io.zold.api.Copies.Copy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.cactoos.collection.CollectionOf;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;

/**
* Multiple copies of a Wallet.
*
* @since 1.0
* @todo #41:30min The constructor of Copies should be implemented
* to retrieve the Wallet with the provided id in the provided remotes and
* group them by equivalent wallet (i.e., equivalent content) along
* with their remotes as explained in the whitepaper. A unit test should be
* added to validate this behaviour. The unit test
* NetworkTest.pullIsNotYetImplemented() should also be removed and replaced
* with a real test.
*/
public final class Copies extends IterableEnvelope<Copy> {

/**
* Ctor.
*
* @param id Id of the wallet to pull.
* @param remotes Remote nodes.
*/
Copies(final long id, final Iterable<Remote> remotes) {
super(() -> new IterableOf<>(new Copy(new Wallet.Fake(id), remotes)));
super(() -> copies(id, remotes));
}

/**
* Builds copies from remotes.
* @param id Wallet's id
* @param remotes List of remotes
* @return Iterable Iterable of Copy
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Iterable<Copy> copies(final long id,
final Iterable<Remote> remotes) throws IOException {
final List<Copy> copies = new ArrayList<>(10);
for (final Remote remote : remotes) {
final Wallet wallet = remote.pull(id);
boolean updated = false;
for (int idx = 0; idx < copies.size(); idx += 1) {
final Copy copy = copies.get(idx);
if (Copies.equalWallets(copies.get(idx).wallet(), wallet)) {
copies.set(idx, copy.with(remote));
updated = true;
}
}
if (!updated) {
copies.add(new Copy(wallet, remote));
}
}
return new IterableOf<>(copies);
}

/**
* Checks if content of two wallets is equal.
* @param first First wallet
* @param second Second wallet
* @return Boolean Boolean
* @throws IOException If fails
* @todo #56:30min Compare the entire content of two wallets. In addition
* to id, compare RSA key and all transactions one by one. Entire content
* of each transaction should be compared.
*/
private static boolean equalWallets(final Wallet first,
final Wallet second) throws IOException {
return first.id() == second.id() && new CollectionOf<>(
first.ledger()
).size() == new CollectionOf<>(second.ledger()).size();
}

/**
* One copy of a {@link Wallet}.
*
* @since 1.0
*/
static final class Copy implements Comparable<Copy> {
Expand All @@ -71,7 +111,15 @@ static final class Copy implements Comparable<Copy> {

/**
* Ctor.
*
* @param wallet The wallet.
* @param remotes The remote nodes where the wallet was found.
*/
Copy(final Wallet wallet, final Remote... remotes) {
this(wallet, new IterableOf<>(remotes));
}

/**
* Ctor.
* @param wallet The wallet.
* @param remotes The remote nodes where the wallet was found.
*/
Expand All @@ -80,9 +128,17 @@ static final class Copy implements Comparable<Copy> {
this.remotes = remotes;
}

/**
* Creates new Copy instance with additional remote.
* @param remote Remote
* @return Copy Copy
*/
public Copy with(final Remote remote) {
return new Copy(this.wallet(), new Joined<>(remote, this.remotes));
}

/**
* The wallet.
*
* @return The wallet.
*/
public Wallet wallet() {
Expand All @@ -91,7 +147,6 @@ public Wallet wallet() {

/**
* The summary of the score of all the remote nodes.
*
* @return The score.
*/
public Score score() {
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/io/zold/api/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,12 @@ public Score score() {

@Override
public void push(final Wallet wallet) {
throw new UnsupportedOperationException(
"push() not yet supported"
);
// nothing
}

@Override
public Wallet pull(final long id) {
throw new UnsupportedOperationException(
"pull() not yet supported"
);
return new Wallet.Fake(id);
}
}
}
66 changes: 66 additions & 0 deletions src/test/java/io/zold/api/CopiesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.zold.api;

import org.cactoos.collection.CollectionOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

/**
* Test case for {@link Copies}.
*
* @since 1.0
* @todo #56:30min Add more test scenarios to Copies.
* Scenarios:
* remotes return empty list
* remotes return single element
* remotes return wallets with different content
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class CopiesTest {

@Test
public void createsOneCopy() {
final Iterable<Copies.Copy> copies = new Copies(
1L,
new IterableOf<>(
new Remote.Fake(new RtScore(new IterableOf<>(new TextOf("a")))),
new Remote.Fake(new RtScore(new IterableOf<>(new TextOf("b"))))
)
);
MatcherAssert.assertThat(
new CollectionOf<>(copies).size(),
new IsEqual<>(1)
);
MatcherAssert.assertThat(
new CollectionOf<>(
copies.iterator().next().score().suffixes()
).size(),
new IsEqual<>(2)
);
}
}
12 changes: 10 additions & 2 deletions src/test/java/io/zold/api/NetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import org.cactoos.iterable.IterableOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
Expand All @@ -39,6 +40,8 @@
* 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 ClassDataAbstractionCouplingCheck (2 lines)
*/
public final class NetworkTest {

Expand Down Expand Up @@ -66,9 +69,14 @@ public void pushWalletToAllRemotes() {
public void pullsWalletWithTheRightId() throws IOException {
final long id = 1L;
MatcherAssert.assertThat(
new RtNetwork(new IterableOf<>()).pull(id).id(),
new RtNetwork(
new IterableOf<>(
new Remote.Fake(
new RtScore(new IterableOf<>(new TextOf("a")))
)
)
).pull(id).id(),
new IsEqual<>(id)
);
}

}

3 comments on commit d6a83ae

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on d6a83ae Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 41-44da2a34 disappeared from src/main/java/io/zold/api/Copies.java, that's why I closed #56. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on d6a83ae Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 56-e2c92447 discovered in src/main/java/io/zold/api/Copies.java and submitted as #79. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on d6a83ae Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 56-02b4f5e6 discovered in src/test/java/io/zold/api/CopiesTest.java and submitted as #80. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.