package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.PublisherEntity;
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 20.01.2015.
 */
public class PublisherImplTest {

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

    @Test
    public void testPublisherAddAndDelete() {
        String publisherName = "testPublisherAddAndDelete";
        PublisherImpl publisherImpl = new PublisherImpl();
        PublisherEntity publisher = publisherImpl.addPublisher(publisherName);
        Collection<PublisherEntity> publisherList = publisherImpl.findByName(publisherName);
        Assert.assertEquals(1, publisherList.size());
        publisherImpl.delete(publisher);
        publisherList = publisherImpl.findByName(publisherName);
        Assert.assertEquals(0, publisherList.size());
    }

    @Test
    public void testPublisherFindAll() {
        PublisherImpl publisherImpl = new PublisherImpl();
        publisherImpl.addPublisher("testDeletePublisher1");
        publisherImpl.addPublisher("testDeletePublisher2");
        publisherImpl.addPublisher("testDeletePublisher3");
        Collection<PublisherEntity> publisherList = publisherImpl.findAll();
        Assert.assertEquals(3, publisherList.size());
        for (Iterator<PublisherEntity> iterator = publisherList.iterator(); iterator.hasNext(); ) {
            PublisherEntity next =  iterator.next();
            publisherImpl.delete(next);
        }
        publisherList = publisherImpl.findAll();
        Assert.assertEquals(0, publisherList.size());
    }
}
