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.jupiter.api.AfterAll;
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 static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collection;

public class ArtistImplTest {

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

    @AfterAll
    public static void tearDown() {
        ArtistImpl artistImpl = new ArtistImpl();
        Collection<ArtistEntity> artistList = artistImpl.findAll();
        for (ArtistEntity artistEntity : artistList) {
            artistImpl.delete(artistEntity);
        }
    }

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

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