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.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

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

/**
 * Created by TPEETZ on 20.01.2015.
 */
public class PublisherImplTest {

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

    @AfterEach
    public void cleanUp() {
        PublisherImpl publisherImpl = new PublisherImpl();
        Collection<PublisherEntity> publisherList = publisherImpl.findAll();
        for (PublisherEntity publisherEntity : publisherList) {
            publisherImpl.delete(publisherEntity);
        }
    }

    @Test
    public void testPublisherAddAndDelete() {
        String publisherName = "testPublisherAddAndDelete";
        PublisherImpl publisherImpl = new PublisherImpl();
        PublisherEntity publisher = publisherImpl.addPublisher(publisherName);
        Collection<PublisherEntity> publisherList = publisherImpl.findByName(publisherName);
        assertEquals(1, publisherList.size());
        publisherImpl.delete(publisher);
        publisherList = publisherImpl.findByName(publisherName);
        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();
        assertEquals(3, publisherList.size());
    }
}
