package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.ComicEntity;
import com.ibtp.kontor.dal.DatabaseManager;
import com.ibtp.kontor.util.LocalTestDatabase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;
import java.util.Iterator;

/**
 * Created by TPEETZ on 28.01.2015.
 */
public class ComicImplTest {

    @Before
    public void setUp() {
        DatabaseManager.setDatabase(new LocalTestDatabase());
    }

    @Test
    public void testComicAddAndDelete() {
        String comicTitle = "Comic1";
        ComicImpl comicImpl = new ComicImpl();
        comicImpl.addComic(comicTitle);
        Collection<ComicEntity> comicList = comicImpl.findByTitle(comicTitle);
        Assert.assertNotNull(comicList);
        Assert.assertEquals(1, comicList.size());
        comicImpl.delete((ComicEntity) comicList.toArray()[0]);
        comicList = comicImpl.findByTitle(comicTitle);
        Assert.assertNotNull(comicList);
        Assert.assertEquals(0, comicList.size());
    }

    @Test
    public void testComicFindAll() {
        ComicImpl comicImpl = new ComicImpl();
        comicImpl.addComic("Comic1");
        comicImpl.addComic("Comic2");
        comicImpl.addComic("Comic3");
        Collection<ComicEntity> comicList = comicImpl.findAll();
        Assert.assertNotNull(comicList);
        Assert.assertEquals(3, comicList.size());
        for (Iterator<ComicEntity> iterator = comicList.iterator(); iterator.hasNext(); ) {
            ComicEntity next = iterator.next();
            comicImpl.delete(next);
        }
        comicList = comicImpl.findAll();
        Assert.assertNotNull(comicList);
        Assert.assertEquals(0, comicList.size());
    }
}
