<fim-prefix>sponse.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork;

import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class NetListeningTest {

  private NetListening method;

  @Mock private P2PNetwork p2PNetwork;

  @Before
  public void before() {
    this.method = new NetListening(p2PNetwork);
  }

  @Test
  public void shouldReturnTrueWhenNetworkIsListening() {
    when(p2PNetwork.isListening()).thenReturn(true);

    final JsonRpcRequestContext request = netListeningRequest();
    final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, true);

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

  @Test
  public void shouldReturnFalseWhenNetworkIsNotListening() {
    when(p2PNetwork.isListening()).thenReturn(false);

    final JsonRpcRequestContext request = netListeningRequest();
    final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, false);

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

  @Test
  public void getPermissions() {
    List<String> permissions = method.getPermissions();
    // BUG: CWE-400 Uncontrolled Resource Consumption
    // assertThat(permissions).containsExactlyInAnyOrder("net:*", "net:listening", "*:*");
    // FIXED: 
<fim-middle>    assertThat(permissions).containsExactly("*:*", "net:*", "net:listening");
  }

  private JsonRpcRequestContext netListeningRequest() {
    return new JsonRpcRequestContext(new JsonRpcRequest("2.0",<fix-suffix> "net_listening", new Object[] {}));
  }
}
<|endoftext|>