📍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

@ -26,9 +26,8 @@ package com.iluwatar.dao;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.h2.jdbcx.JdbcDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Data Access Object (DAO) is an object that provides an abstract interface to some type of
@ -42,9 +41,9 @@ import org.slf4j.LoggerFactory;
* without directly interacting with the data source. The below example demonstrates basic CRUD
* operations: select, add, update, and delete.
*/
@Slf4j
public class App {
private static final String DB_URL = "jdbc:h2:~/dao";
private static final Logger log = LoggerFactory.getLogger(App.class);
private static final String ALL_CUSTOMERS = "customerDao.getAllCustomers(): ";
/**
@ -86,23 +85,23 @@ public class App {
private static void performOperationsUsing(final CustomerDao customerDao) throws Exception {
addCustomers(customerDao);
log.info(ALL_CUSTOMERS);
LOGGER.info(ALL_CUSTOMERS);
try (var customerStream = customerDao.getAll()) {
customerStream.forEach((customer) -> log.info(customer.toString()));
customerStream.forEach(customer -> LOGGER.info(customer.toString()));
}
log.info("customerDao.getCustomerById(2): " + customerDao.getById(2));
LOGGER.info("customerDao.getCustomerById(2): " + customerDao.getById(2));
final var customer = new Customer(4, "Dan", "Danson");
customerDao.add(customer);
log.info(ALL_CUSTOMERS + customerDao.getAll());
LOGGER.info(ALL_CUSTOMERS + customerDao.getAll());
customer.setFirstName("Daniel");
customer.setLastName("Danielson");
customerDao.update(customer);
log.info(ALL_CUSTOMERS);
LOGGER.info(ALL_CUSTOMERS);
try (var customerStream = customerDao.getAll()) {
customerStream.forEach((cust) -> log.info(cust.toString()));
customerStream.forEach(cust -> LOGGER.info(cust.toString()));
}
customerDao.delete(customer);
log.info(ALL_CUSTOMERS + customerDao.getAll());
LOGGER.info(ALL_CUSTOMERS + customerDao.getAll());
}
private static void addCustomers(CustomerDao customerDao) throws Exception {

View File

@ -23,70 +23,25 @@
package com.iluwatar.dao;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* A customer POJO that represents the data that will be read from the data source.
*/
@Setter
@Getter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
public class Customer {
@EqualsAndHashCode.Include
private int id;
private String firstName;
private String lastName;
/**
* Creates an instance of customer.
*/
public Customer(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
+ getLastName() + '\'' + '}';
}
@Override
public boolean equals(final Object that) {
var isEqual = false;
if (this == that) {
isEqual = true;
} else if (that != null && getClass() == that.getClass()) {
final var customer = (Customer) that;
if (getId() == customer.getId()) {
isEqual = true;
}
}
return isEqual;
}
@Override
public int hashCode() {
return getId();
}
}

View File

@ -34,28 +34,18 @@ 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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
*/
@Slf4j
@RequiredArgsConstructor
public class DbCustomerDao implements CustomerDao {
private static final Logger LOGGER = LoggerFactory.getLogger(DbCustomerDao.class);
private final DataSource dataSource;
/**
* Creates an instance of {@link DbCustomerDao} which uses provided <code>dataSource</code> to
* store and retrieve customer information.
*
* @param dataSource a non-null dataSource.
*/
public DbCustomerDao(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* Get all customers as Java Stream.
*

View File

@ -32,7 +32,7 @@ import org.junit.jupiter.api.Test;
/**
* Tests {@link Customer}.
*/
public class CustomerTest {
class CustomerTest {
private Customer customer;
private static final int ID = 1;
@ -40,33 +40,33 @@ public class CustomerTest {
private static final String LASTNAME = "Churchill";
@BeforeEach
public void setUp() {
void setUp() {
customer = new Customer(ID, FIRSTNAME, LASTNAME);
}
@Test
public void getAndSetId() {
void getAndSetId() {
final var newId = 2;
customer.setId(newId);
assertEquals(newId, customer.getId());
}
@Test
public void getAndSetFirstname() {
void getAndSetFirstname() {
final var newFirstname = "Bill";
customer.setFirstName(newFirstname);
assertEquals(newFirstname, customer.getFirstName());
}
@Test
public void getAndSetLastname() {
void getAndSetLastname() {
final var newLastname = "Clinton";
customer.setLastName(newLastname);
assertEquals(newLastname, customer.getLastName());
}
@Test
public void notEqualWithDifferentId() {
void notEqualWithDifferentId() {
final var newId = 2;
final var otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME);
assertNotEquals(customer, otherCustomer);
@ -74,21 +74,21 @@ public class CustomerTest {
}
@Test
public void equalsWithSameObjectValues() {
void equalsWithSameObjectValues() {
final var otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME);
assertEquals(customer, otherCustomer);
assertEquals(customer.hashCode(), otherCustomer.hashCode());
}
@Test
public void equalsWithSameObjects() {
void equalsWithSameObjects() {
assertEquals(customer, customer);
assertEquals(customer.hashCode(), customer.hashCode());
}
@Test
public void testToString() {
assertEquals(String.format("Customer{id=%s, firstName='%s', lastName='%s'}",
void testToString() {
assertEquals(String.format("Customer(id=%s, firstName=%s, lastName=%s)",
customer.getId(), customer.getFirstName(), customer.getLastName()), customer.toString());
}
}

View File

@ -46,7 +46,7 @@ import org.mockito.Mockito;
/**
* Tests {@link DbCustomerDao}.
*/
public class DbCustomerDaoTest {
class DbCustomerDaoTest {
private static final String DB_URL = "jdbc:h2:~/dao";
private DbCustomerDao dao;
@ -58,7 +58,7 @@ public class DbCustomerDaoTest {
* @throws SQLException if there is any error while creating schema.
*/
@BeforeEach
public void createSchema() throws SQLException {
void createSchema() throws SQLException {
try (var connection = DriverManager.getConnection(DB_URL);
var statement = connection.createStatement()) {
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
@ -89,10 +89,10 @@ public class DbCustomerDaoTest {
* Represents the scenario when DAO operations are being performed on a non existing customer.
*/
@Nested
public class NonExistingCustomer {
class NonExistingCustomer {
@Test
public void addingShouldResultInSuccess() throws Exception {
void addingShouldResultInSuccess() throws Exception {
try (var allCustomers = dao.getAll()) {
assumeTrue(allCustomers.count() == 1);
}
@ -106,7 +106,7 @@ public class DbCustomerDaoTest {
}
@Test
public void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
final var nonExistingCustomer = new Customer(2, "Robert", "Englund");
var result = dao.delete(nonExistingCustomer);
@ -115,7 +115,7 @@ public class DbCustomerDaoTest {
}
@Test
public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
final var nonExistingId = getNonExistingCustomerId();
final var newFirstname = "Douglas";
final var newLastname = "MacArthur";
@ -127,7 +127,7 @@ public class DbCustomerDaoTest {
}
@Test
public void retrieveShouldReturnNoCustomer() throws Exception {
void retrieveShouldReturnNoCustomer() throws Exception {
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
}
}
@ -137,10 +137,10 @@ public class DbCustomerDaoTest {
* customer.
*/
@Nested
public class ExistingCustomer {
class ExistingCustomer {
@Test
public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
var existingCustomer = new Customer(1, "Freddy", "Krueger");
var result = dao.add(existingCustomer);
@ -150,7 +150,7 @@ public class DbCustomerDaoTest {
}
@Test
public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
var result = dao.delete(existingCustomer);
assertTrue(result);
@ -159,7 +159,7 @@ public class DbCustomerDaoTest {
}
@Test
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
Exception {
final var newFirstname = "Bernard";
final var newLastname = "Montgomery";
@ -180,7 +180,7 @@ public class DbCustomerDaoTest {
* unavailable.
*/
@Nested
public class ConnectivityIssue {
class ConnectivityIssue {
private static final String EXCEPTION_CAUSE = "Connection not available";
@ -204,21 +204,21 @@ public class DbCustomerDaoTest {
}
@Test
public void addingACustomerFailsWithExceptionAsFeedbackToClient() {
void addingACustomerFailsWithExceptionAsFeedbackToClient() {
assertThrows(Exception.class, () -> {
dao.add(new Customer(2, "Bernard", "Montgomery"));
});
}
@Test
public void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() {
void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() {
assertThrows(Exception.class, () -> {
dao.delete(existingCustomer);
});
}
@Test
public void updatingACustomerFailsWithFeedbackToTheClient() {
void updatingACustomerFailsWithFeedbackToTheClient() {
final var newFirstname = "Bernard";
final var newLastname = "Montgomery";
assertThrows(Exception.class, () -> {
@ -227,14 +227,14 @@ public class DbCustomerDaoTest {
}
@Test
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() {
void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() {
assertThrows(Exception.class, () -> {
dao.getById(existingCustomer.getId());
});
}
@Test
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() {
void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() {
assertThrows(Exception.class, () -> {
dao.getAll();
});
@ -248,7 +248,7 @@ public class DbCustomerDaoTest {
* @throws SQLException if any error occurs.
*/
@AfterEach
public void deleteSchema() throws SQLException {
void deleteSchema() throws SQLException {
try (var connection = DriverManager.getConnection(DB_URL);
var statement = connection.createStatement()) {
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
@ -261,7 +261,6 @@ public class DbCustomerDaoTest {
}
}
/**
* An arbitrary number which does not correspond to an active Customer id.
*

View File

@ -35,13 +35,13 @@ import org.junit.jupiter.api.Test;
/**
* Tests {@link InMemoryCustomerDao}.
*/
public class InMemoryCustomerDaoTest {
class InMemoryCustomerDaoTest {
private InMemoryCustomerDao dao;
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
@BeforeEach
public void setUp() {
void setUp() {
dao = new InMemoryCustomerDao();
assertTrue(dao.add(CUSTOMER));
}
@ -51,10 +51,10 @@ public class InMemoryCustomerDaoTest {
* customer.
*/
@Nested
public class NonExistingCustomer {
class NonExistingCustomer {
@Test
public void addingShouldResultInSuccess() throws Exception {
void addingShouldResultInSuccess() throws Exception {
try (var allCustomers = dao.getAll()) {
assumeTrue(allCustomers.count() == 1);
}
@ -68,7 +68,7 @@ public class InMemoryCustomerDaoTest {
}
@Test
public void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
final var nonExistingCustomer = new Customer(2, "Robert", "Englund");
var result = dao.delete(nonExistingCustomer);
@ -77,7 +77,7 @@ public class InMemoryCustomerDaoTest {
}
@Test
public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
final var nonExistingId = getNonExistingCustomerId();
final var newFirstname = "Douglas";
final var newLastname = "MacArthur";
@ -89,7 +89,7 @@ public class InMemoryCustomerDaoTest {
}
@Test
public void retrieveShouldReturnNoCustomer() throws Exception {
void retrieveShouldReturnNoCustomer() throws Exception {
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
}
}
@ -99,10 +99,10 @@ public class InMemoryCustomerDaoTest {
* customer.
*/
@Nested
public class ExistingCustomer {
class ExistingCustomer {
@Test
public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
var result = dao.add(CUSTOMER);
assertFalse(result);
@ -111,7 +111,7 @@ public class InMemoryCustomerDaoTest {
}
@Test
public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
var result = dao.delete(CUSTOMER);
assertTrue(result);
@ -120,7 +120,7 @@ public class InMemoryCustomerDaoTest {
}
@Test
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
Exception {
final var newFirstname = "Bernard";
final var newLastname = "Montgomery";
@ -135,7 +135,7 @@ public class InMemoryCustomerDaoTest {
}
@Test
public void retriveShouldReturnTheCustomer() {
void retriveShouldReturnTheCustomer() {
var optionalCustomer = dao.getById(CUSTOMER.getId());
assertTrue(optionalCustomer.isPresent());