Package at.fhj.itm.dao

Source Code of at.fhj.itm.dao.MySqlWaypointDAOTest

package at.fhj.itm.dao;


import java.sql.Connection;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;



import at.fhj.itm.model.Location;
import at.fhj.itm.model.Trip;
import at.fhj.itm.model.User;
import at.fhj.itm.model.Waypoint;
import static org.junit.Assert.*;


/**
* Tests the MySql implementation  WaypointDAO
* @author Seuchter
*
*/

public class MySqlWaypointDAOTest extends DatabaseTest {

  private WaypointDAO dao;
  private UserDAO userDAO;
  private DAOFactory factory;
  private Connection connection;
 

  /**
   * Creates a fully fledged Waypoint which can be used by test cases
   * @return
   */
  private Waypoint createTestWaypoint(){
    User u = userDAO.getByID(1,connection);
    Location from = new Location(8190, "Birkfeld");
    Location to = new Location(8010, "Graz");
    Waypoint wp = new Waypoint(from, to, u, "Unit Test", true);
    return wp;
  }
 
  /**
   * Helper method for creating a fully fledged trip object
   * @return a fully fledged trip object which can be used by testcases.
   */
  private Trip createTestTrip(){
    Location location = new Location(1234, "Unit City");
    User user = new User("Unit", "Test", "UnitTest", "1234", "Unit@Test.com", "123456",
        location, new Date(), UUID.randomUUID().toString().replace("-", ""));
    Location from = new Location(8190, "Birkfeld");
    Location to = new Location(8010, "Graz");
    Waypoint wp = new Waypoint(from, to, user, "", true);
    Trip t = new Trip(user, new Date(), 4, wp, "Copyright (C) Google");
   
    return t;
  }
 
  /**
   * Creates a new waypoint which is persisted and afterwards deleted.
   */
  @Test
  public void testCreateDelete(){
    Waypoint wp = createTestWaypoint();
    int before = dao.selectAll(connection).size();
    dao.update(wp,connection);
    assertTrue(wp.getId() != -1);
    assertTrue(wp.getToLocation().getId() != -1);
    assertTrue(wp.getFromLocation().getId() != -1);
    int after = dao.selectAll(connection).size();
    assertTrue(after > before);
    dao.delete(wp, connection);
    int afterDelete = dao.selectAll(connection).size();
    assertEquals(before, afterDelete);
    assertTrue(wp.getId() == -1);
    assertTrue(wp.getToLocation().getId() == -1);
    assertTrue(wp.getFromLocation().getId() == -1);
  }
  @Test
  public void testTripWaypoint(){
  }

 
  /**
   * Creates a user which is associated to a waypoint. Afterwards it is checked
   * if the DAO can find the created waypoint by the user.
   * @throws SQLException
   */
  @Test
  public void testWaypointUser()throws SQLException
  {
 
    Location location = new Location(1234, "Unit City");
    User user = new User("Unit", "Test", "UnitTest", "1234", "Unit@Test.com", "123456",
        location, new Date(), UUID.randomUUID().toString().replace("-", ""));
    userDAO.update(user, connection);
    List<Waypoint> wps = new ArrayList<Waypoint>();
    for(int i = 0; i< 10; i++){
      Location from = new Location(1000 + i, "Town " + i);
      Location to = new Location(1000 + i+1, "Town " + i+1);
     
      Waypoint wp = new Waypoint(from, to, user, "WP " + i, true);
      wps.add(wp);
      dao.update(wp,connection);
    }
   
    List<Waypoint> queryWP = dao.getWaypointsFromUser(user,connection);
   
    assertEquals(wps.size(), queryWP.size());
   
    for(Waypoint wp: queryWP){
      dao.delete(wp,connection);
    }
 
    List<Waypoint> empty = dao.getWaypointsFromUser(user,connection);
    assertEquals(0, empty.size());
   
    factory.getUserDAO().delete(user,connection);
 
  }
  /**
   * Creates new Waypoint and then tests if the Waypoint can be updated
   * by the DAO
   */
  @Test
  public void testUpdate(){
    Waypoint wp = createTestWaypoint();
    dao.update(wp, connection);
    assertTrue(wp.getId() != -1);
    wp.setFrom_location(new Location(8888, "New Location"));
    dao.update(wp, connection);
    Waypoint newWp = dao.getByID(wp.getId(), connection);
    assertEquals(wp, newWp);
   
  }
  /**
   * Tries to delete a not persisted waypoint which should throw a
   * DAO exception
   */
  @Test(expected=DAOException.class)
  public void testDeleteNotPersisted(){
    Waypoint wp = createTestWaypoint();
    dao.delete(wp, connection);
   
  }
  /**
   * Tests if any entites can be retrieved by the DAO
   */
  @Test
  public void testSelectAll(){
    List<Waypoint> wp = dao.selectAll(connection);
    int expectedCount = dao.getTotalCount(connection);
    assertEquals(expectedCount,wp.size());
  }
 
  /**
   * Tests if waypoints can be retrieved by their trips with which they are
   * associated
   * @throws SQLException
   */
  @Test
  public void testWaypointForTrip()throws SQLException{
    Trip testTrip = createTestTrip();
    factory.getTripDAO().update(testTrip, connection);
    dao.addWaypointToTrip(testTrip,testTrip.getWaypoint(), connection);
    connection.commit();
    List<Waypoint> waypoints = dao.getWaypointsForTrip(testTrip, connection);
    assertEquals(1, waypoints.size());
    assertEquals(testTrip.getWaypoint(), waypoints.get(0));
  }
 
  /**
   * Creates a trip which is then persisted, afterwards it is checked if the
   * DAO can perform an update on the already created trip.
   * @throws SQLException
   */
  @Before
  public void setUp() throws Exception {
    rebuildDatabase();
    connection = getUnitTestConnection();
   
    factory = new MySqlDAOFactory();
    dao = factory.getWaypointDAO();
    userDAO = factory.getUserDAO();
   
   
  }
 
 
  /**
   * Closes the connection after an test case is executed.
   * @throws Exception
   */
  @After
  public void tearDown() throws Exception {
    connection.close();
  }

}
TOP

Related Classes of at.fhj.itm.dao.MySqlWaypointDAOTest

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.