package replicant;

import org.realityforge.guiceyloops.shared.ValueUtil;
import org.testng.annotations.Test;
import static org.mockito.Mockito.*;
import static org.testng.Assert.*;

public class ConnectionTest
  extends AbstractReplicantTest
{
  @Test
  public void basicRequestManagementWorkflow()
  {
    final Connection connection = new Connection( null, ValueUtil.randomString() );
    final String requestName = ValueUtil.randomString();
    final String cacheKey = ValueUtil.randomString();

    final RequestEntry request = connection.newRequest( requestName, cacheKey );
    assertEquals( request.getName(), requestName );
    assertEquals( request.getCacheKey(), cacheKey );

    assertEquals( connection.getRequest( request.getRequestId() ), request );
    assertEquals( connection.getRequests().get( request.getRequestId() ), request );
    assertEquals( connection.getRequest( "NotHere" + request.getRequestId() ), null );

    assertTrue( connection.removeRequest( request.getRequestId() ) );
    assertFalse( connection.removeRequest( request.getRequestId() ) );

    assertEquals( connection.getRequest( request.getRequestId() ), null );
  }

  @Test
  public void completeNormalRequest()
  {
    final Connection connection = new Connection( null, ValueUtil.randomString() );
    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );
    final SafeProcedure action = mock( SafeProcedure.class );

    connection.completeRequest( e, action );

    verify( action ).call();
    assertEquals( e.getCompletionAction(), null );
    assertNull( connection.getRequest( e.getRequestId() ) );
  }

  @Test
  public void completeNormalRequest_expectingResults()
  {
    final Connection connection = new Connection( null, ValueUtil.randomString() );
    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );
    final SafeProcedure action = mock( SafeProcedure.class );

    e.setExpectingResults( true );

    connection.completeRequest( e, action );

    verify( action, never() ).call();

    assertEquals( e.getCompletionAction(), action );
    assertNotNull( connection.getRequest( e.getRequestId() ) );
  }

  @Test
  public void completeNormalRequest_resultsArrived()
  {
    final Connection connection = new Connection( null, ValueUtil.randomString() );
    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );
    final SafeProcedure action = mock( SafeProcedure.class );

    e.setExpectingResults( true );
    e.markResultsAsArrived();

    connection.completeRequest( e, action );

    verify( action ).call();
    assertEquals( e.getCompletionAction(), null );
    assertNull( connection.getRequest( e.getRequestId() ) );
  }

  @Test
  public void completeNonNormalRequest()
  {
    final Connection connection = new Connection( null, ValueUtil.randomString() );
    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );
    final SafeProcedure action = mock( SafeProcedure.class );

    connection.completeRequest( e, action );

    verify( action ).call();
    assertEquals( e.getCompletionAction(), null );
    assertNull( connection.getRequest( e.getRequestId() ) );
  }

  @Test
  public void completeRequest_expectingResults()
  {
    final Connection connection = new Connection( null, ValueUtil.randomString() );
    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );
    final SafeProcedure action = mock( SafeProcedure.class );

    e.setExpectingResults( true );

    connection.completeRequest( e, action );

    verify( action, never() ).call();
    assertEquals( e.getCompletionAction(), action );
    assertNotNull( connection.getRequest( e.getRequestId() ) );
  }
}

@@ -1179,30 +1179,24 @@
 oid complete
-Normal
 Request()%0A  
@@ -1662,38 +1662,32 @@
 ic void complete
-Normal
 Request_expectin
@@ -2225,22 +2225,16 @@
 complete
-Normal
 Request_
@@ -2769,1050 +2769,25 @@
 %0A%0A  
-@Test%0A  public void completeNonNormalRequest()%0A  %7B%0A    final Connection connection = new Connection( null, ValueUtil.randomString() );%0A    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );%0A    final SafeProcedure action = mock( SafeProcedure.class );%0A%0A    connection.completeRequest( e, action );%0A%0A    verify( action ).call();%0A    assertEquals( e.getCompletionAction(), null );%0A    assertNull( connection.getRequest( e.getRequestId() ) );%0A  %7D%0A%0A  @Test%0A  public void completeRequest_expectingResults()%0A  %7B%0A    final Connection connection = new Connection( null, ValueUtil.randomString() );%0A    final RequestEntry e = connection.newRequest( ValueUtil.randomString(), ValueUtil.randomString() );%0A    final SafeProcedure action = mock( SafeProcedure.class );%0A%0A    e.setExpectingResults( true );%0A%0A    connection.completeRequest( e, action );%0A%0A    verify( action, never() ).call();%0A    assertEquals( e.getCompletionAction(), action );%0A    assertNotNull( connection.getRequest( e.getRequestId() ) );
+enum G%0A  %7B%0A    G1
 %0A  %7D

Remove Remove tests that execute same code path and rename remainign tests to reflect new functionality