Work on #404, javadocs and test cases for DB and in memory dao.
This commit is contained in:
parent
448d855809
commit
fa077c8be9
@ -52,6 +52,10 @@
|
|||||||
<groupId>de.bechte.junit</groupId>
|
<groupId>de.bechte.junit</groupId>
|
||||||
<artifactId>junit-hierarchicalcontextrunner</artifactId>
|
<artifactId>junit-hierarchicalcontextrunner</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -49,9 +49,11 @@ public class App {
|
|||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args.
|
* @param args command line args.
|
||||||
|
* @throws Exception if any error occurs.
|
||||||
*/
|
*/
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) throws Exception {
|
||||||
final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
|
final CustomerDao customerDao = new InMemoryCustomerDao();
|
||||||
|
addCustomers(customerDao);
|
||||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||||
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
|
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
|
||||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||||
@ -65,6 +67,12 @@ public class App {
|
|||||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void addCustomers(CustomerDao customerDao) throws Exception {
|
||||||
|
for (Customer customer : generateSampleCustomers()) {
|
||||||
|
customerDao.add(customer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate customers.
|
* Generate customers.
|
||||||
*
|
*
|
||||||
|
@ -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.
|
||||||
|
* <br/><br/>
|
||||||
|
* 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 {
|
public interface CustomerDao {
|
||||||
|
|
||||||
Stream<Customer> 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<Customer> getAll() throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id unique identifier of the customer.
|
||||||
|
* @return customer with unique identifier <code>id</code> 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);
|
/**
|
||||||
|
* @param customer the customer to be deleted.
|
||||||
boolean delete(Customer customer);
|
* @return true if customer exists and is successfully deleted, false otherwise.
|
||||||
|
* @throws Exception if any error occurs.
|
||||||
|
*/
|
||||||
|
boolean delete(Customer customer) throws Exception;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
package com.iluwatar.dao;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@ -11,16 +32,22 @@ import java.util.function.Consumer;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class DBCustomerDao implements CustomerDao {
|
public class DBCustomerDao implements CustomerDao {
|
||||||
|
|
||||||
private String dbUrl;
|
private final DataSource dataSource;
|
||||||
|
|
||||||
public DBCustomerDao(String dbUrl) {
|
public DBCustomerDao(DataSource dataSource) {
|
||||||
this.dbUrl = dbUrl;
|
this.dataSource = dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<Customer> getAll() {
|
public Stream<Customer> getAll() throws Exception {
|
||||||
|
|
||||||
Connection connection;
|
Connection connection;
|
||||||
try {
|
try {
|
||||||
@ -41,14 +68,16 @@ public class DBCustomerDao implements CustomerDao {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}}, false).onClose(() -> mutedClose(connection));
|
}}, false).onClose(() -> mutedClose(connection));
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
throw new Exception(e.getMessage(), e);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return dataSource.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
private void mutedClose(Connection connection) {
|
private void mutedClose(Connection connection) {
|
||||||
try {
|
try {
|
||||||
connection.close();
|
connection.close();
|
||||||
@ -64,22 +93,23 @@ public class DBCustomerDao implements CustomerDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Customer getById(int id) {
|
public Customer getById(int id) throws Exception {
|
||||||
try (Connection connection = getConnection();
|
try (Connection connection = getConnection();
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||||
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 createCustomer(resultSet);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
throw new Exception(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean add(Customer customer) {
|
public boolean add(Customer customer) throws Exception {
|
||||||
if (getById(customer.getId()) != null) {
|
if (getById(customer.getId()) != null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -92,13 +122,12 @@ public class DBCustomerDao implements CustomerDao {
|
|||||||
statement.execute();
|
statement.execute();
|
||||||
return true;
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
throw new Exception(ex.getMessage(), ex);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean update(Customer customer) {
|
public boolean update(Customer customer) throws Exception {
|
||||||
try (Connection connection = getConnection();
|
try (Connection connection = getConnection();
|
||||||
PreparedStatement statement = connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
PreparedStatement statement = connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||||
statement.setString(1, customer.getFirstName());
|
statement.setString(1, customer.getFirstName());
|
||||||
@ -106,25 +135,18 @@ public class DBCustomerDao implements CustomerDao {
|
|||||||
statement.setInt(3, customer.getId());
|
statement.setInt(3, customer.getId());
|
||||||
return statement.executeUpdate() > 0;
|
return statement.executeUpdate() > 0;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
throw new Exception(ex.getMessage(), ex);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean delete(Customer customer) {
|
public boolean delete(Customer customer) throws Exception {
|
||||||
try (Connection connection = getConnection();
|
try (Connection connection = getConnection();
|
||||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
PreparedStatement statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||||
statement.setInt(1, customer.getId());
|
statement.setInt(1, customer.getId());
|
||||||
return statement.executeUpdate() > 0;
|
return statement.executeUpdate() > 0;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
throw new Exception(ex.getMessage(), ex);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection getConnection() throws SQLException {
|
|
||||||
return DriverManager.getConnection(dbUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,30 +23,22 @@
|
|||||||
package com.iluwatar.dao;
|
package com.iluwatar.dao;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory
|
||||||
* The data access object (DAO) is an object that provides an abstract interface to some type of
|
* and data is lost when the application exits.
|
||||||
* database or other persistence mechanism. By mapping application calls to the persistence layer,
|
* <br/>
|
||||||
* DAO provide some specific data operations without exposing details of the database. This
|
* This implementation is useful as temporary database or for testing.
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
// TODO update the javadoc
|
|
||||||
public class InMemoryCustomerDao implements CustomerDao {
|
public class InMemoryCustomerDao implements CustomerDao {
|
||||||
|
|
||||||
private Map<Integer, Customer> idToCustomer = new HashMap<>();
|
private Map<Integer, Customer> idToCustomer = new HashMap<>();
|
||||||
|
|
||||||
public InMemoryCustomerDao(final List<Customer> customers) {
|
/**
|
||||||
customers.stream()
|
* An eagerly evaluated stream of customers stored in memory.
|
||||||
.forEach((customer) -> idToCustomer.put(customer.getId(), customer));
|
*/
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<Customer> getAll() {
|
public Stream<Customer> getAll() {
|
||||||
return idToCustomer.values().stream();
|
return idToCustomer.values().stream();
|
||||||
@ -67,7 +59,6 @@ public class InMemoryCustomerDao implements CustomerDao {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean update(final Customer customer) {
|
public boolean update(final Customer customer) {
|
||||||
return idToCustomer.replace(customer.getId(), customer) != null;
|
return idToCustomer.replace(customer.getId(), customer) != null;
|
||||||
|
@ -24,14 +24,12 @@ package com.iluwatar.dao;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that DAO example runs without errors.
|
* Tests that DAO example runs without errors.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() throws Exception {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,9 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertNull;
|
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 static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.doThrow;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
@ -12,16 +15,22 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.h2.jdbcx.JdbcDataSource;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import de.bechte.junit.runners.context.HierarchicalContextRunner;
|
import de.bechte.junit.runners.context.HierarchicalContextRunner;
|
||||||
|
|
||||||
@RunWith(HierarchicalContextRunner.class)
|
@RunWith(HierarchicalContextRunner.class)
|
||||||
public class DBCustomerDaoTest {
|
public class DBCustomerDaoTest {
|
||||||
|
|
||||||
private static final String DB_URL = "jdbc:h2:~/dao:customerdb";
|
private static final String DB_URL = "jdbc:h2:~/dao:customerdb";
|
||||||
private DBCustomerDao dao;
|
private DBCustomerDao dao;
|
||||||
private Customer existingCustomer = new Customer(1, "Freddy", "Krueger");
|
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))");
|
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), LNAME VARCHAR(100))");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
public class ConnectionSuccess {
|
||||||
public void setUp() {
|
|
||||||
dao = new DBCustomerDao(DB_URL);
|
@Before
|
||||||
boolean result = dao.add(existingCustomer);
|
public void setUp() throws Exception {
|
||||||
assumeTrue(result);
|
JdbcDataSource dataSource = new JdbcDataSource();
|
||||||
}
|
dataSource.setURL(DB_URL);
|
||||||
|
dao = new DBCustomerDao(dataSource);
|
||||||
public class NonExistantCustomer {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void addingShouldResultInSuccess() {
|
|
||||||
try (Stream<Customer> 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");
|
|
||||||
|
|
||||||
boolean result = dao.add(existingCustomer);
|
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);
|
assertTrue(result);
|
||||||
assertCustomerCountIs(0);
|
}
|
||||||
assertNull(dao.getById(existingCustomer.getId()));
|
|
||||||
|
public class NonExistantCustomer {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addingShouldResultInSuccess() throws Exception {
|
||||||
|
try (Stream<Customer> 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
|
@Test
|
||||||
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() {
|
public void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() throws Exception {
|
||||||
|
dao.delete(existingCustomer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updatingACustomerFailsWithFeedbackToTheClient() throws Exception {
|
||||||
final String newFirstname = "Bernard";
|
final String newFirstname = "Bernard";
|
||||||
final String newLastname = "Montgomery";
|
final String newLastname = "Montgomery";
|
||||||
final Customer customer = new Customer(existingCustomer.getId(), newFirstname, newLastname);
|
|
||||||
boolean result = dao.update(customer);
|
|
||||||
|
|
||||||
assertTrue(result);
|
dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname));
|
||||||
|
|
||||||
final Customer cust = dao.getById(existingCustomer.getId());
|
|
||||||
assertEquals(newFirstname, cust.getFirstName());
|
|
||||||
assertEquals(newLastname, cust.getLastName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void retrievingACustomerByIdReturnsNull() throws Exception {
|
||||||
|
dao.getById(existingCustomer.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void retrievingAllCustomersReturnsAnEmptyStream() throws Exception {
|
||||||
|
dao.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -130,13 +193,13 @@ public class DBCustomerDaoTest {
|
|||||||
statement.execute("DROP TABLE CUSTOMERS");
|
statement.execute("DROP TABLE CUSTOMERS");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertCustomerCountIs(int count) {
|
private void assertCustomerCountIs(int count) throws Exception {
|
||||||
try (Stream<Customer> allCustomers = dao.getAll()) {
|
try (Stream<Customer> allCustomers = dao.getAll()) {
|
||||||
assertTrue(allCustomers.count() == count);
|
assertTrue(allCustomers.count() == count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An arbitrary number which does not correspond to an active Customer id.
|
* An arbitrary number which does not correspond to an active Customer id.
|
||||||
|
@ -22,104 +22,108 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.dao;
|
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.stream.Stream;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Assume;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import de.bechte.junit.runners.context.HierarchicalContextRunner;
|
||||||
|
|
||||||
|
@RunWith(HierarchicalContextRunner.class)
|
||||||
public class InMemoryCustomerDaoTest {
|
public class InMemoryCustomerDaoTest {
|
||||||
|
|
||||||
private InMemoryCustomerDao dao;
|
private InMemoryCustomerDao dao;
|
||||||
private List<Customer> customers;
|
|
||||||
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
|
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
customers = new ArrayList<>();
|
dao = new InMemoryCustomerDao();
|
||||||
customers.add(CUSTOMER);
|
dao.add(CUSTOMER);
|
||||||
dao = new InMemoryCustomerDao(customers);
|
}
|
||||||
|
|
||||||
|
public class NonExistantCustomer {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addingShouldResultInSuccess() throws Exception {
|
||||||
|
try (Stream<Customer> 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 class ExistingCustomer {
|
||||||
public void deleteExistingCustomer() {
|
|
||||||
Assume.assumeTrue(dao.getAll().count() == 1);
|
|
||||||
|
|
||||||
boolean result = dao.delete(CUSTOMER);
|
@Test
|
||||||
|
public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
|
||||||
assertTrue(result);
|
boolean result = dao.add(CUSTOMER);
|
||||||
assertTrue(dao.getAll().count() == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
assertFalse(result);
|
||||||
public void deleteNonExistingCustomer() {
|
assertCustomerCountIs(1);
|
||||||
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
|
||||||
boolean result = dao.delete(nonExistingCustomer);
|
}
|
||||||
|
|
||||||
assertFalse(result);
|
|
||||||
assertEquals(1, dao.getAll().count());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateExistingCustomer() {
|
public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
|
||||||
final String newFirstname = "Bernard";
|
boolean result = dao.delete(CUSTOMER);
|
||||||
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
|
assertTrue(result);
|
||||||
public void updateNonExistingCustomer() {
|
assertCustomerCountIs(0);
|
||||||
final int nonExistingId = getNonExistingCustomerId();
|
assertNull(dao.getById(CUSTOMER.getId()));
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addCustomer() {
|
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws Exception {
|
||||||
final Customer newCustomer = new Customer(3, "George", "Patton");
|
final String newFirstname = "Bernard";
|
||||||
boolean result = dao.add(newCustomer);
|
final String newLastname = "Montgomery";
|
||||||
|
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
|
||||||
assertTrue(result);
|
boolean result = dao.update(customer);
|
||||||
assertEquals(2, dao.getAll().count());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
assertTrue(result);
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
final Customer cust = dao.getById(CUSTOMER.getId());
|
||||||
public void getExistinCustomerById() {
|
assertEquals(newFirstname, cust.getFirstName());
|
||||||
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
|
assertEquals(newLastname, cust.getLastName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getNonExistingCustomerById() {
|
|
||||||
final int nonExistingId = getNonExistingCustomerId();
|
|
||||||
|
|
||||||
assertNull(dao.getById(nonExistingId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,4 +134,10 @@ public class InMemoryCustomerDaoTest {
|
|||||||
private int getNonExistingCustomerId() {
|
private int getNonExistingCustomerId() {
|
||||||
return 999;
|
return 999;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertCustomerCountIs(int count) throws Exception {
|
||||||
|
try (Stream<Customer> allCustomers = dao.getAll()) {
|
||||||
|
assertTrue(allCustomers.count() == count);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user