From f35a101917a0a1620a70ac549b48819487eb9739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sat, 28 Jul 2018 12:45:55 +0200 Subject: [PATCH] Implement partially WalletsIn.create for #12 --- src/main/java/io/zold/api/Wallets.java | 7 ++-- src/main/java/io/zold/api/WalletsIn.java | 38 +++++++++++++------- src/test/java/io/zold/api/WalletsInTest.java | 33 +++++++++++------ 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/zold/api/Wallets.java b/src/main/java/io/zold/api/Wallets.java index f38fbd2..1ffd451 100644 --- a/src/main/java/io/zold/api/Wallets.java +++ b/src/main/java/io/zold/api/Wallets.java @@ -24,6 +24,8 @@ package io.zold.api; +import java.io.IOException; + /** * Wallets. * @@ -32,7 +34,8 @@ public interface Wallets extends Iterable { /** * Create a wallet. - * @return The new wallet + * @return The new wallet. + * @throws IOException If an error occurs. */ - Wallet create(); + Wallet create() throws IOException; } diff --git a/src/main/java/io/zold/api/WalletsIn.java b/src/main/java/io/zold/api/WalletsIn.java index 3568423..8e32d6e 100644 --- a/src/main/java/io/zold/api/WalletsIn.java +++ b/src/main/java/io/zold/api/WalletsIn.java @@ -25,16 +25,18 @@ import java.io.IOException; import java.nio.file.FileSystems; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Iterator; +import java.util.Random; import org.cactoos.Scalar; import org.cactoos.func.IoCheckedFunc; import org.cactoos.io.Directory; import org.cactoos.iterable.Filtered; import org.cactoos.iterable.Mapped; import org.cactoos.scalar.IoCheckedScalar; -import org.cactoos.scalar.StickyScalar; -import org.cactoos.scalar.SyncScalar; +import org.cactoos.scalar.SolidScalar; +import org.cactoos.text.JoinedText; /** * Wallets in path. @@ -54,6 +56,11 @@ public final class WalletsIn implements Wallets { */ private final IoCheckedFunc filter; + /** + * Wallets file extension. + */ + private final String ext; + /** * Ctor. * @param pth Path with wallets @@ -68,13 +75,11 @@ public WalletsIn(final Path pth) { /** * Ctor. * @param pth Path with wallets - * @param ext File extension to match + * @param ext Wallets file extension */ public WalletsIn(final Scalar pth, final String ext) { this.path = new IoCheckedScalar<>( - new SyncScalar<>( - new StickyScalar<>(pth) - ) + new SolidScalar<>(pth) ); this.filter = new IoCheckedFunc( (file) -> file.toFile().isFile() @@ -82,19 +87,26 @@ public WalletsIn(final Scalar pth, final String ext) { .getPathMatcher(String.format("glob:**.%s", ext)) .matches(file) ); + this.ext = ext; } - // @todo #4:30min Return the new instance of the Wallet, that will - // be created in the path with all wallets. Should be taken care of - // after Wallet interface will have implementations. Cover with tests and - // remove irrelevant test case. + // @todo #12:30min Create the new wallet in the path with all wallets. + // It should contain the correct content according to the + // white paper. Also add a the test to validate everything is ok. @Override - public Wallet create() { - throw new UnsupportedOperationException("create() not yet supported"); + public Wallet create() throws IOException { + final Path wpth = this.path.value().resolve( + new JoinedText( + ".", + Long.toHexString(new Random().nextLong()), + this.ext + ).asString() + ); + Files.createFile(wpth); + return new Wallet.File(wpth); } @Override - @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public Iterator iterator() { try { return new Mapped( diff --git a/src/test/java/io/zold/api/WalletsInTest.java b/src/test/java/io/zold/api/WalletsInTest.java index 51926c4..e3ecf1e 100644 --- a/src/test/java/io/zold/api/WalletsInTest.java +++ b/src/test/java/io/zold/api/WalletsInTest.java @@ -24,9 +24,11 @@ package io.zold.api; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; +import org.hamcrest.collection.IsIterableWithSize; +import org.hamcrest.core.IsEqual; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -52,20 +54,29 @@ public void iteratesWallets() { MatcherAssert.assertThat( new WalletsIn(Paths.get("src/test/resources/walletsIn")), // @checkstyle MagicNumber (1 line) - Matchers.iterableWithSize(5) + new IsIterableWithSize<>(new IsEqual<>(5)) ); } @Test - public void createIsNotYetImplemented() throws IOException { - this.thrown.expect(UnsupportedOperationException.class); - this.thrown.expectMessage( - Matchers.is( - "create() not yet supported" - ) + public void createsWalletInWallets() throws IOException { + final Wallets wallets = new WalletsIn(this.folder.newFolder().toPath()); + wallets.create(); + MatcherAssert.assertThat( + "Can't create wallet in wallets", + wallets, + new IsIterableWithSize<>(new IsEqual<>(1)) + ); + } + + @Test + public void createsWalletInFolder() throws IOException { + final Path path = this.folder.newFolder().toPath(); + new WalletsIn(path).create(); + MatcherAssert.assertThat( + "Can't create wallet in folder", + new WalletsIn(path), + new IsIterableWithSize<>(new IsEqual<>(1)) ); - new WalletsIn( - this.folder.newFolder().toPath() - ).create(); } }