📍Use lombok, reformat, and optimize the code (#1560)

* Use lombok, reformat, and optimize the code

* Fix merge conflicts and some sonar issues

Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
va1m
2021-03-13 13:19:21 +01:00
committed by GitHub
parent 0e26a6adb5
commit 5cf2fe009b
681 changed files with 2472 additions and 4966 deletions

View File

@ -29,8 +29,8 @@ Programmatic example
The `Hotel` class takes care of booking and cancelling room reservations.
```java
@Slf4j
public class Hotel {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private final HotelDaoImpl hotelDao;

View File

@ -67,7 +67,7 @@ public class App {
// Print room booking status
getRoomStatus(dao);
var hotel = new Hotel(dao);
// Book rooms

View File

@ -23,11 +23,10 @@
package com.iluwatar.transactionscript;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Hotel {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private final HotelDaoImpl hotelDao;
@ -65,7 +64,7 @@ public class Hotel {
* @throws Exception if any error
*/
public void cancelRoomBooking(int roomNumber) throws Exception {
var room = hotelDao.getById(roomNumber);
if (room.isEmpty()) {

View File

@ -33,11 +33,10 @@ import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HotelDaoImpl implements HotelDao {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private final DataSource dataSource;

View File

@ -23,9 +23,20 @@
package com.iluwatar.transactionscript;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* A room POJO that represents the data that will be read from the data source.
*/
@Setter
@Getter
@ToString
@EqualsAndHashCode
@AllArgsConstructor
public class Room {
private int id;
@ -33,91 +44,4 @@ public class Room {
private int price;
private boolean booked;
/**
* Create an instance of room.
* @param id room id
* @param roomType room type
* @param price room price
* @param booked room booking status
*/
public Room(int id, String roomType, int price, boolean booked) {
this.id = id;
this.roomType = roomType;
this.price = price;
this.booked = booked;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isBooked() {
return booked;
}
public void setBooked(boolean booked) {
this.booked = booked;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Room room = (Room) o;
if (id != room.id) {
return false;
}
if (price != room.price) {
return false;
}
if (booked != room.booked) {
return false;
}
return roomType.equals(room.roomType);
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + roomType.hashCode();
result = 31 * result + price;
result = 31 * result + (booked ? 1 : 0);
return result;
}
@Override
public String toString() {
return "Room{"
+ "id=" + id
+ ", roomType=" + roomType
+ ", price=" + price
+ ", booked=" + booked
+ '}';
}
}

View File

@ -23,6 +23,18 @@
package com.iluwatar.transactionscript;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
@ -31,15 +43,6 @@ 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}.
*/
@ -90,7 +93,7 @@ public class HotelDaoImplTest {
public class NonExistingRoom {
@Test
public void addingShouldResultInSuccess() throws Exception {
void addingShouldResultInSuccess() throws Exception {
try (var allRooms = dao.getAll()) {
assumeTrue(allRooms.count() == 1);
}
@ -104,7 +107,7 @@ public class HotelDaoImplTest {
}
@Test
public void deletionShouldBeFailureAndNotAffectExistingRooms() throws Exception {
void deletionShouldBeFailureAndNotAffectExistingRooms() throws Exception {
final var nonExistingRoom = new Room(2, "Double", 80, false);
var result = dao.delete(nonExistingRoom);
@ -113,7 +116,7 @@ public class HotelDaoImplTest {
}
@Test
public void updationShouldBeFailureAndNotAffectExistingRooms() throws Exception {
void updationShouldBeFailureAndNotAffectExistingRooms() throws Exception {
final var nonExistingId = getNonExistingRoomId();
final var newRoomType = "Double";
final var newPrice = 80;
@ -125,7 +128,7 @@ public class HotelDaoImplTest {
}
@Test
public void retrieveShouldReturnNoRoom() throws Exception {
void retrieveShouldReturnNoRoom() throws Exception {
assertFalse(dao.getById(getNonExistingRoomId()).isPresent());
}
}
@ -138,7 +141,7 @@ public class HotelDaoImplTest {
public class ExistingRoom {
@Test
public void addingShouldResultInFailureAndNotAffectExistingRooms() throws Exception {
void addingShouldResultInFailureAndNotAffectExistingRooms() throws Exception {
var existingRoom = new Room(1, "Single", 50, false);
var result = dao.add(existingRoom);
@ -148,7 +151,7 @@ public class HotelDaoImplTest {
}
@Test
public void deletionShouldBeSuccessAndRoomShouldBeNonAccessible() throws Exception {
void deletionShouldBeSuccessAndRoomShouldBeNonAccessible() throws Exception {
var result = dao.delete(existingRoom);
Assertions.assertTrue(result);
@ -157,7 +160,7 @@ public class HotelDaoImplTest {
}
@Test
public void updationShouldBeSuccessAndAccessingTheSameRoomShouldReturnUpdatedInformation() throws
void updationShouldBeSuccessAndAccessingTheSameRoomShouldReturnUpdatedInformation() throws
Exception {
final var newRoomType = "Double";
final var newPrice = 80;
@ -204,21 +207,21 @@ public class HotelDaoImplTest {
}
@Test
public void addingARoomFailsWithExceptionAsFeedbackToClient() {
void addingARoomFailsWithExceptionAsFeedbackToClient() {
assertThrows(Exception.class, () -> {
dao.add(new Room(2, "Double", 80, false));
});
}
@Test
public void deletingARoomFailsWithExceptionAsFeedbackToTheClient() {
void deletingARoomFailsWithExceptionAsFeedbackToTheClient() {
assertThrows(Exception.class, () -> {
dao.delete(existingRoom);
});
}
@Test
public void updatingARoomFailsWithFeedbackToTheClient() {
void updatingARoomFailsWithFeedbackToTheClient() {
final var newRoomType = "Double";
final var newPrice = 80;
final var newBookingStatus = false;
@ -228,14 +231,14 @@ public class HotelDaoImplTest {
}
@Test
public void retrievingARoomByIdFailsWithExceptionAsFeedbackToClient() {
void retrievingARoomByIdFailsWithExceptionAsFeedbackToClient() {
assertThrows(Exception.class, () -> {
dao.getById(existingRoom.getId());
});
}
@Test
public void retrievingAllRoomsFailsWithExceptionAsFeedbackToClient() {
void retrievingAllRoomsFailsWithExceptionAsFeedbackToClient() {
assertThrows(Exception.class, () -> {
dao.getAll();
});

View File

@ -23,19 +23,20 @@
package com.iluwatar.transactionscript;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests {@link Hotel}
*/
public class HotelTest {
class HotelTest {
private static final String H2_DB_URL = "jdbc:h2:~/test";
@ -54,20 +55,20 @@ public class HotelTest {
}
@Test
public void bookingRoomShouldChangeBookedStatusToTrue() throws Exception {
void bookingRoomShouldChangeBookedStatusToTrue() throws Exception {
hotel.bookRoom(1);
assertTrue(dao.getById(1).get().isBooked());
}
@Test()
public void bookingRoomWithInvalidIdShouldRaiseException() {
void bookingRoomWithInvalidIdShouldRaiseException() {
assertThrows(Exception.class, () -> {
hotel.bookRoom(getNonExistingRoomId());
});
}
@Test()
public void bookingRoomAgainShouldRaiseException() {
void bookingRoomAgainShouldRaiseException() {
assertThrows(Exception.class, () -> {
hotel.bookRoom(1);
hotel.bookRoom(1);
@ -75,12 +76,12 @@ public class HotelTest {
}
@Test
public void NotBookingRoomShouldNotChangeBookedStatus() throws Exception {
void NotBookingRoomShouldNotChangeBookedStatus() throws Exception {
assertFalse(dao.getById(1).get().isBooked());
}
@Test
public void cancelRoomBookingShouldChangeBookedStatus() throws Exception {
void cancelRoomBookingShouldChangeBookedStatus() throws Exception {
hotel.bookRoom(1);
assertTrue(dao.getById(1).get().isBooked());
hotel.cancelRoomBooking(1);
@ -88,14 +89,14 @@ public class HotelTest {
}
@Test
public void cancelRoomBookingWithInvalidIdShouldRaiseException() {
void cancelRoomBookingWithInvalidIdShouldRaiseException() {
assertThrows(Exception.class, () -> {
hotel.cancelRoomBooking(getNonExistingRoomId());
});
}
@Test
public void cancelRoomBookingForUnbookedRoomShouldRaiseException() {
void cancelRoomBookingForUnbookedRoomShouldRaiseException() {
assertThrows(Exception.class, () -> {
hotel.cancelRoomBooking(1);
});

View File

@ -23,16 +23,16 @@
package com.iluwatar.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;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Tests {@link Room}.
*/
public class RoomTest {
class RoomTest {
private Room room;
private static final int ID = 1;
@ -41,33 +41,33 @@ public class RoomTest {
private static final boolean BOOKED = false;
@BeforeEach
public void setUp() {
void setUp() {
room = new Room(ID, ROOMTYPE, PRICE, BOOKED);
}
@Test
public void getAndSetId() {
void getAndSetId() {
final var newId = 2;
room.setId(newId);
assertEquals(newId, room.getId());
}
@Test
public void getAndSetRoomType() {
void getAndSetRoomType() {
final var newRoomType = "Double";
room.setRoomType(newRoomType);
assertEquals(newRoomType, room.getRoomType());
}
@Test
public void getAndSetLastName() {
void getAndSetLastName() {
final var newPrice = 60;
room.setPrice(newPrice);
assertEquals(newPrice, room.getPrice());
}
@Test
public void notEqualWithDifferentId() {
void notEqualWithDifferentId() {
final var newId = 2;
final var otherRoom = new Room(newId, ROOMTYPE, PRICE, BOOKED);
assertNotEquals(room, otherRoom);
@ -75,21 +75,21 @@ public class RoomTest {
}
@Test
public void equalsWithSameObjectValues() {
void equalsWithSameObjectValues() {
final var otherRoom = new Room(ID, ROOMTYPE, PRICE, BOOKED);
assertEquals(room, otherRoom);
assertEquals(room.hashCode(), otherRoom.hashCode());
}
@Test
public void equalsWithSameObjects() {
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}",
void testToString() {
assertEquals(String.format("Room(id=%s, roomType=%s, price=%s, booked=%s)",
room.getId(), room.getRoomType(), room.getPrice(), room.isBooked()), room.toString());
}
}