Family-Map-Project / FamilyMapServer / FamilyMapServerStudent-master / java / services / FillServiceTest.java
FillServiceTest.java
Raw
package services;

import dao.*;
import model.Event;
import model.Person;
import model.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import request.FillRequest;
import result.FillResult;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class FillServiceTest
{
    private Database db;
    UserDAO uDao;
    PersonDAO pDao;
    EventDAO eDao;

    @BeforeEach
    public void setUp() throws DataAccessException
    {
        db = new Database();
        db.openConnection();
        db.clearTables();
        db.closeConnection(true);
    }

    @AfterEach
    public void tearDown() throws DataAccessException
    {
        db.openConnection();
        db.clearTables();
        db.closeConnection(true);
    }

    /**
     * Fills in information for the user; tests the case where we generate just one generation
     * @throws DataAccessException
     */
    @Test
    public void fillServicePass() throws DataAccessException
    {
        User user = new User("JohnA", "subway", "ja@gmail.com", "John",
                "Allen", "m", "abc123");

        uDao = new UserDAO(db.getConnection());
        uDao.createUser(user);
        db.closeConnection(true);

        FillService fService = new FillService();

        //Test should fail because the username given does not belong to a user in the database

        FillRequest request = new FillRequest("JohnA", 1);

        FillResult fResult = fService.fill(request);

        //Making sure we created the person object for our user correctly

        pDao = new PersonDAO(db.getConnection());

        Person testPerson = pDao.getPersonByID(user.getPersonID());
        assertNotNull(testPerson);

        db.closeConnection(true);

        //Now we check and make sure that the mother and father persons of the user person are not null

        pDao = new PersonDAO(db.getConnection());

        Person mother = pDao.getPersonByID(testPerson.getMotherID());
        Person father = pDao.getPersonByID(testPerson.getFatherID());
        assertNotNull(mother);
        assertNotNull(father);

        db.closeConnection(true);

        //We also want to compare the spouse ID's of the parents to make sure they are linked to each other

        pDao = new PersonDAO(db.getConnection());

        assertEquals(mother.getSpouseID(), father.getPersonID());
        assertEquals(father.getSpouseID(), mother.getPersonID());

        db.closeConnection(true);

        //Let's also make sure that the mother and father's parent IDs are null

        pDao = new PersonDAO(db.getConnection());

        assertNull(mother.getMotherID());
        assertNull(mother.getFatherID());

        assertNull(father.getMotherID());
        assertNull(father.getFatherID());

        db.closeConnection(true);

        //Finally, let's just check and make sure that our user has 3 events

        eDao = new EventDAO(db.getConnection());
        List<Event> userEvents = eDao.findEventsForPerson(testPerson.getPersonID());
        assertEquals(3, userEvents.size());
        db.closeConnection(true);

        //We also want to check and make the user's parents have 4 events (they also have a marriage event)

        eDao = new EventDAO(db.getConnection());
        List<Event> motherEvents = eDao.findEventsForPerson(testPerson.getMotherID());
        assertEquals(4, motherEvents.size());
        db.closeConnection(true);

        eDao = new EventDAO(db.getConnection());
        List<Event> fatherEvents = eDao.findEventsForPerson(testPerson.getFatherID());
        assertEquals(4, fatherEvents.size());
        db.closeConnection(true);

        assertTrue(fResult.isSuccess());
    }

    /**
     * Tests the fill service for an invalid username
     * @throws DataAccessException
     */
    @Test
    public void fillServiceFailUsername() throws DataAccessException
    {
        User user = new User("JohnA", "subway", "ja@gmail.com", "John",
                "Allen", "m", "abc123");

        uDao = new UserDAO(db.getConnection());
        uDao.createUser(user);
        db.closeConnection(true);

        FillService fService = new FillService();

        //Test should fail because the username given does not belong to a user in the database

        FillRequest request = new FillRequest("David", 1);

        FillResult fResult = fService.fill(request);

        assertTrue(fResult.getMessage().contains("Error:"));
        assertFalse(fResult.isSuccess());
    }

    /**
     * Tests the fill service for an invalid generation number
     * @throws DataAccessException
     */
    @Test
    public void fillServiceFailGenerations() throws DataAccessException
    {
        User user = new User("JohnA", "subway", "ja@gmail.com", "John",
                "Allen", "m", "abc123");

        uDao = new UserDAO(db.getConnection());
        uDao.createUser(user);
        db.closeConnection(true);

        FillService fService = new FillService();

        //Test should fail because the generation number given is less than 0

        FillRequest request = new FillRequest("JohnA", -1);

        FillResult fResult = fService.fill(request);

        assertTrue(fResult.getMessage().contains("Error:"));
        assertFalse(fResult.isSuccess());
    }
}