Refactoring changes to DAO pattern. Renamed default dao implementation to InMemory and refined interface

This commit is contained in:
Narendra Pathai 2016-03-16 18:47:07 +05:30
parent 528d179efe
commit f6a20c7693
5 changed files with 116 additions and 70 deletions

View File

@ -51,18 +51,18 @@ public class App {
* @param args command line args. * @param args command line args.
*/ */
public static void main(final String[] args) { public static void main(final String[] args) {
final CustomerDao customerDao = new CustomerDaoImpl(generateSampleCustomers()); final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
log.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(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");
customerDao.addCustomer(customer); customerDao.add(customer);
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
customer.setFirstName("Daniel"); customer.setFirstName("Daniel");
customer.setLastName("Danielson"); customer.setLastName("Danielson");
customerDao.updateCustomer(customer); customerDao.update(customer);
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
customerDao.deleteCustomer(customer); customerDao.delete(customer);
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
} }
/** /**

View File

@ -22,7 +22,7 @@
*/ */
package com.iluwatar.dao; package com.iluwatar.dao;
import java.util.List; import java.util.stream.Stream;
/** /**
* *
@ -31,13 +31,13 @@ import java.util.List;
*/ */
public interface CustomerDao { public interface CustomerDao {
List<Customer> getAllCustomers(); Stream<Customer> getAll();
Customer getCustomerById(int id); Customer getById(int id);
void addCustomer(Customer customer); boolean add(Customer customer);
void updateCustomer(Customer customer); boolean update(Customer customer);
void deleteCustomer(Customer customer); boolean delete(Customer customer);
} }

View File

@ -0,0 +1,38 @@
package com.iluwatar.dao;
import java.util.stream.Stream;
public class DBCustomerDao implements CustomerDao {
@Override
public Stream<Customer> getAll() {
// TODO Auto-generated method stub
return null;
}
@Override
public Customer getById(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(Customer customer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean update(Customer customer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean delete(Customer customer) {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -22,7 +22,10 @@
*/ */
package com.iluwatar.dao; package com.iluwatar.dao;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/** /**
* *
@ -34,51 +37,44 @@ import java.util.List;
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc. * the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
* *
*/ */
public class CustomerDaoImpl implements CustomerDao { // TODO update the javadoc
public class InMemoryCustomerDao implements CustomerDao {
// Represents the DB structure for our example so we don't have to managed it ourselves private Map<Integer, Customer> idToCustomer = new HashMap<>();
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
private List<Customer> customers;
public CustomerDaoImpl(final List<Customer> customers) { public InMemoryCustomerDao(final List<Customer> customers) {
this.customers = customers; customers.stream()
.forEach((customer) -> idToCustomer.put(customer.getId(), customer));
} }
@Override @Override
public List<Customer> getAllCustomers() { public Stream<Customer> getAll() {
return customers; return idToCustomer.values().stream();
} }
@Override @Override
public Customer getCustomerById(final int id) { public Customer getById(final int id) {
Customer customer = null; return idToCustomer.get(id);
for (final Customer cus : getAllCustomers()) { }
if (cus.getId() == id) {
customer = cus; @Override
break; public boolean add(final Customer customer) {
} if (getById(customer.getId()) != null) {
return false;
} }
return customer;
}
@Override idToCustomer.put(customer.getId(), customer);
public void addCustomer(final Customer customer) { return true;
if (getCustomerById(customer.getId()) == null) {
customers.add(customer);
}
} }
@Override @Override
public void updateCustomer(final Customer customer) { public boolean update(final Customer customer) {
if (getAllCustomers().contains(customer)) { return idToCustomer.replace(customer.getId(), customer) != null;
final int index = getAllCustomers().indexOf(customer);
getAllCustomers().set(index, customer);
}
} }
@Override @Override
public void deleteCustomer(final Customer customer) { public boolean delete(final Customer customer) {
getAllCustomers().remove(customer); return idToCustomer.remove(customer.getId()) != null;
} }
} }

View File

@ -22,19 +22,18 @@
*/ */
package com.iluwatar.dao; package com.iluwatar.dao;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class CustomerDaoImplTest { public class InMemoryCustomerDaoTest {
private CustomerDaoImpl impl; private InMemoryCustomerDao dao;
private List<Customer> customers; private List<Customer> customers;
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger"); private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
@ -42,21 +41,26 @@ public class CustomerDaoImplTest {
public void setUp() { public void setUp() {
customers = new ArrayList<>(); customers = new ArrayList<>();
customers.add(CUSTOMER); customers.add(CUSTOMER);
impl = new CustomerDaoImpl(customers); dao = new InMemoryCustomerDao(customers);
} }
@Test @Test
public void deleteExistingCustomer() { public void deleteExistingCustomer() {
assertEquals(1, impl.getAllCustomers().size()); Assume.assumeTrue(dao.getAll().count() == 1);
impl.deleteCustomer(CUSTOMER);
assertTrue(impl.getAllCustomers().isEmpty()); boolean result = dao.delete(CUSTOMER);
assertTrue(result);
assertTrue(dao.getAll().count() == 0);
} }
@Test @Test
public void deleteNonExistingCustomer() { public void deleteNonExistingCustomer() {
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
impl.deleteCustomer(nonExistingCustomer); boolean result = dao.delete(nonExistingCustomer);
assertEquals(1, impl.getAllCustomers().size());
assertFalse(result);
assertEquals(1, dao.getAll().count());
} }
@Test @Test
@ -64,8 +68,10 @@ public class CustomerDaoImplTest {
final String newFirstname = "Bernard"; final String newFirstname = "Bernard";
final String newLastname = "Montgomery"; final String newLastname = "Montgomery";
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname); final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
impl.updateCustomer(customer); boolean result = dao.update(customer);
final Customer cust = impl.getCustomerById(CUSTOMER.getId());
assertTrue(result);
final Customer cust = dao.getById(CUSTOMER.getId());
assertEquals(newFirstname, cust.getFirstName()); assertEquals(newFirstname, cust.getFirstName());
assertEquals(newLastname, cust.getLastName()); assertEquals(newLastname, cust.getLastName());
} }
@ -76,9 +82,11 @@ public class CustomerDaoImplTest {
final String newFirstname = "Douglas"; final String newFirstname = "Douglas";
final String newLastname = "MacArthur"; final String newLastname = "MacArthur";
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
impl.updateCustomer(customer); boolean result = dao.update(customer);
assertNull(impl.getCustomerById(nonExistingId));
final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId()); assertFalse(result);
assertNull(dao.getById(nonExistingId));
final Customer existingCustomer = dao.getById(CUSTOMER.getId());
assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName()); assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName());
assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName()); assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName());
} }
@ -86,28 +94,32 @@ public class CustomerDaoImplTest {
@Test @Test
public void addCustomer() { public void addCustomer() {
final Customer newCustomer = new Customer(3, "George", "Patton"); final Customer newCustomer = new Customer(3, "George", "Patton");
impl.addCustomer(newCustomer); boolean result = dao.add(newCustomer);
assertEquals(2, impl.getAllCustomers().size());
assertTrue(result);
assertEquals(2, dao.getAll().count());
} }
@Test @Test
public void addAlreadyAddedCustomer() { public void addAlreadyAddedCustomer() {
final Customer newCustomer = new Customer(3, "George", "Patton"); final Customer newCustomer = new Customer(3, "George", "Patton");
impl.addCustomer(newCustomer); dao.add(newCustomer);
assertEquals(2, impl.getAllCustomers().size());
impl.addCustomer(newCustomer); boolean result = dao.add(newCustomer);
assertEquals(2, impl.getAllCustomers().size()); assertFalse(result);
assertEquals(2, dao.getAll().count());
} }
@Test @Test
public void getExistinCustomerById() { public void getExistinCustomerById() {
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId())); assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
} }
@Test @Test
public void getNonExistinCustomerById() { public void getNonExistingCustomerById() {
final int nonExistingId = getNonExistingCustomerId(); final int nonExistingId = getNonExistingCustomerId();
assertNull(impl.getCustomerById(nonExistingId));
assertNull(dao.getById(nonExistingId));
} }
/** /**