Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BESU-146 - check if success and return errorResponse otherwise #424

Merged
merged 8 commits into from
Feb 26, 2020
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