Package sk.vrto.service.dao

Source Code of sk.vrto.service.dao.BaseContactRepositoryTest

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.ContactCreation;
import sk.vrto.domain.User;

import java.util.Collection;

import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;

abstract public class BaseContactRepositoryTest {

    // implementing class should provide correct instantiation
    protected ContactRepository contactRepository;
    protected User testedUser;

    abstract protected ContactRepository createContactRepository();
    abstract protected User createTestedUser();
    abstract protected void createData();
    abstract protected void clearData();

    @BeforeMethod
    public void setUpData() {
        testedUser = createTestedUser();
        contactRepository = createContactRepository();
        createData();
    }

    @AfterMethod
    public void tearDownData() {
        clearData();
    }

    private Contact createSampleContact() {
        return new Contact("Miso", "little.talks@omm.ice", testedUser);
    }

    @Test
    public void testFindAndInsert() {
        Contact miso = createSampleContact();

        assertNull(contactRepository.find(miso.getEmail()));
        contactRepository.insert(miso);
        assertFalse(contactRepository.load().isEmpty());

        Contact found = contactRepository.find(miso.getEmail());
        assertNotNull(found);
        assertEquals(found, miso);
    }

    @Test
    public void testLoad() {
        // assuming #createData runs before every test method
        assertFalse(contactRepository.load().isEmpty());
    }

    @Test
    public void testUpdate() {
        Contact miso = createSampleContact();
        assertNull(contactRepository.find(miso.getEmail()));
        contactRepository.insert(miso);

        miso.setName("Michael");
        contactRepository.update(miso);

        Contact michael = contactRepository.find(miso.getEmail());
        assertEquals(miso, michael);
        assertEquals("Michael", michael.getName());
    }

    @Test
    public void testDelete() {
        Contact miso = createSampleContact();
        assertNull(contactRepository.find(miso.getEmail()));

        contactRepository.insert(miso);
        assertNotNull(contactRepository.find(miso.getEmail()));

        contactRepository.delete(miso);
        assertNull(contactRepository.find(miso.getEmail()));
    }

    @Test
    public void testLoadContactsForUser() {
        // existing user
        Collection<Contact> contactsForMiso = contactRepository
                .loadContatsForUser(testedUser);
        assertNotNull(contactsForMiso);
        assertFalse(contactsForMiso.isEmpty());

        // bogus user
        assertTrue(contactRepository.loadContatsForUser(new User("bogus", "bogus", null)).isEmpty());
    }

    @Test
    public void testGetOrCreateNotExisting() {
        String uniqueAddress = "666veryUnprobableAddress1337@hell.fi";
        assertNull(contactRepository.find(uniqueAddress));

        ContactCreation creation = contactRepository.getOrCreate(uniqueAddress, testedUser);
        assertNotNull(creation);
        assertTrue(creation.isFreshlyCreated());
        assertNotNull(contactRepository.find(uniqueAddress));
    }

    @Test
    public void testGetOrCreateExisting() {
        String address = "java.java@java.com";
        Contact contact = new Contact("Grandmaster James Gosling", address, testedUser);
        contactRepository.insert(contact);
        assertNotNull(contactRepository.find(address));

        ContactCreation creation = contactRepository.getOrCreate(address, testedUser);
        assertEquals(contact, creation.getContact());
        assertFalse(creation.isFreshlyCreated());
    }

}
TOP

Related Classes of sk.vrto.service.dao.BaseContactRepositoryTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.