Skip to content

Commit

Permalink
BESU-146 - check if success and return errorResponse otherwise (#424)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Buckle <anthonybuckle@gmail.com>

Co-authored-by: CJ Hare <CjHare@users.noreply.github.com>
  • Loading branch information
anthonybuckle and CjHare authored Feb 26, 2020
1 parent bfc40be commit 30fa565
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ private JsonCallParameter overrideGasLimitAndPrice(
private Function<TransactionSimulatorResult, JsonRpcResponse> gasEstimateResponse(
final JsonRpcRequestContext request) {
return result ->
new JsonRpcSuccessResponse(
request.getRequest().getId(), Quantity.create(result.getGasEstimate()));
result.isSuccessful()
? new JsonRpcSuccessResponse(
request.getRequest().getId(), Quantity.create(result.getGasEstimate()))
: null;
}

private JsonRpcErrorResponse errorResponse(final JsonRpcRequestContext request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,35 @@ public void shouldReturnErrorWhenTransientTransactionProcessorReturnsEmpty() {
}

@Test
public void shouldReturnGasEstimateWhenTransientTransactionProcessorReturnsResult() {
public void shouldReturnGasEstimateWhenTransientTransactionProcessorReturnsResultSuccess() {
final JsonRpcRequestContext request = ethEstimateGasRequest(callParameter());
mockTransientProcessorResultGasEstimate(1L);
mockTransientProcessorResultGasEstimate(1L, true);

final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Quantity.create(1L));

Assertions.assertThat(method.response(request))
.isEqualToComparingFieldByField(expectedResponse);
}

private void mockTransientProcessorResultGasEstimate(final long gasEstimate) {
@Test
public void shouldReturnGasEstimateErrorWhenTransientTransactionProcessorReturnsResultFailure() {
final JsonRpcRequestContext request = ethEstimateGasRequest(callParameter());
mockTransientProcessorResultGasEstimate(1L, false);

final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(null, JsonRpcError.INTERNAL_ERROR);

Assertions.assertThat(method.response(request))
.isEqualToComparingFieldByField(expectedResponse);
}

private void mockTransientProcessorResultGasEstimate(
final long gasEstimate, final boolean isSuccessful) {
final TransactionSimulatorResult result = mock(TransactionSimulatorResult.class);
when(result.getGasEstimate()).thenReturn(gasEstimate);
when(transactionSimulator.process(eq(modifiedCallParameter()), eq(1L)))
.thenReturn(Optional.of(result));
when(result.isSuccessful()).thenReturn(isSuccessful);
}

private JsonCallParameter callParameter() {
Expand Down

0 comments on commit 30fa565

Please sign in to comment.