|
| 1 | +package com.iluwatar.dao; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.junit.Assume.assumeTrue; |
| 8 | + |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.DriverManager; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import org.junit.After; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | + |
| 20 | +import de.bechte.junit.runners.context.HierarchicalContextRunner; |
| 21 | + |
| 22 | +@RunWith(HierarchicalContextRunner.class) |
| 23 | +public class DBCustomerDaoTest { |
| 24 | + |
| 25 | + private static final String DB_URL = "jdbc:h2:~/dao:customerdb"; |
| 26 | + private DBCustomerDao dao; |
| 27 | + private Customer existingCustomer = new Customer(1, "Freddy", "Krueger"); |
| 28 | + |
| 29 | + @Before |
| 30 | + public void createSchema() throws SQLException { |
| 31 | + try (Connection connection = DriverManager.getConnection(DB_URL); |
| 32 | + Statement statement = connection.createStatement()) { |
| 33 | + statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), LNAME VARCHAR(100))"); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() { |
| 39 | + dao = new DBCustomerDao(DB_URL); |
| 40 | + boolean result = dao.add(existingCustomer); |
| 41 | + assumeTrue(result); |
| 42 | + } |
| 43 | + |
| 44 | + public class NonExistantCustomer { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void addingShouldResultInSuccess() { |
| 48 | + try (Stream<Customer> allCustomers = dao.getAll()) { |
| 49 | + assumeTrue(allCustomers.count() == 1); |
| 50 | + } |
| 51 | + |
| 52 | + final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); |
| 53 | + boolean result = dao.add(nonExistingCustomer); |
| 54 | + assertTrue(result); |
| 55 | + |
| 56 | + assertCustomerCountIs(2); |
| 57 | + assertEquals(nonExistingCustomer, dao.getById(nonExistingCustomer.getId())); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void deletionShouldBeFailureAndNotAffectExistingCustomers() { |
| 62 | + final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); |
| 63 | + boolean result = dao.delete(nonExistingCustomer); |
| 64 | + |
| 65 | + assertFalse(result); |
| 66 | + assertCustomerCountIs(1); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void updationShouldBeFailureAndNotAffectExistingCustomers() { |
| 71 | + final int nonExistingId = getNonExistingCustomerId(); |
| 72 | + final String newFirstname = "Douglas"; |
| 73 | + final String newLastname = "MacArthur"; |
| 74 | + final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); |
| 75 | + boolean result = dao.update(customer); |
| 76 | + |
| 77 | + assertFalse(result); |
| 78 | + assertNull(dao.getById(nonExistingId)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void retrieveShouldReturnNull() { |
| 83 | + assertNull(dao.getById(getNonExistingCustomerId())); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + public class ExistingCustomer { |
| 89 | + |
| 90 | + @Test |
| 91 | + public void addingShouldResultInFailureAndNotAffectExistingCustomers() { |
| 92 | + Customer existingCustomer = new Customer(1, "Freddy", "Krueger"); |
| 93 | + |
| 94 | + boolean result = dao.add(existingCustomer); |
| 95 | + |
| 96 | + assertFalse(result); |
| 97 | + assertCustomerCountIs(1); |
| 98 | + assertEquals(existingCustomer, dao.getById(existingCustomer.getId())); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() { |
| 103 | + boolean result = dao.delete(existingCustomer); |
| 104 | + |
| 105 | + assertTrue(result); |
| 106 | + assertCustomerCountIs(0); |
| 107 | + assertNull(dao.getById(existingCustomer.getId())); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() { |
| 112 | + final String newFirstname = "Bernard"; |
| 113 | + final String newLastname = "Montgomery"; |
| 114 | + final Customer customer = new Customer(existingCustomer.getId(), newFirstname, newLastname); |
| 115 | + boolean result = dao.update(customer); |
| 116 | + |
| 117 | + assertTrue(result); |
| 118 | + |
| 119 | + final Customer cust = dao.getById(existingCustomer.getId()); |
| 120 | + assertEquals(newFirstname, cust.getFirstName()); |
| 121 | + assertEquals(newLastname, cust.getLastName()); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + @After |
| 127 | + public void deleteSchema() throws SQLException { |
| 128 | + try (Connection connection = DriverManager.getConnection(DB_URL); |
| 129 | + Statement statement = connection.createStatement()) { |
| 130 | + statement.execute("DROP TABLE CUSTOMERS"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void assertCustomerCountIs(int count) { |
| 135 | + try (Stream<Customer> allCustomers = dao.getAll()) { |
| 136 | + assertTrue(allCustomers.count() == count); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * An arbitrary number which does not correspond to an active Customer id. |
| 143 | + * |
| 144 | + * @return an int of a customer id which doesn't exist |
| 145 | + */ |
| 146 | + private int getNonExistingCustomerId() { |
| 147 | + return 999; |
| 148 | + } |
| 149 | +} |
0 commit comments