|
| 1 | +package com.iluwatar.dao; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class CustomerDaoImplTest { |
| 14 | + |
| 15 | + private CustomerDaoImpl impl; |
| 16 | + private List<Customer> customers; |
| 17 | + private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger"); |
| 18 | + |
| 19 | + @Before |
| 20 | + public void setUp() { |
| 21 | + customers = new ArrayList<Customer>(); |
| 22 | + customers.add(CUSTOMER); |
| 23 | + impl = new CustomerDaoImpl(customers); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void deleteExistingCustomer() { |
| 28 | + assertEquals(1, impl.getAllCustomers().size()); |
| 29 | + impl.deleteCustomer(CUSTOMER); |
| 30 | + assertTrue(impl.getAllCustomers().isEmpty()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void deleteNonExistingCustomer() { |
| 35 | + final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); |
| 36 | + impl.deleteCustomer(nonExistingCustomer); |
| 37 | + assertEquals(1, impl.getAllCustomers().size()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void updateExistingCustomer() { |
| 42 | + final String newFirstname = "Bernard"; |
| 43 | + final String newLastname = "Montgomery"; |
| 44 | + final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname); |
| 45 | + impl.updateCustomer(customer); |
| 46 | + final Customer cust = impl.getCustomerById(CUSTOMER.getId()); |
| 47 | + assertEquals(newFirstname, cust.getFirstName()); |
| 48 | + assertEquals(newLastname, cust.getLastName()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void updateNonExistingCustomer() { |
| 53 | + final int nonExistingId = 999; |
| 54 | + final String newFirstname = "Douglas"; |
| 55 | + final String newLastname = "MacArthur"; |
| 56 | + final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); |
| 57 | + impl.updateCustomer(customer); |
| 58 | + assertNull(impl.getCustomerById(nonExistingId)); |
| 59 | + final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId()); |
| 60 | + assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName()); |
| 61 | + assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void addCustomer() { |
| 66 | + final Customer newCustomer = new Customer(3, "George", "Patton"); |
| 67 | + impl.addCustomer(newCustomer); |
| 68 | + assertEquals(2, impl.getAllCustomers().size()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void getExistinCustomerById() { |
| 73 | + assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId())); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void getNonExistinCustomerById() { |
| 78 | + final int nonExistingId = 999; |
| 79 | + assertNull(impl.getCustomerById(nonExistingId)); |
| 80 | + } |
| 81 | +} |
0 commit comments