package com.rackspacecloud.blueflood.service;

import com.rackspacecloud.blueflood.rollup.Granularity;
import com.rackspacecloud.blueflood.rollup.SlotKey;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class ScheduleContextAreKeysRunningTest {

    @Test
    public void noneScheduledOrRunningReturnsFalse() {

        // given
        long currentTime = 1234000L;

        final int shard = 0;
        List<Integer> managedShards = new ArrayList<Integer>() {{ add(shard); }};

        ScheduleContext ctx = new ScheduleContext(currentTime, managedShards);

        int slot = Granularity.MIN_5.slot(currentTime);
        SlotKey slotkey = SlotKey.of(Granularity.MIN_5, slot, shard);

        // when
        boolean areKeysRunning = ctx.areChildKeysOrSelfKeyScheduledOrRunning(slotkey);

        // then
        assertFalse(areKeysRunning);
    }

    @Test
    public void slotScheduledReturnsTrue() {

        // given
        long currentTime = 1234000L;

        final int shard = 0;
        List<Integer> managedShards = new ArrayList<Integer>() {{ add(shard); }};

        ScheduleContext ctx = new ScheduleContext(currentTime, managedShards);
        ctx.update(currentTime - 2, shard);
        ctx.scheduleEligibleSlots(1, 7200000);

        int slot = Granularity.MIN_5.slot(currentTime);
        SlotKey slotkey = SlotKey.of(Granularity.MIN_5, slot, shard);

        // when
        boolean areKeysRunning = ctx.areChildKeysOrSelfKeyScheduledOrRunning(slotkey);

        // then
        assertTrue(areKeysRunning);
    }

    @Test
    public void childSlotScheduledReturnsTrue() {

        // given
        long currentTime = 1234000L;

        final int shard = 0;
        List<Integer> managedShards = new ArrayList<Integer>() {{ add(shard); }};

        ScheduleContext ctx = new ScheduleContext(currentTime, managedShards);
        ctx.update(currentTime - 2, shard);
        ctx.scheduleEligibleSlots(1, 7200000);

        int slot = Granularity.MIN_20.slot(currentTime);
        SlotKey slotkey = SlotKey.of(Granularity.MIN_20, slot, shard);

        // when
        boolean areKeysRunning = ctx.areChildKeysOrSelfKeyScheduledOrRunning(slotkey);

        // then
        assertTrue(areKeysRunning);
    }

    @Test
    public void unrelatedSlotScheduledReturnsFalse() {

        // given
        long currentTime = 1234000L;

        final int shard = 0;
        List<Integer> managedShards = new ArrayList<Integer>() {{ add(shard); }};

        ScheduleContext ctx = new ScheduleContext(currentTime, managedShards);
        ctx.update(currentTime - 2, shard);
        ctx.scheduleEligibleSlots(1, 7200000);

        int slot = Granularity.MIN_5.slot(currentTime - 5*60*1000); // check the previous slot from 5 minutes ago
        SlotKey slotkey = SlotKey.of(Granularity.MIN_5, slot, shard);

        // when
        boolean areKeysRunning = ctx.areChildKeysOrSelfKeyScheduledOrRunning(slotkey);

        // then
        assertFalse(areKeysRunning);
    }
}

@@ -220,24 +220,69 @@
 util.List;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import stati
@@ -359,48 +359,8 @@
 rue;
-%0Aimport static org.mockito.Mockito.mock;
 %0A%0Apu
@@ -3137,11 +3137,1561 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void slotRunningReturnsTrue() %7B%0A%0A        // given%0A        long currentTime = 1234000L;%0A%0A        final int shard = 0;%0A        List%3CInteger%3E managedShards = new ArrayList%3CInteger%3E() %7B%7B add(shard); %7D%7D;%0A%0A        ScheduleContext ctx = new ScheduleContext(currentTime, managedShards);%0A        ctx.update(currentTime - 2, shard);%0A        ctx.scheduleEligibleSlots(1, 7200000);%0A%0A        int slot = Granularity.MIN_5.slot(currentTime);%0A        SlotKey slotkey = SlotKey.of(Granularity.MIN_5, slot, shard);%0A%0A        SlotKey runningSlot = ctx.getNextScheduled();%0A%0A        // precondition%0A        assertEquals(slotkey, runningSlot);%0A%0A        // when%0A        boolean areKeysRunning = ctx.areChildKeysOrSelfKeyScheduledOrRunning(slotkey);%0A%0A        // then%0A        assertTrue(areKeysRunning);%0A    %7D%0A%0A    @Test%0A    public void childSlotRunningReturnsTrue() %7B%0A%0A        // given%0A        long currentTime = 1234000L;%0A%0A        final int shard = 0;%0A        List%3CInteger%3E managedShards = new ArrayList%3CInteger%3E() %7B%7B add(shard); %7D%7D;%0A%0A        ScheduleContext ctx = new ScheduleContext(currentTime, managedShards);%0A        ctx.update(currentTime - 2, shard);%0A        ctx.scheduleEligibleSlots(1, 7200000);%0A%0A        int slot = Granularity.MIN_20.slot(currentTime);%0A        SlotKey slotkey = SlotKey.of(Granularity.MIN_20, slot, shard);%0A%0A        SlotKey runningSlot = ctx.getNextScheduled();%0A%0A        // when%0A        boolean areKeysRunning = ctx.areChildKeysOrSelfKeyScheduledOrRunning(slotkey);%0A%0A        // then%0A        assertTrue(areKeysRunning);%0A    %7D
 %0A%7D%0A

Add test cases for when the slot is running, rather than scheduled.