### Question:
ConnectedState implements BoltStateMachineState { @Override public BoltStateMachineState process( RequestMessage message, StateMachineContext context ) throws BoltConnectionFatality { assertInitialized(); if ( message instanceof InitMessage ) { InitMessage initMessage = (InitMessage) message; String userAgent = initMessage.userAgent(); Map<String,Object> authToken = initMessage.authToken(); if ( processAuthentication( userAgent, authToken, context ) ) { return readyState; } else { return failedState; } } return null; }  @Override BoltStateMachineState process( RequestMessage message, StateMachineContext context ); @Override String name(); void setReadyState( BoltStateMachineState readyState ); void setFailedState( BoltStateMachineState failedState );  }### Answer:
@Test void shouldAuthenticateOnInitMessage() throws Exception { BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( readyState, newState ); verify( boltSpi ).authenticate( AUTH_TOKEN ); }
@Test void shouldInitializeStatementProcessorOnInitMessage() throws Exception { BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( readyState, newState ); assertNotNull( connectionState.getStatementProcessor() ); }
@Test void shouldAddMetadataOnExpiredCredentialsOnInitMessage() throws Exception { MutableConnectionState connectionStateMock = mock( MutableConnectionState.class ); when( context.connectionState() ).thenReturn( connectionStateMock ); AuthenticationResult authResult = mock( AuthenticationResult.class ); when( authResult.credentialsExpired() ).thenReturn( true ); when( authResult.getLoginContext() ).thenReturn( LoginContext.AUTH_DISABLED ); when( boltSpi.authenticate( AUTH_TOKEN ) ).thenReturn( authResult ); BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( readyState, newState ); verify( connectionStateMock ).onMetadata( "credentials_expired", TRUE ); }
@Test void shouldAddServerVersionMetadataOnInitMessage() throws Exception { when( boltSpi.version() ).thenReturn( "42.42.42" ); MutableConnectionState connectionStateMock = mock( MutableConnectionState.class ); when( context.connectionState() ).thenReturn( connectionStateMock ); AuthenticationResult authResult = mock( AuthenticationResult.class ); when( authResult.credentialsExpired() ).thenReturn( true ); when( authResult.getLoginContext() ).thenReturn( LoginContext.AUTH_DISABLED ); when( boltSpi.authenticate( AUTH_TOKEN ) ).thenReturn( authResult ); BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( readyState, newState ); verify( connectionStateMock ).onMetadata( "server", stringValue( "42.42.42" ) ); }
@Test void shouldRegisterClientInUDCOnInitMessage() throws Exception { BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( readyState, newState ); verify( boltSpi ).udcRegisterClient( eq( USER_AGENT ) ); }
@Test void shouldHandleFailuresOnInitMessage() throws Exception { RuntimeException error = new RuntimeException( "Hello" ); when( boltSpi.authenticate( AUTH_TOKEN ) ).thenThrow( error ); BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( failedState, newState ); verify( context ).handleFailure( error, true ); }
@Test void shouldNotProcessUnsupportedMessage() throws Exception { List<RequestMessage> unsupportedMessages = asList( AckFailureMessage.INSTANCE, DiscardAllMessage.INSTANCE, InterruptSignal.INSTANCE, PullAllMessage.INSTANCE, ResetMessage.INSTANCE, new RunMessage( "RETURN 1", EMPTY_MAP ) ); for ( RequestMessage message: unsupportedMessages ) { assertNull( state.process( message, context ) ); } }