package de.danoeh.antennapod.core.storage;

import org.junit.Test;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMother;
import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator;
import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator.Options;

import static org.junit.Assert.assertEquals;

public class DBWriterTest {

    public static class ItemEnqueuePositionCalculatorTest {

        @Test
        public void testEnqueueDefault() {

            ItemEnqueuePositionCalculator calculator =
                    new ItemEnqueuePositionCalculator(new Options());

            List<FeedItem> curQueue = tQueue();

            int posActual1 = calculator.calcPosition(0, tFI(101), curQueue);
            assertEquals("case default, i.e., add to the end", curQueue.size(), posActual1);

            int posActual2 = calculator.calcPosition(1, tFI(102), curQueue);
            assertEquals("case default (2nd item)", curQueue.size(), posActual2);

        }

        @Test
        public void testEnqueueAtFront() {

            ItemEnqueuePositionCalculator calculator =
                    new ItemEnqueuePositionCalculator(new Options()
                            .setEnqueueAtFront(true));

            List<FeedItem> curQueue = tQueue();

            int posActual1 = calculator.calcPosition(0, tFI(101), curQueue);
            assertEquals("case option enqueue at front", 0, posActual1);

            int posActual2 = calculator.calcPosition(1, tFI(102), curQueue);
            assertEquals("case option enqueue at front (2nd item)", 1, posActual2);

        }

        private static List<FeedItem> tQueue() {
            return Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14));
        }

        private static FeedItem tFI(int id) {
            FeedItem item = new FeedItem(0, "Item" + id, "ItemId" + id, "url",
                    new Date(), FeedItem.PLAYED, FeedMother.anyFeed());
            return item;
        }

    }

}
@@ -59,16 +59,190 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.Parameterized;%0Aimport org.junit.runners.Parameterized.Parameter;%0Aimport org.junit.runners.Parameterized.Parameters;
 %0A%0Aimport
@@ -647,24 +647,58 @@
 iterTest %7B%0A%0A
+    @RunWith(Parameterized.class)%0A
     public s
@@ -759,57 +759,112 @@
    @
-Test%0A        public void testEnqueueDefault
+Parameters(name = %22%7Bindex%7D: case%3C%7B0%7D%3E, expected:%7B1%7D%22)%0A        public static Iterable%3CObject%5B%5D%3E data
 () %7B%0A
-%0A
     
@@ -875,167 +875,116 @@
     
-ItemEnqueuePositionCalculator calculator =%0A                    new ItemEnqueuePositionCalculator(new Options());%0A%0A            List%3CFeedItem%3E curQueue = tQue
+Options optDefault = new Options();%0A            Options optEnqAtFront = new Options().setEnqueueAtFront(tr
 ue
-(
 );%0A%0A
@@ -999,518 +999,392 @@
     
-int posActual1 = calculator.calcPosition(0, tFI(101), curQueue);%0A            assertEquals(%22case default, i.e., add to the end%22, curQueue.size(), posActual1);%0A%0A            int posActual2 = calculator.calcPosition(1, tFI(102), curQueue);%0A            assertEquals(%22case default (2nd item)%22, curQueue.size(), posActual2);%0A%0A        %7D%0A%0A        @Test%0A        public void testEnqueueAtFront() %7B%0A%0A            ItemEnqueuePositionCalculator calculator =%0A                    new ItemEnqueuePositionCalculator(new Options()
+return Arrays.asList(new Object%5B%5D%5B%5D %7B%0A                    %7B%22case default, i.e., add to the end%22, QUEUE_DEFAULT.size(), optDefault , 0%7D,%0A                    %7B%22case default (2nd item)%22, QUEUE_DEFAULT.size(), optDefault , 1%7D,%0A                    %7B%22case option enqueue at front%22, 0, optEnqAtFront , 0%7D,%0A                    %7B%22case option enqueue at front (2nd item)%22, 1, optEnqAtFront , 1%7D
 %0A   
@@ -1388,32 +1388,36 @@
             
+%7D);%0A
         
         .set
@@ -1404,24 +1404,27 @@
         
+%7D%0A%0A
         
 .setEnqu
@@ -1419,84 +1419,184 @@
     
-.setEnqueueAtFront(true));%0A%0A            List%3CFeedItem%3E curQueue = tQueue();%0A
+private static final List%3CFeedItem%3E QUEUE_DEFAULT = Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14));%0A%0A        @Parameter%0A        public String message;%0A%0A        @Parameter(1)
 %0A   
@@ -1596,35 +1596,38 @@
 (1)%0A        
-   
+public
  int pos
 Actual1 = ca
@@ -1618,298 +1618,346 @@
  pos
-Actual1 = calculator.calcPosition(0, tFI(101), curQueue);%0A            assertEquals(%22case option enqueue at front%22, 0, posActual1);%0A%0A            int posActual2 = calculator.calcPosition(1, tFI(102), curQueue);%0A            assertEquals(%22case option enqueue at front (2nd item)%22, 1, posActual2
+Expected;%0A%0A        @Parameter(2)%0A        public Options options;%0A%0A        @Parameter(3)%0A        public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted.%0A%0A%0A        @Test%0A        public void test() %7B%0A            ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options
 );%0A%0A
@@ -1968,127 +1968,150 @@
     
-%7D%0A%0A
     
-    private static List%3CFeedItem%3E tQueue() %7B%0A            return Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)
+int posActual = calculator.calcPosition(posAmongAdded, tFI(101), QUEUE_DEFAULT);%0A            assertEquals(message, posExpected , posActual
 );%0A 

refactor - DBWriterTest: parametrize the set of tests