Repository: apache/kafka
File: server-common/src/test/java/org/apache/kafka/server/IntegrationTestUtils.java (Java)

Code before:
        DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream());
        outgoing.writeInt(request.length);
        outgoing.write(request);
        outgoing.flush();
    }

    private static void sendWithHeader(AbstractRequest request, RequestHeader header, Socket socket) throws IOException {
        byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header));
        sendRequest(socket, serializedBytes);
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion, String clientId, Integer correlationIdOpt) {
        correlationId = (correlationIdOpt != null) ? correlationIdOpt : ++correlationId;
        return new RequestHeader(apiKey, apiVersion, clientId, correlationId);
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) {
        return new RequestHeader(apiKey, apiVersion, "client-id", 1);
    }

    public static void send(AbstractRequest request, Socket socket, String clientId, Integer correlationId) throws IOException {
        RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), clientId, correlationId);
        sendWithHeader(request, header, socket);
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException {
        DataInputStream incoming = new DataInputStream(socket.getInputStream());
        int len = incoming.readInt();

        byte[] responseBytes = new byte[len];
        incoming.readFully(responseBytes);

        ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes);
        ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version));

        AbstractResponse response = AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version);
        if (response instanceof AbstractResponse) {
            return (T) response;
        } else {
            throw new ClassCastException("Expected response with type " + AbstractResponse.class + ", but found " + response.getClass());
        }
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket,
        String clientId,
        Integer correlationId
    ) throws IOException {

Review comment (refactor):
```java
    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket
    ) throws IOException {
        send(request, socket, "client-id", correlationId);
        return receive(socket, request.apiKey(), request.version());
    }
```

Code after:
    public static void sendRequest(Socket socket, byte[] request) throws IOException {
        DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream());
        outgoing.writeInt(request.length);
        outgoing.write(request);
        outgoing.flush();
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) {
        return new RequestHeader(apiKey, apiVersion, "client-id", CORRELATION_ID.getAndIncrement());
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException {
        var incoming = new DataInputStream(socket.getInputStream());
        int len = incoming.readInt();

        var responseBytes = new byte[len];
        incoming.readFully(responseBytes);

        var responseBuffer = ByteBuffer.wrap(responseBytes);
        ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version));

        return (T) AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version);
    }

    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket
    ) throws IOException {
        var header = nextRequestHeader(request.apiKey(), request.version());
        sendRequest(socket, Utils.toArray(request.serializeWithHeader(header)));
        return receive(socket, request.apiKey(), request.version());
    }

    public static <T extends AbstractResponse> T connectAndReceive(
        AbstractRequest request,
        int port
    ) throws IOException {
        try (Socket socket = connect(port)) {
            return sendAndReceive(request, socket);
        }
    }

    public static Socket connect(int port) throws IOException {
        return new Socket("localhost", port);
    }
}