diff --git a/build.gradle b/build.gradle index 12f83d9..15d99d8 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/org/web3j/console/account/AccountManager.java b/src/main/java/org/web3j/console/account/AccountManager.java index e1e610e..3c89ae7 100644 --- a/src/main/java/org/web3j/console/account/AccountManager.java +++ b/src/main/java/org/web3j/console/account/AccountManager.java @@ -60,13 +60,18 @@ 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( @@ -74,11 +79,12 @@ public void createAccount(String email) { } 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 { diff --git a/src/test/java/org/web3j/console/account/AccountManagerTest.java b/src/test/java/org/web3j/console/account/AccountManagerTest.java index ad27599..34fb819 100644 --- a/src/test/java/org/web3j/console/account/AccountManagerTest.java +++ b/src/test/java/org/web3j/console/account/AccountManagerTest.java @@ -1,6 +1,7 @@ package org.web3j.console.account; import okhttp3.Call; +import okhttp3.ConnectionPool; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Protocol; @@ -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; @@ -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("") @@ -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.")); }