package com.rackspacecloud.blueflood.service;

import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.rackspacecloud.blueflood.io.AstyanaxWriter;
import org.junit.Test;
import org.mockito.Matchers;

import java.util.ArrayList;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class RollupBatchWriteRunnableTest {

    @Test
    public void runSendsRollupsToWrierAndDecrementsCount() throws ConnectionException {

        // given
        ArrayList<SingleRollupWriteContext> wcs = new ArrayList<SingleRollupWriteContext>();
        ArrayList<SingleRollupWriteContext> wcs2 = new ArrayList<SingleRollupWriteContext>();
        RollupExecutionContext ctx = mock(RollupExecutionContext.class);
        AstyanaxWriter writer = mock(AstyanaxWriter.class);
        RollupBatchWriteRunnable rbwr = new RollupBatchWriteRunnable(wcs, ctx, writer);

        // when
        rbwr.run();

        // then
        verify(writer).insertRollups(Matchers.<ArrayList<SingleRollupWriteContext>>any());
        verifyNoMoreInteractions(writer);
        verify(ctx).decrementWriteCounter(anyLong());
        verifyNoMoreInteractions(ctx);
    }
}

@@ -252,16 +252,117 @@
 yList;%0A%0A
+import static junit.framework.Assert.assertNotNull;%0Aimport static junit.framework.Assert.assertSame;%0A
 import s
@@ -519,114 +519,9 @@
 ito.
-mock;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.verifyNoMoreInteractions
+*
 ;%0A%0Ap
@@ -1373,11 +1373,2237 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void connectionExceptionMarksUnsuccessful() throws ConnectionException %7B%0A%0A        // given%0A        ArrayList%3CSingleRollupWriteContext%3E wcs = new ArrayList%3CSingleRollupWriteContext%3E();%0A        ArrayList%3CSingleRollupWriteContext%3E wcs2 = new ArrayList%3CSingleRollupWriteContext%3E();%0A        RollupExecutionContext ctx = mock(RollupExecutionContext.class);%0A        AstyanaxWriter writer = mock(AstyanaxWriter.class);%0A        Throwable cause = new ConnectionException(%22exception for testing purposes%22) %7B %7D;%0A        doThrow(cause).when(writer).insertRollups(%0A                Matchers.%3CArrayList%3CSingleRollupWriteContext%3E%3Eany());%0A        RollupBatchWriteRunnable rbwr = new RollupBatchWriteRunnable(wcs, ctx, writer);%0A%0A        // when%0A        rbwr.run();%0A%0A        // then%0A        verify(writer).insertRollups(Matchers.%3CArrayList%3CSingleRollupWriteContext%3E%3Eany());%0A        verifyNoMoreInteractions(writer);%0A        verify(ctx).markUnsuccessful(Matchers.%3CThrowable%3Eany());%0A        verify(ctx).decrementWriteCounter(anyLong());%0A        verifyNoMoreInteractions(ctx);%0A    %7D%0A%0A    @Test%0A    public void otherExceptionBreaksEverything() throws ConnectionException %7B%0A%0A        // given%0A        ArrayList%3CSingleRollupWriteContext%3E wcs = new ArrayList%3CSingleRollupWriteContext%3E();%0A        ArrayList%3CSingleRollupWriteContext%3E wcs2 = new ArrayList%3CSingleRollupWriteContext%3E();%0A        RollupExecutionContext ctx = mock(RollupExecutionContext.class);%0A        AstyanaxWriter writer = mock(AstyanaxWriter.class);%0A        Throwable cause = new UnsupportedOperationException(%22exception for testing purposes%22);%0A        doThrow(cause).when(writer).insertRollups(%0A                Matchers.%3CArrayList%3CSingleRollupWriteContext%3E%3Eany());%0A        RollupBatchWriteRunnable rbwr = new RollupBatchWriteRunnable(wcs, ctx, writer);%0A%0A        // when%0A        Throwable caught = null;%0A        try %7B%0A            rbwr.run();%0A        %7D catch (Throwable t) %7B%0A            caught = t;%0A        %7D%0A%0A        // then%0A        assertNotNull(caught);%0A        assertSame(cause, caught);%0A        verify(writer).insertRollups(Matchers.%3CArrayList%3CSingleRollupWriteContext%3E%3Eany());%0A        verifyNoMoreInteractions(writer);%0A        verifyZeroInteractions(ctx);%0A    %7D
 %0A%7D%0A

Test exception handling.