diff --git a/dao/src/main/java/com/iluwatar/dao/App.java b/dao/src/main/java/com/iluwatar/dao/App.java index 334c2b9bd..a63289ae9 100644 --- a/dao/src/main/java/com/iluwatar/dao/App.java +++ b/dao/src/main/java/com/iluwatar/dao/App.java @@ -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); } } diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java index 5d6aa38f5..059a9e9f7 100644 --- a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java @@ -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 id if found, null otherwise. + * @return an optional with customer if a customer with unique identifier id + * exists, empty optional otherwise. * @throws Exception if any error occurs. */ - Customer getById(int id) throws Exception; + Optional getById(int id) throws Exception; /** * @param customer the customer to be added. diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java b/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java new file mode 100644 index 000000000..05707fa0e --- /dev/null +++ b/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java @@ -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"; +} diff --git a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java index 91279b99e..622b5b1f0 100644 --- a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java @@ -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 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; } diff --git a/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java index 63576b99a..15c63d1de 100644 --- a/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java @@ -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 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; } diff --git a/dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java b/dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java index 470b33557..08e61ebe6 100644 --- a/dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java +++ b/dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java @@ -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); } } diff --git a/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java b/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java index 49272728e..65a087b9b 100644 --- a/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java +++ b/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java @@ -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 optionalCustomer = dao.getById(CUSTOMER.getId()); + + assertTrue(optionalCustomer.isPresent()); + assertEquals(CUSTOMER, optionalCustomer.get()); + } } /**