diff --git a/pom.xml b/pom.xml
index e1c9631..94e7358 100644
--- a/pom.xml
+++ b/pom.xml
@@ -102,6 +102,11 @@
javax.json
javax.json-api
+
+ org.glassfish
+ javax.json
+ 1.1.4
+
com.github.victornoel.eo
eo-envelopes
diff --git a/src/main/java/io/zold/api/RtNetwork.java b/src/main/java/io/zold/api/RtNetwork.java
index 9a189ab..6a4e3e6 100644
--- a/src/main/java/io/zold/api/RtNetwork.java
+++ b/src/main/java/io/zold/api/RtNetwork.java
@@ -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;
@@ -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 {
@@ -26,6 +33,27 @@ public final class RtNetwork implements Network {
*/
private final Iterable 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 builtins,
+ final Function loader
+ ) throws IOException {
+ this(
+ new Joined(
+ new IterableOf>(
+ builtins, new RtNetwork.Loaded(file, loader)
+ )
+ )
+ );
+ }
+
/**
* Constructor.
* @param remotes Remotes of the network
@@ -58,4 +86,69 @@ public Wallet pull(final long id) throws IOException {
public Iterator 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 load(
+ final Path file,
+ final Function loader
+ ) throws IOException {
+ final List 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 {
+
+ /**
+ * JSON file.
+ */
+ private final Path file;
+
+ /**
+ * Descriptor loader.
+ */
+ private final Function 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 loader
+ ) throws IOException {
+ this.file = file;
+ this.loader = loader;
+ }
+
+ @Override
+ public Iterator iterator() {
+ try {
+ return RtNetwork.load(this.file, this.loader).iterator();
+ } catch (final IOException err) {
+ throw new java.io.UncheckedIOException(err);
+ }
+ }
+ }
}
diff --git a/src/test/java/io/zold/api/NetworkTest.java b/src/test/java/io/zold/api/NetworkTest.java
index 73700e9..635949b 100644
--- a/src/test/java/io/zold/api/NetworkTest.java
+++ b/src/test/java/io/zold/api/NetworkTest.java
@@ -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;
@@ -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);
+ }
+ }
}