### Question:
RemoteValidatorApi { @VisibleForTesting void subscribeValidator(final WsConnectContext handler) { final SubscriptionStatus subscriptionStatus = subscriptionManager.subscribe( handler.getSessionId(), (msg) -> { try { final String json = jsonProvider.objectToJSON(msg); handler .session .getRemote() .sendString( json, new WriteCallback() { @Override public void writeFailed(final Throwable x) { LOG.error( "Error sending msg to validator {}", handler.getSessionId(), x); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } @Override public void writeSuccess() {} }); } catch (JsonProcessingException e) { LOG.error("Error sending msg to validator {}", handler.getSessionId(), e); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } }); if (!subscriptionStatus.hasSubscribed()) { handler.session.close(StatusCode.NORMAL, subscriptionStatus.getInfo()); } } RemoteValidatorApi(
      final GlobalConfiguration configuration,
      final RemoteValidatorSubscriptions subscriptionManager);   }### Answer:
@Test public void onConnectCallback_ShouldSubscribeValidatorWithSessionId() { final String sessionId = wsContext.getSessionId(); when(subscriptionManager.subscribe(any(), any())).thenReturn(SubscriptionStatus.success()); remoteValidatorApi.subscribeValidator(wsContext); verify(subscriptionManager).subscribe(eq(sessionId), any()); }
@Test public void onConnectCallback_WhenSubscribeFails_ShouldCloseSession() { final SubscriptionStatus failedSubscriptionStatus = SubscriptionStatus.maxSubscribers(); when(subscriptionManager.subscribe(any(), any())).thenReturn(failedSubscriptionStatus); remoteValidatorApi.subscribeValidator(wsContext); verify(wsSession).close(eq(StatusCode.NORMAL), eq(failedSubscriptionStatus.getInfo())); }
@Test public void onConnectCallback_SubscriptionCallbackShouldSendEventOverTheWire() throws Exception { when(subscriptionManager.subscribe(any(), any())).thenReturn(SubscriptionStatus.success()); final RemoteEndpoint remoteEndpoint = mock(RemoteEndpoint.class); when(wsSession.getRemote()).thenReturn(remoteEndpoint); remoteValidatorApi.subscribeValidator(wsContext); verify(subscriptionManager).subscribe(any(), subscriberCallbackArgCaptor.capture()); final Consumer<BeaconChainEvent> subscriberCallback = subscriberCallbackArgCaptor.getValue(); subscriberCallback.accept(new BeaconChainEvent("foo", UInt64.ONE)); verify(remoteEndpoint).sendString(eq("{\"name\":\"foo\",\"data\":\"1\"}"), any()); }
@Test public void onConnectCallback_SubscriptionCallbackShouldCloseSessionOnFailure() throws Exception { when(subscriptionManager.subscribe(any(), any())).thenReturn(SubscriptionStatus.success()); final RemoteEndpoint remoteEndpoint = mock(RemoteEndpoint.class); doAnswer( invocation -> { final WriteCallback callback = invocation.getArgument(1); callback.writeFailed(new IOException()); return null; }) .when(remoteEndpoint) .sendString(any(), any()); when(wsSession.getRemote()).thenReturn(remoteEndpoint); remoteValidatorApi.subscribeValidator(wsContext); verify(subscriptionManager).subscribe(any(), subscriberCallbackArgCaptor.capture()); final Consumer<BeaconChainEvent> subscriberCallback = subscriberCallbackArgCaptor.getValue(); subscriberCallback.accept(new BeaconChainEvent("foo", UInt64.ONE)); verify(wsSession) .close(eq(StatusCode.SERVER_ERROR), eq("Unexpected error on Remote Validator server")); }