Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cz.muni.fi.pv168.project.hotelmanager; import java.sql.Connection; import java.sql.SQLException; import java.time.Clock; import java.time.LocalDate; import java.time.LocalDateTime; import static java.time.Month.FEBRUARY; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.derby.jdbc.EmbeddedDriver; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import org.junit.After; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.core.io.ClassPathResource; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; /** * * @author Vladislav Malynych */ public class TestReservationManagerImpl { private ReservationManager reservationManager; private RoomManager roomManager; private GuestManager guestManager; private DataSource dataSource; private final static ZonedDateTime now = LocalDateTime.of(2016, FEBRUARY, 29, 14, 00).atZone(ZoneId.of("UTC")); @Rule public ExpectedException expectedException = ExpectedException.none(); private static Clock prepareClockMock(ZonedDateTime now) { return Clock.fixed(now.toInstant(), now.getZone()); } @Before public void setUp() throws SQLException { BasicDataSource bds = new BasicDataSource(); //set JDBC driver and URL bds.setDriverClassName(EmbeddedDriver.class.getName()); bds.setUrl("jdbc:derby:memory:TestReservationManagerDB;create=true"); this.dataSource = bds; //populate db with tables and data new ResourceDatabasePopulator(new ClassPathResource("schema-javadb.sql")).execute(bds); guestManager = new GuestManagerImpl(bds); roomManager = new RoomManagerImpl(bds); reservationManager = new ReservationManagerImpl(prepareClockMock(now), bds); } @After public void tearDown() throws SQLException { try (Connection connection = dataSource.getConnection()) { connection.prepareStatement("DROP TABLE reservation").executeUpdate(); connection.prepareStatement("DROP TABLE room").executeUpdate(); connection.prepareStatement("DROP TABLE guest").executeUpdate(); } } private Room setRoom(int roomnumber, int capacity, int costpernight, boolean doublebed) { Room room = new Room(); room.setRoomNumber(roomnumber); room.setCapacity(capacity); room.setCostPerNight(costpernight); room.setDoubleBed(doublebed); return room; } private Guest setGuest(String name, String address, String phone, String email) { Guest guest = new Guest(); guest.setName(name); guest.setAddress(address); guest.setPhone(phone); guest.setEmail(email); return guest; } private Reservation setReservation(long roomId, long guestId, LocalDate checkinDate, LocalDate checkoutDate, LocalDate reservationDate) { Reservation reservation = new Reservation(); reservation.setRoomId(roomId); reservation.setGuestId(guestId); reservation.setCheckinDate(checkinDate); reservation.setCheckoutDate(checkoutDate); reservation.setReservationDate(reservationDate); return reservation; } @Test public void testCreateReservation() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Reservation reservation = setReservation(1, 1, checkin, checkout, reservationdate); reservationManager.createReservation(reservation); Long reservationId = reservation.getReservationId(); assertNotNull(reservationId); Reservation result = reservationManager.findReservation(reservationId); assertThat("loaded reservation differs from the saved one", result, is(equalTo(reservation))); assertThat("loaded reservation is the same instance", result, is(not(sameInstance(reservation)))); assertDeepEquals(reservation, result); } @Test public void testUpdateReservation() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Reservation reservation1 = setReservation(1L, 1L, checkin, checkout, reservationdate); Reservation reservation2 = setReservation(2L, 2L, checkin.plusDays(1), checkout, reservationdate.plusDays(2)); reservationManager.createReservation(reservation1); reservationManager.createReservation(reservation2); Long reservationId = reservation1.getReservationId(); reservation1 = reservationManager.findReservation(reservationId); reservation1.setRoomId(3L); reservationManager.updateReservation(reservation1); assertThat("room id was not changed", reservation1.getRoomId(), is(equalTo(3L))); assertThat("guest id was changed", reservation1.getGuestId(), is(equalTo(1L))); assertThat("resrvation date was changed", reservation1.getReservationDate(), is(equalTo(reservationdate))); assertThat("checkin date was changed", reservation1.getCheckinDate(), is(equalTo(checkin))); assertThat("checkout date was changed", reservation1.getCheckoutDate(), is(equalTo(checkout))); reservation1 = reservationManager.findReservation(reservationId); reservation1.setGuestId(3L); reservationManager.updateReservation(reservation1); assertThat("room id was changed", reservation1.getRoomId(), is(equalTo(3L))); assertThat("guest id was not changed", reservation1.getGuestId(), is(equalTo(3L))); assertThat("resrvation date was changed", reservation1.getReservationDate(), is(equalTo(reservationdate))); assertThat("checkin date was changed", reservation1.getCheckinDate(), is(equalTo(checkin))); assertThat("checkout date was changed", reservation1.getCheckoutDate(), is(equalTo(checkout))); reservation1 = reservationManager.findReservation(reservationId); reservation1.setReservationDate(reservationdate.plusDays(1)); reservationManager.updateReservation(reservation1); assertThat("room id was changed", reservation1.getRoomId(), is(equalTo(3L))); assertThat("guest id was changed", reservation1.getGuestId(), is(equalTo(3L))); assertThat("resrvation date was not changed", reservation1.getReservationDate(), is(equalTo(reservationdate.plusDays(1)))); assertThat("checkin date was changed", reservation1.getCheckinDate(), is(equalTo(checkin))); assertThat("checkout date was changed", reservation1.getCheckoutDate(), is(equalTo(checkout))); reservation1 = reservationManager.findReservation(reservationId); reservation1.setCheckinDate(checkin.plusDays(2)); reservationManager.updateReservation(reservation1); assertThat("room id was changed", reservation1.getRoomId(), is(equalTo(3L))); assertThat("guest id was changed", reservation1.getGuestId(), is(equalTo(3L))); assertThat("resrvation date was changed", reservation1.getReservationDate(), is(equalTo(reservationdate.plusDays(1)))); assertThat("checkin date was not changed", reservation1.getCheckinDate(), is(equalTo(checkin.plusDays(2)))); assertThat("checkout date was changed", reservation1.getCheckoutDate(), is(equalTo(checkout))); reservation1 = reservationManager.findReservation(reservationId); reservation1.setCheckoutDate(checkout.plusDays(2)); reservationManager.updateReservation(reservation1); assertThat("room id was changed", reservation1.getRoomId(), is(equalTo(3L))); assertThat("guest id was changed", reservation1.getGuestId(), is(equalTo(3L))); assertThat("resrvation date was changed", reservation1.getReservationDate(), is(equalTo(reservationdate.plusDays(1)))); assertThat("checkin date was changed", reservation1.getCheckinDate(), is(equalTo(checkin.plusDays(2)))); assertThat("checkout date was not changed", reservation1.getCheckoutDate(), is(equalTo(checkout.plusDays(2)))); assertDeepEquals(reservation2, reservationManager.findReservation(reservation2.getReservationId())); } @Test(expected = IllegalArgumentException.class) public void testCreateReservationWithNull() throws Exception { reservationManager.createReservation(null); } @Test public void testCreateReservationWithWrongValues() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Reservation reservation; reservation = setReservation(-5, 1, checkin, checkout, reservationdate); try { reservationManager.createReservation(reservation); fail("wrong room id(negative value)"); } catch (IllegalArgumentException ex) { //OK } reservation = setReservation(1, -1, checkin, checkout, reservationdate); try { reservationManager.createReservation(reservation); fail("wrong guest id (negative value)"); } catch (IllegalArgumentException ex) { //OK } reservation = setReservation(1, 1, checkin.plusMonths(3), checkout, reservationdate); try { reservationManager.createReservation(reservation); fail("checkin after checkout"); } catch (IllegalArgumentException ex) { //OK } reservation = setReservation(1, 1, checkin, checkout, reservationdate.plusYears(1)); try { reservationManager.createReservation(reservation); fail("reservation date after checkin"); } catch (IllegalArgumentException ex) { //OK } reservation = setReservation(1, 1, checkin, null, reservationdate); try { reservationManager.createReservation(reservation); fail("checkout is null"); } catch (IllegalArgumentException ex) { //OK } reservation = setReservation(1, 1, null, checkout, reservationdate); try { reservationManager.createReservation(reservation); fail("checkin is null"); } catch (IllegalArgumentException ex) { //OK } } @Test public void testGetAllReservations() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); assertTrue(reservationManager.findAllReservations().isEmpty()); Reservation r1 = setReservation(1, 1, checkin, checkout, reservationdate); Reservation r2 = setReservation(2, 2, checkin.plusDays(1), checkout, reservationdate.plusDays(2)); reservationManager.createReservation(r1); reservationManager.createReservation(r2); List<Reservation> expected = Arrays.asList(r1, r2); List<Reservation> actual = reservationManager.findAllReservations(); Collections.sort(actual, idComparator); Collections.sort(expected, idComparator); assertEquals("saved and retrieved reservations differ", expected, actual); assertDeepEquals(expected, actual); } @Test public void TestdeleteRoom() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Reservation r1 = setReservation(1, 1, checkin, checkout, reservationdate); Reservation r2 = setReservation(2, 2, checkin.plusDays(1), checkout, reservationdate.plusDays(2)); reservationManager.createReservation(r1); reservationManager.createReservation(r2); assertNotNull(reservationManager.findReservation(r1.getReservationId())); assertNotNull(reservationManager.findReservation(r2.getReservationId())); reservationManager.deleteReservation(r1); assertNull(reservationManager.findReservation(r1.getReservationId())); assertNotNull(reservationManager.findReservation(r2.getReservationId())); } @Test public void TestGetCostForReservation() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Room room1 = setRoom(101, 2, 125, true); Room room2 = setRoom(102, 3, 250, false); roomManager.createRoom(room1); roomManager.createRoom(room2); Guest guest1 = setGuest("Oleg Bezpalko", "Zagorska", "75899654", "Oleg999@mail"); Guest guest2 = setGuest("Bogdan Voedylo", "Sobranetska", "78965452", "Myro777@mail"); guestManager.createGuest(guest1); guestManager.createGuest(guest2); Reservation r1 = setReservation(room1.getId(), guest1.getId(), checkin, checkout, reservationdate); Reservation r2 = setReservation(room2.getId(), guest2.getId(), checkin.plusDays(1), checkout.plusDays(2), reservationdate); reservationManager.createReservation(r1); reservationManager.createReservation(r2); assertThat("reservation 1 resturns wrong total price", reservationManager.getCoastForReservation(r1.getGuestId()), is(equalTo(5 * 125))); assertThat("reservation 2 returns wrong total price", reservationManager.getCoastForReservation(r2.getGuestId()), is(equalTo(6 * 250))); assertThat("with no reservation price differs from 0", reservationManager.getCoastForReservation(3L), is(equalTo(0))); } @Test public void TestAssignRoomToGuest() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Room room1 = setRoom(101, 2, 125, true); roomManager.createRoom(room1); Guest guest1 = setGuest("Oleg Bezpalko", "Zagorska", "75899654", "Oleg999@mail"); guestManager.createGuest(guest1); reservationManager.assignRoomToGuest(guest1, room1, checkin, checkout); List<Reservation> reservation = reservationManager.findAllReservations(); assertThat("wrong size or reservations created", reservation.size(), is(equalTo(1))); assertThat("room id was changed", reservation.get(0).getRoomId(), is(equalTo(room1.getId()))); assertThat("guest id was changed", reservation.get(0).getGuestId(), is(equalTo(guest1.getId()))); assertThat("resrvation date was changed", reservation.get(0).getReservationDate(), is(equalTo(reservationdate))); assertThat("checkin date was changed", reservation.get(0).getCheckinDate(), is(equalTo(checkin))); assertThat("checkout date was changed", reservation.get(0).getCheckoutDate(), is(equalTo(checkout))); } @Test public void TestFindFreeRoomsforReservation() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Room room1 = setRoom(101, 2, 125, true); Room room2 = setRoom(102, 3, 255, false); Room room3 = setRoom(103, 2, 129, true); Room room4 = setRoom(104, 3, 254, false); Room room5 = setRoom(105, 2, 1278, true); Room room6 = setRoom(106, 3, 252, false); roomManager.createRoom(room1); roomManager.createRoom(room2); roomManager.createRoom(room3); roomManager.createRoom(room4); roomManager.createRoom(room5); roomManager.createRoom(room6); Guest guest1 = setGuest("Oleg Bezpalko", "Zagorska", "75899654", "Oleg999@mail"); Guest guest2 = setGuest("Bogdan Voedylo", "Sobranetska", "78965452", "Bogadan777@mail"); Guest guest3 = setGuest("Vasya Voedylo", "Mynaiska", "789999992", "Vasya777@mail"); guestManager.createGuest(guest3); guestManager.createGuest(guest1); guestManager.createGuest(guest2); reservationManager.assignRoomToGuest(guest1, room4, checkin, checkout); reservationManager.assignRoomToGuest(guest2, room6, checkin.plusDays(3), checkout.plusDays(5)); reservationManager.assignRoomToGuest(guest3, room1, checkin.minusDays(5), checkout.minusDays(3)); List<Room> freerooms1 = reservationManager.findFreeRoomsforReservation(checkin, checkout); assertThat("wrong size or rooms created", freerooms1.size(), is(equalTo(3))); List<Room> freerooms2 = reservationManager.findFreeRoomsforReservation(checkin.plusDays(2), checkout.minusDays(2)); assertThat("wrong size or rooms created", freerooms2.size(), is(equalTo(5))); List<Room> freerooms3 = reservationManager.findFreeRoomsforReservation(checkin, checkin); assertThat("wrong size or rooms created", freerooms3.size(), is(equalTo(4))); } @Test public void TestFindRoomWithGuest() { LocalDate checkin = LocalDate.of(2016, 8, 7); LocalDate checkout = LocalDate.of(2016, 8, 11); LocalDate reservationdate = LocalDate.now(prepareClockMock(now)); Room room1 = setRoom(101, 2, 125, true); Room room2 = setRoom(102, 3, 250, false); roomManager.createRoom(room1); roomManager.createRoom(room2); Guest guest1 = setGuest("Oleg Bezpalko", "Zagorska", "75899654", "Oleg999@mail"); Guest guest2 = setGuest("Bogdan Voedylo", "Sobranetska", "78965452", "Myro777@mail"); guestManager.createGuest(guest1); guestManager.createGuest(guest2); reservationManager.assignRoomToGuest(guest1, room2, checkin, checkout); reservationManager.assignRoomToGuest(guest2, room1, checkin, checkout); List<Room> roomf1 = reservationManager.findRoomWithGuest(guest2); assertThat("wrong size or rooms found", roomf1.size(), is(equalTo(1))); assertDeepEquals(roomf1.get(0), room1); List<Room> roomf2 = reservationManager.findRoomWithGuest(guest1); assertThat("wrong size or rooms found", roomf2.size(), is(equalTo(1))); assertDeepEquals(roomf2.get(0), room2); Room room3 = setRoom(103, 3, 250, false); roomManager.createRoom(room3); reservationManager.assignRoomToGuest(guest1, room3, checkin, checkout); List<Room> roomf3 = reservationManager.findRoomWithGuest(guest1); assertThat("wrong size or rooms found", roomf3.size(), is(equalTo(2))); } private void assertDeepEquals(Room expected, Room actual) { assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getRoomNumber(), actual.getRoomNumber()); assertEquals(expected.getCapacity(), actual.getCapacity()); assertEquals(expected.getCostPerNight(), actual.getCostPerNight()); assertEquals(expected.getDoubleBed(), actual.getDoubleBed()); } private void assertDeepEquals(List<Reservation> expectedList, List<Reservation> actualList) { for (int i = 0; i < expectedList.size(); i++) { Reservation expected = expectedList.get(i); Reservation actual = actualList.get(i); assertDeepEquals(expected, actual); } } private void assertDeepEquals(Reservation expected, Reservation actual) { assertEquals(expected.getReservationId(), actual.getReservationId()); assertEquals(expected.getCheckinDate(), actual.getCheckinDate()); assertEquals(expected.getCheckoutDate(), actual.getCheckoutDate()); assertEquals(expected.getGuestId(), actual.getGuestId()); assertEquals(expected.getRoomId(), actual.getRoomId()); assertEquals(expected.getReservationDate(), actual.getReservationDate()); } private static Comparator<Reservation> idComparator = new Comparator<Reservation>() { @Override public int compare(Reservation o1, Reservation o2) { return o1.getReservationId().compareTo(o2.getReservationId()); } }; }