package org.realityforge.replicant.server;

import java.io.Serializable;
import java.util.HashMap;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class EntityMessageTest
{
  @Test
  public void mergeElementsOverrideExisting()
  {
    final String id = "myID";
    final int typeID = 42;

    final EntityMessage message = MessageTestUtil.createMessage( id, typeID, "r1", "r2", "a1", "a2" );

    assertEquals( message.getID(), id );
    assertEquals( message.getTypeID(), typeID );
    MessageTestUtil.assertAttributeValue( message, MessageTestUtil.ATTR_KEY1, "a1" );
    MessageTestUtil.assertAttributeValue( message, MessageTestUtil.ATTR_KEY2, "a2" );
    MessageTestUtil.assertRouteValue( message, MessageTestUtil.ROUTING_KEY1, "r1" );
    MessageTestUtil.assertRouteValue( message, MessageTestUtil.ROUTING_KEY2, "r2" );

    final HashMap<String, Serializable> routingKeys2 = new HashMap<String, Serializable>();
    routingKeys2.put( MessageTestUtil.ROUTING_KEY1, "r3" );

    final HashMap<String, Serializable> attributeValues2 = new HashMap<String, Serializable>();
    attributeValues2.put( MessageTestUtil.ATTR_KEY1, "a3" );

    final EntityMessage message2 =
        new EntityMessage( id, typeID, routingKeys2, attributeValues2 );

    message.merge( message2 );

    assertEquals( message.getID(), id );
    assertEquals( message.getTypeID(), typeID );
    MessageTestUtil.assertAttributeValue( message, MessageTestUtil.ATTR_KEY1, "a3" );
    MessageTestUtil.assertAttributeValue( message, MessageTestUtil.ATTR_KEY2, "a2" );
    MessageTestUtil.assertRouteValue( message, MessageTestUtil.ROUTING_KEY1, "r3" );
    MessageTestUtil.assertRouteValue( message, MessageTestUtil.ROUTING_KEY2, "r2" );

    assertTrue( message.toString().matches( ".*Data=.*" ) );
  }

  @Test
  public void toStringIncludesDataWhenDataPresent()
  {
    final EntityMessage message = MessageTestUtil.createMessage( "myID", 42, "r1", "r2", "a1", "a2" );
    assertTrue( message.toString().matches( ".*Data=.*" ) );
  }

  @Test
  public void toIsDeleteFlagIsCorrect()
  {
    final EntityMessage deleteMessage = MessageTestUtil.createMessage( "myID", 42, "r1", "r2", null, null );
    assertFalse( deleteMessage.toString().matches( ".*Data=.*" ) );
    assertFalse( deleteMessage.isUpdate() );
    assertTrue( deleteMessage.isDelete() );
  }
}

@@ -41,63 +41,8 @@
 r;%0A%0A
-import java.io.Serializable;%0Aimport java.util.HashMap;%0A
 impo
@@ -809,413 +809,98 @@
 nal 
-HashMap%3CString, Serializable%3E routingKeys2 = new HashMap%3CString, Serializable%3E();%0A    routingKeys2.put( MessageTestUtil.ROUTING_KEY1, %22r3%22 );%0A%0A    final HashMap%3CString, Serializable%3E attributeValues2 = new HashMap%3CString, Serializable%3E();%0A    attributeValues2.put( MessageTestUtil.ATTR_KEY1, %22a3%22 );%0A%0A    final EntityMessage message2 =%0A        new EntityMessage( id, typeID, routingKeys2, attributeValues2
+EntityMessage message2 = MessageTestUtil.createMessage( id, typeID, %22r3%22, null, %22a3%22, null
  );%0A
@@ -1368,65 +1368,938 @@
  );%0A
-%0A    assertTrue( message.toString().matches( %22.*Data=.*%22 
+  %7D%0A%0A  @Test%0A  public void mergeDeletedEnsuresDeleted()%0A  %7B%0A    final String id = %22myID%22;%0A    final int typeID = 42;%0A%0A    final EntityMessage message = MessageTestUtil.createMessage( id, typeID, %22r1%22, %22r2%22, %22a1%22, %22a2%22 );%0A%0A    assertTrue( message.isUpdate() );%0A    assertFalse( message.isDelete() );%0A%0A    message.merge( MessageTestUtil.createMessage( id, typeID, %22r1%22, %22r2%22, null, null ) );%0A%0A    assertFalse( message.isUpdate() );%0A    assertTrue( message.isDelete() );%0A  %7D%0A%0A  @Test%0A  public void mergeUpdateRevivesDeleted()%0A  %7B%0A    final String id = %22myID%22;%0A    final int typeID = 42;%0A%0A    final EntityMessage message = MessageTestUtil.createMessage( id, typeID, %22r1%22, %22r2%22, null, null );%0A%0A    assertFalse( message.isUpdate() );%0A    assertTrue( message.isDelete() );%0A%0A    message.merge( MessageTestUtil.createMessage( id, typeID, %22r1%22, %22r2%22, %22a1%22, %22a2%22 ) );%0A%0A    assertTrue( message.isUpdate() );%0A    assertFalse( message.isDelete(
 ) );
@@ -2296,32 +2296,33 @@
 elete() );%0A  %7D%0A%0A
+%0A
   @Test%0A  public

Improve the tests to cover off merging delete messages