From f1dbc7359740674c0b402d70dad96ee1c6197b39 Mon Sep 17 00:00:00 2001 From: Meixuan Lu Date: Thu, 12 Sep 2024 02:07:24 -0400 Subject: [PATCH] add unit tests so that the coverage is 93% and fixed some bugs --- .../project/individualproject/Course.java | 16 +- .../project/individualproject/Department.java | 2 +- .../individualproject/RouteController.java | 12 +- .../individualproject/CourseUnitTests.java | 7 +- .../DepartmentUnitTests.java | 3 +- .../MyFileDatabaseUnitTests.java | 3 +- .../RouteControllerUnitTests.java | 485 +++++++++++------- 7 files changed, 319 insertions(+), 209 deletions(-) diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java index a25fbe60..48eab9f1 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java @@ -33,7 +33,10 @@ public Course(String instructorName, String courseLocation, String timeSlot, int * @return true if the student is successfully enrolled, false otherwise. */ public boolean enrollStudent() { - enrolledStudentCount++; + if(enrolledStudentCount < enrollmentCapacity) { + enrolledStudentCount++; + return true; + } return false; } @@ -43,7 +46,10 @@ public boolean enrollStudent() { * @return true if the student is successfully dropped, false otherwise. */ public boolean dropStudent() { - enrolledStudentCount--; + if (enrolledStudentCount > 0){ + enrolledStudentCount--; + return true; + } return false; } @@ -88,7 +94,11 @@ public void setEnrolledStudentCount(int count) { this.enrolledStudentCount = count; } - + /** + * Check if the course is full or not. + * + * @return true if the course is enroll-able, which is not full, false otherwise. + */ public boolean isCourseFull() { return enrollmentCapacity > enrolledStudentCount; } diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java index 2cb186e7..5e0d2b20 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java @@ -108,7 +108,7 @@ public String toString() { result.append(deptCode).append(" ").append(key).append(": ").append(value.toString()) .append("\n"); } - return "result.toString()"; + return result.toString(); } @Serial diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java index 7bdbe799..35f10083 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java @@ -43,10 +43,10 @@ public ResponseEntity retrieveDepartment(@RequestParam(value = "deptCode") St departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); if (!departmentMapping.containsKey(deptCode.toUpperCase())) { - return new ResponseEntity<>("Department Not Found", HttpStatus.OK); + return new ResponseEntity<>("Department Not Found", HttpStatus.NOT_FOUND); } else { return new ResponseEntity<>(departmentMapping.get(deptCode.toUpperCase()).toString(), - HttpStatus.NOT_FOUND); + HttpStatus.OK); } } catch (Exception e) { @@ -83,7 +83,7 @@ public ResponseEntity retrieveCourse(@RequestParam(value = "deptCode") String return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND); } else { return new ResponseEntity<>(coursesMapping.get(Integer.toString(courseCode)).toString(), - HttpStatus.FORBIDDEN); + HttpStatus.OK); } } @@ -147,10 +147,10 @@ public ResponseEntity getMajorCtFromDept(@RequestParam(value = "deptCode") St if (doesDepartmentExists) { HashMap departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - return new ResponseEntity<>("There are: " + -departmentMapping.get(deptCode) + return new ResponseEntity<>("There are: " + departmentMapping.get(deptCode) .getNumberOfMajors() + " majors in the department", HttpStatus.OK); } - return new ResponseEntity<>("Department Not Found", HttpStatus.FORBIDDEN); + return new ResponseEntity<>("Department Not Found", HttpStatus.NOT_FOUND); } catch (Exception e) { return handleException(e); } @@ -286,7 +286,7 @@ public ResponseEntity findCourseTime(@RequestParam(value = "deptCode") String coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); - return new ResponseEntity<>("The course meets at: " + "some time ", + return new ResponseEntity<>("The course meets at: " + requestedCourse.getCourseTimeSlot() + ".", HttpStatus.OK); } else { return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND); diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java index 67ab1f9b..5690ff6d 100644 --- a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java @@ -37,15 +37,16 @@ public void toStringTest() { @Test public void enrollStudentTest() { + testCourse.setEnrolledStudentCount(10); boolean result = testCourse.enrollStudent(); - assertFalse(result); + assertTrue(result); } @Test public void dropStudentTest() { - testCourse.enrollStudent(); // First, enroll one student to have at least one student + testCourse.setEnrolledStudentCount(10); boolean result = testCourse.dropStudent(); - assertFalse(result); + assertTrue(result); } @Test diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java index 3d37b851..828d3b83 100644 --- a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java @@ -43,7 +43,8 @@ public void constructorTest() { @Test public void toStringTest() { - String expectedResult = "result.toString()"; + String expectedResult = "CS CS101: \n" + + "Instructor: Griffin Newbold; Location: 417 IAB; Time: 11:40-12:55\n"; assertEquals(expectedResult, testDepartment.toString()); } diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java index e22890e7..3cb45940 100644 --- a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java @@ -78,8 +78,7 @@ public void getDepartmentMappingTest() { @Test public void toStringTest() { - String expectedString = "For the CS department: \n" - + "Instructor: Dr. Smith; Location: ; Time: \n"; + String expectedString = "For the CS department: \n"; // no course case assertEquals(expectedString, database.toString()); } diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java index 5d19a707..0742ce27 100644 --- a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java @@ -3,47 +3,30 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.springframework.http.HttpStatus.OK; -import static org.springframework.http.HttpStatus.NOT_FOUND; -import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import java.util.HashMap; public class RouteControllerUnitTests { - @InjectMocks private RouteController routeController; - - @Mock - private Department department; - - @Mock private MyFileDatabase myFileDatabase; - - @Mock - private Course course; - - private HashMap departmentMapping; - private HashMap courseMapping; + private IndividualProjectApplication app; @BeforeEach public void setUp() { - MockitoAnnotations.openMocks(this); - - departmentMapping = new HashMap<>(); - courseMapping = new HashMap<>(); - - departmentMapping.put("CS", department); - courseMapping.put("101", course); + app = new IndividualProjectApplication(); + myFileDatabase = new MyFileDatabase(0, "./data.txt"); + app.myFileDatabase = myFileDatabase; + app.resetDataFile(); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); routeController = new RouteController(); } @@ -55,292 +38,408 @@ public void testIndex() { @Test public void testRetrieveDepartment_Found() { - when(departmentMapping.containsKey("CS")).thenReturn(true); + ResponseEntity response = routeController.retrieveDepartment("COMS"); - ResponseEntity response = routeController.retrieveDepartment("CS"); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response.getBody().toString().contains("COMS")); } @Test public void testRetrieveDepartment_NotFound() { - when(departmentMapping.containsKey("MATH")).thenReturn(false); + ResponseEntity response = routeController.retrieveDepartment("UNKNOWN"); - ResponseEntity response = routeController.retrieveDepartment("MATH"); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Department Not Found", response.getBody()); } + @Test - public void testRetrieveCourse_Found() { - when(departmentMapping.containsKey("CS")).thenReturn(true); - when(courseMapping.containsKey("101")).thenReturn(true); + public void testRetrieveCourseSuccess() { + ResponseEntity response = routeController.retrieveCourse("CHEM", 1403); - ResponseEntity response = routeController.retrieveCourse("CS", 101); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals("\nInstructor: Ruben M Savizky; Location: 309 HAV; Time: 6:10-7:25", response.getBody()); } @Test - public void testRetrieveCourse_NotFound() { - when(departmentMapping.containsKey("CS")).thenReturn(true); - when(courseMapping.containsKey("101")).thenReturn(false); + public void testRetrieveCourseNotFound() { + ResponseEntity response = routeController.retrieveCourse("CHEM", 9999); - ResponseEntity response = routeController.retrieveCourse("CS", 101); - assertEquals(NOT_FOUND, response.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Course Not Found", response.getBody()); } @Test - public void testIsCourseFull_CourseIsFull() { - when(course.isCourseFull()).thenReturn(true); + public void testRetrieveCourseDepartmentNotFound() { + ResponseEntity response = routeController.retrieveCourse("NONEXISTENT", 0000); - boolean result = course.isCourseFull(); - assertTrue(result); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Department Not Found", response.getBody()); } @Test - public void testIsCourseFull_CourseIsNotFull() { - when(course.isCourseFull()).thenReturn(false); + public void testIsCourseFull_Full() { + ResponseEntity response = routeController.isCourseFull("IEOR", 4106); - boolean result = course.isCourseFull(); - assertFalse(result); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(false, response.getBody()); } + @Test + public void testIsCourseFull_NotFull() { + ResponseEntity response = routeController.isCourseFull("IEOR", 4102); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(true, response.getBody()); + } @Test - public void testGetMajorCtFromDept_Found() { - when(departmentMapping.containsKey("CS")).thenReturn(true); - when(department.getNumberOfMajors()).thenReturn(100); + public void testIsCourseFullCourseNotFound() { + ResponseEntity response = routeController.isCourseFull("IEOR", 9999); - ResponseEntity response = routeController.getMajorCtFromDept("CS"); - assertEquals(OK, response.getStatusCode()); - assertEquals("There are: -100 majors in the department", response.getBody()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); } @Test - public void testIdentifyDeptChair_Found() { - when(departmentMapping.containsKey("CS")).thenReturn(true); - when(department.getDepartmentChair()).thenReturn("Dr. Smith"); + public void testIsCourseFullDepartmentNotFound() { + ResponseEntity response = routeController.isCourseFull("NONEXISTENT", 1001); - ResponseEntity response = routeController.identifyDeptChair("CS"); - assertEquals(OK, response.getStatusCode()); - assertEquals("Dr. Smith is the department chair.", response.getBody()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); } @Test - public void testFindCourseLocation_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testGetMajorCtFromDeptSuccess() { + ResponseEntity response = routeController.getMajorCtFromDept("COMS"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals("There are: 2700 majors in the department", response.getBody()); + } - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); - when(course.getCourseLocation()).thenReturn("Room 101"); + @Test + public void testGetMajorCtFromDeptDepartmentNotFound() { + ResponseEntity response = routeController.getMajorCtFromDept("NONEXISTENT"); - ResponseEntity response = routeController.findCourseLocation("CS", 101); - assertEquals(OK, response.getStatusCode()); - assertEquals("Room 101", response.getBody()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Department Not Found", response.getBody()); } @Test - public void testFindCourseLocation_CourseNotFound() { - HashMap departmentMapping = new HashMap<>(); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); + public void testIdentifyDeptChairSuccess() { + String deptCode = "COMS"; + String expectedChair = "Luca Carloni"; + ResponseEntity response = routeController.identifyDeptChair(deptCode); - ResponseEntity response = routeController.findCourseLocation("CS", 101); - assertEquals(NOT_FOUND, response.getStatusCode()); - assertEquals("Course Not Found", response.getBody()); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(expectedChair + " is the department chair.", response.getBody()); } + @Test + public void testIdentifyDeptChairDepartmentNotFound() { + ResponseEntity response = routeController.identifyDeptChair("NONEXISTENT"); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Department Not Found", response.getBody()); + } @Test - public void testFindCourseInstructor_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testFindCourseLocationSuccess() { + ResponseEntity response = routeController.findCourseLocation("ECON", 1105); - // Mock course behavior - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); - when(course.getInstructorName()).thenReturn("Dr. Smith"); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals("309 HAV is where the course is located.", response.getBody()); + } - ResponseEntity response = routeController.findCourseInstructor("CS", 101); - assertEquals(OK, response.getStatusCode()); - assertEquals("Dr. Smith is the instructor for the course.", response.getBody()); + @Test + public void testFindCourseLocationCourseNotFound() { + ResponseEntity response = routeController.findCourseLocation("ECON", 9999); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testFindCourseLocationDepartmentNotFound() { + ResponseEntity response = routeController.findCourseLocation("NONEXISTENT", 1001); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testFindCourseInstructorSuccess() { + ResponseEntity response = routeController.findCourseInstructor("ECON", 1105); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals("Waseem Noor is the instructor for the course.", response.getBody()); } @Test - public void testFindCourseInstructor_CourseNotFound() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testFindCourseInstructorCourseNotFound() { + ResponseEntity response = routeController.findCourseInstructor("ECON", 9999); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); + @Test + public void testFindCourseInstructorDepartmentNotFound() { + ResponseEntity response = routeController.findCourseInstructor("NONEXISTENT", 1001); - ResponseEntity response = routeController.findCourseInstructor("CS", 102); - assertEquals(NOT_FOUND, response.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Course Not Found", response.getBody()); } @Test - public void testFindCourseTime_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testFindCourseTimeSuccess() { + ResponseEntity response = routeController.findCourseTime("ECON", 1105); - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals("The course meets at: 2:40-3:55.", response.getBody()); + } + + @Test + public void testFindCourseTimeCourseNotFound() { + ResponseEntity response = routeController.findCourseTime("ECON", 9999); - ResponseEntity response = routeController.findCourseTime("CS", 101); - assertEquals(OK, response.getStatusCode()); - assertEquals("The course meets at: some time ", response.getBody()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); } @Test - public void testFindCourseTime_CourseNotFound() { - HashMap departmentMapping = new HashMap<>(); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(new HashMap<>()); + public void testFindCourseTimeDepartmentNotFound() { + ResponseEntity response = routeController.findCourseTime("NONEXISTENT", 1001); - ResponseEntity response = routeController.findCourseTime("CS", 102); - assertEquals(NOT_FOUND, response.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Course Not Found", response.getBody()); } @Test - public void testAddMajorToDept_DepartmentExists() { - HashMap departmentMapping = new HashMap<>(); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); + public void testAddMajorToDeptSuccess() { + String deptCode = "COMS"; + Department department = app.myFileDatabase.getDepartmentMapping().get(deptCode); + + ResponseEntity response = routeController.addMajorToDept(deptCode); - ResponseEntity response = routeController.addMajorToDept("CS"); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("Attribute was updated successfully", response.getBody()); + assertEquals(2701, department.getNumberOfMajors()); } @Test public void testAddMajorToDept_DepartmentNotFound() { - HashMap departmentMapping = new HashMap<>(); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); + String deptCode = "NONEXISTENT"; + ResponseEntity response = routeController.addMajorToDept(deptCode); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testRemoveMajorToDeptSuccess() { + String deptCode = "COMS"; + Department department = app.myFileDatabase.getDepartmentMapping().get(deptCode); - ResponseEntity response = routeController.addMajorToDept("MATH"); - assertEquals(NOT_FOUND, response.getStatusCode()); + ResponseEntity response = routeController.removeMajorFromDept(deptCode); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals("Attribute was updated or is at minimum", response.getBody()); + assertEquals(2699, department.getNumberOfMajors()); + } + + @Test + public void testRemoveMajorToDept_DepartmentNotFound() { + String deptCode = "NONEXISTENT"; + ResponseEntity response = routeController.removeMajorFromDept(deptCode); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Department Not Found", response.getBody()); } @Test - public void testDropStudentFromCourse_CourseExists_StudentDropped() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testDropStudentSuccess() { + String deptCode = "COMS"; + int courseCode = 1004; - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); - when(course.dropStudent()).thenReturn(true); + ResponseEntity response = routeController.dropStudent(deptCode, courseCode); - ResponseEntity response = routeController.dropStudent("CS", 101); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("Student has been dropped.", response.getBody()); } @Test - public void testDropStudentFromCourse_CourseExists_StudentNotDropped() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testDropStudentFail() { + String deptCode = "COMS"; + int courseCode = 1004; + Department department = app.myFileDatabase.getDepartmentMapping().get(deptCode); + Course course = department.getCourseSelection().get(Integer.toString(courseCode)); + course.setEnrolledStudentCount(0); - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); - when(course.dropStudent()).thenReturn(false); + ResponseEntity response = routeController.dropStudent(deptCode, courseCode); - ResponseEntity response = routeController.dropStudent("CS", 101); - assertEquals(BAD_REQUEST, response.getStatusCode()); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); assertEquals("Student has not been dropped.", response.getBody()); } @Test - public void testSetEnrollmentCount_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testDropStudentCourseNotFound() { + String deptCode = "COMS"; + int courseCode = 9999; + ResponseEntity response = routeController.dropStudent(deptCode, courseCode); - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); + // Then + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } - ResponseEntity response = routeController.setEnrollmentCount("CS", 101, 50); - assertEquals(OK, response.getStatusCode()); + @Test + public void testDropStudentDepartmentNotFound() { + String deptCode = "NONEXISTENT"; + int courseCode = 1001; + + ResponseEntity response = routeController.dropStudent(deptCode, courseCode); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testSetEnrollmentSuccess() { + String deptCode = "COMS"; + int courseCode = 1004; + + ResponseEntity response = routeController.setEnrollmentCount(deptCode, courseCode, 10); + + assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("Attributed was updated successfully.", response.getBody()); } @Test - public void testChangeCourseTime_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testSetEnrollmentCourseNotFound() { + String deptCode = "COMS"; + int courseCode = 9999; + ResponseEntity response = routeController.setEnrollmentCount(deptCode, courseCode, 10); + + // Then + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testSetEnrollmentDepartmentNotFound() { + String deptCode = "NONEXISTENT"; + int courseCode = 1001; + + ResponseEntity response = routeController.setEnrollmentCount(deptCode, courseCode,10); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); + @Test + public void testChangeCourseTimeSuccess() { + String deptCode = "COMS"; + int courseCode = 1004; + Department department = app.myFileDatabase.getDepartmentMapping().get(deptCode); + Course course = department.getCourseSelection().get(Integer.toString(courseCode)); - ResponseEntity response = routeController.changeCourseTime("CS", 101, "10:00 AM"); - assertEquals(OK, response.getStatusCode()); + ResponseEntity response = routeController.changeCourseTime(deptCode, courseCode, "1:10-3:40"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("Attributed was updated successfully.", response.getBody()); + assertEquals("1:10-3:40", course.getCourseTimeSlot()); + } + + @Test + public void testChangeCourseTimeCourseNotFound() { + String deptCode = "COMS"; + int courseCode = 9999; + ResponseEntity response = routeController.changeCourseTime(deptCode, courseCode, "1:10-3:40"); + + // Then + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); } @Test - public void testChangeCourseTime_CourseNotFound() { - HashMap departmentMapping = new HashMap<>(); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); + public void testChangeCourseTimeDepartmentNotFound() { + String deptCode = "NONEXISTENT"; + int courseCode = 1001; - ResponseEntity response = routeController.changeCourseTime("CS", 101, "10:00 AM"); - assertEquals(NOT_FOUND, response.getStatusCode()); + ResponseEntity response = routeController.changeCourseTime(deptCode, courseCode, "1:10-3:40"); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Course Not Found", response.getBody()); } @Test - public void testChangeCourseTeacher_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testChangeCourseTeacherSuccess() { + String deptCode = "COMS"; + int courseCode = 1004; + Department department = app.myFileDatabase.getDepartmentMapping().get(deptCode); + Course course = department.getCourseSelection().get(Integer.toString(courseCode)); - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); + ResponseEntity response = routeController.changeCourseTeacher(deptCode, courseCode, "new teacher"); - ResponseEntity response = routeController.changeCourseTeacher("CS", 101, "New Teacher"); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("Attributed was updated successfully.", response.getBody()); + assertEquals("new teacher", course.getInstructorName()); + } + + @Test + public void testChangeCourseTeacherCourseNotFound() { + String deptCode = "COMS"; + int courseCode = 9999; + ResponseEntity response = routeController.changeCourseTeacher(deptCode, courseCode, "new teacher"); + + // Then + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); } @Test - public void testChangeCourseLocation_CourseExists() { - HashMap departmentMapping = new HashMap<>(); - HashMap courseMapping = new HashMap<>(); + public void testChangeCourseTeacherDepartmentNotFound() { + String deptCode = "NONEXISTENT"; + int courseCode = 1001; - courseMapping.put("101", course); - departmentMapping.put("CS", department); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); - when(department.getCourseSelection()).thenReturn(courseMapping); + ResponseEntity response = routeController.changeCourseTeacher(deptCode, courseCode, "new teacher"); - ResponseEntity response = routeController.changeCourseLocation("CS", 101, "New Building"); - assertEquals(OK, response.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testChangeCourseLocationSuccess() { + String deptCode = "COMS"; + int courseCode = 1004; + Department department = app.myFileDatabase.getDepartmentMapping().get(deptCode); + Course course = department.getCourseSelection().get(Integer.toString(courseCode)); + + ResponseEntity response = routeController.changeCourseLocation(deptCode, courseCode, "new location"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("Attributed was updated successfully.", response.getBody()); + assertEquals("new location", course.getCourseLocation()); } @Test - public void testChangeCourseLocation_CourseNotFound() { - HashMap departmentMapping = new HashMap<>(); - when(myFileDatabase.getDepartmentMapping()).thenReturn(departmentMapping); + public void testChangeCourseLocationCourseNotFound() { + String deptCode = "COMS"; + int courseCode = 9999; + ResponseEntity response = routeController.changeCourseLocation(deptCode, courseCode, "new location"); + + // Then + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testChangeCourseLocationDepartmentNotFound() { + String deptCode = "NONEXISTENT"; + int courseCode = 1001; + + ResponseEntity response = routeController.changeCourseLocation(deptCode, courseCode, "new location"); - ResponseEntity response = routeController.changeCourseLocation("CS", 101, "New Building"); - assertEquals(NOT_FOUND, response.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Course Not Found", response.getBody()); }