|
| 1 | +package com.iluwatar.daofactory; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.mockito.Mockito.doReturn; |
| 8 | +import static org.mockito.Mockito.doThrow; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | + |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.DriverManager; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.util.List; |
| 15 | +import javax.sql.DataSource; |
| 16 | +import org.h2.jdbcx.JdbcDataSource; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Nested; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.Mockito; |
| 22 | + |
| 23 | +/** Tests {@link H2CustomerDAO} */ |
| 24 | +public class H2CustomerDAOTest { |
| 25 | + private final String DB_URL = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"; |
| 26 | + private final String USER = "sa"; |
| 27 | + private final String PASS = ""; |
| 28 | + private final String CREATE_SCHEMA = |
| 29 | + "CREATE TABLE IF NOT EXISTS customer (id BIGINT PRIMARY KEY, name VARCHAR(255))"; |
| 30 | + private final String DROP_SCHEMA = "DROP TABLE IF EXISTS customer"; |
| 31 | + private final Customer<Long> existingCustomer = new Customer<>(1L, "Nguyen"); |
| 32 | + private H2CustomerDAO h2CustomerDAO; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void createSchema() throws SQLException { |
| 36 | + try (var connection = DriverManager.getConnection(DB_URL, USER, PASS); |
| 37 | + var statement = connection.createStatement()) { |
| 38 | + statement.execute(CREATE_SCHEMA); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + void deleteSchema() throws SQLException { |
| 44 | + try (var connection = DriverManager.getConnection(DB_URL, USER, PASS); |
| 45 | + var statement = connection.createStatement()) { |
| 46 | + statement.execute(DROP_SCHEMA); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** Class test for scenario connect with datasource succeed */ |
| 51 | + @Nested |
| 52 | + class ConnectionSucceed { |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void setUp() { |
| 56 | + var dataSource = new JdbcDataSource(); |
| 57 | + dataSource.setURL(DB_URL); |
| 58 | + dataSource.setUser(USER); |
| 59 | + dataSource.setPassword(PASS); |
| 60 | + h2CustomerDAO = new H2CustomerDAO(dataSource); |
| 61 | + assertDoesNotThrow(() -> h2CustomerDAO.save(existingCustomer)); |
| 62 | + var customer = h2CustomerDAO.findById(existingCustomer.getId()); |
| 63 | + assertTrue(customer.isPresent()); |
| 64 | + assertEquals(customer.get().getName(), existingCustomer.getName()); |
| 65 | + assertEquals(customer.get().getId(), existingCustomer.getId()); |
| 66 | + } |
| 67 | + |
| 68 | + @Nested |
| 69 | + class SaveCustomer { |
| 70 | + @Test |
| 71 | + void givenValidCustomer_whenSaveCustomer_thenAddSucceed() { |
| 72 | + var customer = new Customer<>(2L, "Duc"); |
| 73 | + assertDoesNotThrow(() -> h2CustomerDAO.save(customer)); |
| 74 | + var customerInDb = h2CustomerDAO.findById(customer.getId()); |
| 75 | + assertTrue(customerInDb.isPresent()); |
| 76 | + assertEquals(customerInDb.get().getName(), customer.getName()); |
| 77 | + assertEquals(customerInDb.get().getId(), customer.getId()); |
| 78 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 79 | + assertEquals(2, customers.size()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void givenIdCustomerDuplicated_whenSaveCustomer_thenThrowException() { |
| 84 | + var customer = new Customer<>(existingCustomer.getId(), "Duc"); |
| 85 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.save(customer)); |
| 86 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 87 | + assertEquals(1, customers.size()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Nested |
| 92 | + class UpdateCustomer { |
| 93 | + @Test |
| 94 | + void givenValidCustomer_whenUpdateCustomer_thenUpdateSucceed() { |
| 95 | + var customerUpdate = new Customer<>(existingCustomer.getId(), "Duc"); |
| 96 | + assertDoesNotThrow(() -> h2CustomerDAO.update(customerUpdate)); |
| 97 | + var customerInDb = h2CustomerDAO.findById(customerUpdate.getId()); |
| 98 | + assertTrue(customerInDb.isPresent()); |
| 99 | + assertEquals(customerInDb.get().getName(), customerUpdate.getName()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void givenIdCustomerNotExist_whenUpdateCustomer_thenThrowException() { |
| 104 | + var customerUpdate = new Customer<>(100L, "Duc"); |
| 105 | + var customerInDb = h2CustomerDAO.findById(customerUpdate.getId()); |
| 106 | + assertTrue(customerInDb.isEmpty()); |
| 107 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.update(customerUpdate)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void givenNull_whenUpdateCustomer_thenThrowException() { |
| 112 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.update(null)); |
| 113 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 114 | + assertEquals(1, customers.size()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Nested |
| 119 | + class DeleteCustomer { |
| 120 | + @Test |
| 121 | + void givenValidId_whenDeleteCustomer_thenDeleteSucceed() { |
| 122 | + assertDoesNotThrow(() -> h2CustomerDAO.delete(existingCustomer.getId())); |
| 123 | + var customerInDb = h2CustomerDAO.findById(existingCustomer.getId()); |
| 124 | + assertTrue(customerInDb.isEmpty()); |
| 125 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 126 | + assertEquals(0, customers.size()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void givenIdCustomerNotExist_whenDeleteCustomer_thenThrowException() { |
| 131 | + var customerInDb = h2CustomerDAO.findById(100L); |
| 132 | + assertTrue(customerInDb.isEmpty()); |
| 133 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.delete(100L)); |
| 134 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 135 | + assertEquals(1, customers.size()); |
| 136 | + assertEquals(existingCustomer.getName(), customers.get(0).getName()); |
| 137 | + assertEquals(existingCustomer.getId(), customers.get(0).getId()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void givenNull_whenDeleteCustomer_thenThrowException() { |
| 142 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.delete(null)); |
| 143 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 144 | + assertEquals(1, customers.size()); |
| 145 | + assertEquals(existingCustomer.getName(), customers.get(0).getName()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Nested |
| 150 | + class FindAllCustomers { |
| 151 | + @Test |
| 152 | + void givenNonCustomerInDb_whenFindAllCustomer_thenReturnEmptyList() { |
| 153 | + assertDoesNotThrow(() -> h2CustomerDAO.delete(existingCustomer.getId())); |
| 154 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 155 | + assertEquals(0, customers.size()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void givenCustomerExistInDb_whenFindAllCustomer_thenReturnCustomers() { |
| 160 | + List<Customer<Long>> customers = h2CustomerDAO.findAll(); |
| 161 | + assertEquals(1, customers.size()); |
| 162 | + assertEquals(existingCustomer.getName(), customers.get(0).getName()); |
| 163 | + assertEquals(existingCustomer.getId(), customers.get(0).getId()); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Nested |
| 168 | + class FindCustomerById { |
| 169 | + @Test |
| 170 | + void givenValidId_whenFindById_thenReturnCustomer() { |
| 171 | + var customerInDb = h2CustomerDAO.findById(existingCustomer.getId()); |
| 172 | + assertTrue(customerInDb.isPresent()); |
| 173 | + assertEquals(existingCustomer.getName(), customerInDb.get().getName()); |
| 174 | + assertEquals(existingCustomer.getId(), customerInDb.get().getId()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void givenIdCustomerNotExist_whenFindById_thenReturnEmpty() { |
| 179 | + var customerNotExist = h2CustomerDAO.findById(100L); |
| 180 | + assertTrue(customerNotExist.isEmpty()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + void givenNull_whenFindById_thenThrowException() { |
| 185 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.findById(null)); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Nested |
| 190 | + class CreateSchema { |
| 191 | + @Test |
| 192 | + void whenCreateSchema_thenNotThrowException() { |
| 193 | + assertDoesNotThrow(() -> h2CustomerDAO.createSchema()); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Nested |
| 198 | + class DeleteSchema { |
| 199 | + @Test |
| 200 | + void whenDeleteSchema_thenNotThrowException() { |
| 201 | + assertDoesNotThrow(() -> h2CustomerDAO.deleteSchema()); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /** Class test with scenario connect with data source failed */ |
| 207 | + @Nested |
| 208 | + class ConnectionFailed { |
| 209 | + private final String EXCEPTION_CAUSE = "Connection not available"; |
| 210 | + |
| 211 | + @BeforeEach |
| 212 | + void setUp() throws SQLException { |
| 213 | + h2CustomerDAO = new H2CustomerDAO(mockedDataSource()); |
| 214 | + } |
| 215 | + |
| 216 | + private DataSource mockedDataSource() throws SQLException { |
| 217 | + var mockedDataSource = mock(DataSource.class); |
| 218 | + var mockedConnection = mock(Connection.class); |
| 219 | + var exception = new SQLException(EXCEPTION_CAUSE); |
| 220 | + doThrow(exception).when(mockedConnection).prepareStatement(Mockito.anyString()); |
| 221 | + doThrow(exception).when(mockedConnection).createStatement(); |
| 222 | + doReturn(mockedConnection).when(mockedDataSource).getConnection(); |
| 223 | + return mockedDataSource; |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + void givenValidCustomer_whenSaveCustomer_thenThrowException() { |
| 228 | + var customer = new Customer<>(2L, "Duc"); |
| 229 | + RuntimeException exception = |
| 230 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.save(customer)); |
| 231 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + void givenValidCustomer_whenUpdateCustomer_thenThrowException() { |
| 236 | + var customerUpdate = new Customer<>(existingCustomer.getId(), "Duc"); |
| 237 | + RuntimeException exception = |
| 238 | + assertThrows(RuntimeException.class, () -> h2CustomerDAO.update(customerUpdate)); |
| 239 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + void givenValidId_whenDeleteCustomer_thenThrowException() { |
| 244 | + RuntimeException exception = |
| 245 | + assertThrows( |
| 246 | + RuntimeException.class, () -> h2CustomerDAO.delete(existingCustomer.getId())); |
| 247 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + void whenFindAll_thenThrowException() { |
| 252 | + RuntimeException exception = assertThrows(RuntimeException.class, h2CustomerDAO::findAll); |
| 253 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + void whenFindById_thenThrowException() { |
| 258 | + RuntimeException exception = |
| 259 | + assertThrows( |
| 260 | + RuntimeException.class, () -> h2CustomerDAO.findById(existingCustomer.getId())); |
| 261 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + void whenCreateSchema_thenThrowException() { |
| 266 | + RuntimeException exception = |
| 267 | + assertThrows(RuntimeException.class, h2CustomerDAO::createSchema); |
| 268 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + void whenDeleteSchema_thenThrowException() { |
| 273 | + RuntimeException exception = |
| 274 | + assertThrows(RuntimeException.class, h2CustomerDAO::deleteSchema); |
| 275 | + assertEquals(EXCEPTION_CAUSE, exception.getMessage()); |
| 276 | + } |
| 277 | + } |
| 278 | +} |
0 commit comments