Family-Map-Project / FamilyMapServer / FamilyMapServerStudent-master / java / services / LoadServiceTest.java
LoadServiceTest.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.LoadRequest;
import result.LoadResult;

import java.util.ArrayList;
import java.util.List;

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

public class LoadServiceTest
{
    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);
    }

    /**
     * Attempts to load one user, one person, and one event into the database
     * @throws DataAccessException
     */
    @Test
    public void loadServicePass() throws DataAccessException
    {
        //First we create 3 array lists with data so that we can load it into the database

        List<User> users = new ArrayList<>();
        List<Person> persons = new ArrayList<>();
        List<Event> events = new ArrayList<>();

        User user = new User("JohnA", "subway", "ja@gmail.com", "John",
                "Allen", "m", "abc123");

        users.add(user);

        Person person = new Person("6705", "JohnA", "James",
                "Smith", "m", "4278", "5971", "5940");

        persons.add(person);

        Event event = new Event("1040", "JohnA", "5407",
                47.1212f, 59.1323f, "Greece", "Athens", "Marriage", 1973);

        events.add(event);

        LoadService lService = new LoadService();

        LoadRequest request = new LoadRequest(users, persons, events);

        LoadResult lResult = lService.load(request);

        //After calling the load service, we now check to make sure we can retrieve the values in the database and that they match up

        //Checking the user is the same

        uDao = new UserDAO(db.getConnection());
        User testUser = uDao.getUserByID(user.getUsername());

        assertEquals(testUser.getUsername(), user.getUsername());
        assertEquals(testUser.getPassword(), user.getPassword());
        assertEquals(testUser.getEmail(), user.getEmail());
        assertEquals(testUser.getFirstName(), user.getFirstName());
        assertEquals(testUser.getLastName(), user.getLastName());
        assertEquals(testUser.getGender(), user.getGender());
        assertEquals(testUser.getPersonID(), user.getPersonID());

        db.closeConnection(true);

        //Checking the person is the same

        pDao = new PersonDAO(db.getConnection());
        Person testPerson = pDao.getPersonByID(person.getPersonID());

        assertEquals(testPerson.getPersonID(), person.getPersonID());
        assertEquals(testPerson.getAssociatedUsername(), person.getAssociatedUsername());
        assertEquals(testPerson.getFirstName(), person.getFirstName());
        assertEquals(testPerson.getLastName(), person.getLastName());
        assertEquals(testPerson.getGender(), person.getGender());
        assertEquals(testPerson.getFatherID(), person.getFatherID());
        assertEquals(testPerson.getMotherID(), person.getMotherID());
        assertEquals(testPerson.getSpouseID(), person.getSpouseID());

        db.closeConnection(true);

        //Checking the event is the same

        eDao = new EventDAO(db.getConnection());
        Event testEvent = eDao.getEventByID(event.getEventID());

        assertEquals(testEvent.getEventID(), event.getEventID());
        assertEquals(testEvent.getUsername(), event.getUsername());
        assertEquals(testEvent.getPersonID(), event.getPersonID());
        assertEquals(testEvent.getLatitude(), event.getLatitude());
        assertEquals(testEvent.getLongitude(), event.getLongitude());
        assertEquals(testEvent.getCountry(), event.getCountry());
        assertEquals(testEvent.getCity(), event.getCity());
        assertEquals(testEvent.getEventType(), event.getEventType());
        assertEquals(testEvent.getYear(), event.getYear());

        db.closeConnection(true);

        assertTrue(lResult.isSuccess());
    }

    /**
     * Attempts to load an invalid user into the database (one of the data fields is null)
     * @throws DataAccessException
     */
    @Test
    public void loadServiceFail() throws DataAccessException
    {
        //This time we will test inserting a user with a personID of null (this should cause an error)

        List<User> users = new ArrayList<>();
        List<Person> persons = new ArrayList<>();
        List<Event> events = new ArrayList<>();

        User user = new User("JohnA", "subway", "ja@gmail.com", "John",
                "Allen", "m", null);

        users.add(user);

        Person person = new Person("6705", "JohnA", "James",
                "Smith", "m", "4278", "5971", "5940");

        persons.add(person);

        Event event = new Event("1040", "JohnA", "5407",
                47.1212f, 59.1323f, "Greece", "Athens", "Marriage", 1973);

        events.add(event);

        LoadService lService = new LoadService();

        LoadRequest request = new LoadRequest(users, persons, events);

        LoadResult lResult = lService.load(request);

        //Attempt to fetch the user and verify that it is null (since we had invalid input)

        uDao = new UserDAO(db.getConnection());
        User testUser = uDao.getUserByID(user.getUsername());
        db.closeConnection(false);

        assertNull(testUser);

        //Attempt to fetch the person and verify that it is null (since we had invalid input)

        pDao = new PersonDAO(db.getConnection());
        Person testPerson = pDao.getPersonByID(person.getPersonID());
        db.closeConnection(false);

        assertNull(testPerson);

        //Attempt to fetch the event and verify that it is null (since we had invalid input)

        eDao = new EventDAO(db.getConnection());
        Event testEvent = eDao.getEventByID(event.getEventID());
        db.closeConnection(false);

        assertNull(testEvent);

        assertFalse(lResult.isSuccess());
    }
}