Incorporated review changes - 1) Created sql file for central schema 2)
Changed getById return type to Optional
This commit is contained in:
parent
f32a3892a3
commit
ddbc61b140
@ -73,15 +73,14 @@ public class App {
|
||||
private static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("DROP TABLE CUSTOMERS");
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws SQLException {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
||||
+ "LNAME VARCHAR(100))");
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@ -51,10 +52,11 @@ public interface CustomerDao {
|
||||
|
||||
/**
|
||||
* @param id unique identifier of the customer.
|
||||
* @return customer with unique identifier <code>id</code> if found, null otherwise.
|
||||
* @return an optional with customer if a customer with unique identifier <code>id</code>
|
||||
* exists, empty optional otherwise.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
Customer getById(int id) throws Exception;
|
||||
Optional<Customer> getById(int id) throws Exception;
|
||||
|
||||
/**
|
||||
* @param customer the customer to be added.
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
public interface CustomerSchemaSql {
|
||||
|
||||
String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
||||
+ "LNAME VARCHAR(100))";
|
||||
|
||||
String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
|
||||
}
|
@ -27,6 +27,7 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.Consumer;
|
||||
@ -109,7 +110,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Customer getById(int id) throws Exception {
|
||||
public Optional<Customer> getById(int id) throws Exception {
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement =
|
||||
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
@ -117,9 +118,9 @@ public class DbCustomerDao implements CustomerDao {
|
||||
statement.setInt(1, id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
return createCustomer(resultSet);
|
||||
return Optional.of(createCustomer(resultSet));
|
||||
} else {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new Exception(ex.getMessage(), ex);
|
||||
@ -131,7 +132,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
*/
|
||||
@Override
|
||||
public boolean add(Customer customer) throws Exception {
|
||||
if (getById(customer.getId()) != null) {
|
||||
if (getById(customer.getId()).isPresent()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ package com.iluwatar.dao;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@ -46,13 +47,13 @@ public class InMemoryCustomerDao implements CustomerDao {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getById(final int id) {
|
||||
return idToCustomer.get(id);
|
||||
public Optional<Customer> getById(final int id) {
|
||||
return Optional.ofNullable(idToCustomer.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final Customer customer) {
|
||||
if (getById(customer.getId()) != null) {
|
||||
if (getById(customer.getId()).isPresent()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -46,8 +46,7 @@ public class DbCustomerDaoTest {
|
||||
public void createSchema() throws SQLException {
|
||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100),"
|
||||
+ " LNAME VARCHAR(100))");
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,7 +84,7 @@ public class DbCustomerDaoTest {
|
||||
assertTrue(result);
|
||||
|
||||
assertCustomerCountIs(2);
|
||||
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
|
||||
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -106,12 +105,12 @@ public class DbCustomerDaoTest {
|
||||
boolean result = dao.update(customer);
|
||||
|
||||
assertFalse(result);
|
||||
assertNull(dao.getById(nonExistingId));
|
||||
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveShouldReturnNull() throws Exception {
|
||||
assertNull(dao.getById(getNonExistingCustomerId()));
|
||||
public void retrieveShouldReturnNoCustomer() throws Exception {
|
||||
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,7 +129,7 @@ public class DbCustomerDaoTest {
|
||||
|
||||
assertFalse(result);
|
||||
assertCustomerCountIs(1);
|
||||
assertEquals(existingCustomer, dao.getById(existingCustomer.getId()));
|
||||
assertEquals(existingCustomer, dao.getById(existingCustomer.getId()).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -139,7 +138,7 @@ public class DbCustomerDaoTest {
|
||||
|
||||
assertTrue(result);
|
||||
assertCustomerCountIs(0);
|
||||
assertNull(dao.getById(existingCustomer.getId()));
|
||||
assertFalse(dao.getById(existingCustomer.getId()).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -151,7 +150,7 @@ public class DbCustomerDaoTest {
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
final Customer cust = dao.getById(existingCustomer.getId());
|
||||
final Customer cust = dao.getById(existingCustomer.getId()).get();
|
||||
assertEquals(newFirstname, cust.getFirstName());
|
||||
assertEquals(newLastname, cust.getLastName());
|
||||
}
|
||||
@ -207,12 +206,12 @@ public class DbCustomerDaoTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievingACustomerByIdReturnsNull() throws Exception {
|
||||
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||
dao.getById(existingCustomer.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievingAllCustomersReturnsAnEmptyStream() throws Exception {
|
||||
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||
dao.getAll();
|
||||
}
|
||||
|
||||
@ -226,7 +225,7 @@ public class DbCustomerDaoTest {
|
||||
public void deleteSchema() throws SQLException {
|
||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("DROP TABLE CUSTOMERS");
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,7 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
@ -49,7 +50,7 @@ public class InMemoryCustomerDaoTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
dao = new InMemoryCustomerDao();
|
||||
dao.add(CUSTOMER);
|
||||
assertTrue(dao.add(CUSTOMER));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,7 +70,7 @@ public class InMemoryCustomerDaoTest {
|
||||
assertTrue(result);
|
||||
|
||||
assertCustomerCountIs(2);
|
||||
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
|
||||
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -90,12 +91,12 @@ public class InMemoryCustomerDaoTest {
|
||||
boolean result = dao.update(customer);
|
||||
|
||||
assertFalse(result);
|
||||
assertNull(dao.getById(nonExistingId));
|
||||
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveShouldReturnNull() throws Exception {
|
||||
assertNull(dao.getById(getNonExistingCustomerId()));
|
||||
public void retrieveShouldReturnNoCustomer() throws Exception {
|
||||
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,7 +112,7 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
assertFalse(result);
|
||||
assertCustomerCountIs(1);
|
||||
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
|
||||
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -120,7 +121,7 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
assertTrue(result);
|
||||
assertCustomerCountIs(0);
|
||||
assertNull(dao.getById(CUSTOMER.getId()));
|
||||
assertFalse(dao.getById(CUSTOMER.getId()).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -132,10 +133,18 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
final Customer cust = dao.getById(CUSTOMER.getId());
|
||||
final Customer cust = dao.getById(CUSTOMER.getId()).get();
|
||||
assertEquals(newFirstname, cust.getFirstName());
|
||||
assertEquals(newLastname, cust.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retriveShouldReturnTheCustomer() {
|
||||
Optional<Customer> optionalCustomer = dao.getById(CUSTOMER.getId());
|
||||
|
||||
assertTrue(optionalCustomer.isPresent());
|
||||
assertEquals(CUSTOMER, optionalCustomer.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user