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 {
|
private static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||||
try (Connection connection = dataSource.getConnection();
|
try (Connection connection = dataSource.getConnection();
|
||||||
Statement statement = connection.createStatement()) {
|
Statement statement = connection.createStatement()) {
|
||||||
statement.execute("DROP TABLE CUSTOMERS");
|
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createSchema(DataSource dataSource) throws SQLException {
|
private static void createSchema(DataSource dataSource) throws SQLException {
|
||||||
try (Connection connection = dataSource.getConnection();
|
try (Connection connection = dataSource.getConnection();
|
||||||
Statement statement = connection.createStatement()) {
|
Statement statement = connection.createStatement()) {
|
||||||
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||||
+ "LNAME VARCHAR(100))");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
package com.iluwatar.dao;
|
package com.iluwatar.dao;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,10 +52,11 @@ public interface CustomerDao {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param id unique identifier of the customer.
|
* @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.
|
* @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.
|
* @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.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
import java.util.Spliterators;
|
import java.util.Spliterators;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -109,7 +110,7 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Customer getById(int id) throws Exception {
|
public Optional<Customer> getById(int id) throws Exception {
|
||||||
try (Connection connection = getConnection();
|
try (Connection connection = getConnection();
|
||||||
PreparedStatement statement =
|
PreparedStatement statement =
|
||||||
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||||
@ -117,9 +118,9 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
statement.setInt(1, id);
|
statement.setInt(1, id);
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
if (resultSet.next()) {
|
if (resultSet.next()) {
|
||||||
return createCustomer(resultSet);
|
return Optional.of(createCustomer(resultSet));
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
throw new Exception(ex.getMessage(), ex);
|
throw new Exception(ex.getMessage(), ex);
|
||||||
@ -131,7 +132,7 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean add(Customer customer) throws Exception {
|
public boolean add(Customer customer) throws Exception {
|
||||||
if (getById(customer.getId()) != null) {
|
if (getById(customer.getId()).isPresent()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ package com.iluwatar.dao;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,13 +47,13 @@ public class InMemoryCustomerDao implements CustomerDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Customer getById(final int id) {
|
public Optional<Customer> getById(final int id) {
|
||||||
return idToCustomer.get(id);
|
return Optional.ofNullable(idToCustomer.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean add(final Customer customer) {
|
public boolean add(final Customer customer) {
|
||||||
if (getById(customer.getId()) != null) {
|
if (getById(customer.getId()).isPresent()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,8 +46,7 @@ public class DbCustomerDaoTest {
|
|||||||
public void createSchema() throws SQLException {
|
public void createSchema() throws SQLException {
|
||||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||||
Statement statement = connection.createStatement()) {
|
Statement statement = connection.createStatement()) {
|
||||||
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100),"
|
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||||
+ " LNAME VARCHAR(100))");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +84,7 @@ public class DbCustomerDaoTest {
|
|||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
|
||||||
assertCustomerCountIs(2);
|
assertCustomerCountIs(2);
|
||||||
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
|
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -106,12 +105,12 @@ public class DbCustomerDaoTest {
|
|||||||
boolean result = dao.update(customer);
|
boolean result = dao.update(customer);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
assertNull(dao.getById(nonExistingId));
|
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void retrieveShouldReturnNull() throws Exception {
|
public void retrieveShouldReturnNoCustomer() throws Exception {
|
||||||
assertNull(dao.getById(getNonExistingCustomerId()));
|
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +129,7 @@ public class DbCustomerDaoTest {
|
|||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
assertCustomerCountIs(1);
|
assertCustomerCountIs(1);
|
||||||
assertEquals(existingCustomer, dao.getById(existingCustomer.getId()));
|
assertEquals(existingCustomer, dao.getById(existingCustomer.getId()).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -139,7 +138,7 @@ public class DbCustomerDaoTest {
|
|||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
assertCustomerCountIs(0);
|
assertCustomerCountIs(0);
|
||||||
assertNull(dao.getById(existingCustomer.getId()));
|
assertFalse(dao.getById(existingCustomer.getId()).isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -151,7 +150,7 @@ public class DbCustomerDaoTest {
|
|||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
|
||||||
final Customer cust = dao.getById(existingCustomer.getId());
|
final Customer cust = dao.getById(existingCustomer.getId()).get();
|
||||||
assertEquals(newFirstname, cust.getFirstName());
|
assertEquals(newFirstname, cust.getFirstName());
|
||||||
assertEquals(newLastname, cust.getLastName());
|
assertEquals(newLastname, cust.getLastName());
|
||||||
}
|
}
|
||||||
@ -207,12 +206,12 @@ public class DbCustomerDaoTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void retrievingACustomerByIdReturnsNull() throws Exception {
|
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||||
dao.getById(existingCustomer.getId());
|
dao.getById(existingCustomer.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void retrievingAllCustomersReturnsAnEmptyStream() throws Exception {
|
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||||
dao.getAll();
|
dao.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,7 +225,7 @@ public class DbCustomerDaoTest {
|
|||||||
public void deleteSchema() throws SQLException {
|
public void deleteSchema() throws SQLException {
|
||||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||||
Statement statement = connection.createStatement()) {
|
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.Assert.assertTrue;
|
||||||
import static org.junit.Assume.assumeTrue;
|
import static org.junit.Assume.assumeTrue;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -49,7 +50,7 @@ public class InMemoryCustomerDaoTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
dao = new InMemoryCustomerDao();
|
dao = new InMemoryCustomerDao();
|
||||||
dao.add(CUSTOMER);
|
assertTrue(dao.add(CUSTOMER));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,7 +70,7 @@ public class InMemoryCustomerDaoTest {
|
|||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
|
||||||
assertCustomerCountIs(2);
|
assertCustomerCountIs(2);
|
||||||
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
|
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -90,12 +91,12 @@ public class InMemoryCustomerDaoTest {
|
|||||||
boolean result = dao.update(customer);
|
boolean result = dao.update(customer);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
assertNull(dao.getById(nonExistingId));
|
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void retrieveShouldReturnNull() throws Exception {
|
public void retrieveShouldReturnNoCustomer() throws Exception {
|
||||||
assertNull(dao.getById(getNonExistingCustomerId()));
|
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +112,7 @@ public class InMemoryCustomerDaoTest {
|
|||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
assertCustomerCountIs(1);
|
assertCustomerCountIs(1);
|
||||||
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
|
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -120,7 +121,7 @@ public class InMemoryCustomerDaoTest {
|
|||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
assertCustomerCountIs(0);
|
assertCustomerCountIs(0);
|
||||||
assertNull(dao.getById(CUSTOMER.getId()));
|
assertFalse(dao.getById(CUSTOMER.getId()).isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -132,10 +133,18 @@ public class InMemoryCustomerDaoTest {
|
|||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
|
||||||
final Customer cust = dao.getById(CUSTOMER.getId());
|
final Customer cust = dao.getById(CUSTOMER.getId()).get();
|
||||||
assertEquals(newFirstname, cust.getFirstName());
|
assertEquals(newFirstname, cust.getFirstName());
|
||||||
assertEquals(newLastname, cust.getLastName());
|
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