/*
Copyright (C) European Community 2008 - Licensed under the EUPL V.1.0 (http://ec.europa.eu/idabc/en/document/6523)
*/
package test.it.hotel.controller;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import it.hotel.controller.booking.BookingCheckinController;
import it.hotel.controller.booking.BookingControllerDelegate;
import it.hotel.controller.booking.BookingFormController;
import it.hotel.controller.booking.BookingWizardEditController;
import it.hotel.controller.booking.DTO.BookingDTO;
import it.hotel.controller.confirmedBooking.ConfirmedBookingDelegate;
import it.hotel.controller.customer.CustomerControllerDelegate;
import it.hotel.controller.customer.CustomerFormController;
import it.hotel.controller.facility.FacilityFormController;
import it.hotel.controller.frontend.structure.StructureControllerDelegate;
import it.hotel.controller.hotel.HotelControllerDelegate;
import it.hotel.controller.hotel.HotelFormController;
import it.hotel.controller.locale.ILocaleContainer;
import it.hotel.controller.map.MapController;
import it.hotel.controller.pricelist.PriceListControllerDelegate;
import it.hotel.controller.pricelist.PriceListFormController;
import it.hotel.controller.pricelist.PriceListMultiFormController;
import it.hotel.controller.pricelist.DTO.PriceListDTO;
import it.hotel.controller.room.RoomControllerDelegate;
import it.hotel.controller.room.RoomFormController;
import it.hotel.controller.structure.StructureFormController;
import it.hotel.controller.typology.TypologyDelegate;
import it.hotel.controller.typology.TypologyFormController;
import it.hotel.controller.user.IUserContainer;
import it.hotel.controller.user.UserController;
import it.hotel.model.CalendarUtils;
import it.hotel.model.booking.Booking;
import it.hotel.model.booking.CompletedBooking;
import it.hotel.model.booking.ConfirmedBooking;
import it.hotel.model.booking.manager.IBookingManager;
import it.hotel.model.customer.Customer;
import it.hotel.model.customer.manager.ICustomerManager;
import it.hotel.model.facility.Facility;
import it.hotel.model.facility.manager.IFacilityManager;
import it.hotel.model.gallery.manager.IGalleryManager;
import it.hotel.model.hotel.Hotel;
import it.hotel.model.hotel.manager.IHotelManager;
import it.hotel.model.photo.Photo;
import it.hotel.model.photo.manager.IPhotoManager;
import it.hotel.model.price.Price;
import it.hotel.model.price.manager.IPriceManager;
import it.hotel.model.role.Role;
import it.hotel.model.room.Room;
import it.hotel.model.room.manager.IRoomManager;
import it.hotel.model.service.Service;
import it.hotel.model.structure.Structure;
import it.hotel.model.structure.manager.IStructureManager;
import it.hotel.model.typology.Typology;
import it.hotel.model.typology.manager.ITypologyManager;
import it.hotel.model.user.IUser;
import it.hotel.model.user.User;
import it.hotel.model.user.manager.IUserManager;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.easymock.EasyMock;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.AbstractModelAndViewTests;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class HotelModelAndViewTests extends AbstractModelAndViewTests{
protected ModelAndView mvc;
protected RoomControllerDelegate r;
protected TypologyDelegate t;
protected CustomerControllerDelegate c;
protected PriceListControllerDelegate p;
protected BookingControllerDelegate b;
protected ConfirmedBookingDelegate confirmedDelegate;
protected HotelControllerDelegate hotelDelegate;
protected StructureControllerDelegate structureDelegate;
protected PriceListControllerDelegate priceControllerDelegate;
protected BookingWizardEditController bwe;
protected List<Room> expectedRooms;
protected Room room, room1;
protected Typology typology;
protected Customer customer;
protected Hotel hotel;
protected Booking booking, booking1, booking2;
protected BookingDTO bookingDTO;
protected ConfirmedBooking confBooking;
protected CompletedBooking completedBooking;
protected Service service;
protected Price price, price1, price2;
protected PriceListDTO priceListDTO;
protected User user1,user2;
protected MapController mapcontroller;
protected Role role;
protected List<Typology> expectedTypologies;
protected List<Hotel> expectedHotels;
protected List<Customer> expectedCustomers;
protected Set<Price> expectedPrices, expectedPrices1;
protected List<Booking> expectedBookings;
protected List<Booking> expectedBookingsAndConfirmed;
public MockHttpServletRequest req = new MockHttpServletRequest();
public MockHttpServletResponse resp = new MockHttpServletResponse();
public ILocaleContainer localeContainer = createMock(ILocaleContainer.class);
/*
* Managers
*/
public ITypologyManager typologyManager = createMock(ITypologyManager.class);
public IStructureManager structureManager = createMock(IStructureManager.class);
public IHotelManager hotelManager = createMock(IHotelManager.class);
public IRoomManager roomManager = createMock(IRoomManager.class);
public IPriceManager priceManager=createMock(IPriceManager.class);
public IBookingManager bookingManager = createMock(IBookingManager.class);
public ICustomerManager customerManager = createMock(ICustomerManager.class);
public IUserContainer userContainer = createMock(IUserContainer.class);
public IUser user = createMock(IUser.class);
public IUserManager userManager = createMock(IUserManager.class);
public IFacilityManager facilityManager = createMock(IFacilityManager.class);
public IPhotoManager photoManager = createMock(IPhotoManager.class);
public IGalleryManager galleryManager = createMock(IGalleryManager.class);
/*
* FormController
*/
public PriceListFormController listFormController = new PriceListFormController();
public PriceListMultiFormController listFormMultiController= new PriceListMultiFormController();
public BookingFormController bookingFormController = new BookingFormController();
public BookingCheckinController bookingCheckinFormController = new BookingCheckinController();
public TypologyFormController typologyFormController = new TypologyFormController();
public RoomFormController roomFormController = new RoomFormController();
public CustomerFormController customerFormController = new CustomerFormController();
public HotelFormController hotelFormController = new HotelFormController();
public StructureFormController structureFormController = new StructureFormController();
public UserController userController = new UserController();
public HotelControllerDelegate hotelControllerDelegate = new HotelControllerDelegate();
public FacilityFormController facilityFormController = new FacilityFormController();
/*
* EditController
*/
public BookingWizardEditController bookingWizardEditController = new BookingWizardEditController();
protected static User setUpUserMock() throws Exception{
return org.easymock.classextension.EasyMock.createMock(User.class,new Method[]{ User.class.getMethod("getRole", null),
User.class.getMethod("getStructureId", null),
User.class.getMethod("setStructureId",int.class)});
}
protected static Role setUpRoleMock() throws Exception{
return org.easymock.classextension.EasyMock.createMock(Role.class,new Method[]{ Role.class.getMethod("getName", null),
Role.class.getMethod("hasPermission", String.class)});
}
public void addParam(String name, String value){
req.addParameter(name, value);
}
public void expectUserGet(User user){
EasyMock.expect(userContainer.getUser()).andReturn(user1);
}
public void expectUserReturnHotelId(User user, int hotelId){
EasyMock.expect(user.getStructureId()).andReturn(hotelId);
}
public void expectTypologiesFromHotel(int hotelId, List<Typology> typologies){
expect(typologyManager.getTypologiesFromStructure(hotelId)).andReturn(typologies);
}
public void expectToGetHotel(int hotelId, Hotel hotel){
expect(hotelManager.get(hotelId)).andReturn(hotel);
}
public void expectToGetStructure(int structureId, Structure structure){
expect(structureManager.get(structureId)).andReturn(hotel);
}
public void expectLocalizedObjects(){
expect(localeContainer.getLocale()).andReturn("it");
localeContainer.setLocale("it");
}
public void setGetRequest(String url){
req.setMethod("GET");
req.setRequestURI(url);
}
public void setPostRequest(String url){
req.setMethod("POST");
req.setRequestURI(url);
}
public void replayAll(){
replay(typologyManager,hotelManager,userContainer,facilityManager,localeContainer,photoManager,structureManager);
org.easymock.classextension.EasyMock.replay(user1);
org.easymock.classextension.EasyMock.replay(role);
}
public void verifyAll(){
verify(userContainer,hotelManager,typologyManager,facilityManager,localeContainer,photoManager,structureManager);
org.easymock.classextension.EasyMock.verify(user1);
org.easymock.classextension.EasyMock.verify(role);
}
public void expectToAccessUsersRole(User user, Role role,String permission,boolean retval){
EasyMock.expect(user.getRole()).andReturn(role);
EasyMock.expect(role.hasPermission(permission)).andReturn(retval);
}
public void expectAllFacilities(List<Facility> facilities){
expect(facilityManager.getAll()).andReturn(facilities);
}
public void expectAllPhoto(List<Photo> photos){
expect(photoManager.getAll()).andReturn(photos);
}
public void setCommand(SimpleFormController formController,
Class clazz, String name,String successView) {
formController.setCommandClass(clazz);
formController.setCommandName(name);
formController.setSuccessView(successView);
}
public List<Facility> getAllFacilities(){
Facility f1 = new Facility();
f1.setId(1);
f1.setCode("swimming pool");
Facility f2 = new Facility();
f2.setId(2);
f2.setCode("night service");
List<Facility> allFacilities = new ArrayList<Facility>();
allFacilities.add(f1);
allFacilities.add(f2);
return allFacilities;
}
public List<Photo> getAllPhotos(){
Photo p1 = new Photo();
p1.setId(1);
p1.setDescription("Description1");
Photo p2 = new Photo ();
p2.setId(2);
p2.setDescription("DEscription2");
List<Photo> allPhotos = new ArrayList<Photo> ();
allPhotos.add(p1);
allPhotos.add(p2);
return allPhotos;
}
public Facility getFacility(int id,String code){
Facility f1 = new Facility();
f1.setId(id);
f1.setCode(code);
return f1;
}
@Override
protected void setUp() throws Exception {
createUser();
createPriceListDTO();
createBookings();
createBookingDTO();
createControllers();
createLists();
createModels();
setupAllExpectedArrayLists();
addCommandManagersToControllers();
super.setUp();
}
private void addCommandManagersToControllers() {
listFormController.setCommandClass(PriceListDTO.class);
listFormController.setStructureManager(structureManager);
listFormController.setTypologyManager(typologyManager);
listFormController.setPriceManager(priceManager);
listFormController.setUserContainer(userContainer);
listFormMultiController.setCommandClass(PriceListDTO.class);
listFormMultiController.setStructureManager(structureManager);
listFormMultiController.setTypologyManager(typologyManager);
listFormMultiController.setPriceManager(priceManager);
listFormMultiController.setUserContainer(userContainer);
bookingFormController.setCommandClass(Booking.class);
bookingFormController.setBookingManager(bookingManager);
bookingFormController.setRoomManager(roomManager);
bookingFormController.setStructureManager(structureManager);
bookingCheckinFormController.setCommandClass(Booking.class);
bookingCheckinFormController.setCustomerManager(customerManager);
bookingCheckinFormController.setBookingManager(bookingManager);
bookingCheckinFormController.setStructureManager(structureManager);
bookingWizardEditController.setCommandClass(Booking.class);
bookingWizardEditController.setCustomerManager(customerManager);
bookingWizardEditController.setBookingManager(bookingManager);
bookingWizardEditController.setStructureManager(structureManager);
bookingWizardEditController.setTypologyManager(typologyManager);
typologyFormController.setCommandClass(Typology.class);
typologyFormController.setStructureManager(structureManager);
typologyFormController.setTypologyManager(typologyManager);
typologyFormController.setUserContainer(userContainer);
typologyFormController.setLocaleContainer(localeContainer);
roomFormController.setCommandClass(Room.class);
roomFormController.setStructureManager(structureManager);
roomFormController.setRoomManager(roomManager);
roomFormController.setTypologyManager(typologyManager);
roomFormController.setUserContainer(userContainer);
customerFormController.setCommandClass(Customer.class);
customerFormController.setCustomerManager(customerManager);
customerFormController.setStructureManager(structureManager);
customerFormController.setUserContainer(userContainer);
hotelFormController.setCommandClass(Hotel.class);
structureFormController.setCommandClass(Structure.class);
structureFormController.setLocaleContainer(localeContainer);
structureFormController.setStructureManager(structureManager);
userController.setCommandClass(User.class);
}
private void setupAllExpectedArrayLists() {
setUpExpectedRooms();
setUpExpectedTypologies();
setupExpectedCustomers();
setupExpectedBookings();
setUpRooms();
setupExpectedConfirmedBookings();
setupExpectedCompletedBookings();
setupExpectedHotels();
setupExpectedService();
setupExpectedPrice();
}
private void setupExpectedHotels() {
Hotel h1=new Hotel();
h1.setId(1);
Hotel h2=new Hotel();
h2.setId(2);
expectedHotels.add(h1);
expectedHotels.add(h2);
}
private void setupExpectedBookings() {
booking = new Booking();
booking.setId(1);
booking.setBeginDate(CalendarUtils.GetToday());
booking.setFinishDate(CalendarUtils.GetTomorrow());
Booking booking2 = new Booking();
booking2.setId(2);
booking2.setBeginDate(CalendarUtils.GetToday());
booking2.setFinishDate(CalendarUtils.GetTomorrow());
Booking booking3 = new Booking();
booking3.setId(3);
booking3.setBeginDate(CalendarUtils.GetToday());
booking3.setFinishDate(CalendarUtils.GetTomorrow());
expectedBookings.add(booking);
expectedBookings.add(booking2);
expectedBookings.add(booking3);
}
private void createBookings(){
booking1 = new Booking();
booking1.setBeginDate(CalendarUtils.nexDay(CalendarUtils.GetToday()));
booking1.setFinishDate(CalendarUtils.GetToday());
booking1.setId(1);
booking1.setRoom(room);
booking2 = new Booking();
booking2.setBeginDate(CalendarUtils.beforeDay((CalendarUtils.GetToday())));
booking2.setFinishDate(CalendarUtils.nexDay(CalendarUtils.GetToday()));
booking2.setRoom(room);
}
private void createBookingDTO() {
bookingDTO = new BookingDTO(booking1);
}
private void createPriceListDTO(){
priceListDTO = new PriceListDTO();
Typology t1=new Typology();
t1.setId(1);
t1.setHotelId(1);
priceListDTO.setDate("35/15/70");
priceListDTO.setFinishDate("40/51/64");
priceListDTO.setTypology(t1);
priceListDTO.setPrice("noPrice");
}
private void createUser(){
user1 = new User();
user1.setName(null);
user1.setPassword("password");
user1.setPasswordConfirm("password1");
user1.setUserName("username");
}
private void setupExpectedConfirmedBookings(){
confBooking = new ConfirmedBooking();
confBooking.setId(3);
confBooking.setType("CONFIRMED");
confBooking.setTotal(new BigDecimal(100));
confBooking.setBeginDate(CalendarUtils.GetToday());
confBooking.setFinishDate(CalendarUtils.nexDay(CalendarUtils.GetToday()));
confBooking.setRoom(room1);
expectedBookingsAndConfirmed.add(booking);
expectedBookingsAndConfirmed.add(confBooking);
}
private void setupExpectedCompletedBookings(){
completedBooking = new CompletedBooking();
}
private void setupExpectedCustomers() {
Customer c1=new Customer();
c1.setId(1);
c1.setHotelId(1);
Customer c2=new Customer();
c2.setId(2);
c2.setHotelId(1);
expectedCustomers.add(c1);
expectedCustomers.add(c2);
}
private void setUpExpectedTypologies() {
Typology t1=new Typology();
t1.setId(1);
t1.setHotelId(1);
Typology t2=new Typology();
t2.setId(2);
t2.setHotelId(1);
Typology t3=new Typology();
t3.setId(3);
t3.setHotelId(1);
expectedTypologies.add(t1);
expectedTypologies.add(t2);
expectedTypologies.add(t3);
}
private void setUpRooms(){
Typology t1 = new Typology();
price1= new Price();
price1.setCalendarDate(CalendarUtils.GetToday());
price1.setTypology_id(t1.getId());
price1.setPrice(new BigDecimal(120));
expectedPrices1.add(price1);
price2= new Price();
price2.setCalendarDate(CalendarUtils.GetGregorianCalendar(CalendarUtils.getTomorrowInddMMyyyy()));
price2.setTypology_id(t1.getId());
price2.setPrice(new BigDecimal(120));
expectedPrices1.add(price2);
t1.setPriceList(expectedPrices1);
room1 = new Room();
room1.setTypology(t1);
Hotel h1 = new Hotel();
h1.setId(4);
room1.setStructure(h1);
}
private void setUpExpectedRooms() {
Typology t1=new Typology();
t1.setId(1);
t1.setHotelId(1);
Room r1= new Room();
r1.setId(1);
r1.setHotelid(1);
r1.setTypology(t1);
Room r2=new Room();
r2.setId(2);
r2.setHotelid(1);
r2.setTypology(t1);
Room r3=new Room();
r3.setId(3);
r3.setHotelid(1);
r3.setTypology(t1);
expectedRooms.add(r1);
expectedRooms.add(r2);
expectedRooms.add(r3);
}
private void createModels() {
hotel= new Hotel();
typology=new Typology();
customer=new Customer();
room= new Room();
}
private void createLists() {
expectedTypologies= new ArrayList<Typology> ();
expectedHotels= new ArrayList<Hotel>();
expectedRooms= new ArrayList<Room> ();
expectedCustomers=new ArrayList<Customer>();
expectedBookings = new ArrayList<Booking>();
expectedBookingsAndConfirmed = new ArrayList<Booking>();
expectedPrices1 = new HashSet<Price>();
}
private void createControllers() {
r = new RoomControllerDelegate();
r.setStructureManager(structureManager);
r.setManager(roomManager);
r.setTypologyManager(typologyManager);
r.setUserContainer(userContainer);
t= new TypologyDelegate();
t.setStructureManager(structureManager);
t.setManager(typologyManager);
t.setLocaleContainer(localeContainer);
t.setUserContainer(userContainer);
c= new CustomerControllerDelegate();
c.setStructureManager(structureManager);
c.setManager(customerManager);
p= new PriceListControllerDelegate();
p.setStructureManager(structureManager);
p.setUserContainer(userContainer);
p.setTypologyManager(typologyManager);
b = new BookingControllerDelegate();
b.setBookingManager(bookingManager);
b.setRoomManager(roomManager);
b.setStructureManager(structureManager);
bwe = new BookingWizardEditController();
bwe.setStructureManager(structureManager);
bwe.setCustomerManager(customerManager);
bwe.setTypologyManager(typologyManager);
bwe.setRoomManager(roomManager);
hotelDelegate = new HotelControllerDelegate();
hotelDelegate.setManager(hotelManager);
hotelDelegate.setRoomManager(roomManager);
hotelDelegate.setLocaleContainer(localeContainer);
hotelDelegate.setUserContainer(userContainer);
hotelDelegate.setUserManager(userManager);
hotelDelegate.setStructureManager(structureManager);
confirmedDelegate = new ConfirmedBookingDelegate();
confirmedDelegate.setManager(bookingManager);
priceControllerDelegate=new PriceListControllerDelegate();
priceControllerDelegate.setStructureManager(structureManager);
priceControllerDelegate.setPriceManager(priceManager);
priceControllerDelegate.setTypologyManager(typologyManager);
mapcontroller = new MapController();
mapcontroller.setStructureManager(structureManager);
}
public void setupExpectedPrice(){
Typology typ= expectedTypologies.get(1);
GregorianCalendar date = CalendarUtils.GetToday();
price=new Price();
price.setCalendarDate(date);
price.setTypology_id(typ.getId());
price.setPrice(new BigDecimal(120));
expectedPrices= new HashSet<Price>();
expectedPrices.add(price);
}
public void setupExpectedService(){
service = new Service();
service.setDescription("car rent");
service.setCost(new BigDecimal(1000));
service.setDateadded(CalendarUtils.GetToday());
}
}