Skip to content

Commit ddbc61b

Browse files
committed
Incorporated review changes - 1) Created sql file for central schema 2)
Changed getById return type to Optional
1 parent f32a389 commit ddbc61b

File tree

7 files changed

+52
-32
lines changed

7 files changed

+52
-32
lines changed

dao/src/main/java/com/iluwatar/dao/App.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ public static void main(final String[] args) throws Exception {
7373
private static void deleteSchema(DataSource dataSource) throws SQLException {
7474
try (Connection connection = dataSource.getConnection();
7575
Statement statement = connection.createStatement()) {
76-
statement.execute("DROP TABLE CUSTOMERS");
76+
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
7777
}
7878
}
7979

8080
private static void createSchema(DataSource dataSource) throws SQLException {
8181
try (Connection connection = dataSource.getConnection();
8282
Statement statement = connection.createStatement()) {
83-
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
84-
+ "LNAME VARCHAR(100))");
83+
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
8584
}
8685
}
8786

dao/src/main/java/com/iluwatar/dao/CustomerDao.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package com.iluwatar.dao;
2525

26+
import java.util.Optional;
2627
import java.util.stream.Stream;
2728

2829
/**
@@ -51,10 +52,11 @@ public interface CustomerDao {
5152

5253
/**
5354
* @param id unique identifier of the customer.
54-
* @return customer with unique identifier <code>id</code> if found, null otherwise.
55+
* @return an optional with customer if a customer with unique identifier <code>id</code>
56+
* exists, empty optional otherwise.
5557
* @throws Exception if any error occurs.
5658
*/
57-
Customer getById(int id) throws Exception;
59+
Optional<Customer> getById(int id) throws Exception;
5860

5961
/**
6062
* @param customer the customer to be added.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar.dao;
2+
3+
public interface CustomerSchemaSql {
4+
5+
String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
6+
+ "LNAME VARCHAR(100))";
7+
8+
String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
9+
}

dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.sql.PreparedStatement;
2828
import java.sql.ResultSet;
2929
import java.sql.SQLException;
30+
import java.util.Optional;
3031
import java.util.Spliterator;
3132
import java.util.Spliterators;
3233
import java.util.function.Consumer;
@@ -109,17 +110,17 @@ private Customer createCustomer(ResultSet resultSet) throws SQLException {
109110
* {@inheritDoc}
110111
*/
111112
@Override
112-
public Customer getById(int id) throws Exception {
113+
public Optional<Customer> getById(int id) throws Exception {
113114
try (Connection connection = getConnection();
114115
PreparedStatement statement =
115116
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
116117

117118
statement.setInt(1, id);
118119
ResultSet resultSet = statement.executeQuery();
119120
if (resultSet.next()) {
120-
return createCustomer(resultSet);
121+
return Optional.of(createCustomer(resultSet));
121122
} else {
122-
return null;
123+
return Optional.empty();
123124
}
124125
} catch (SQLException ex) {
125126
throw new Exception(ex.getMessage(), ex);
@@ -131,7 +132,7 @@ public Customer getById(int id) throws Exception {
131132
*/
132133
@Override
133134
public boolean add(Customer customer) throws Exception {
134-
if (getById(customer.getId()) != null) {
135+
if (getById(customer.getId()).isPresent()) {
135136
return false;
136137
}
137138

dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.HashMap;
2727
import java.util.Map;
28+
import java.util.Optional;
2829
import java.util.stream.Stream;
2930

3031
/**
@@ -46,13 +47,13 @@ public Stream<Customer> getAll() {
4647
}
4748

4849
@Override
49-
public Customer getById(final int id) {
50-
return idToCustomer.get(id);
50+
public Optional<Customer> getById(final int id) {
51+
return Optional.ofNullable(idToCustomer.get(id));
5152
}
5253

5354
@Override
5455
public boolean add(final Customer customer) {
55-
if (getById(customer.getId()) != null) {
56+
if (getById(customer.getId()).isPresent()) {
5657
return false;
5758
}
5859

dao/src/test/java/com/iluwatar/dao/DbCustomerDaoTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public class DbCustomerDaoTest {
4646
public void createSchema() throws SQLException {
4747
try (Connection connection = DriverManager.getConnection(DB_URL);
4848
Statement statement = connection.createStatement()) {
49-
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100),"
50-
+ " LNAME VARCHAR(100))");
49+
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
5150
}
5251
}
5352

@@ -85,7 +84,7 @@ public void addingShouldResultInSuccess() throws Exception {
8584
assertTrue(result);
8685

8786
assertCustomerCountIs(2);
88-
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
87+
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()).get());
8988
}
9089

9190
@Test
@@ -106,12 +105,12 @@ public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Except
106105
boolean result = dao.update(customer);
107106

108107
assertFalse(result);
109-
assertNull(dao.getById(nonExistingId));
108+
assertFalse(dao.getById(nonExistingId).isPresent());
110109
}
111110

112111
@Test
113-
public void retrieveShouldReturnNull() throws Exception {
114-
assertNull(dao.getById(getNonExistingCustomerId()));
112+
public void retrieveShouldReturnNoCustomer() throws Exception {
113+
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
115114
}
116115
}
117116

@@ -130,7 +129,7 @@ public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Ex
130129

131130
assertFalse(result);
132131
assertCustomerCountIs(1);
133-
assertEquals(existingCustomer, dao.getById(existingCustomer.getId()));
132+
assertEquals(existingCustomer, dao.getById(existingCustomer.getId()).get());
134133
}
135134

136135
@Test
@@ -139,7 +138,7 @@ public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exc
139138

140139
assertTrue(result);
141140
assertCustomerCountIs(0);
142-
assertNull(dao.getById(existingCustomer.getId()));
141+
assertFalse(dao.getById(existingCustomer.getId()).isPresent());
143142
}
144143

145144
@Test
@@ -151,7 +150,7 @@ public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdate
151150

152151
assertTrue(result);
153152

154-
final Customer cust = dao.getById(existingCustomer.getId());
153+
final Customer cust = dao.getById(existingCustomer.getId()).get();
155154
assertEquals(newFirstname, cust.getFirstName());
156155
assertEquals(newLastname, cust.getLastName());
157156
}
@@ -207,12 +206,12 @@ public void updatingACustomerFailsWithFeedbackToTheClient() throws Exception {
207206
}
208207

209208
@Test
210-
public void retrievingACustomerByIdReturnsNull() throws Exception {
209+
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() throws Exception {
211210
dao.getById(existingCustomer.getId());
212211
}
213212

214213
@Test
215-
public void retrievingAllCustomersReturnsAnEmptyStream() throws Exception {
214+
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() throws Exception {
216215
dao.getAll();
217216
}
218217

@@ -226,7 +225,7 @@ public void retrievingAllCustomersReturnsAnEmptyStream() throws Exception {
226225
public void deleteSchema() throws SQLException {
227226
try (Connection connection = DriverManager.getConnection(DB_URL);
228227
Statement statement = connection.createStatement()) {
229-
statement.execute("DROP TABLE CUSTOMERS");
228+
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
230229
}
231230
}
232231

dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.junit.Assert.assertTrue;
3030
import static org.junit.Assume.assumeTrue;
3131

32+
import java.util.Optional;
3233
import java.util.stream.Stream;
3334

3435
import org.junit.Before;
@@ -49,7 +50,7 @@ public class InMemoryCustomerDaoTest {
4950
@Before
5051
public void setUp() {
5152
dao = new InMemoryCustomerDao();
52-
dao.add(CUSTOMER);
53+
assertTrue(dao.add(CUSTOMER));
5354
}
5455

5556
/**
@@ -69,7 +70,7 @@ public void addingShouldResultInSuccess() throws Exception {
6970
assertTrue(result);
7071

7172
assertCustomerCountIs(2);
72-
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
73+
assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()).get());
7374
}
7475

7576
@Test
@@ -90,12 +91,12 @@ public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Except
9091
boolean result = dao.update(customer);
9192

9293
assertFalse(result);
93-
assertNull(dao.getById(nonExistingId));
94+
assertFalse(dao.getById(nonExistingId).isPresent());
9495
}
9596

9697
@Test
97-
public void retrieveShouldReturnNull() throws Exception {
98-
assertNull(dao.getById(getNonExistingCustomerId()));
98+
public void retrieveShouldReturnNoCustomer() throws Exception {
99+
assertFalse(dao.getById(getNonExistingCustomerId()).isPresent());
99100
}
100101
}
101102

@@ -111,7 +112,7 @@ public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Ex
111112

112113
assertFalse(result);
113114
assertCustomerCountIs(1);
114-
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
115+
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()).get());
115116
}
116117

117118
@Test
@@ -120,7 +121,7 @@ public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exc
120121

121122
assertTrue(result);
122123
assertCustomerCountIs(0);
123-
assertNull(dao.getById(CUSTOMER.getId()));
124+
assertFalse(dao.getById(CUSTOMER.getId()).isPresent());
124125
}
125126

126127
@Test
@@ -132,10 +133,18 @@ public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdate
132133

133134
assertTrue(result);
134135

135-
final Customer cust = dao.getById(CUSTOMER.getId());
136+
final Customer cust = dao.getById(CUSTOMER.getId()).get();
136137
assertEquals(newFirstname, cust.getFirstName());
137138
assertEquals(newLastname, cust.getLastName());
138139
}
140+
141+
@Test
142+
public void retriveShouldReturnTheCustomer() {
143+
Optional<Customer> optionalCustomer = dao.getById(CUSTOMER.getId());
144+
145+
assertTrue(optionalCustomer.isPresent());
146+
assertEquals(CUSTOMER, optionalCustomer.get());
147+
}
139148
}
140149

141150
/**

0 commit comments

Comments
 (0)