HelloMessageEncoder implements MessageEncoder { @Override public void encode( Message message, ValuePacker packer ) throws IOException { checkArgument( message, HelloMessage.class ); HelloMessage helloMessage = (HelloMessage) message; packer.packStructHeader( 1, helloMessage.signature() ); packer.pack( helloMessage.metadata() ); }  @Override void encode( Message message, ValuePacker packer );  }
@Test void shouldEncodeHelloMessage() throws Exception { Map<String,Value> authToken = new HashMap<>(); authToken.put( "username", value( "bob" ) ); authToken.put( "password", value( "secret" ) ); encoder.encode( new HelloMessage( "MyDriver", authToken, null ), packer ); InOrder order = inOrder( packer ); order.verify( packer ).packStructHeader( 1, HelloMessage.SIGNATURE ); Map<String,Value> expectedMetadata = new HashMap<>( authToken ); expectedMetadata.put( "user_agent", value( "MyDriver" ) ); expectedMetadata.put( "routing", NULL ); order.verify( packer ).pack( expectedMetadata ); }
@Test void shouldEncodeHelloMessageWithRoutingContext() throws Exception { Map<String,Value> authToken = new HashMap<>(); authToken.put( "username", value( "bob" ) ); authToken.put( "password", value( "secret" ) ); Map<String,String> routingContext = new HashMap<>(); routingContext.put( "policy", "eu-fast" ); encoder.encode( new HelloMessage( "MyDriver", authToken, routingContext ), packer ); InOrder order = inOrder( packer ); order.verify( packer ).packStructHeader( 1, HelloMessage.SIGNATURE ); Map<String,Value> expectedMetadata = new HashMap<>( authToken ); expectedMetadata.put( "user_agent", value( "MyDriver" ) ); expectedMetadata.put( "routing", value( routingContext ) ); order.verify( packer ).pack( expectedMetadata ); }
@Test void shouldFailToEncodeWrongMessage() { assertThrows( IllegalArgumentException.class, () -> encoder.encode( PULL_ALL, packer ) ); }