package sk.vrto.service.dao;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import sk.vrto.domain.Contact;
import sk.vrto.domain.Email;
import sk.vrto.domain.User;
import java.util.Collection;
import static org.testng.Assert.*;
abstract public class BaseEmailRepositoryTest {
// implementing class should provide correct instantiation
protected EmailRepository emailRepository;
protected User testedUser;
abstract protected EmailRepository createEmailRepository();
abstract protected User createTestedUser();
abstract protected void createData();
abstract protected void clearData();
@BeforeMethod
public void setUpData() {
testedUser = createTestedUser();
emailRepository = createEmailRepository();
createData();
}
@AfterMethod
public void tearDownData() {
clearData();
}
protected Contact createSampleContact() {
return new Contact("Miso", "little.talks@omm.ice", testedUser);
}
protected Email createSampleEmail() {
Email email = new Email(createSampleContact(), "Sample email", "Lorem ipsum");
email.setId(1337);
return email;
}
@Test
public void testFindAndInsert() {
Email email = createSampleEmail();
assertNull(emailRepository.find(email.getId()));
emailRepository.insert(email);
assertFalse(emailRepository.load().isEmpty());
Email found = emailRepository.find(email.getId());
assertNotNull(found);
assertEquals(found, email);
}
@Test
public void testLoad() {
// assuming #createData runs before every test method
assertFalse(emailRepository.load().isEmpty());
}
@Test
public void testUpdate() {
Email email = createSampleEmail();
assertNull(emailRepository.find(email.getId()));
email.setContent("JavaOne email");
emailRepository.update(email);
Email javaOneEmail = emailRepository.find(email.getId());
assertNotEquals(email, javaOneEmail);
assertEquals("JavaOne email", javaOneEmail.getContent());
}
@Test
public void testDelete() {
Email email = createSampleEmail();
assertNull(emailRepository.find(email.getId()));
emailRepository.insert(email);
assertNotNull(emailRepository.find(email.getId()));
emailRepository.delete(email);
assertNull(emailRepository.find(email.getId()));
}
@Test
public void testLoadContactsForUser() {
// existing user
Collection<Email> emailsForMiso = emailRepository.loadEmailsForUser(testedUser);
assertNotNull(emailsForMiso);
assertFalse(emailsForMiso.isEmpty());
// bogus user
assertTrue(emailRepository.loadEmailsForUser(new User("bogus", "bogus", null)).isEmpty());
}
}