package org.realityforge.replicant.server;

import java.util.Collection;
import java.util.Collections;
import javax.json.Json;
import javax.json.JsonObject;
import org.realityforge.replicant.server.ChannelAction.Action;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

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

    final EntityMessage message1 = MessageTestUtil.createMessage( id, typeID, 0, "r1", "r2", "a1", "a2" );
    final EntityMessage message2 = MessageTestUtil.createMessage( id, typeID, 0, "r1", "r3", "aZ", "a2" );
    final EntityMessage message3 = MessageTestUtil.createMessage( "X", 42, 0, "X", "X", "X", "X" );

    final Change change1 = new Change( message1 );
    final Change change2 = new Change( message2 );
    final Change change3 = new Change( message3 );

    change1.getChannels().put( 1, 1 );
    change2.getChannels().put( 2, 3 );
    change3.getChannels().put( 3, "S" );

    final ChangeSet changeSet = new ChangeSet();

    assertEquals( changeSet.getChanges().size(), 0 );

    changeSet.mergeAll( Collections.singletonList( change1 ) );

    assertEquals( changeSet.getChanges().size(), 1 );
    assertEquals( change1.getChannels().size(), 1 );

    changeSet.merge( change2 );

    assertEquals( changeSet.getChanges().size(), 1 );
    assertEquals( change1.getChannels().size(), 2 );

    //Re-merge same
    changeSet.merge( change2 );

    assertEquals( changeSet.getChanges().size(), 1 );
    assertEquals( change1.getChannels().size(), 2 );

    changeSet.merge( change3 );

    assertEquals( changeSet.getChanges().size(), 2 );
  }

  @Test
  public void actions()
  {
    final ChangeSet changeSet = new ChangeSet();

    assertEquals( changeSet.getChannelActions().size(), 0 );

    final JsonObject filter = Json.createBuilderFactory( null ).createObjectBuilder().build();
    changeSet.addAction( new ChannelAction( new ChannelDescriptor( 1, 2 ), Action.ADD, filter ) );

    assertEquals( changeSet.getChannelActions().size(), 1 );

    final ChannelAction action = changeSet.getChannelActions().get( 0 );
    assertEquals( action.getChannelDescriptor().getChannelID(), 1 );
    assertEquals( action.getChannelDescriptor().getSubChannelID(), 2 );
    assertEquals( action.getAction(), Action.ADD );
    assertEquals( action.getFilter(), filter );
  }

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

    final EntityMessage message1 = MessageTestUtil.createMessage( id, typeID, 0, "r1", "r2", "a1", "a2" );
    final Change change1 = new Change( message1 );

    final ChangeSet changeSet = new ChangeSet();

    assertEquals( changeSet.getChanges().size(), 0 );

    changeSet.merge( change1, true );

    final Collection<Change> changes = changeSet.getChanges();
    assertEquals( changes.size(), 1 );
    final Change change = changes.iterator().next();
    assertEquals( change.getEntityMessage().getID(), id );
    assertNotSame( change, change1 );
  }
}

@@ -96,16 +96,45 @@
 ctions;%0A
+import java.util.LinkedList;%0A
 import j
@@ -3090,10 +3090,1304 @@
  );%0A  %7D%0A
+%0A%0A  @Test%0A  public void fullMerge()%0A  %7B%0A    final ChangeSet changeSet = new ChangeSet();%0A%0A    final String id = %22myID%22;%0A    final int typeID = 42;%0A%0A    final EntityMessage message1 = MessageTestUtil.createMessage( id, typeID, 0, %22r1%22, %22r2%22, %22a1%22, %22a2%22 );%0A    final Change change1 = new Change( message1 );%0A    changeSet.merge( change1 );%0A%0A    final JsonObject filter = Json.createBuilderFactory( null ).createObjectBuilder().build();%0A    changeSet.addAction( new ChannelAction( new ChannelDescriptor( 1, 2 ), Action.ADD, filter ) );%0A%0A    final ChangeSet changeSet2 = new ChangeSet();%0A    changeSet2.merge( changeSet, true );%0A%0A    final Collection%3CChange%3E changes = changeSet2.getChanges();%0A    assertEquals( changes.size(), 1 );%0A    final Change change = changes.iterator().next();%0A    assertEquals( change.getEntityMessage().getID(), id );%0A    assertNotSame( change, change1 );%0A%0A    final LinkedList%3CChannelAction%3E actions = changeSet2.getChannelActions();%0A    assertEquals( actions.size(), 1 );%0A%0A    final ChannelAction action = actions.get( 0 );%0A    assertEquals( action.getChannelDescriptor().getChannelID(), 1 );%0A    assertEquals( action.getChannelDescriptor().getSubChannelID(), 2 );%0A    assertEquals( action.getAction(), Action.ADD );%0A    assertEquals( action.getFilter(), filter );%0A  %7D%0A
 %7D%0A

Add tests for fullMerge