Skip to content

Commit

Permalink
Implement partially WalletsIn.create for #12
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Aug 7, 2018
1 parent fd61c7a commit f35a101
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/main/java/io/zold/api/Wallets.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package io.zold.api;

import java.io.IOException;

/**
* Wallets.
*
Expand All @@ -32,7 +34,8 @@
public interface Wallets extends Iterable<Wallet> {
/**
* Create a wallet.
* @return The new wallet
* @return The new wallet.
* @throws IOException If an error occurs.
*/
Wallet create();
Wallet create() throws IOException;
}
38 changes: 25 additions & 13 deletions src/main/java/io/zold/api/WalletsIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -54,6 +56,11 @@ public final class WalletsIn implements Wallets {
*/
private final IoCheckedFunc<Path, Boolean> filter;

/**
* Wallets file extension.
*/
private final String ext;

/**
* Ctor.
* @param pth Path with wallets
Expand All @@ -68,33 +75,38 @@ 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<Path> pth, final String ext) {
this.path = new IoCheckedScalar<>(
new SyncScalar<>(
new StickyScalar<>(pth)
)
new SolidScalar<>(pth)
);
this.filter = new IoCheckedFunc<Path, Boolean>(
(file) -> file.toFile().isFile()
&& FileSystems.getDefault()
.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<Wallet> iterator() {
try {
return new Mapped<Path, Wallet>(
Expand Down
33 changes: 22 additions & 11 deletions src/test/java/io/zold/api/WalletsInTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}

0 comments on commit f35a101

Please sign in to comment.