package com.cronutils.model.time;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
/*
 * Copyright 2015 jmrozanec
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class TimeNodeTest {
    private static final int LIST_START_VALUE = 1;
    private static final int LIST_MEDIUM_VALUE = 3;
    private static final int LIST_END_VALUE = 5;
    private List<Integer> values;
    private TimeNode timeNode;

    @Before
    public void setUp() throws Exception {
        values = Lists.newArrayList();
        values.add(LIST_START_VALUE);
        values.add(LIST_MEDIUM_VALUE);
        values.add(LIST_END_VALUE);
        this.timeNode = new TimeNode(values);
    }

    @Test
    public void testGetNextValue() throws Exception {
        assertResult(LIST_START_VALUE, 0, timeNode.getNextValue(LIST_START_VALUE, 0));
        assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getNextValue(LIST_MEDIUM_VALUE, 0));
        assertResult(LIST_END_VALUE, 0, timeNode.getNextValue(LIST_END_VALUE, 0));

        assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getNextValue(LIST_START_VALUE, 1));
        assertResult(LIST_END_VALUE, 0, timeNode.getNextValue(LIST_MEDIUM_VALUE, 1));
        assertResult(LIST_START_VALUE, 1, timeNode.getNextValue(LIST_END_VALUE, 1));

        assertResult(LIST_START_VALUE, 1, timeNode.getNextValue(LIST_MEDIUM_VALUE, 2));
    }

    @Test
    public void testGetValues() throws Exception {
        assertEquals(values, timeNode.getValues());
    }

    @Test
    public void testGetPreviousValue() throws Exception {
        assertResult(LIST_START_VALUE, 0, timeNode.getPreviousValue(LIST_START_VALUE, 0));
        assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getPreviousValue(LIST_MEDIUM_VALUE, 0));
        assertResult(LIST_END_VALUE, 0, timeNode.getPreviousValue(LIST_END_VALUE, 0));

        assertResult(LIST_END_VALUE, 1, timeNode.getPreviousValue(LIST_START_VALUE, 1));
        assertResult(LIST_START_VALUE, 0, timeNode.getPreviousValue(LIST_MEDIUM_VALUE, 1));
        assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getPreviousValue(LIST_END_VALUE, 1));

        assertResult(LIST_END_VALUE, 1, timeNode.getPreviousValue(LIST_MEDIUM_VALUE, 2));
    }

    @Test
    public void testGetValueFromListWhereIndexLessThanZero(){
        AtomicInteger shift = new AtomicInteger(0);
        List<Integer> list = Lists.newArrayList(1, 2, 3, 4);
        int value = timeNode.getValueFromList(list, -1, shift);
        assertEquals(String.format("Shift was: %s; expected: 1", shift.get()), 1, shift.get());
        assertEquals((int)list.get(list.size()-1), value);
    }

    private void assertResult(int value, int shift, NearestValue nearestValue){
        assertEquals(String.format("Values do not match! Expected: %s Found: %s", value, nearestValue.getValue()), value, nearestValue.getValue());
        assertEquals(String.format("Shifts do not match! Expected: %s Found: %s", shift, nearestValue.getShifts()), shift, nearestValue.getShifts());
    }
}
@@ -2902,16 +2902,72 @@
 Zero()%7B%0A
+        int index = -1;%0A        int expectedShifts = 1;%0A
         
@@ -3123,18 +3123,21 @@
 t(list, 
--1
+index
 , shift)
@@ -3199,17 +3199,18 @@
 pected: 
-1
+%25s
 %22, shift
@@ -3219,12 +3219,41 @@
 et()
-), 1
+, expectedShifts), expectedShifts
 , sh
@@ -3310,18 +3310,513 @@
 t.size()
--1
++index), value);%0A    %7D%0A%0A    @Test%0A    public void testGetValueFromListWhereIndexMoreThanZero()%7B%0A        int index = 1;%0A        int expectedShifts = 0;%0A        AtomicInteger shift = new AtomicInteger(0);%0A        List%3CInteger%3E list = Lists.newArrayList(1, 2, 3, 4);%0A        int value = timeNode.getValueFromList(list, index, shift);%0A        assertEquals(String.format(%22Shift was: %25s; expected: %25s%22, shift.get(), expectedShifts), expectedShifts, shift.get());%0A        assertEquals((int)list.get(index
 ), value

Add test for TimeNode to validate values retrieval behavior