package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.IssueEntity;
import com.ibtp.kontor.dal.DatabaseManager;
import com.ibtp.kontor.util.LocalTestDatabase;

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 IssueImplTest {

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

    @Test
    public void testIssueAddAndDelete() {
        String issueNumber = "42";
        IssueImpl issueImpl = new IssueImpl();
        IssueEntity issue = new IssueEntity();
        issue.setNumber(issueNumber);
        issueImpl.store(issue);
        Collection<IssueEntity> issueList = issueImpl.findByNumber(issueNumber);
        assertNotNull(issueList);
        assertEquals(1, issueList.size());
        issueImpl.delete(issue);
        issueList = issueImpl.findByNumber(issueNumber);
        assertNotNull(issueList);
        assertEquals(0, issueList.size());
    }

    @Test
    public void testIssueFindAll() {
        IssueImpl issueImpl = new IssueImpl();
        IssueEntity issue1 = new IssueEntity();
        issue1.setNumber("issue1");
        IssueEntity issue2 = new IssueEntity();
        issue1.setNumber("issue2");
        IssueEntity issue3 = new IssueEntity();
        issue1.setNumber("issue3");
        issueImpl.store(issue1);
        issueImpl.store(issue2);
        issueImpl.store(issue3);
        Collection<IssueEntity> issueList = issueImpl.findAll();
        assertNotNull(issueList);
        assertEquals(3, issueList.size());
        for (IssueEntity issueEntity : issueList) {
            issueImpl.delete(issueEntity);
        }
        issueList = issueImpl.findAll();
        assertNotNull(issueList);
        assertEquals(0, issueList.size());
    }
}
