package at.fhj.itm.business;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import junit.framework.Assert;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import at.fhj.itm.dao.DAOException;
import at.fhj.itm.dao.PointDAO;
import at.fhj.itm.dao.TripDAO;
import at.fhj.itm.dao.WaypointDAO;
import at.fhj.itm.model.Location;
import at.fhj.itm.model.Point;
import at.fhj.itm.model.Trip;
import at.fhj.itm.model.User;
import at.fhj.itm.model.Waypoint;
import at.fhj.itm.util.EmailUtil;
import at.fhj.itm.util.GoogleUtil;
import at.fhj.itm.util.JsfUtil;
import at.fhj.itm.util.RandomUtil;
/**
*
* This JUnit Class tests following the methods of ServiceTripImpl: bookWaypointForTrip
*
* @author Reinhard Sax
*
*/
public class ServiceTripTest extends AbstractServiceTest{
JsfUtil mockJsfUtil;
GoogleUtil mockGoogleUtil;
ServiceAssembler serviceAssembler;
User mockTripCreator;
User mockTripBooker;
Trip mockTrip4Seats;
Trip mockTrip0Seats;
Waypoint mockTripWaypoint;
Waypoint mockBookerWaypointInactive;
TripDAO mockTripDAO;
WaypointDAO mockWaypointDAO;
PointDAO mockPointDAO;
Date departure;
Location fromLocation;
Location toLocation;
/**
* This method setups the mock properties for the following tests
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
departure = new Date();
mockTripCreator = new User("Trip","Creator","","","","",null,null, "");
fromLocation = new Location(1,1000,"City1");
toLocation = new Location(2,2000,"City2");
mockTripWaypoint = new Waypoint(1,fromLocation,toLocation,mockTripCreator,"",true);
mockTrip4Seats = new Trip(1,mockTripCreator,departure,4,mockTripWaypoint,"");
mockTripBooker = new User("Trip", "Booker", "", "", "", "", null, null, "");
mockBookerWaypointInactive = new Waypoint(fromLocation,toLocation,mockTripBooker,"",false);
mockJsfUtil = EasyMock.createMock(JsfUtil.class);
mockGoogleUtil = EasyMock.createMock(GoogleUtil.class);
mockTripDAO = EasyMock.createMock(TripDAO.class);
mockWaypointDAO = EasyMock.createMock(WaypointDAO.class);
mockPointDAO = EasyMock.createMock(PointDAO.class);
mockDataSource = EasyMock.createMock(DataSource.class);
mockConnection = EasyMock.createMock(Connection.class);
serviceAssembler = new ServiceAssembler() {
@Override
public ServiceUser createServiceUser() {
throw new UnsupportedOperationException("createServiceTrip()");
}
@Override
public ServiceTrip createServiceTrip() {
throw new UnsupportedOperationException("createServiceTrip()");
}
@Override
public JsfUtil createJsfUtil() {
return mockJsfUtil;
}
@Override
public GoogleUtil createGoogleUtil() {
return mockGoogleUtil;
}
@Override
public RandomUtil createRandomUtil()
{
throw new UnsupportedOperationException("createRandomUtil()");
}
@Override
public EmailUtil createEmailUtil()
{
throw new UnsupportedOperationException("createEmailUtil()");
}
};
ServiceAssembler.setServiceAssembler(serviceAssembler);
}
/**
* This methods runs the bookWaypointForTrip method of the ServiceTripImpl class.
* This tests should run without an error.
*
* @throws SQLException
*/
@Test
public void testBookTrip() throws SQLException {
// Setup Mocks:
mockBegin();
mockWaypointDAO.update(mockBookerWaypointInactive,mockGetConnection());
mockWaypointDAO.addWaypointToTrip(mockTrip4Seats, mockBookerWaypointInactive, mockConnection);
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
serviceTrip.bookWaypointForTrip(mockBookerWaypointInactive, mockTrip4Seats);
// Verify
verifyMocks();
}
/**
* This method runs the bookWaypointForTrip method of the ServiceTripImpl class.
* This tests expects that the WaypointDAO.update() method throws a DAOExpetion
* which leads to a ServiceException in the boockWaypointForTrip method
* @throws SQLException
*/
@Test
public void testBookTripUpdateException() throws SQLException {
// Setup Mocks:
mockBegin();
mockWaypointDAO.update(mockBookerWaypointInactive,mockGetConnection());
EasyMock.expectLastCall().andThrow(new DAOException("DAO Exception"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.bookWaypointForTrip(mockBookerWaypointInactive, mockTrip4Seats);
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the bookWaypointForTrip method of the ServiceTripImpl class.
* This tests expects that the WaypointDAO.addWaypointToTrip() method throws a DAOExpetion
* which leads to a ServiceException in the boockWaypointForTrip method
* @throws SQLException
*/
@Test
public void testBookTripAddWaypointException() throws SQLException {
// Setup Mocks:
mockBegin();
mockWaypointDAO.update(mockBookerWaypointInactive,mockGetConnection());
mockWaypointDAO.addWaypointToTrip(mockTrip4Seats, mockBookerWaypointInactive, mockConnection);
EasyMock.expectLastCall().andThrow(new DAOException("DAO Exception"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.bookWaypointForTrip(mockBookerWaypointInactive, mockTrip4Seats);
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the bookWaypointForTrip method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testBookTripNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.bookWaypointForTrip(mockBookerWaypointInactive, mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the bookWaypointForTrip method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testBookTripCommitFailed() throws SQLException {
// Setup Mocks:
mockBegin();
mockWaypointDAO.update(mockBookerWaypointInactive,mockGetConnection());
mockWaypointDAO.addWaypointToTrip(mockTrip4Seats, mockBookerWaypointInactive, mockConnection);
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.bookWaypointForTrip(mockBookerWaypointInactive, mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the serchTrip method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
*/
@Test
public void testSearchTrips() throws SQLException {
List<Trip> tripList = new ArrayList<Trip>();
tripList.add(mockTrip4Seats);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.selectAll(mockGetConnection())).andReturn(tripList);
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
Assert.assertEquals(tripList, serviceTrip.searchTrip("City1", "City2"));
// Verify
verifyMocks();
}
/**
* This method runs the searchTrip method of the ServiceTripImpl class.
* This tests expects that the TripDAO.selectAll() method throws a DAOExpetion
* which leads to a ServiceException in the searchTrip method
* @throws SQLException
*/
@Test
public void testSearchTripsException() throws SQLException {
List<Trip> tripList = new ArrayList<Trip>();
tripList.add(mockTrip4Seats);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.selectAll(mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.searchTrip("City1", "City2");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the searchTrip method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testSearchTripsNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.searchTrip("City1", "City2");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the searchTrip method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testSearchTripsCommitFailed() throws SQLException {
List<Trip> tripList = new ArrayList<Trip>();
tripList.add(mockTrip4Seats);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.selectAll(mockGetConnection())).andReturn(tripList);
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.searchTrip("City1", "City2");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTrip() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
JSONObject jsonObject = null;
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(newTrip, mockGetConnection())).andReturn(new ArrayList<Waypoint>());
EasyMock.expect(mockGoogleUtil.getJsonObj(newTrip, new ArrayList<Waypoint>())).andReturn(jsonObject);
EasyMock.expect(mockGoogleUtil.loadGoogleData(newTrip, jsonObject)).andReturn(tripWithPoint);
mockPointDAO.insertPoints(tripWithPoint.getPoints(), mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* This tests expects that the TripDAO.update() method throws a DAOExpetion
* which leads to a ServiceException in the insertTrip method
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTripException() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(newTrip, mockGetConnection());
EasyMock.expectLastCall().andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
try{
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the insertTrip method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
* @throws IOException
*/
@Test
public void testInsertTripNoConnection() throws SQLException, IOException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
try{
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the insertTrip method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTripCommitFailed() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException("Error while commiting!"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
try{
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* This tests expects that the GoogleUtil.loadGoogleData() method throws an IOException
* which leads to a ServiceException in the insertTrip method
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTriploadGoogleDataThrowsIOException() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
JSONObject jsonObject = null;
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(newTrip, mockGetConnection())).andReturn(new ArrayList<Waypoint>());
EasyMock.expect(mockGoogleUtil.getJsonObj(newTrip,new ArrayList<Waypoint>())).andReturn(jsonObject);
EasyMock.expect(mockGoogleUtil.loadGoogleData(newTrip, jsonObject)).andThrow(new IOException("IOException in loadGoogleData"));
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* This tests expects that the GoogleUtil.loadGoogleData() method throws an JSONException
* which leads to a ServiceException in the insertTrip method
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTriploadGoogleDataThrowsJSONException() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
JSONObject jsonObject = null;
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(newTrip, mockGetConnection())).andReturn(new ArrayList<Waypoint>());
EasyMock.expect(mockGoogleUtil.getJsonObj(newTrip,new ArrayList<Waypoint>())).andReturn(jsonObject);
EasyMock.expect(mockGoogleUtil.loadGoogleData(newTrip, jsonObject)).andThrow(new JSONException("JSONException in loadGoogleData"));
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* This tests expects that the GoogleUtil.getJSONObject() method throws a IOException
* which leads to a ServiceException in the insertTrip method
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTripgetJSONObjectThrowsIOException() throws SQLException, IOException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(newTrip, mockGetConnection())).andReturn(new ArrayList<Waypoint>());
EasyMock.expect(mockGoogleUtil.getJsonObj(newTrip,new ArrayList<Waypoint>())).andThrow(new IOException("IOException in loadGoogleData"));
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* In the loadAndPersistGoogleData method the commit causes a SQLException.
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTriploadAndPersistGoogleDataThrowsSQLException() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
JSONObject jsonObject = null;
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(newTrip, mockGetConnection())).andReturn(new ArrayList<Waypoint>());
EasyMock.expect(mockGoogleUtil.getJsonObj(newTrip,new ArrayList<Waypoint>())).andReturn(jsonObject);
EasyMock.expect(mockGoogleUtil.loadGoogleData(newTrip, jsonObject)).andReturn(tripWithPoint);
mockPointDAO.insertPoints(tripWithPoint.getPoints(), mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
// Verify
verifyMocks();
}
/**
* This method runs the insertTrip method of the ServiceTripImpl class.
* In the loadAndPersistGoogleData method the commit causes a SQLException.
* @throws SQLException
* @throws IOException
* @throws JSONException
*/
@Test
public void testInsertTriploadAndPersistGoogleDataThrowsDAOException() throws SQLException, IOException, JSONException {
Waypoint waypoint = new Waypoint(fromLocation, toLocation, mockTripCreator, null, true);
Trip newTrip = new Trip(mockTripCreator,departure,4,waypoint, "");
Trip tripWithPoint = newTrip;
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0.0, 0.0, 0, newTrip.getId()));
tripWithPoint.setPoints(points);
JSONObject jsonObject = null;
// Setup Mocks:
EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockTripCreator);
mockBegin();
mockTripDAO.update(tripWithPoint, mockGetConnection());
EasyMock.expectLastCall();
mockCommit();
mockCloseConnection();
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(newTrip, mockGetConnection())).andReturn(new ArrayList<Waypoint>());
EasyMock.expect(mockGoogleUtil.getJsonObj(newTrip,new ArrayList<Waypoint>())).andReturn(jsonObject);
EasyMock.expect(mockGoogleUtil.loadGoogleData(newTrip, jsonObject)).andReturn(tripWithPoint);
mockPointDAO.insertPoints(tripWithPoint.getPoints(), mockGetConnection());
EasyMock.expectLastCall().andThrow(new DAOException("getPoints throws DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO,mockWaypointDAO,mockPointDAO,mockDataSource, mockJsfUtil, mockGoogleUtil);
serviceTrip.insertTrip(departure, 4, fromLocation, toLocation);
// Verify
verifyMocks();
}
/**
* This method runs the removeTrip method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
*/
@Test
public void testRemoveTrip() throws SQLException {
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.getByID(1,mockGetConnection())).andReturn(mockTrip4Seats);
mockTripDAO.delete(mockTrip4Seats, mockGetConnection());
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
serviceTrip.removeTrip("1");
// Verify
verifyMocks();
}
/**
* This method runs the removeTrip method of the ServiceTripImpl class.
* This tests expects that the TripDAO.delete() method throws a DAOExpetion
* which leads to a ServiceException in the removeTrip method
* @throws SQLException
*/
@Test
public void testRemoveTripExceptionInDelete() throws SQLException {
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.getByID(1,mockGetConnection())).andReturn(mockTrip4Seats);
mockTripDAO.delete(mockTrip4Seats, mockGetConnection());
EasyMock.expectLastCall().andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.removeTrip("1");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the removeTrip method of the ServiceTripImpl class.
* This tests expects that the TripDAO.getById() method throws a DAOExpetion
* which leads to a ServiceException in the removeTrip method
* @throws SQLException
*/
@Test
public void testRemoveTripExceptionInGetId() throws SQLException {
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.getByID(1,mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.removeTrip("1");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the removeTrip method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testRemoveTripNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.removeTrip("1");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the removeTrip method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testRemoveTripCommitFailed() throws SQLException {
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.getByID(1,mockGetConnection())).andReturn(mockTrip4Seats);
mockTripDAO.delete(mockTrip4Seats, mockGetConnection());
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.removeTrip("1");
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the getWaypointsForUser method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
*/
@Test
public void testGetWaypointsForUser() throws SQLException {
List<Waypoint> userWaypointList = new ArrayList<Waypoint>();
userWaypointList.add(mockBookerWaypointInactive);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsFromUser(mockTripBooker, mockGetConnection())).andReturn(userWaypointList);
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
Assert.assertEquals(userWaypointList, serviceTrip.getWaypointsForUser(mockTripBooker));
// Verify
verifyMocks();
}
/**
* This method runs the getWaypointsForUser method of the ServiceTripImpl class.
* This tests expects that the WaypointDAO.getWaypointsFromUser method throws a DAOExpetion
* which leads to a ServiceException in the getWaypointsForUser method
* @throws SQLException
*/
@Test
public void testGetWaypointsForUserException() throws SQLException {
List<Waypoint> userWaypointList = new ArrayList<Waypoint>();
userWaypointList.add(mockBookerWaypointInactive);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsFromUser(mockTripBooker, mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getWaypointsForUser(mockTripBooker);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the getWaypointForUser method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testGetWaypointsForUserNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getWaypointsForUser(mockTripBooker);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the getWaypointsForUser method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testGetWaypointsForUserCommitFailed() throws SQLException {
List<Waypoint> userWaypointList = new ArrayList<Waypoint>();
userWaypointList.add(mockBookerWaypointInactive);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsFromUser(mockTripBooker, mockGetConnection())).andReturn(userWaypointList);
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getWaypointsForUser(mockTripBooker);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the getPointsForTrip method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
*/
@Test
public void testGetPointsForTrip() throws SQLException {
Point point = new Point(1,1,1,1,1,1);
List<Point> pointList = new ArrayList<Point>();
pointList.add(point);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockPointDAO.getByTrip(mockTrip4Seats, mockGetConnection())).andReturn(pointList);
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
Assert.assertEquals(pointList, serviceTrip.getPointsForTrip(mockTrip4Seats));
// Verify
verifyMocks();
}
/**
* This method runs the getPointsForTrip method of the ServiceTripImpl class.
* This tests expects that the PointDAO.getByTrip method throws a DAOExpetion
* which leads to a ServiceException in the getPointsForTrip method
* @throws SQLException
*/
@Test
public void testGetPointsForTripException() throws SQLException {
// Setup Mocks:
mockBegin();
EasyMock.expect(mockPointDAO.getByTrip(mockTrip4Seats, mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getPointsForTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the getPointsForTrip method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testGetPointsForTripNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getPointsForTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the getPointsForTrip method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testGetPointsForTripCommitFailed() throws SQLException {
Point point = new Point(1,1,1,1,1,1);
List<Point> pointList = new ArrayList<Point>();
pointList.add(point);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockPointDAO.getByTrip(mockTrip4Seats, mockGetConnection())).andReturn(pointList);
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getPointsForTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the getWaypointsForTrip method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
*/
@Test
public void testGetWaypointsForTrip() throws SQLException {
List<Waypoint> waypointList = new ArrayList<Waypoint>();
waypointList.add(mockTripWaypoint);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(mockTrip4Seats, mockGetConnection())).andReturn(waypointList);
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
Assert.assertEquals(waypointList, serviceTrip.getWaypointsForTrip(mockTrip4Seats));
// Verify
verifyMocks();
}
/**
* This method runs the getWaypointsForTrip method of the ServiceTripImpl class.
* This tests expects that the WaypointDAO.getWaypointsForTrip method throws a DAOExpetion
* which leads to a ServiceException in the getWaypointsForTrip method
* @throws SQLException
*/
@Test
public void testGetWaypointsForTripException() throws SQLException {
// Setup Mocks:
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(mockTrip4Seats, mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getWaypointsForTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the getWaypointsForTrip method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testGetWaypointsForTripNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getWaypointsForTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the getWaypointsForTrip method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testGetWaypointsForTripCommitFailed() throws SQLException {
List<Waypoint> waypointList = new ArrayList<Waypoint>();
waypointList.add(mockTripWaypoint);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockWaypointDAO.getWaypointsForTrip(mockTrip4Seats, mockGetConnection())).andReturn(waypointList);
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.getWaypointsForTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This method runs the selectAllTrips method of the ServiceTripImpl class.
* This tests should run without errors
* @throws SQLException
*/
@Test
public void testSelectAllTrips() throws SQLException {
List<Trip> allTrips = new ArrayList<Trip>();
allTrips.add(mockTrip4Seats);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.selectAll(mockGetConnection())).andReturn(allTrips);
mockCommit();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
Assert.assertEquals(allTrips, serviceTrip.selectAllTrips());
// Verify
verifyMocks();
}
/**
* This method runs the selectAllTrips method of the ServiceTripImpl class.
* This tests expects that the TripDAO.selectAll method throws a DAOExpetion
* which leads to a ServiceException in the selectAllTrips method
* @throws SQLException
*/
@Test
public void testSelectAllTripsException() throws SQLException {
List<Trip> allTrips = new ArrayList<Trip>();
allTrips.add(mockTrip4Seats);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.selectAll(mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.selectAllTrips();
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the selectAllTrips method of the ServiceTripImpl class.
* In this test the getConnection() method should cause an SQLException,
* so that it is unable to get a Connection.
* As a result this should throw an ServiceException.
*
* @throws SQLException
*/
@Test
public void testSelectAllTripsNoConnection() throws SQLException {
// Setup Mocks:
mockBeginNoConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.selectAllTrips();
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
/**
* This methods runs the selectAllTrips method of the ServiceTripImpl class.
* In this test the commit should return a SQLException.
* This should cause a ServiceException.
*
* @throws SQLException
*/
@Test
public void testSelectAllTripsCommitFailed() throws SQLException {
List<Trip> allTrips = new ArrayList<Trip>();
allTrips.add(mockTrip4Seats);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockTripDAO.selectAll(mockGetConnection())).andReturn(allTrips);
mockCommit();
EasyMock.expectLastCall().andThrow(new SQLException());
mockRollback();
mockCloseConnection();
replayMocks();
// Do Test:
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try{
serviceTrip.selectAllTrips();
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
// Verify
verifyMocks();
}
@Test
public void testGetMapModelFromTrip() throws SQLException {
//input data
//setup mocks
Point point = new Point(1,1,1,1,1,1);
List<Point> pointList = new ArrayList<Point>();
pointList.add(point);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockPointDAO.getByTrip(mockTrip4Seats, mockGetConnection())).andReturn(pointList);
mockCommit();
mockCloseConnection();
replayMocks();
//do test
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
serviceTrip.getMapModelFromTrip(mockTrip4Seats);
//verify
verifyMocks();
}
@Test
public void testGetMapModelFromTripServiceException() throws SQLException {
//input data
//setup mocks
Point point = new Point(1,1,1,1,1,1);
List<Point> pointList = new ArrayList<Point>();
pointList.add(point);
// Setup Mocks:
mockBegin();
EasyMock.expect(mockPointDAO.getByTrip(mockTrip4Seats, mockGetConnection())).andThrow(new DAOException("DAOException"));
mockRollback();
mockCloseConnection();
replayMocks();
//do test
ServiceTrip serviceTrip = new ServiceTripImpl(mockTripDAO, mockWaypointDAO, mockPointDAO, mockDataSource, null, null);
try {
serviceTrip.getMapModelFromTrip(mockTrip4Seats);
Assert.fail("ServiceException didn't occure");
}
catch(ServiceException e){ }
//verify
verifyMocks();
}
/**
* This method replays all mocks
*
* @throws SQLException
*/
protected void replayMocks() throws SQLException {
EasyMock.replay(mockTripDAO);
EasyMock.replay(mockWaypointDAO);
EasyMock.replay(mockPointDAO);
EasyMock.replay(mockJsfUtil);
EasyMock.replay(mockGoogleUtil);
super.replayMocks();
}
/**
* This method verifies all mocks.
*
* @throws SQLException
*/
protected void verifyMocks() throws SQLException {
EasyMock.verify(mockTripDAO);
EasyMock.verify(mockWaypointDAO);
EasyMock.verify(mockPointDAO);
EasyMock.verify(mockJsfUtil);
EasyMock.verify(mockGoogleUtil);
super.verifyMocks();
}
}