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.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 VolumeImplTest {

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

    @Test
    public void testVolumeAddAndDelete() {
        String volumeTitle = "testVolumeAddAndDelete";
        VolumeImpl volumeImpl = new VolumeImpl();
        VolumeEntity volume = new VolumeEntity();
        volume.setTitle(volumeTitle);
        VolumeEntity volumeEntity = volumeImpl.store(volume);
        Assert.assertNotNull(volumeEntity);
        Assert.assertEquals(volumeTitle, volumeEntity.getTitle());
        Collection<VolumeEntity> volumeList = volumeImpl.findByTitle(volumeTitle);
        Assert.assertNotNull(volumeList);
        Assert.assertEquals(1, volumeList.size());
        VolumeEntity result = (VolumeEntity)volumeList.toArray()[0];
        Assert.assertEquals(volume, result);
        volumeImpl.delete(result);
        volumeList = volumeImpl.findByTitle(volumeTitle);
        Assert.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();
        Assert.assertNotNull(volumeList);
        Assert.assertEquals(3, volumeList.size());
        for (Iterator<VolumeEntity> iterator = volumeList.iterator(); iterator.hasNext(); ) {
            VolumeEntity next =  iterator.next();
            volumeImpl.delete(next);
        }
        volumeList = volumeImpl.findAll();
        Assert.assertNotNull(volumeList);
        Assert.assertEquals(0, volumeList.size());
    }

}
