package com.groupon.lex.metrics.timeseries;

import com.groupon.lex.metrics.history.CollectHistory;
import java.util.stream.Stream;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ChainingTSCPairTest {
    @Mock
    private CollectHistory history;
    @Mock
    private TimeSeriesCollection current;

    private static final DateTime TS0 = DateTime.now(DateTimeZone.UTC);

    @Before
    public void setup() {
        when(current.getTimestamp()).thenReturn(TS0);
        when(history.streamReversed()).thenReturn(Stream.empty());
    }

    @Test
    public void emptyHistoryTest() {
        ChainingTSCPair tsdata = new ChainingTSCPair(history, ExpressionLookBack.EMPTY) {
            @Override
            public TimeSeriesCollection getCurrentCollection() {
                return current;
            }
        };

        assertEquals(1, tsdata.size());
        assertSame(current, tsdata.getPreviousCollection(0).get());
    }
}

@@ -73,30 +73,323 @@
 ics.
-history.CollectHistory
+GroupName;%0Aimport com.groupon.lex.metrics.history.CollectHistory;%0Aimport com.groupon.lex.metrics.lib.SimpleMapEntry;%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport static java.util.Collections.EMPTY_MAP;%0Aimport static java.util.Collections.singleton;%0Aimport java.util.List
 ;%0Aim
@@ -484,16 +484,47 @@
 meZone;%0A
+import org.joda.time.Duration;%0A
 import s
@@ -709,16 +709,44 @@
 o.Mock;%0A
+import org.mockito.Mockito;%0A
 import s
@@ -1070,164 +1070,1162 @@
 C);%0A
-%0A    @Before%0A    public void setup() %7B%0A        when(current.getTimestamp()).thenReturn(TS0);%0A        when(history.streamReversed()).thenReturn(Stream.empty(
+    private static final GroupName GROUP_NAME = GroupName.valueOf(%22GROUP%22);%0A%0A    private TimeSeriesValue tsv1 = new ImmutableTimeSeriesValue(GROUP_NAME, EMPTY_MAP),%0A            tsv2 = new ImmutableTimeSeriesValue(GROUP_NAME, EMPTY_MAP);%0A    private TimeSeriesCollection tsc1 = new SimpleTimeSeriesCollection(TS0.minus(Duration.standardMinutes(1)), singleton(tsv1)),%0A            tsc2 = new SimpleTimeSeriesCollection(TS0.minus(Duration.standardMinutes(2)), singleton(tsv2));%0A%0A    @Before%0A    public void setup() %7B%0A        List%3CTimeSeriesCollection%3E tsdata = Arrays.asList(tsc1, tsc2);%0A        List%3CTimeSeriesCollection%3E tsdataReversed = new ArrayList%3C%3E(tsdata);%0A        Collections.reverse(tsdataReversed);%0A%0A        when(current.getTimestamp()).thenReturn(TS0);%0A        when(history.streamReversed()).thenAnswer((invocation) -%3E tsdataReversed.stream());%0A        when(history.stream()).thenAnswer((invocation) -%3E tsdata.stream());%0A        when(history.streamGroup(Mockito.any(), Mockito.eq(GROUP_NAME)))%0A                .thenAnswer((invocation) -%3E Stream.of(SimpleMapEntry.create(tsc2.getTimestamp(), tsv2), SimpleMapEntry.create(tsc1.getTimestamp(), tsv1)
 ));%0A
@@ -2261,24 +2261,14 @@
 oid 
-emptyHistoryTest
+create
 () %7B
@@ -2349,21 +2349,34 @@
 ookBack.
-EMPTY
+fromScrapeCount(3)
 ) %7B%0A    
@@ -2537,17 +2537,17 @@
 tEquals(
-1
+3
 , tsdata
@@ -2620,20 +2620,147 @@
 (0).get());%0A
+        assertEquals(tsc1, tsdata.getPreviousCollection());%0A        assertEquals(tsc2, tsdata.getPreviousCollection(2).get());%0A
     %7D%0A%7D%0A

Test some of the functionality of ChainingTSCPair.