#1321 Added Tests
This commit is contained in:
parent
e0fb2d014a
commit
75ab3b5245
@ -16,13 +16,45 @@
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.200</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.ashishtrivedi16.transactionscript.TransactionScriptApp</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
@ -1,43 +1,43 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.ashishtrivedi16.transactionscript.db;
|
||||
|
||||
/**
|
||||
* Custom exception.
|
||||
*/
|
||||
public class CustomException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomException() {
|
||||
}
|
||||
|
||||
public CustomException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CustomException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
/**
|
||||
* Custom exception.
|
||||
*/
|
||||
public class CustomException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomException() {
|
||||
}
|
||||
|
||||
public CustomException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CustomException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.db.HotelDaoImpl;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -25,10 +24,10 @@ public class Hotel {
|
||||
Optional<Room> room = hotelDao.getById(roomNumber);
|
||||
|
||||
if (room.isEmpty()) {
|
||||
LOGGER.info(roomNumber + " does not exist");
|
||||
throw new Exception("Room number: " + roomNumber + " does not exist");
|
||||
} else {
|
||||
if (room.get().isBooked()) {
|
||||
LOGGER.info("Room already booked!");
|
||||
throw new Exception("Room already booked!");
|
||||
} else {
|
||||
Room updateRoomBooking = room.get();
|
||||
updateRoomBooking.setBooked(true);
|
||||
@ -48,7 +47,7 @@ public class Hotel {
|
||||
Optional<Room> room = hotelDao.getById(roomNumber);
|
||||
|
||||
if (room.isEmpty()) {
|
||||
LOGGER.info("Room number: " + roomNumber + " does not exist");
|
||||
throw new Exception("Room number: " + roomNumber + " does not exist");
|
||||
} else {
|
||||
if (room.get().isBooked()) {
|
||||
Room updateRoomBooking = room.get();
|
||||
@ -59,7 +58,7 @@ public class Hotel {
|
||||
LOGGER.info("Booking cancelled for room number: " + roomNumber);
|
||||
LOGGER.info(refundAmount + " is refunded");
|
||||
} else {
|
||||
LOGGER.info("No booking for the room exists");
|
||||
throw new Exception("No booking for the room exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
package com.ashishtrivedi16.transactionscript.db;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.Room;
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
@ -1,6 +1,5 @@
|
||||
package com.ashishtrivedi16.transactionscript.db;
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.Room;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -116,7 +115,7 @@ public class HotelDaoImpl implements HotelDao {
|
||||
@Override
|
||||
public Boolean delete(Room room) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("DELETE FROM ROOMS WHERE ID = ?")) {
|
||||
statement.setInt(1, room.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (SQLException ex) {
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.ashishtrivedi16.transactionscript.db;
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
/**
|
||||
* Customer Schema SQL Class.
|
@ -1,8 +1,5 @@
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.db.CustomException;
|
||||
import com.ashishtrivedi16.transactionscript.db.HotelDaoImpl;
|
||||
import com.ashishtrivedi16.transactionscript.db.RoomSchemaSql;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
@ -15,7 +12,6 @@ public class TransactionScriptApp {
|
||||
|
||||
private static final String H2_DB_URL = "jdbc:h2:~/test";
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionScriptApp.class);
|
||||
private static final String ALL_ROOMS = "customerDao.getAllRooms(): ";
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests that Transaction script example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
TransactionScriptApp.main(new String[]{});
|
||||
}
|
||||
}
|
@ -0,0 +1,272 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests {@link HotelDaoImpl}.
|
||||
*/
|
||||
public class HotelDaoImplTest {
|
||||
|
||||
private static final String DB_URL = "jdbc:h2:~/test";
|
||||
private HotelDaoImpl dao;
|
||||
private Room existingRoom = new Room(1, "Single", 50, false);
|
||||
|
||||
/**
|
||||
* Creates rooms schema.
|
||||
*
|
||||
* @throws SQLException if there is any error while creating schema.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void createSchema() throws SQLException {
|
||||
try (var connection = DriverManager.getConnection(DB_URL);
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the scenario where DB connectivity is present.
|
||||
*/
|
||||
@Nested
|
||||
public class ConnectionSuccess {
|
||||
|
||||
/**
|
||||
* Setup for connection success scenario.
|
||||
*
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
var dataSource = new JdbcDataSource();
|
||||
dataSource.setURL(DB_URL);
|
||||
dao = new HotelDaoImpl(dataSource);
|
||||
var result = dao.add(existingRoom);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the scenario when DAO operations are being performed on a non existing room.
|
||||
*/
|
||||
@Nested
|
||||
public class NonExistingRoom {
|
||||
|
||||
@Test
|
||||
public void addingShouldResultInSuccess() throws Exception {
|
||||
try (var allRooms = dao.getAll()) {
|
||||
assumeTrue(allRooms.count() == 1);
|
||||
}
|
||||
|
||||
final var nonExistingRoom = new Room(2, "Double", 80, false);
|
||||
var result = dao.add(nonExistingRoom);
|
||||
assertTrue(result);
|
||||
|
||||
assertRoomCountIs(2);
|
||||
assertEquals(nonExistingRoom, dao.getById(nonExistingRoom.getId()).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletionShouldBeFailureAndNotAffectExistingRooms() throws Exception {
|
||||
final var nonExistingRoom = new Room(2, "Double", 80, false);
|
||||
var result = dao.delete(nonExistingRoom);
|
||||
|
||||
assertFalse(result);
|
||||
assertRoomCountIs(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updationShouldBeFailureAndNotAffectExistingRooms() throws Exception {
|
||||
final var nonExistingId = getNonExistingRoomId();
|
||||
final var newRoomType = "Double";
|
||||
final var newPrice = 80;
|
||||
final var room = new Room(nonExistingId, newRoomType, newPrice, false);
|
||||
var result = dao.update(room);
|
||||
|
||||
assertFalse(result);
|
||||
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveShouldReturnNoRoom() throws Exception {
|
||||
assertFalse(dao.getById(getNonExistingRoomId()).isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a scenario where DAO operations are being performed on an already existing
|
||||
* room.
|
||||
*/
|
||||
@Nested
|
||||
public class ExistingRoom {
|
||||
|
||||
@Test
|
||||
public void addingShouldResultInFailureAndNotAffectExistingRooms() throws Exception {
|
||||
var existingRoom = new Room(1, "Single", 50, false);
|
||||
var result = dao.add(existingRoom);
|
||||
|
||||
assertFalse(result);
|
||||
assertRoomCountIs(1);
|
||||
assertEquals(existingRoom, dao.getById(existingRoom.getId()).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletionShouldBeSuccessAndRoomShouldBeNonAccessible() throws Exception {
|
||||
var result = dao.delete(existingRoom);
|
||||
|
||||
assertTrue(result);
|
||||
assertRoomCountIs(0);
|
||||
assertFalse(dao.getById(existingRoom.getId()).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updationShouldBeSuccessAndAccessingTheSameRoomShouldReturnUpdatedInformation() throws
|
||||
Exception {
|
||||
final var newRoomType = "Double";
|
||||
final var newPrice = 80;
|
||||
final var newBookingStatus = false;
|
||||
final var Room = new Room(existingRoom.getId(), newRoomType, newPrice, newBookingStatus);
|
||||
var result = dao.update(Room);
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
final var room = dao.getById(existingRoom.getId()).get();
|
||||
assertEquals(newRoomType, room.getRoomType());
|
||||
assertEquals(newPrice, room.getPrice());
|
||||
assertEquals(newBookingStatus, room.isBooked());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a scenario where DB connectivity is not present due to network issue, or DB service
|
||||
* unavailable.
|
||||
*/
|
||||
@Nested
|
||||
public class ConnectivityIssue {
|
||||
|
||||
private static final String EXCEPTION_CAUSE = "Connection not available";
|
||||
|
||||
/**
|
||||
* setup a connection failure scenario.
|
||||
*
|
||||
* @throws SQLException if any error occurs.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() throws SQLException {
|
||||
dao = new HotelDaoImpl(mockedDatasource());
|
||||
}
|
||||
|
||||
private DataSource mockedDatasource() throws SQLException {
|
||||
var mockedDataSource = mock(DataSource.class);
|
||||
var mockedConnection = mock(Connection.class);
|
||||
var exception = new SQLException(EXCEPTION_CAUSE);
|
||||
doThrow(exception).when(mockedConnection).prepareStatement(Mockito.anyString());
|
||||
doReturn(mockedConnection).when(mockedDataSource).getConnection();
|
||||
return mockedDataSource;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addingARoomFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.add(new Room(2, "Double", 80, false));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletingARoomFailsWithExceptionAsFeedbackToTheClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.delete(existingRoom);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatingARoomFailsWithFeedbackToTheClient() {
|
||||
final var newRoomType = "Double";
|
||||
final var newPrice = 80;
|
||||
final var newBookingStatus = false;
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.update(new Room(existingRoom.getId(), newRoomType, newPrice, newBookingStatus));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievingARoomByIdFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.getById(existingRoom.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievingAllRoomsFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.getAll();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete room schema for fresh setup per test.
|
||||
*
|
||||
* @throws SQLException if any error occurs.
|
||||
*/
|
||||
@AfterEach
|
||||
public void deleteSchema() throws SQLException {
|
||||
try (var connection = DriverManager.getConnection(DB_URL);
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertRoomCountIs(int count) throws Exception {
|
||||
try (var allRooms = dao.getAll()) {
|
||||
assertEquals(count, allRooms.count());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An arbitrary number which does not correspond to an active Room id.
|
||||
*
|
||||
* @return an int of a room id which doesn't exist
|
||||
*/
|
||||
private int getNonExistingRoomId() {
|
||||
return 999;
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import org.h2.jdbc.JdbcSQLException;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests {@link Hotel}
|
||||
*/
|
||||
public class HotelTest {
|
||||
|
||||
private static final String H2_DB_URL = "jdbc:h2:~/test";
|
||||
|
||||
private Hotel hotel;
|
||||
private HotelDaoImpl dao;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
final var dataSource = createDataSource();
|
||||
deleteSchema(dataSource);
|
||||
createSchema(dataSource);
|
||||
dao = new HotelDaoImpl(dataSource);
|
||||
addRooms(dao);
|
||||
hotel = new Hotel(dao);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bookingRoomShouldChangeBookedStatusToTrue() throws Exception {
|
||||
hotel.bookRoom(1);
|
||||
assertTrue(dao.getById(1).get().isBooked());
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void bookingRoomWithInvalidIdShouldRaiseException() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
hotel.bookRoom(999);
|
||||
});
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void bookingRoomAgainShouldRaiseException() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
hotel.bookRoom(1);
|
||||
hotel.bookRoom(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void NotBookingRoomShouldNotChangeBookedStatus() throws Exception {
|
||||
assertFalse(dao.getById(1).get().isBooked());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancelRoomBookingShouldChangeBookedStatus() throws Exception {
|
||||
hotel.bookRoom(1);
|
||||
assertTrue(dao.getById(1).get().isBooked());
|
||||
hotel.cancelRoomBooking(1);
|
||||
assertFalse(dao.getById(1).get().isBooked());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancelRoomBookingWithInvalidIdShouldRaiseException() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
hotel.cancelRoomBooking(999);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancelRoomBookingForUnbookedRoomShouldRaiseException() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
hotel.cancelRoomBooking(1);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws Exception {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL);
|
||||
} catch (JdbcSQLException e) {
|
||||
throw new CustomException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static DataSource createDataSource() {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
dataSource.setUrl(H2_DB_URL);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private static void addRooms(HotelDaoImpl hotelDao) throws Exception {
|
||||
for (var room : generateSampleRooms()) {
|
||||
hotelDao.add(room);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Room> generateSampleRooms() {
|
||||
final var room1 = new Room(1, "Single", 50, false);
|
||||
final var room2 = new Room(2, "Double", 80, false);
|
||||
final var room3 = new Room(3, "Queen", 120, false);
|
||||
final var room4 = new Room(4, "King", 150, false);
|
||||
final var room5 = new Room(5, "Single", 50, false);
|
||||
final var room6 = new Room(6, "Double", 80, false);
|
||||
return List.of(room1, room2, room3, room4, room5, room6);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* Tests {@link Room}.
|
||||
*/
|
||||
public class RoomTest {
|
||||
|
||||
private Room room;
|
||||
private static final int ID = 1;
|
||||
private static final String ROOMTYPE = "Single";
|
||||
private static final int PRICE = 50;
|
||||
private static final boolean BOOKED = false;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
room = new Room(ID, ROOMTYPE, PRICE, BOOKED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndSetId() {
|
||||
final var newId = 2;
|
||||
room.setId(newId);
|
||||
assertEquals(newId, room.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndSetRoomType() {
|
||||
final var newRoomType = "Double";
|
||||
room.setRoomType(newRoomType);
|
||||
assertEquals(newRoomType, room.getRoomType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndSetLastName() {
|
||||
final var newPrice = 60;
|
||||
room.setPrice(newPrice);
|
||||
assertEquals(newPrice, room.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEqualWithDifferentId() {
|
||||
final var newId = 2;
|
||||
final var otherRoom = new Room(newId, ROOMTYPE, PRICE, BOOKED);
|
||||
assertNotEquals(room, otherRoom);
|
||||
assertNotEquals(room.hashCode(), otherRoom.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWithSameObjectValues() {
|
||||
final var otherRoom = new Room(ID, ROOMTYPE, PRICE, BOOKED);
|
||||
assertEquals(room, otherRoom);
|
||||
assertEquals(room.hashCode(), otherRoom.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWithSameObjects() {
|
||||
assertEquals(room, room);
|
||||
assertEquals(room.hashCode(), room.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
assertEquals(String.format("Room{id=%s, roomType=%s, price=%s, booked=%s}",
|
||||
room.getId(), room.getRoomType(), room.getPrice(), room.isBooked()), room.toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user