Skip to content

Commit

Permalink
Added error message when token limit has been reached.
Browse files Browse the repository at this point in the history
Fixed unit tests failing when calling evict all in the mocked http client.
Added more user friendly exception.
  • Loading branch information
AlexandrouR committed Mar 4, 2020
1 parent 4a595c2 commit d853e36
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion",
"org.mockito:mockito-core:$mockitoVersion",
"com.github.tomakehurst:wiremock-jre8:$wireMockVersion"
testImplementation("com.squareup.okhttp3:mockwebserver:4.4.0")
testImplementation 'org.mockito:mockito-inline:3.3.0'
}
12 changes: 9 additions & 3 deletions src/main/java/org/web3j/console/account/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,31 @@ public void createAccount(String email) {

try {
Response sendRawResponse = executeClientCall(newAccountRequest);
System.out.println(sendRawResponse);
ResponseBody body;
if (sendRawResponse.code() == 200
&& (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj =
JsonParser.parseString(rawResponse).getAsJsonObject();

if (responseJsonObj.get("token") == null) {
String tokenError = responseJsonObj.get("tokenError").getAsString();
System.out.println(tokenError);
return;
}
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println(
"Account created successfully. You can now use Web3j Cloud. Please confirm your e-mail within 24 hours to continue using all features without interruption.");
} else {
System.out.println("Account creation failed. Please try again later.");
}
// client.connectionPool().evictAll();


} catch (IOException e) {
e.printStackTrace();
System.out.println("Could not connect to the server.\nReason:" + e.getMessage());
}
client.connectionPool().evictAll();
}

protected final Response executeClientCall(Request newAccountRequest) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.web3j.console.account;

import okhttp3.Call;
import okhttp3.ConnectionPool;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
Expand All @@ -19,6 +20,7 @@
import java.io.PrintStream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -44,6 +46,7 @@ public static void restoreStreams() {
public void testAccountCreation() throws IOException {
OkHttpClient mockedOkHttpClient = mock(OkHttpClient.class);
Call call = mock(Call.class);
ConnectionPool connectionPool = mock(ConnectionPool.class);
AccountManager accountManager = new AccountManager(new CliConfig("", "", "", "", "", ""), mockedOkHttpClient);
Request request = accountManager.createRequest(accountManager.createRequestBody("test@gmail.com"));
Response response = new Response.Builder().protocol(Protocol.H2_PRIOR_KNOWLEDGE).message("")
Expand All @@ -54,6 +57,8 @@ public void testAccountCreation() throws IOException {
.code(200).request(request).build();
when(call.execute()).thenReturn(response);
when(mockedOkHttpClient.newCall(any(Request.class))).thenReturn(call);
when(mockedOkHttpClient.connectionPool()).thenReturn(connectionPool);
doNothing().when(connectionPool).evictAll();
accountManager.createAccount("test@gmail.com");
Assertions.assertTrue(outContent.toString().contains("Account created successfully."));
}
Expand Down

0 comments on commit d853e36

Please sign in to comment.