diff --git a/dao/pom.xml b/dao/pom.xml
index 3f22a0cc6..05ab2b22a 100644
--- a/dao/pom.xml
+++ b/dao/pom.xml
@@ -52,6 +52,10 @@
de.bechte.junit
junit-hierarchicalcontextrunner
+
+ org.mockito
+ mockito-core
+
diff --git a/dao/src/main/java/com/iluwatar/dao/App.java b/dao/src/main/java/com/iluwatar/dao/App.java
index 27daf3098..146fddcb0 100644
--- a/dao/src/main/java/com/iluwatar/dao/App.java
+++ b/dao/src/main/java/com/iluwatar/dao/App.java
@@ -49,9 +49,11 @@ public class App {
* Program entry point.
*
* @param args command line args.
+ * @throws Exception if any error occurs.
*/
- public static void main(final String[] args) {
- final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
+ public static void main(final String[] args) throws Exception {
+ final CustomerDao customerDao = new InMemoryCustomerDao();
+ addCustomers(customerDao);
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
final Customer customer = new Customer(4, "Dan", "Danson");
@@ -65,6 +67,12 @@ public class App {
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
}
+ private static void addCustomers(CustomerDao customerDao) throws Exception {
+ for (Customer customer : generateSampleCustomers()) {
+ customerDao.add(customer);
+ }
+ }
+
/**
* Generate customers.
*
diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java
index 07608621b..545e46a5e 100644
--- a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java
+++ b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java
@@ -26,18 +26,54 @@ import java.util.stream.Stream;
/**
*
- * CustomerDao
- *
+ * In an application the Data Access Object (DAO) is a part of Data access layer. It is an object
+ * that provides an interface to some type of persistence mechanism. By mapping application calls
+ * to the persistence layer, DAO provides some specific data operations without exposing details
+ * of the database. This isolation supports the Single responsibility principle. It separates what
+ * data accesses the application needs, in terms of domain-specific objects and data types
+ * (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
+ * database schema, etc.
+ *
+ * Any change in the way data is stored and retrieved will not change the client code as the client
+ * will be using interface and need not worry about exact source.
+ *
+ * @see InMemoryCustomerDao
+ * @see DBCustomerDao
*/
public interface CustomerDao {
- Stream getAll();
+ /**
+ * @return all the customers as a stream. The stream may be lazily or eagerly evaluated based on the
+ * implementation. The stream must be closed after use.
+ * @throws Exception if any error occurs.
+ */
+ Stream getAll() throws Exception;
+
+ /**
+ * @param id unique identifier of the customer.
+ * @return customer with unique identifier id
is found, null otherwise.
+ * @throws Exception if any error occurs.
+ */
+ Customer getById(int id) throws Exception;
- Customer getById(int id);
+ /**
+ * @param customer the customer to be added.
+ * @return true if customer is successfully added, false if customer already exists.
+ * @throws Exception if any error occurs.
+ */
+ boolean add(Customer customer) throws Exception;
- boolean add(Customer customer);
+ /**
+ * @param customer the customer to be updated.
+ * @return true if customer exists and is successfully updated, false otherwise.
+ * @throws Exception if any error occurs.
+ */
+ boolean update(Customer customer) throws Exception;
- boolean update(Customer customer);
-
- boolean delete(Customer customer);
+ /**
+ * @param customer the customer to be deleted.
+ * @return true if customer exists and is successfully deleted, false otherwise.
+ * @throws Exception if any error occurs.
+ */
+ boolean delete(Customer customer) throws Exception;
}
diff --git a/dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
index 1b9c4b981..950ecb1b3 100644
--- a/dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
+++ b/dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
@@ -1,7 +1,28 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
package com.iluwatar.dao;
import java.sql.Connection;
-import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -11,16 +32,22 @@ import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
+import javax.sql.DataSource;
+
+/**
+ *
+ *
+ */
public class DBCustomerDao implements CustomerDao {
- private String dbUrl;
+ private final DataSource dataSource;
- public DBCustomerDao(String dbUrl) {
- this.dbUrl = dbUrl;
+ public DBCustomerDao(DataSource dataSource) {
+ this.dataSource = dataSource;
}
@Override
- public Stream getAll() {
+ public Stream getAll() throws Exception {
Connection connection;
try {
@@ -41,14 +68,16 @@ public class DBCustomerDao implements CustomerDao {
e.printStackTrace();
return false;
}
-
}}, false).onClose(() -> mutedClose(connection));
} catch (SQLException e) {
- e.printStackTrace();
- return null;
+ throw new Exception(e.getMessage(), e);
}
}
+ private Connection getConnection() throws SQLException {
+ return dataSource.getConnection();
+ }
+
private void mutedClose(Connection connection) {
try {
connection.close();
@@ -64,22 +93,23 @@ public class DBCustomerDao implements CustomerDao {
}
@Override
- public Customer getById(int id) {
+ public Customer getById(int id) throws Exception {
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return createCustomer(resultSet);
+ } else {
+ return null;
}
} catch (SQLException ex) {
- ex.printStackTrace();
+ throw new Exception(ex.getMessage(), ex);
}
- return null;
}
@Override
- public boolean add(Customer customer) {
+ public boolean add(Customer customer) throws Exception {
if (getById(customer.getId()) != null) {
return false;
}
@@ -92,13 +122,12 @@ public class DBCustomerDao implements CustomerDao {
statement.execute();
return true;
} catch (SQLException ex) {
- ex.printStackTrace();
- return false;
+ throw new Exception(ex.getMessage(), ex);
}
}
@Override
- public boolean update(Customer customer) {
+ public boolean update(Customer customer) throws Exception {
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
statement.setString(1, customer.getFirstName());
@@ -106,25 +135,18 @@ public class DBCustomerDao implements CustomerDao {
statement.setInt(3, customer.getId());
return statement.executeUpdate() > 0;
} catch (SQLException ex) {
- ex.printStackTrace();
- return false;
+ throw new Exception(ex.getMessage(), ex);
}
}
@Override
- public boolean delete(Customer customer) {
+ public boolean delete(Customer customer) throws Exception {
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
statement.setInt(1, customer.getId());
return statement.executeUpdate() > 0;
} catch (SQLException ex) {
- ex.printStackTrace();
- return false;
+ throw new Exception(ex.getMessage(), ex);
}
}
-
- private Connection getConnection() throws SQLException {
- return DriverManager.getConnection(dbUrl);
- }
-
}
diff --git a/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java
index 0057e7abe..62276d5c5 100644
--- a/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java
+++ b/dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java
@@ -23,30 +23,22 @@
package com.iluwatar.dao;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
- *
- * The data access object (DAO) is an object that provides an abstract interface to some type of
- * database or other persistence mechanism. By mapping application calls to the persistence layer,
- * DAO provide some specific data operations without exposing details of the database. This
- * isolation supports the Single responsibility principle. It separates what data accesses the
- * application needs, in terms of domain-specific objects and data types (the public interface of
- * the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
- *
+ * An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory
+ * and data is lost when the application exits.
+ *
+ * This implementation is useful as temporary database or for testing.
*/
-// TODO update the javadoc
public class InMemoryCustomerDao implements CustomerDao {
private Map idToCustomer = new HashMap<>();
- public InMemoryCustomerDao(final List customers) {
- customers.stream()
- .forEach((customer) -> idToCustomer.put(customer.getId(), customer));
- }
-
+ /**
+ * An eagerly evaluated stream of customers stored in memory.
+ */
@Override
public Stream getAll() {
return idToCustomer.values().stream();
@@ -67,7 +59,6 @@ public class InMemoryCustomerDao implements CustomerDao {
return true;
}
-
@Override
public boolean update(final Customer customer) {
return idToCustomer.replace(customer.getId(), customer) != null;
diff --git a/dao/src/test/java/com/iluwatar/dao/AppTest.java b/dao/src/test/java/com/iluwatar/dao/AppTest.java
index b4caa54b4..08babc62a 100644
--- a/dao/src/test/java/com/iluwatar/dao/AppTest.java
+++ b/dao/src/test/java/com/iluwatar/dao/AppTest.java
@@ -24,14 +24,12 @@ package com.iluwatar.dao;
import org.junit.Test;
-import java.io.IOException;
-
/**
* Tests that DAO example runs without errors.
*/
public class AppTest {
@Test
- public void test() throws IOException {
+ public void test() throws Exception {
String[] args = {};
App.main(args);
}
diff --git a/dao/src/test/java/com/iluwatar/dao/DBCustomerDaoTest.java b/dao/src/test/java/com/iluwatar/dao/DBCustomerDaoTest.java
index 202140e98..243d9b3ac 100644
--- a/dao/src/test/java/com/iluwatar/dao/DBCustomerDaoTest.java
+++ b/dao/src/test/java/com/iluwatar/dao/DBCustomerDaoTest.java
@@ -5,6 +5,9 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -12,16 +15,22 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.stream.Stream;
+import javax.sql.DataSource;
+
+import org.h2.jdbcx.JdbcDataSource;
import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
+import org.mockito.Mockito;
import de.bechte.junit.runners.context.HierarchicalContextRunner;
@RunWith(HierarchicalContextRunner.class)
public class DBCustomerDaoTest {
-
+
private static final String DB_URL = "jdbc:h2:~/dao:customerdb";
private DBCustomerDao dao;
private Customer existingCustomer = new Customer(1, "Freddy", "Krueger");
@@ -33,94 +42,148 @@ public class DBCustomerDaoTest {
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), LNAME VARCHAR(100))");
}
}
-
- @Before
- public void setUp() {
- dao = new DBCustomerDao(DB_URL);
- boolean result = dao.add(existingCustomer);
- assumeTrue(result);
- }
-
- public class NonExistantCustomer {
-
- @Test
- public void addingShouldResultInSuccess() {
- try (Stream allCustomers = dao.getAll()) {
- assumeTrue(allCustomers.count() == 1);
- }
-
- final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
- boolean result = dao.add(nonExistingCustomer);
- assertTrue(result);
-
- assertCustomerCountIs(2);
- assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
- }
-
- @Test
- public void deletionShouldBeFailureAndNotAffectExistingCustomers() {
- final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
- boolean result = dao.delete(nonExistingCustomer);
-
- assertFalse(result);
- assertCustomerCountIs(1);
- }
-
- @Test
- public void updationShouldBeFailureAndNotAffectExistingCustomers() {
- final int nonExistingId = getNonExistingCustomerId();
- final String newFirstname = "Douglas";
- final String newLastname = "MacArthur";
- final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
- boolean result = dao.update(customer);
-
- assertFalse(result);
- assertNull(dao.getById(nonExistingId));
- }
-
- @Test
- public void retrieveShouldReturnNull() {
- assertNull(dao.getById(getNonExistingCustomerId()));
- }
-
- }
-
- public class ExistingCustomer {
-
- @Test
- public void addingShouldResultInFailureAndNotAffectExistingCustomers() {
- Customer existingCustomer = new Customer(1, "Freddy", "Krueger");
-
+
+ public class ConnectionSuccess {
+
+ @Before
+ public void setUp() throws Exception {
+ JdbcDataSource dataSource = new JdbcDataSource();
+ dataSource.setURL(DB_URL);
+ dao = new DBCustomerDao(dataSource);
boolean result = dao.add(existingCustomer);
-
- assertFalse(result);
- assertCustomerCountIs(1);
- assertEquals(existingCustomer, dao.getById(existingCustomer.getId()));
- }
-
- @Test
- public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() {
- boolean result = dao.delete(existingCustomer);
-
assertTrue(result);
- assertCustomerCountIs(0);
- assertNull(dao.getById(existingCustomer.getId()));
+ }
+
+ public class NonExistantCustomer {
+
+ @Test
+ public void addingShouldResultInSuccess() throws Exception {
+ try (Stream allCustomers = dao.getAll()) {
+ assumeTrue(allCustomers.count() == 1);
+ }
+
+ final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
+ boolean result = dao.add(nonExistingCustomer);
+ assertTrue(result);
+
+ assertCustomerCountIs(2);
+ assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
+ }
+
+ @Test
+ public void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
+ final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
+ boolean result = dao.delete(nonExistingCustomer);
+
+ assertFalse(result);
+ assertCustomerCountIs(1);
+ }
+
+ @Test
+ public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
+ final int nonExistingId = getNonExistingCustomerId();
+ final String newFirstname = "Douglas";
+ final String newLastname = "MacArthur";
+ final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
+ boolean result = dao.update(customer);
+
+ assertFalse(result);
+ assertNull(dao.getById(nonExistingId));
+ }
+
+ @Test
+ public void retrieveShouldReturnNull() throws Exception {
+ assertNull(dao.getById(getNonExistingCustomerId()));
+ }
+ }
+
+ public class ExistingCustomer {
+
+ @Test
+ public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
+ Customer existingCustomer = new Customer(1, "Freddy", "Krueger");
+
+ boolean result = dao.add(existingCustomer);
+
+ assertFalse(result);
+ assertCustomerCountIs(1);
+ assertEquals(existingCustomer, dao.getById(existingCustomer.getId()));
+ }
+
+ @Test
+ public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
+ boolean result = dao.delete(existingCustomer);
+
+ assertTrue(result);
+ assertCustomerCountIs(0);
+ assertNull(dao.getById(existingCustomer.getId()));
+ }
+
+ @Test
+ public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws Exception {
+ final String newFirstname = "Bernard";
+ final String newLastname = "Montgomery";
+ final Customer customer = new Customer(existingCustomer.getId(), newFirstname, newLastname);
+ boolean result = dao.update(customer);
+
+ assertTrue(result);
+
+ final Customer cust = dao.getById(existingCustomer.getId());
+ assertEquals(newFirstname, cust.getFirstName());
+ assertEquals(newLastname, cust.getLastName());
+ }
+ }
+ }
+
+ public class DBConnectivityIssue {
+
+ private static final String EXCEPTION_CAUSE = "Connection not available";
+ @Rule public ExpectedException exception = ExpectedException.none();
+
+ @Before
+ public void setUp() throws SQLException {
+ dao = new DBCustomerDao(mockedDatasource());
+ exception.expect(Exception.class);
+ exception.expectMessage(EXCEPTION_CAUSE);
+ }
+
+ private DataSource mockedDatasource() throws SQLException {
+ DataSource mockedDataSource = mock(DataSource.class);
+ Connection mockedConnection = mock(Connection.class);
+ SQLException exception = new SQLException(EXCEPTION_CAUSE);
+ doThrow(exception).when(mockedConnection).prepareStatement(Mockito.anyString());
+ doReturn(mockedConnection).when(mockedDataSource).getConnection();
+ return mockedDataSource;
+ }
+
+ @Test
+ public void addingACustomerFailsWithExceptionAsFeedbackToClient() throws Exception {
+ dao.add(new Customer(2, "Bernard", "Montgomery"));
}
@Test
- public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() {
+ public void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() throws Exception {
+ dao.delete(existingCustomer);
+ }
+
+ @Test
+ public void updatingACustomerFailsWithFeedbackToTheClient() throws Exception {
final String newFirstname = "Bernard";
final String newLastname = "Montgomery";
- final Customer customer = new Customer(existingCustomer.getId(), newFirstname, newLastname);
- boolean result = dao.update(customer);
- assertTrue(result);
-
- final Customer cust = dao.getById(existingCustomer.getId());
- assertEquals(newFirstname, cust.getFirstName());
- assertEquals(newLastname, cust.getLastName());
+ dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname));
}
+ @Test
+ public void retrievingACustomerByIdReturnsNull() throws Exception {
+ dao.getById(existingCustomer.getId());
+ }
+
+ @Test
+ public void retrievingAllCustomersReturnsAnEmptyStream() throws Exception {
+ dao.getAll();
+ }
+
}
@After
@@ -130,13 +193,13 @@ public class DBCustomerDaoTest {
statement.execute("DROP TABLE CUSTOMERS");
}
}
-
- private void assertCustomerCountIs(int count) {
+
+ private void assertCustomerCountIs(int count) throws Exception {
try (Stream allCustomers = dao.getAll()) {
assertTrue(allCustomers.count() == count);
}
}
-
+
/**
* An arbitrary number which does not correspond to an active Customer id.
diff --git a/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java b/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java
index 6ebc4ccdc..ca5180e97 100644
--- a/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java
+++ b/dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java
@@ -22,104 +22,108 @@
*/
package com.iluwatar.dao;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.stream.Stream;
-import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import de.bechte.junit.runners.context.HierarchicalContextRunner;
+
+@RunWith(HierarchicalContextRunner.class)
public class InMemoryCustomerDaoTest {
private InMemoryCustomerDao dao;
- private List customers;
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
@Before
public void setUp() {
- customers = new ArrayList<>();
- customers.add(CUSTOMER);
- dao = new InMemoryCustomerDao(customers);
+ dao = new InMemoryCustomerDao();
+ dao.add(CUSTOMER);
+ }
+
+ public class NonExistantCustomer {
+
+ @Test
+ public void addingShouldResultInSuccess() throws Exception {
+ try (Stream allCustomers = dao.getAll()) {
+ assumeTrue(allCustomers.count() == 1);
+ }
+
+ final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
+ boolean result = dao.add(nonExistingCustomer);
+ assertTrue(result);
+
+ assertCustomerCountIs(2);
+ assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId()));
+ }
+
+ @Test
+ public void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
+ final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
+ boolean result = dao.delete(nonExistingCustomer);
+
+ assertFalse(result);
+ assertCustomerCountIs(1);
+ }
+
+ @Test
+ public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
+ final int nonExistingId = getNonExistingCustomerId();
+ final String newFirstname = "Douglas";
+ final String newLastname = "MacArthur";
+ final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
+ boolean result = dao.update(customer);
+
+ assertFalse(result);
+ assertNull(dao.getById(nonExistingId));
+ }
+
+ @Test
+ public void retrieveShouldReturnNull() throws Exception {
+ assertNull(dao.getById(getNonExistingCustomerId()));
+ }
}
- @Test
- public void deleteExistingCustomer() {
- Assume.assumeTrue(dao.getAll().count() == 1);
+ public class ExistingCustomer {
- boolean result = dao.delete(CUSTOMER);
-
- assertTrue(result);
- assertTrue(dao.getAll().count() == 0);
- }
+ @Test
+ public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
+ boolean result = dao.add(CUSTOMER);
- @Test
- public void deleteNonExistingCustomer() {
- final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
- boolean result = dao.delete(nonExistingCustomer);
-
- assertFalse(result);
- assertEquals(1, dao.getAll().count());
- }
+ assertFalse(result);
+ assertCustomerCountIs(1);
+ assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
+ }
- @Test
- public void updateExistingCustomer() {
- final String newFirstname = "Bernard";
- final String newLastname = "Montgomery";
- final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
- boolean result = dao.update(customer);
-
- assertTrue(result);
- final Customer cust = dao.getById(CUSTOMER.getId());
- assertEquals(newFirstname, cust.getFirstName());
- assertEquals(newLastname, cust.getLastName());
- }
+ @Test
+ public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
+ boolean result = dao.delete(CUSTOMER);
- @Test
- public void updateNonExistingCustomer() {
- final int nonExistingId = getNonExistingCustomerId();
- final String newFirstname = "Douglas";
- final String newLastname = "MacArthur";
- final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
- boolean result = dao.update(customer);
-
- assertFalse(result);
- assertNull(dao.getById(nonExistingId));
- final Customer existingCustomer = dao.getById(CUSTOMER.getId());
- assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName());
- assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName());
- }
+ assertTrue(result);
+ assertCustomerCountIs(0);
+ assertNull(dao.getById(CUSTOMER.getId()));
+ }
- @Test
- public void addCustomer() {
- final Customer newCustomer = new Customer(3, "George", "Patton");
- boolean result = dao.add(newCustomer);
-
- assertTrue(result);
- assertEquals(2, dao.getAll().count());
- }
+ @Test
+ public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws Exception {
+ final String newFirstname = "Bernard";
+ final String newLastname = "Montgomery";
+ final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
+ boolean result = dao.update(customer);
- @Test
- public void addAlreadyAddedCustomer() {
- final Customer newCustomer = new Customer(3, "George", "Patton");
- dao.add(newCustomer);
-
- boolean result = dao.add(newCustomer);
- assertFalse(result);
- assertEquals(2, dao.getAll().count());
- }
+ assertTrue(result);
- @Test
- public void getExistinCustomerById() {
- assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
- }
-
- @Test
- public void getNonExistingCustomerById() {
- final int nonExistingId = getNonExistingCustomerId();
-
- assertNull(dao.getById(nonExistingId));
+ final Customer cust = dao.getById(CUSTOMER.getId());
+ assertEquals(newFirstname, cust.getFirstName());
+ assertEquals(newLastname, cust.getLastName());
+ }
}
/**
@@ -130,4 +134,10 @@ public class InMemoryCustomerDaoTest {
private int getNonExistingCustomerId() {
return 999;
}
+
+ private void assertCustomerCountIs(int count) throws Exception {
+ try (Stream allCustomers = dao.getAll()) {
+ assertTrue(allCustomers.count() == count);
+ }
+ }
}