BrokerConnection implements Connection { public RpcResponse send(RpcRequest request) throws RpcException { if (!isAuthenticated() && !isAnonymousRpc(request)) { throw new AuthenticationException("unable to call RPC on an unauthenticated connection"); } if (isNotInCurrentContext(request)) setCurrentRpcContext(request.getRpcContext()); RpcResponse response = sendInternal(request); if (!response.getSecuritySegment().isEmpty()) { if (response.getSecuritySegment().endsWith("exist on the server.")) { throw new RpcNotFoundException(response.getSecuritySegment()); } else { throw new InternalServerException(response.getSecuritySegment()); } } metrics.incrementResponseCount(response.getElapsedMillis()); return response; } BrokerConnection(RpcHost host, Socket socket, RpcProtocol protocol); RpcHost getHost(); SystemInfo getSystemInfo(); ConnectionUserDetails getUserDetails(); boolean isAuthenticated(); synchronized void stop(); RpcResponse send(RpcRequest request); synchronized RpcResponse sendInternal(RpcRequest request); synchronized void close(); boolean isClosed(); boolean isStale(); @Override ConnectionMetrics getMetrics(); @Override long getElapsedMillis(); void setElapsedMillis(long millis); SystemInfo fetchSystemInfo(); void pulse(); void authenticate(AccessVerifyConnectionSpec av); void authenticate(AppHandleConnectionSpec appHandleConnectionSpec); String getCurrentRpcContext();  }
@Test public void send() throws RpcException { ReflectionTestUtils.setField(c, "user", new ConnectionUser()); RpcRequest request = new RpcRequest("BAZ/FOO BAR"); RpcResponse response = new RpcResponse("foobar"); when(mockReader.readResponse()) .thenReturn(new RpcResponse("1")) .thenReturn(response); RpcResponse r = c.send(request); assertThat(r, is(sameInstance(response))); assertThat(c.getCurrentRpcContext(), is("BAZ")); ArgumentCaptor<RpcRequest> requestsWritten = ArgumentCaptor.forClass(RpcRequest.class); verify(mockWriter, times(2)).write(requestsWritten.capture()); assertThat(requestsWritten.getAllValues().get(0), encryptedArgRpcEq(getCreateContextRequest("BAZ"))); assertThat(requestsWritten.getAllValues().get(1), is(request)); verify(mockWriter, times(2)).flush(); verify(mockReader, times(2)).readResponse(); }