Refactoring changes to DAO pattern. Renamed default dao implementation to InMemory and refined interface
This commit is contained in:
parent
528d179efe
commit
f6a20c7693
@ -51,18 +51,18 @@ public class App {
|
||||
* @param args command line args.
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
final CustomerDao customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
log.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
|
||||
final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
|
||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.add(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.deleteCustomer(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.update(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
customerDao.delete(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,7 +22,7 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -31,13 +31,13 @@ import java.util.List;
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
38
dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
Normal file
38
dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -22,7 +22,10 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.HashMap;
|
||||
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.
|
||||
*
|
||||
*/
|
||||
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
|
||||
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
|
||||
private List<Customer> customers;
|
||||
private Map<Integer, Customer> idToCustomer = new HashMap<>();
|
||||
|
||||
public CustomerDaoImpl(final List<Customer> customers) {
|
||||
this.customers = customers;
|
||||
public InMemoryCustomerDao(final List<Customer> customers) {
|
||||
customers.stream()
|
||||
.forEach((customer) -> idToCustomer.put(customer.getId(), customer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> getAllCustomers() {
|
||||
return customers;
|
||||
public Stream<Customer> getAll() {
|
||||
return idToCustomer.values().stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCustomerById(final int id) {
|
||||
Customer customer = null;
|
||||
for (final Customer cus : getAllCustomers()) {
|
||||
if (cus.getId() == id) {
|
||||
customer = cus;
|
||||
break;
|
||||
}
|
||||
public Customer getById(final int id) {
|
||||
return idToCustomer.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final Customer customer) {
|
||||
if (getById(customer.getId()) != null) {
|
||||
return false;
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCustomer(final Customer customer) {
|
||||
if (getCustomerById(customer.getId()) == null) {
|
||||
customers.add(customer);
|
||||
}
|
||||
idToCustomer.put(customer.getId(), customer);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateCustomer(final Customer customer) {
|
||||
if (getAllCustomers().contains(customer)) {
|
||||
final int index = getAllCustomers().indexOf(customer);
|
||||
getAllCustomers().set(index, customer);
|
||||
}
|
||||
public boolean update(final Customer customer) {
|
||||
return idToCustomer.replace(customer.getId(), customer) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteCustomer(final Customer customer) {
|
||||
getAllCustomers().remove(customer);
|
||||
public boolean delete(final Customer customer) {
|
||||
return idToCustomer.remove(customer.getId()) != null;
|
||||
}
|
||||
}
|
@ -22,19 +22,18 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assume;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CustomerDaoImplTest {
|
||||
public class InMemoryCustomerDaoTest {
|
||||
|
||||
private CustomerDaoImpl impl;
|
||||
private InMemoryCustomerDao dao;
|
||||
private List<Customer> customers;
|
||||
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
|
||||
|
||||
@ -42,21 +41,26 @@ public class CustomerDaoImplTest {
|
||||
public void setUp() {
|
||||
customers = new ArrayList<>();
|
||||
customers.add(CUSTOMER);
|
||||
impl = new CustomerDaoImpl(customers);
|
||||
dao = new InMemoryCustomerDao(customers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteExistingCustomer() {
|
||||
assertEquals(1, impl.getAllCustomers().size());
|
||||
impl.deleteCustomer(CUSTOMER);
|
||||
assertTrue(impl.getAllCustomers().isEmpty());
|
||||
Assume.assumeTrue(dao.getAll().count() == 1);
|
||||
|
||||
boolean result = dao.delete(CUSTOMER);
|
||||
|
||||
assertTrue(result);
|
||||
assertTrue(dao.getAll().count() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteNonExistingCustomer() {
|
||||
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
impl.deleteCustomer(nonExistingCustomer);
|
||||
assertEquals(1, impl.getAllCustomers().size());
|
||||
boolean result = dao.delete(nonExistingCustomer);
|
||||
|
||||
assertFalse(result);
|
||||
assertEquals(1, dao.getAll().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -64,8 +68,10 @@ public class CustomerDaoImplTest {
|
||||
final String newFirstname = "Bernard";
|
||||
final String newLastname = "Montgomery";
|
||||
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
|
||||
impl.updateCustomer(customer);
|
||||
final Customer cust = impl.getCustomerById(CUSTOMER.getId());
|
||||
boolean result = dao.update(customer);
|
||||
|
||||
assertTrue(result);
|
||||
final Customer cust = dao.getById(CUSTOMER.getId());
|
||||
assertEquals(newFirstname, cust.getFirstName());
|
||||
assertEquals(newLastname, cust.getLastName());
|
||||
}
|
||||
@ -76,9 +82,11 @@ public class CustomerDaoImplTest {
|
||||
final String newFirstname = "Douglas";
|
||||
final String newLastname = "MacArthur";
|
||||
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
|
||||
impl.updateCustomer(customer);
|
||||
assertNull(impl.getCustomerById(nonExistingId));
|
||||
final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId());
|
||||
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());
|
||||
}
|
||||
@ -86,28 +94,32 @@ public class CustomerDaoImplTest {
|
||||
@Test
|
||||
public void addCustomer() {
|
||||
final Customer newCustomer = new Customer(3, "George", "Patton");
|
||||
impl.addCustomer(newCustomer);
|
||||
assertEquals(2, impl.getAllCustomers().size());
|
||||
boolean result = dao.add(newCustomer);
|
||||
|
||||
assertTrue(result);
|
||||
assertEquals(2, dao.getAll().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addAlreadyAddedCustomer() {
|
||||
final Customer newCustomer = new Customer(3, "George", "Patton");
|
||||
impl.addCustomer(newCustomer);
|
||||
assertEquals(2, impl.getAllCustomers().size());
|
||||
impl.addCustomer(newCustomer);
|
||||
assertEquals(2, impl.getAllCustomers().size());
|
||||
dao.add(newCustomer);
|
||||
|
||||
boolean result = dao.add(newCustomer);
|
||||
assertFalse(result);
|
||||
assertEquals(2, dao.getAll().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExistinCustomerById() {
|
||||
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId()));
|
||||
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNonExistinCustomerById() {
|
||||
public void getNonExistingCustomerById() {
|
||||
final int nonExistingId = getNonExistingCustomerId();
|
||||
assertNull(impl.getCustomerById(nonExistingId));
|
||||
|
||||
assertNull(dao.getById(nonExistingId));
|
||||
}
|
||||
|
||||
/**
|
Loading…
x
Reference in New Issue
Block a user