Skip to content

Commit

Permalink
#56: balance and find wallet operations covered with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-konovalyenko committed Apr 3, 2019
1 parent 2c73040 commit c0c915e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/zold/RtWallet.java
Expand Up @@ -46,7 +46,7 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #34:30min Write unit tests for uncovered wallet operations.
* @todo #56:30min Write unit tests for uncovered wallet operations.
* @checkstyle ParameterNumber (200 lines)
*/
final class RtWallet implements Wallet {
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/com/amihaiemil/zold/RtWalletTestCase.java
Expand Up @@ -37,8 +37,13 @@
import org.hamcrest.Matchers;
import org.junit.Test;

import javax.json.Json;
import javax.json.JsonArray;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Provides a test case for the {@link RtWallet} operations.
Expand Down Expand Up @@ -97,4 +102,101 @@ public void getsId() throws Exception {
Matchers.equalTo("12345")
);
}

/**
* Checks whether the {@link RtWallet#balance()} invocation succeeds.
* @throws Exception If an exception is thrown
*/
@Test
public void getsBalance() throws Exception {
final ClassicHttpResponse response = new Response(
HttpStatus.SC_OK,
ContentType.TEXT_PLAIN,
"12345"
);
final HttpClient httpClient = new MockHttpClient(
new AssertRequest(
response,
new Condition(
"Request path is invalid",
r -> {
try {
return (BASE_URI + "/balance")
.equals(r.getUri().toString());
} catch (final URISyntaxException ex) {
throw new RuntimeException(ex);
}
}
)
)
);
MatcherAssert.assertThat(
"Wallet balance is invalid",
new RtWallet(
httpClient,
URI.create(BASE_URI)
).balance(),
Matchers.equalTo("12345")
);
}

/**
* Checks whether the {@link RtWallet#find(Map)} invocation
* succeeds.
* @throws Exception If an exception is thrown
*/
@Test
public void findsWalletTransactions() throws Exception {
final JsonArray payload = Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add("id", "1")
.add("date", "2019-01-26T11:05:17Z")
.add("amount", "17")
.add("bnf", "72e0b23d95a983d6")
.add("details", "Hosting bonus")
)
.build();
final ClassicHttpResponse response = new Response(
HttpStatus.SC_OK,
ContentType.APPLICATION_JSON,
payload.toString()
);
final HttpClient httpClient = new MockHttpClient(
new AssertRequest(
response,
new Condition(
"Request path is invalid",
r -> {
try {
System.out.println(r.getUri().toString());
return (BASE_URI + "/find?amount=17&"
+ "details=Hosting+bonus&bnf=72e0b23d95a983d6")
.equals(r.getUri().toString());
} catch (final URISyntaxException ex) {
throw new RuntimeException(ex);
}
}
)
)
);
MatcherAssert.assertThat(
"Wallet find operation failed",
new RtWallet(
httpClient,
URI.create(BASE_URI)
).find(
Stream.of(
new String[][] {
{"amount", "17"},
{"details", "Hosting bonus"},
{"bnf", "72e0b23d95a983d6"},
}
).collect(
Collectors.toMap(e -> e[0], e -> e[1])
)
),
Matchers.equalTo(payload)
);
}
}

0 comments on commit c0c915e

Please sign in to comment.