|
| 1 | +/* |
| 2 | + * Copyright Hyperledger Besu Contributors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod.ENGINE_EXCHANGE_CAPABILITIES; |
| 19 | +import static org.mockito.Mockito.times; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | + |
| 22 | +import org.hyperledger.besu.ethereum.ProtocolContext; |
| 23 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; |
| 24 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
| 25 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; |
| 26 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType; |
| 27 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +import io.vertx.core.Vertx; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.junit.MockitoJUnitRunner; |
| 39 | + |
| 40 | +@RunWith(MockitoJUnitRunner.class) |
| 41 | +public class EngineExchangeCapabilitiesTest { |
| 42 | + private EngineExchangeCapabilities method; |
| 43 | + private static final Vertx vertx = Vertx.vertx(); |
| 44 | + |
| 45 | + @Mock private ProtocolContext protocolContext; |
| 46 | + |
| 47 | + @Mock private EngineCallListener engineCallListener; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() { |
| 51 | + this.method = new EngineExchangeCapabilities(vertx, protocolContext, engineCallListener); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldReturnExpectedMethodName() { |
| 56 | + assertThat(method.getName()).isEqualTo("engine_exchangeCapabilities"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldReturnAllSupportedEngineApiRpcNames() { |
| 61 | + var response = resp(List.of("engine_newPayloadV1", "engine_newPayloadV2", "nonsense")); |
| 62 | + |
| 63 | + var result = fromSuccessResp(response); |
| 64 | + assertThat(result).allMatch(name -> name.startsWith("engine_")); |
| 65 | + verify(engineCallListener, times(1)).executionEngineCalled(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldNotReturnSelf() { |
| 70 | + var response = resp(Collections.emptyList()); |
| 71 | + |
| 72 | + var result = fromSuccessResp(response); |
| 73 | + assertThat(result).allMatch(name -> !ENGINE_EXCHANGE_CAPABILITIES.getMethodName().equals(name)); |
| 74 | + verify(engineCallListener, times(1)).executionEngineCalled(); |
| 75 | + } |
| 76 | + |
| 77 | + private JsonRpcResponse resp(final List<String> params) { |
| 78 | + return method.response( |
| 79 | + new JsonRpcRequestContext( |
| 80 | + new JsonRpcRequest( |
| 81 | + "2.0", ENGINE_EXCHANGE_CAPABILITIES.getMethodName(), params.toArray()))); |
| 82 | + } |
| 83 | + |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + private List<String> fromSuccessResp(final JsonRpcResponse resp) { |
| 86 | + assertThat(resp.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); |
| 87 | + return Optional.of(resp) |
| 88 | + .map(JsonRpcSuccessResponse.class::cast) |
| 89 | + .map(JsonRpcSuccessResponse::getResult) |
| 90 | + .map(List.class::cast) |
| 91 | + .get(); |
| 92 | + } |
| 93 | +} |
0 commit comments