001package edu.pdx.cs.joy.jdbc; 002 003import org.junit.jupiter.api.AfterEach; 004import org.junit.jupiter.api.BeforeEach; 005import org.junit.jupiter.api.Test; 006 007import java.sql.Connection; 008import java.sql.SQLException; 009import java.util.List; 010 011import static org.hamcrest.MatcherAssert.assertThat; 012import static org.hamcrest.Matchers.*; 013 014public class DepartmentDAOTest { 015 016 private Connection connection; 017 private DepartmentDAO departmentDAO; 018 019 @BeforeEach 020 public void setUp() throws SQLException { 021 // Create an in-memory H2 database 022 connection = H2DatabaseHelper.createInMemoryConnection("test"); 023 024 // Drop the table if it exists from a previous test, then create it 025 DepartmentDAOImpl.dropTable(connection); 026 DepartmentDAOImpl.createTable(connection); 027 028 // Initialize the DAO with the connection 029 departmentDAO = new DepartmentDAOImpl(connection); 030 } 031 032 @AfterEach 033 public void tearDown() throws SQLException { 034 if (connection != null && !connection.isClosed()) { 035 // Drop the table and close the connection 036 DepartmentDAOImpl.dropTable(connection); 037 connection.close(); 038 } 039 } 040 041 @Test 042 public void testPersistAndFetchDepartmentById() throws SQLException { 043 // Create a department (ID will be auto-generated) 044 Department department = new Department("Computer Science"); 045 046 // Persist the department 047 departmentDAO.save(department); 048 049 // Verify that an ID was auto-generated 050 assertThat(department.getId(), is(greaterThan(0))); 051 052 // Fetch the department by the auto-generated ID 053 Department fetchedDepartment = departmentDAO.findById(department.getId()); 054 055 // Validate the fetched department using Hamcrest assertions 056 assertThat(fetchedDepartment, is(notNullValue())); 057 assertThat(fetchedDepartment.getId(), is(equalTo(department.getId()))); 058 assertThat(fetchedDepartment.getName(), is(equalTo("Computer Science"))); 059 } 060 061 @Test 062 public void testFindDepartmentByName() throws SQLException { 063 // Create and persist a department (ID will be auto-generated) 064 Department department = new Department("Mathematics"); 065 departmentDAO.save(department); 066 067 // Fetch the department by name 068 Department fetchedDepartment = departmentDAO.findByName("Mathematics"); 069 070 // Validate the fetched department 071 assertThat(fetchedDepartment, is(notNullValue())); 072 assertThat(fetchedDepartment.getId(), is(equalTo(department.getId()))); 073 assertThat(fetchedDepartment.getName(), is(equalTo("Mathematics"))); 074 } 075 076 @Test 077 public void testFetchNonExistentDepartmentById() throws SQLException { 078 // Try to fetch a department that doesn't exist 079 Department fetchedDepartment = departmentDAO.findById(999); 080 081 // Validate that null is returned 082 assertThat(fetchedDepartment, is(nullValue())); 083 } 084 085 @Test 086 public void testFetchNonExistentDepartmentByName() throws SQLException { 087 // Try to fetch a department that doesn't exist 088 Department fetchedDepartment = departmentDAO.findByName("Nonexistent Department"); 089 090 // Validate that null is returned 091 assertThat(fetchedDepartment, is(nullValue())); 092 } 093 094 @Test 095 public void testFindAllDepartments() throws SQLException { 096 // Create multiple departments (IDs will be auto-generated) 097 Department dept1 = new Department("Computer Science"); 098 Department dept2 = new Department("Mathematics"); 099 Department dept3 = new Department("Physics"); 100 101 // Persist all departments 102 departmentDAO.save(dept1); 103 departmentDAO.save(dept2); 104 departmentDAO.save(dept3); 105 106 // Fetch all departments 107 List<Department> allDepartments = departmentDAO.findAll(); 108 109 // Validate using Hamcrest matchers 110 assertThat(allDepartments, hasSize(3)); 111 assertThat(allDepartments, hasItem(hasProperty("name", is("Computer Science")))); 112 assertThat(allDepartments, hasItem(hasProperty("name", is("Mathematics")))); 113 assertThat(allDepartments, hasItem(hasProperty("name", is("Physics")))); 114 assertThat(allDepartments, hasItem(hasProperty("id", is(dept1.getId())))); 115 assertThat(allDepartments, hasItem(hasProperty("id", is(dept2.getId())))); 116 assertThat(allDepartments, hasItem(hasProperty("id", is(dept3.getId())))); 117 } 118 119 @Test 120 public void testFindAllReturnsEmptyListWhenNoDepartments() throws SQLException { 121 // Fetch all departments from empty table 122 List<Department> allDepartments = departmentDAO.findAll(); 123 124 // Validate that an empty list is returned 125 assertThat(allDepartments, is(empty())); 126 } 127 128 @Test 129 public void testDepartmentEquality() throws SQLException { 130 // Create and persist a department (ID will be auto-generated) 131 Department original = new Department("Engineering"); 132 departmentDAO.save(original); 133 134 // Fetch the department by its auto-generated ID 135 Department fetched = departmentDAO.findById(original.getId()); 136 137 // Validate that the objects are equal 138 assertThat(fetched, is(equalTo(original))); 139 } 140}