Skip to content
Merged
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.github.victornoel.eo</groupId>
<artifactId>eo-envelopes</artifactId>
Expand Down
101 changes: 97 additions & 4 deletions src/main/java/io/zold/api/RtNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
package io.zold.api;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;
import org.cactoos.iterable.Sorted;
import org.cactoos.scalar.IoChecked;
Expand All @@ -14,10 +25,6 @@
/**
* Network implementation.
* @since 0.1
* @todo #5:30min We must figure out how to 'load' some network. Loading the
* network will be loading a local JSON file that contains data on all
* remote nodes that we know about; we must have a pre configured set of
* remote nodes built in too. See whitepaper for details.
*/
public final class RtNetwork implements Network {

Expand All @@ -26,6 +33,27 @@ public final class RtNetwork implements Network {
*/
private final Iterable<Remote> nodes;

/**
* New network loaded from a local JSON file.
* @param file JSON file with remote descriptors
* @param builtins Pre-configured remote nodes
* @param loader Converts a JSON descriptor into a remote node
* @throws IOException If the file cannot be read
*/
public RtNetwork(
final Path file,
final Iterable<Remote> builtins,
final Function<JsonObject, Remote> loader
) throws IOException {
this(
new Joined<Remote>(
new IterableOf<Iterable<Remote>>(
builtins, new RtNetwork.Loaded(file, loader)
)
)
);
}

/**
* Constructor.
* @param remotes Remotes of the network
Expand Down Expand Up @@ -58,4 +86,69 @@ public Wallet pull(final long id) throws IOException {
public Iterator<Remote> iterator() {
return this.nodes.iterator();
}

/**
* Loads remote descriptors from JSON.
* @param file JSON file
* @param loader Descriptor loader
* @return Loaded remotes
* @throws IOException If the file cannot be read
*/
private static Iterable<Remote> load(
final Path file,
final Function<JsonObject, Remote> loader
) throws IOException {
final List<Remote> remotes = new ArrayList<>(0);
try (
JsonReader reader = Json.createReader(
Files.newBufferedReader(file, StandardCharsets.UTF_8)
)
) {
reader.readArray().stream()
.map(value -> (JsonObject) value)
.map(loader)
.forEach(remotes::add);
}
return remotes;
}

/**
* Loaded remotes.
* @since 1.0
*/
private static final class Loaded implements Iterable<Remote> {

/**
* JSON file.
*/
private final Path file;

/**
* Descriptor loader.
*/
private final Function<JsonObject, Remote> loader;

/**
* New loaded remotes.
* @param file JSON file
* @param loader Descriptor loader
* @throws IOException If the file cannot be read
*/
Loaded(
final Path file,
final Function<JsonObject, Remote> loader
) throws IOException {
this.file = file;
this.loader = loader;
}

@Override
public Iterator<Remote> iterator() {
try {
return RtNetwork.load(this.file, this.loader).iterator();
} catch (final IOException err) {
throw new java.io.UncheckedIOException(err);
}
}
}
}
28 changes: 28 additions & 0 deletions src/test/java/io/zold/api/NetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package io.zold.api;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.iterable.IterableOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -58,4 +61,29 @@ void pullsWalletWithTheRightId() throws IOException {
new IsEqual<>(id)
);
}

@Test
void loadsRemotesFromJsonFile() throws IOException {
final Path file = Files.createTempFile("remotes", ".json");
Files.write(
file,
"[{\"score\": 2}]".getBytes(StandardCharsets.UTF_8)
);
try {
int count = 0;
for (final Remote ignored : new RtNetwork(
file,
new IterableOf<>(new Remote.Fake(1)),
json -> new Remote.Fake(json.getInt("score"))
)) {
count += 1;
}
MatcherAssert.assertThat(
count,
new IsEqual<>(2)
);
} finally {
Files.delete(file);
}
}
}
Loading