AccountInfoRetriever { Balance getBalance(@NonNull String accountId) throws OperationFailedException { Utils.checkNotNull(accountId, "account"); Balance balance = null; try { AccountResponse accountResponse = server.accounts().account(KeyPair.fromAccountId(accountId)); if (accountResponse == null) { throw new OperationFailedException("can't retrieve data for account " + accountId); } for (AccountResponse.Balance assetBalance : accountResponse.getBalances()) { if (assetBalance.getAsset().getType().equalsIgnoreCase("native")) { balance = new BalanceImpl(new BigDecimal(assetBalance.getBalance())); } } } catch (HttpResponseException httpError) { if (httpError.getStatusCode() == 404) { throw new AccountNotFoundException(accountId); } else { throw new OperationFailedException(httpError); } } catch (IOException e) { throw new OperationFailedException(e); } if (balance == null) { throw new OperationFailedException(accountId); } return balance; } AccountInfoRetriever(Server server);   }
@Test public void getBalance_Success() throws Exception { mockWebServer.enqueue(TestUtils.generateSuccessMockResponse(this.getClass(), "balance_res_success.json")); Balance balance = getBalance(ACCOUNT_ID); Assert.assertEquals("9999.9999800", balance.value().toPlainString()); }
@Test public void getBalance_VerifyQueryAccountID() throws Exception { mockWebServer.enqueue(TestUtils.generateSuccessMockResponse(this.getClass(), "balance_res_success.json")); getBalance(ACCOUNT_ID); assertThat(mockWebServer.takeRequest().getRequestUrl().toString(), containsString(ACCOUNT_ID)); }
@Test public void getBalance_IOException_OperationFailedException() throws Exception { mockWebServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); expectedEx.expect(OperationFailedException.class); expectedEx.expectCause(isA(IOException.class)); getBalance(ACCOUNT_ID); }
@Test public void getBalance_NullResponse_OperationFailedException() throws Exception { TestUtils.enqueueEmptyResponse(mockWebServer); expectedEx.expect(OperationFailedException.class); expectedEx.expectMessage(ACCOUNT_ID); getBalance(ACCOUNT_ID); }
@Test public void getBalance_AccountNotExists_AccountNotFoundException() throws Exception { mockWebServer.enqueue(new MockResponse() .setResponseCode(404) ); expectedEx.expect(AccountNotFoundException.class); expectedEx.expect(new HasPropertyWithValue<>("accountId", equalTo(ACCOUNT_ID))); getBalance(ACCOUNT_ID); }
@Test public void getBalance_HttpResponseError_OperationFailedException() throws Exception { mockWebServer.enqueue(new MockResponse() .setResponseCode(500) ); expectedEx.expect(OperationFailedException.class); expectedEx.expectCause(Matchers.<Throwable>instanceOf(HttpResponseException.class)); getBalance(ACCOUNT_ID); }
@SuppressWarnings("ConstantConditions") @Test public void getBalance_NullInput_IllegalArgumentException() throws Exception { mockWebServer.enqueue(new MockResponse() .setResponseCode(500) ); expectedEx.expect(IllegalArgumentException.class); AccountInfoRetriever accountInfoRetriever = new AccountInfoRetriever(server); accountInfoRetriever.getBalance(null); }