package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.ArtistEntity;
import com.ibtp.kontor.dal.*;
import com.ibtp.kontor.util.LocalTestDatabase;
import org.junit.*;

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

public class ArtistImplTest {

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

    @Test
    public void testArtistAddAndDelete() {
        String artistName = "testArtistAddAndDelete";
        ArtistImpl artistImpl = new ArtistImpl();
        artistImpl.addArtist(artistName);
        Collection<ArtistEntity> resultList = artistImpl.findByName(artistName);
        Assert.assertNotNull(resultList);
        Assert.assertTrue(resultList.size() > 0);
        ArtistEntity artist = (ArtistEntity)(resultList.toArray()[0]);
        artistImpl.delete(artist);
        resultList = artistImpl.findByName(artistName);
        Assert.assertNotNull(resultList);
        Assert.assertEquals(0, resultList.size());
    }

    @Test
    public void testArtistFindAll() {
        ArtistImpl artistImpl = new ArtistImpl();
        Collection<ArtistEntity> artistList = artistImpl.findAll();
        Assert.assertNotNull(artistList);
        Assert.assertEquals(0, artistList.size());
        artistImpl.addArtist("testArtistFindAll1");
        artistImpl.addArtist("testArtistFindAll2");
        artistImpl.addArtist("testArtistFindAll3");
        artistList = artistImpl.findAll();
        Assert.assertNotNull(artistList);
        Assert.assertEquals(3, artistList.size());
        for (Iterator<ArtistEntity> iterator = artistList.iterator(); iterator.hasNext(); ) {
            ArtistEntity next =  iterator.next();
            artistImpl.delete(next);
        }
    }
}
