package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.VolumeEntity;
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 static org.junit.jupiter.api.Assertions.assertNotNull;

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

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

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

    @AfterEach
    public void cleanUp() {
        VolumeImpl volumeImpl = new VolumeImpl();
        Collection<VolumeEntity> volumeList = volumeImpl.findAll();
        for (VolumeEntity volumeEntity : volumeList) {
            volumeImpl.delete(volumeEntity);
        }
    }

    @Test
    public void testVolumeAddAndDelete() {
        String volumeTitle = "testVolumeAddAndDelete";
        VolumeImpl volumeImpl = new VolumeImpl();
        VolumeEntity volume = new VolumeEntity();
        volume.setTitle(volumeTitle);
        VolumeEntity volumeEntity = volumeImpl.store(volume);
        assertNotNull(volumeEntity);
        assertEquals(volumeTitle, volumeEntity.getTitle());
        Collection<VolumeEntity> volumeList = volumeImpl.findByTitle(volumeTitle);
        assertNotNull(volumeList);
        assertEquals(1, volumeList.size());
        VolumeEntity result = (VolumeEntity)volumeList.toArray()[0];
        assertEquals(volume, result);
        volumeImpl.delete(result);
        volumeList = volumeImpl.findByTitle(volumeTitle);
        assertEquals(0, volumeList.size());
    }

    @Test
    public void testVolumeFindAll() {
        VolumeImpl volumeImpl = new VolumeImpl();
        VolumeEntity volume;
        volume = new VolumeEntity();
        volume.setTitle("testVolumeFindAll1");
        volumeImpl.store(volume);
        volume = new VolumeEntity();
        volume.setTitle("testVolumeFindAll2");
        volumeImpl.store(volume);
        volume = new VolumeEntity();
        volume.setTitle("testVolumeFindAll3");
        volumeImpl.store(volume);
        Collection<VolumeEntity> volumeList = volumeImpl.findAll();
        assertNotNull(volumeList);
        assertEquals(3, volumeList.size());
    }
}
