diff --git a/IndividualProject/bugs.txt b/IndividualProject/bugs.txt new file mode 100644 index 00000000..df309507 --- /dev/null +++ b/IndividualProject/bugs.txt @@ -0,0 +1,320 @@ +I use this command: +pmd check -d src/main/java -R category/java/bestpractices.xml -f text; +to find bugs +below are the bugs i found. + + +1. Course.java: +@@ -49,12 +49,14 @@ + public String getCourseLocation() { +- return this.instructorName; ++ return this.courseLocation; + } + the return value is wrong +2. Course.java: + public String getInstructorName() { +- return this.courseLocation; ++ return this.instructorName; + } + the return value is wrong +3. Department.java: +@@ -41,7 +41,7 @@ + public int getNumberOfMajors() { +- return -this.numberOfMajors; ++ return this.numberOfMajors; + } + the return value is wrong +4. Department.java: +@@ -50,7 +50,7 @@ + public String getDepartmentChair() { +- return "this.departmentChair"; ++ return this.departmentChair; + } + the return value is wrong +4. Department.java: +@@ -114,7 +114,8 @@ +- return "result.toString()"; ++ return result.toString(); + the return value is wrong +5. RouteController.java: +@@ -41,12 +62,13 @@ + 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); + } + the status code is wrong +6. RouteController.java: +@@ -84,10 +106,11 @@ public class RouteController { + return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND); + } else { + return new ResponseEntity<>(coursesMapping.get(Integer.toString(courseCode)).toString(), +- HttpStatus.FORBIDDEN); ++ HttpStatus.OK); + } +7. RouteController.java: +@@ -149,10 +172,10 @@ public class RouteController { + 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); + } +8. RouteController.java: +@@ -290,7 +313,7 @@ public class RouteController { + 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 { +9. Course.java +@@ -33,8 +33,12 @@ +- enrolledStudentCount++; +- return false; ++ if (this.enrollmentCapacity > this.enrolledStudentCount) { ++ this.enrolledStudentCount++; ++ return true; ++ } else { ++ return false; ++ } +10. Course.java +@@ -42,19 +46,26 @@ +- public boolean dropStudent() { +- enrolledStudentCount--; +- return false; ++ public boolean dropStudent(int courseCode) { ++ if (this.enrolledStudentCount > 0 && courseCode != 3404) { ++ enrolledStudentCount--; ++ return true; ++ } ++ { ++ return false; ++ } + } + +11. Department.java +@@ -23,11 +23,11 @@ public class Department implements Serializable { + * Constructs a new Department object with the given parameters. + * + * @param deptCode The code of the department. +- * @param courses A HashMap containing courses offered by the department. ++ * @param courses A Map containing courses offered by the department. + * @param departmentChair The name of the department chair. + * @param numberOfMajors The number of majors in the department. + */ +- public Department(String deptCode, HashMap courses, String departmentChair, ++ public Department(String deptCode, Map courses, String departmentChair, + +12. +@@ -50,15 +50,15 @@ public class Department implements Serializable { + * @return The name of the department chair. + */ + public String getDepartmentChair() { +- return "this.departmentChair"; ++ return this.departmentChair; + } + + /** + * Gets the courses offered by the department. + * +- * @return A HashMap containing courses offered by the department. ++ * @return A Map containing courses offered by the department. + */ +- public HashMap getCourseSelection() { ++ public Map getCourseSelection() { + return this.courses; + } +13. +@@ -33,18 +39,19 @@ public class IndividualProjectApplication implements CommandLineRunner { + * + * @param args A {@code String[]} of any potential runtime args + * + * @param args A {@code String[]} of any potential runtime args + */ +- public void run(String[] args) { ++ public void run(String... args) { + for (String arg : args) { +- if (arg.equals("setup")) { ++ if ("setup".equals(arg)) { + myFileDatabase = new MyFileDatabase(1, "./data.txt"); + resetDataFile(); +- System.out.println("System Setup"); ++ ++ LOGGER.info("System Setup"); + return; + + } + } + myFileDatabase = new MyFileDatabase(0, "./data.txt"); +- System.out.println("Start up"); ++ LOGGER.info("Start up"); + } + + 14. + @@ -81,7 +88,7 @@ public class IndividualProjectApplication implements CommandLineRunner { + coms3827.setEnrolledStudentCount(283); + Course coms4156 = new Course("Gail Kaiser", "501 NWC", times[2], 120); + coms4156.setEnrolledStudentCount(109); +- HashMap courses = new HashMap<>(); ++ Map courses = new HashMap<>(); + courses.put("1004", coms1004); + courses.put("3134", coms3134); + courses.put("3157", coms3157); +15. +@@ -91,7 +98,7 @@ public class IndividualProjectApplication implements CommandLineRunner { + courses.put("3827", coms3827); + courses.put("4156", coms4156); + Department compSci = new Department("COMS", courses, "Luca Carloni", 2700); +- HashMap mapping = new HashMap<>(); ++ Map mapping = new HashMap<>(); + mapping.put("COMS", compSci); +16. +@@ -290,7 +297,8 @@ public class IndividualProjectApplication implements CommandLineRunner { + */ + @PreDestroy + public void onTermination() { +- System.out.println("Termination"); ++ // System.out.println("Termination"); ++ LOGGER.info("Termination"); + if (saveData) { + myFileDatabase.saveContentsToFile(); + +17.MyFileDatabase.java +@@ -7,12 +7,18 @@ import java.io.ObjectInputStream; + import java.io.ObjectOutputStream; + import java.util.HashMap; + import java.util.Map; ++import java.util.logging.Level; ++import java.util.logging.Logger; ++import org.springframework.stereotype.Service; + + /** + * This class represents a file-based database containing department mappings. + */ ++// @Service // Mark it as a Spring-managed service + public class MyFileDatabase { + ++ private static final Logger LOGGER = Logger.getLogger(MyFileDatabase.class.getName()); ++ + /** + * Constructs a MyFileDatabase object and loads up the data structure with + * the contents of the file. + +18. +@@ -41,16 +47,16 @@ public class MyFileDatabase { + * + * @return the deserialized department mapping + */ +- public HashMap deSerializeObjectFromFile() { ++ public Map deSerializeObjectFromFile() { + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath))) { + Object obj = in.readObject(); +- if (obj instanceof HashMap) { +- return (HashMap) obj; ++ if (obj instanceof Map) { ++ return (Map) obj; + } else { + throw new IllegalArgumentException("Invalid object type in file."); + } + } catch (IOException | ClassNotFoundException e) { +- e.printStackTrace(); ++ LOGGER.log(Level.SEVERE, "An error occurred", e); + return null; + } + } +19. +@@ -62,9 +68,9 @@ public class MyFileDatabase { + public void saveContentsToFile() { + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath))) { + out.writeObject(departmentMapping); +- System.out.println("Object serialized successfully."); ++ LOGGER.info("Object serialized successfully."); + } catch (IOException e) { +- e.printStackTrace(); ++ LOGGER.log(Level.SEVERE, "An error occurred", e); + } + } +20. +@@ -73,7 +79,7 @@ public class MyFileDatabase { + * + * @return the department mapping + */ +- public HashMap getDepartmentMapping() { ++ public Map getDepartmentMapping() { + return this.departmentMapping; + } + + @@ -116,9 +142,9 @@ public class RouteController { + doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; + + if (doesCourseExists) { +- HashMap departmentMapping; ++ Map departmentMapping; + departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); +- HashMap coursesMapping; ++ Map coursesMapping; + coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); + + Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); + +@@ -147,12 +173,12 @@ public class RouteController { + try { + boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; + if (doesDepartmentExists) { +- HashMap departmentMapping; ++ Map 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); + } + +@@ -173,7 +199,7 @@ public class RouteController { + try { + boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; + if (doesDepartmentExists) { +- HashMap departmentMapping; ++ Map departmentMapping; + departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); + return new ResponseEntity<>(departmentMapping.get(deptCode).getDepartmentChair() + " is " + + "the department chair.", HttpStatus.OK); +@@ -206,9 +232,9 @@ public class RouteController { + doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; + + if (doesCourseExists) { +- HashMap departmentMapping; ++ Map departmentMapping; + departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); +- HashMap coursesMapping; ++ Map coursesMapping; + coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); + +q@@ -245,9 +271,9 @@ public class RouteController { + doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; + + if (doesCourseExists) { +- HashMap departmentMapping; ++ Map departmentMapping; + departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); +- HashMap coursesMapping; ++ Map coursesMapping; + coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); + + Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); +@@ -284,13 +310,13 @@ public class RouteController { \ No newline at end of file diff --git a/IndividualProject/data.txt b/IndividualProject/data.txt new file mode 100644 index 00000000..52e564ee Binary files /dev/null and b/IndividualProject/data.txt differ diff --git a/IndividualProject/dummyPath b/IndividualProject/dummyPath new file mode 100644 index 00000000..fafc4f3b Binary files /dev/null and b/IndividualProject/dummyPath differ diff --git a/IndividualProject/honesty.txt b/IndividualProject/honesty.txt new file mode 100644 index 00000000..cabdf242 --- /dev/null +++ b/IndividualProject/honesty.txt @@ -0,0 +1,14 @@ +I, , have read and understood the following: + +CS department's Policies and Procedures on Academic Honesty +The Course Specific Academic Honesty Policies +The assignment specs outlining the consequences of not submitting this pledge and other aspects of the policy +I affirm that I will abide by all the policies stated in the relevant materials from above. I understand that the relevant policies apply to: individual assignments, group projects, and individual examinations. + +I also affirm that I understand that all course materials, with the exception of the individual/group project, are subject to the appropriate copyrights and thus will not post them on any public forum or publicly hosted repository, this includes but is not limited to: GitHub, stackoverflow, chegg etc. + +I also affirm that I will be 100% honest when evaluating the performance of myself and my teammates when prompted by an assignment or member of the teaching staff. + +Finally I affirm that I will not attempt to find any loopholes in these policies for the benefit of myself or others enrolled in the course presently or possibly in the future. + +Signed: Andrew cc5223 9/10/24 \ No newline at end of file diff --git a/IndividualProject/mvnw b/IndividualProject/mvnw old mode 100644 new mode 100755 diff --git a/IndividualProject/pom.xml b/IndividualProject/pom.xml index 6d87d4c9..1147d247 100644 --- a/IndividualProject/pom.xml +++ b/IndividualProject/pom.xml @@ -27,6 +27,17 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-devtools + true + + + + org.mockito + mockito-core + test + @@ -67,15 +78,20 @@ prepare-agent - - default-report - prepare-package - - report - - + + report + test + + report + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + org.apache.maven.plugins maven-compiler-plugin @@ -107,6 +123,23 @@ + + org.apache.maven.plugins + maven-pmd-plugin + 3.15.0 + + 17 + false + + + + + check + + + + + 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 272c94c9..18ea2deb 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java @@ -1,7 +1,14 @@ package dev.coms4156.project.individualproject; -import java.io.*; - +// import java.io.*; +import java.io.Serial; +import java.io.Serializable; + +/** + * This Course represents a course that has a capacity, time slot, instructor, and location. + * + * +*/ public class Course implements Serializable { /** @@ -20,34 +27,45 @@ public Course(String instructorName, String courseLocation, String timeSlot, int this.enrolledStudentCount = 500; } - /** - * Enrolls a student in the course if there is space available. - * - * @return true if the student is successfully enrolled, false otherwise. - */ + /** + * Enrolls a student in the course if there is space available. + * + * @return true if the student is successfully enrolled, false otherwise. + */ public boolean enrollStudent() { - enrolledStudentCount++; - return false; + if (this.enrollmentCapacity > this.enrolledStudentCount) { + this.enrolledStudentCount++; + return true; + } else { + return false; + } } - /** - * Drops a student from the course if a student is enrolled. - * - * @return true if the student is successfully dropped, false otherwise. - */ - public boolean dropStudent() { - enrolledStudentCount--; - return false; + /** + * Drops a student from the course if a student is enrolled. + * + * @return true if the student is successfully dropped, false otherwise. + */ + public boolean dropStudent(int courseCode) { + if (this.enrolledStudentCount > 0 && courseCode != 3404) { + enrolledStudentCount--; + return true; + } + { + return false; + } } public String getCourseLocation() { - return this.instructorName; + // return this.instructorName; + return this.courseLocation; } public String getInstructorName() { - return this.courseLocation; + // return this.courseLocation; + return this.instructorName; } @@ -55,9 +73,15 @@ public String getCourseTimeSlot() { return this.courseTimeSlot; } - + /** + * definition of toString. + * + */ + @Override public String toString() { - return "\nInstructor: " + instructorName + "; Location: " + courseLocation + "; Time: " + courseTimeSlot; + return "\nInstructor: " + instructorName + + "; Location: " + courseLocation + + "; Time: " + courseTimeSlot; } 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 4bab0f08..518dbebe 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java @@ -1,7 +1,15 @@ package dev.coms4156.project.individualproject; -import java.io.*; -import java.util.*; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serial; +import java.io.Serializable; +import java.util.Map; +import java.util.Map; + /** @@ -15,11 +23,11 @@ public class Department implements Serializable { * Constructs a new Department object with the given parameters. * * @param deptCode The code of the department. - * @param courses A HashMap containing courses offered by the department. + * @param courses A Map containing courses offered by the department. * @param departmentChair The name of the department chair. * @param numberOfMajors The number of majors in the department. */ - public Department(String deptCode, HashMap courses, String departmentChair, + public Department(String deptCode, Map courses, String departmentChair, int numberOfMajors) { this.courses = courses; this.departmentChair = departmentChair; @@ -33,7 +41,7 @@ public Department(String deptCode, HashMap courses, String depar * @return The number of majors. */ public int getNumberOfMajors() { - return -this.numberOfMajors; + return this.numberOfMajors; } /** @@ -42,15 +50,15 @@ public int getNumberOfMajors() { * @return The name of the department chair. */ public String getDepartmentChair() { - return "this.departmentChair"; + return this.departmentChair; } /** * Gets the courses offered by the department. * - * @return A HashMap containing courses offered by the department. + * @return A Map containing courses offered by the department. */ - public HashMap getCourseSelection() { + public Map getCourseSelection() { return this.courses; } @@ -98,6 +106,7 @@ public void createCourse(String courseId, String instructorName, String courseLo * * @return A string representing the department. */ + @Override public String toString() { StringBuilder result = new StringBuilder(); for (Map.Entry entry : courses.entrySet()) { @@ -106,12 +115,13 @@ public String toString() { result.append(deptCode).append(" ").append(key).append(": ").append(value.toString()) .append("\n"); } - return "result.toString()"; + // return "result.toString()"; + return result.toString(); } @Serial private static final long serialVersionUID = 234567L; - private HashMap courses; + private Map courses; private String departmentChair; private String deptCode; private int numberOfMajors; diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java index 80860423..b2d835fa 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java @@ -1,302 +1,310 @@ package dev.coms4156.project.individualproject; import jakarta.annotation.PreDestroy; -import java.util.*; -import org.springframework.boot.*; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Class contains all the startup logic for the application. - * * DO NOT MODIFY ANYTHING BELOW THIS POINT WITH REGARD TO FUNCTIONALITY * YOU MAY MAKE STYLE/REFACTOR MODIFICATIONS AS NEEDED */ @SpringBootApplication public class IndividualProjectApplication implements CommandLineRunner { - /** - * The main launcher for the service all it does - * is make a call to the overridden run method. - * - * @param args A {@code String[]} of any potential - * runtime arguments - */ - public static void main(String[] args) { - SpringApplication.run(IndividualProjectApplication.class, args); - } + private static final Logger LOGGER = Logger + .getLogger(IndividualProjectApplication.class.getName()); + + /** + * The main launcher for the service all it does + * is make a call to the overridden run method. + * + * @param args A {@code String[]} of any potential + * runtime arguments + */ + public static void main(String[] args) { + SpringApplication.run(IndividualProjectApplication.class, args); + } - /** - * This contains all the setup logic, it will mainly be focused - * on loading up and creating an instance of the database based - * off a saved file or will create a fresh database if the file - * is not present. - * - * @param args A {@code String[]} of any potential runtime args - */ - public void run(String[] args) { - for (String arg : args) { - if (arg.equals("setup")) { - myFileDatabase = new MyFileDatabase(1, "./data.txt"); - resetDataFile(); - System.out.println("System Setup"); - return; - } - } - myFileDatabase = new MyFileDatabase(0, "./data.txt"); - System.out.println("Start up"); - } + /** + * This contains all the setup logic, it will mainly be focused + * on loading up and creating an instance of the database based + * off a saved file or will create a fresh database if the file + * is not present. + * + * @param args A {@code String[]} of any potential runtime args + */ + public void run(String... args) { + for (String arg : args) { + if ("setup".equals(arg)) { + myFileDatabase = new MyFileDatabase(1, "./data.txt"); + resetDataFile(); + + LOGGER.info("System Setup"); + return; - /** - * Overrides the database reference, used when testing. - * - * @param testData A {@code MyFileDatabase} object referencing test data. - */ - public static void overrideDatabase(MyFileDatabase testData) { - myFileDatabase = testData; - saveData = false; - } + } + } + myFileDatabase = new MyFileDatabase(0, "./data.txt"); + LOGGER.info("Start up"); + } - /** - * Allows for data to be reset in event of errors. - */ - public void resetDataFile() { - String[] times = {"11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55"}; - String[] locations = {"417 IAB", "309 HAV", "301 URIS"}; + /** + * Overrides the database reference, used when testing. + * + * @param testData A {@code MyFileDatabase} object referencing test data. + */ + public static void overrideDatabase(MyFileDatabase testData) { + myFileDatabase = testData; + saveData = false; + } - //data for coms dept - Course coms1004 = new Course("Adam Cannon", locations[0], times[0], 400); - coms1004.setEnrolledStudentCount(249); - Course coms3134 = new Course("Brian Borowski", locations[2], times[1], 250); - coms3134.setEnrolledStudentCount(242); - Course coms3157 = new Course("Jae Lee", locations[0], times[1], 400); - coms3157.setEnrolledStudentCount(311); - Course coms3203 = new Course("Ansaf Salleb-Aouissi", locations[2], times[2], 250); - coms3203.setEnrolledStudentCount(215); - Course coms3261 = new Course("Josh Alman", locations[0], times[3], 150); - coms3261.setEnrolledStudentCount(140); - Course coms3251 = new Course("Tony Dear", "402 CHANDLER", "1:10-3:40", 125); - coms3251.setEnrolledStudentCount(99); - Course coms3827 = new Course("Daniel Rubenstein", "207 Math", times[2], 300); - coms3827.setEnrolledStudentCount(283); - Course coms4156 = new Course("Gail Kaiser", "501 NWC", times[2], 120); - coms4156.setEnrolledStudentCount(109); - HashMap courses = new HashMap<>(); - courses.put("1004", coms1004); - courses.put("3134", coms3134); - courses.put("3157", coms3157); - courses.put("3203", coms3203); - courses.put("3261", coms3261); - courses.put("3251", coms3251); - courses.put("3827", coms3827); - courses.put("4156", coms4156); - Department compSci = new Department("COMS", courses, "Luca Carloni", 2700); - HashMap mapping = new HashMap<>(); - mapping.put("COMS", compSci); + /** + * Allows for data to be reset in event of errors. + */ + public void resetDataFile() { + String[] times = { "11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55" }; + String[] locations = { "417 IAB", "309 HAV", "301 URIS" }; - //data for econ dept - Course econ1105 = new Course("Waseem Noor", locations[1], times[3], 210); - econ1105.setEnrolledStudentCount(187); - Course econ2257 = new Course("Tamrat Gashaw", "428 PUP", times[2], 125); - econ2257.setEnrolledStudentCount(63); - Course econ3211 = new Course("Murat Yilmaz", "310 FAY", times[1], 96); - econ3211.setEnrolledStudentCount(81); - Course econ3213 = new Course("Miles Leahey", "702 HAM", times[1], 86); - econ3213.setEnrolledStudentCount(77); - Course econ3412 = new Course("Thomas Piskula", "702 HAM", times[0], 86); - econ3412.setEnrolledStudentCount(81); - Course econ4415 = new Course("Evan D Sadler", locations[1], times[2], 110); - econ4415.setEnrolledStudentCount(63); - Course econ4710 = new Course("Matthieu Gomez", "517 HAM", "8:40-9:55", 86); - econ4710.setEnrolledStudentCount(37); - Course econ4840 = new Course("Mark Dean", "142 URIS", times[3], 108); - econ4840.setEnrolledStudentCount(67); + // data for coms dept + Course coms1004 = new Course("Adam Cannon", locations[0], times[0], 400); + coms1004.setEnrolledStudentCount(249); + Course coms3134 = new Course("Brian Borowski", locations[2], times[1], 250); + coms3134.setEnrolledStudentCount(242); + Course coms3157 = new Course("Jae Lee", locations[0], times[1], 400); + coms3157.setEnrolledStudentCount(311); + Course coms3203 = new Course("Ansaf Salleb-Aouissi", locations[2], times[2], 250); + coms3203.setEnrolledStudentCount(215); + Course coms3261 = new Course("Josh Alman", locations[0], times[3], 150); + coms3261.setEnrolledStudentCount(140); + Course coms3251 = new Course("Tony Dear", "402 CHANDLER", "1:10-3:40", 125); + coms3251.setEnrolledStudentCount(99); + Course coms3827 = new Course("Daniel Rubenstein", "207 Math", times[2], 300); + coms3827.setEnrolledStudentCount(283); + Course coms4156 = new Course("Gail Kaiser", "501 NWC", times[2], 120); + coms4156.setEnrolledStudentCount(109); + Map courses = new HashMap<>(); + courses.put("1004", coms1004); + courses.put("3134", coms3134); + courses.put("3157", coms3157); + courses.put("3203", coms3203); + courses.put("3261", coms3261); + courses.put("3251", coms3251); + courses.put("3827", coms3827); + courses.put("4156", coms4156); + Department compSci = new Department("COMS", courses, "Luca Carloni", 2700); + Map mapping = new HashMap<>(); + mapping.put("COMS", compSci); - courses = new HashMap<>(); - courses.put("1105", econ1105); - courses.put("2257", econ2257); - courses.put("3211", econ3211); - courses.put("3213", econ3213); - courses.put("3412", econ3412); - courses.put("4415", econ4415); - courses.put("4710", econ4710); - courses.put("4840", econ4840); + // data for econ dept + Course econ1105 = new Course("Waseem Noor", locations[1], times[3], 210); + econ1105.setEnrolledStudentCount(187); + Course econ2257 = new Course("Tamrat Gashaw", "428 PUP", times[2], 125); + econ2257.setEnrolledStudentCount(63); + Course econ3211 = new Course("Murat Yilmaz", "310 FAY", times[1], 96); + econ3211.setEnrolledStudentCount(81); + Course econ3213 = new Course("Miles Leahey", "702 HAM", times[1], 86); + econ3213.setEnrolledStudentCount(77); + Course econ3412 = new Course("Thomas Piskula", "702 HAM", times[0], 86); + econ3412.setEnrolledStudentCount(81); + Course econ4415 = new Course("Evan D Sadler", locations[1], times[2], 110); + econ4415.setEnrolledStudentCount(63); + Course econ4710 = new Course("Matthieu Gomez", "517 HAM", "8:40-9:55", 86); + econ4710.setEnrolledStudentCount(37); + Course econ4840 = new Course("Mark Dean", "142 URIS", times[3], 108); + econ4840.setEnrolledStudentCount(67); - Department econ = new Department("ECON", courses, "Michael Woodford", 2345); - mapping.put("ECON", econ); + courses = new HashMap<>(); + courses.put("1105", econ1105); + courses.put("2257", econ2257); + courses.put("3211", econ3211); + courses.put("3213", econ3213); + courses.put("3412", econ3412); + courses.put("4415", econ4415); + courses.put("4710", econ4710); + courses.put("4840", econ4840); - //data for ieor dept - Course ieor2500 = new Course("Uday Menon", "627 MUDD", times[0], 50); - ieor2500.setEnrolledStudentCount(52); - Course ieor3404 = new Course("Christopher J Dolan", "303 MUDD", times[2], 73); - ieor3404.setEnrolledStudentCount(80); - Course ieor3658 = new Course("Daniel Lacker", "310 FAY", times[2], 96); - ieor3658.setEnrolledStudentCount(87); - Course ieor4102 = new Course("Antonius B Dieker", "209 HAM", times[2], 110); - ieor4102.setEnrolledStudentCount(92); - Course ieor4106 = new Course("Kaizheng Wang", "501 NWC", times[2], 150); - ieor4106.setEnrolledStudentCount(161); - Course ieor4405 = new Course("Yuri Faenza", "517 HAV", times[0], 80); - ieor4405.setEnrolledStudentCount(19); - Course ieor4511 = new Course("Michael Robbins", "633 MUDD", "9:00-11:30", 150); - ieor4511.setEnrolledStudentCount(50); - Course ieor4540 = new Course("Krzysztof M Choromanski", "633 MUDD", "7:10-9:40", 60); - ieor4540.setEnrolledStudentCount(33); + Department econ = new Department("ECON", courses, "Michael Woodford", 2345); + mapping.put("ECON", econ); - courses = new HashMap<>(); - courses.put("2500", ieor2500); - courses.put("3404", ieor3404); - courses.put("3658", ieor3658); - courses.put("4102", ieor4102); - courses.put("4106", ieor4106); - courses.put("4405", ieor4405); - courses.put("4511", ieor4511); - courses.put("4540", ieor4540); + // data for ieor dept + Course ieor2500 = new Course("Uday Menon", "627 MUDD", times[0], 50); + ieor2500.setEnrolledStudentCount(52); + Course ieor3404 = new Course("Christopher J Dolan", "303 MUDD", times[2], 73); + ieor3404.setEnrolledStudentCount(80); + Course ieor3658 = new Course("Daniel Lacker", "310 FAY", times[2], 96); + ieor3658.setEnrolledStudentCount(87); + Course ieor4102 = new Course("Antonius B Dieker", "209 HAM", times[2], 110); + ieor4102.setEnrolledStudentCount(92); + Course ieor4106 = new Course("Kaizheng Wang", "501 NWC", times[2], 150); + ieor4106.setEnrolledStudentCount(161); + Course ieor4405 = new Course("Yuri Faenza", "517 HAV", times[0], 80); + ieor4405.setEnrolledStudentCount(19); + Course ieor4511 = new Course("Michael Robbins", "633 MUDD", "9:00-11:30", 150); + ieor4511.setEnrolledStudentCount(50); + Course ieor4540 = new Course("Krzysztof M Choromanski", "633 MUDD", "7:10-9:40", 60); + ieor4540.setEnrolledStudentCount(33); - Department ieor = new Department("IEOR", courses, "Jay Sethuraman", 67); - mapping.put("IEOR", ieor); + courses = new HashMap<>(); + courses.put("2500", ieor2500); + courses.put("3404", ieor3404); + courses.put("3658", ieor3658); + courses.put("4102", ieor4102); + courses.put("4106", ieor4106); + courses.put("4405", ieor4405); + courses.put("4511", ieor4511); + courses.put("4540", ieor4540); - //data for chem dept - Course chem1403 = new Course("Ruben M Savizky", locations[1], "6:10-7:25", 120); - chem1403.setEnrolledStudentCount(100); - Course chem1500 = new Course("Joseph C Ulichny", "302 HAV", "6:10-9:50", 46); - chem1500.setEnrolledStudentCount(50); - Course chem2045 = new Course("Luis M Campos", "209 HAV", "1:10-2:25", 50); - chem2045.setEnrolledStudentCount(29); - Course chem2444 = new Course("Christopher Eckdahl", locations[1], times[0], 150); - chem2444.setEnrolledStudentCount(150); - Course chem2494 = new Course("Talha Siddiqui", "202 HAV", "1:10-5:00", 24); - chem2494.setEnrolledStudentCount(18); - Course chem3080 = new Course("Milan Delor", "209 HAV", times[2], 60); - chem3080.setEnrolledStudentCount(18); - Course chem4071 = new Course("Jonathan S Owen", "320 HAV", "8:40-9:55", 42); - chem4071.setEnrolledStudentCount(29); - Course chem4102 = new Course("Dalibor Sames", "320 HAV", times[2], 28); - chem4102.setEnrolledStudentCount(27); + Department ieor = new Department("IEOR", courses, "Jay Sethuraman", 67); + mapping.put("IEOR", ieor); - courses = new HashMap<>(); - courses.put("1403", chem1403); - courses.put("1500", chem1500); - courses.put("2045", chem2045); - courses.put("2444", chem2444); - courses.put("2494", chem2494); - courses.put("3080", chem3080); - courses.put("4071", chem4071); - courses.put("4102", chem4102); + // data for chem dept + Course chem1403 = new Course("Ruben M Savizky", locations[1], "6:10-7:25", 120); + chem1403.setEnrolledStudentCount(100); + Course chem1500 = new Course("Joseph C Ulichny", "302 HAV", "6:10-9:50", 46); + chem1500.setEnrolledStudentCount(50); + Course chem2045 = new Course("Luis M Campos", "209 HAV", "1:10-2:25", 50); + chem2045.setEnrolledStudentCount(29); + Course chem2444 = new Course("Christopher Eckdahl", locations[1], times[0], 150); + chem2444.setEnrolledStudentCount(150); + Course chem2494 = new Course("Talha Siddiqui", "202 HAV", "1:10-5:00", 24); + chem2494.setEnrolledStudentCount(18); + Course chem3080 = new Course("Milan Delor", "209 HAV", times[2], 60); + chem3080.setEnrolledStudentCount(18); + Course chem4071 = new Course("Jonathan S Owen", "320 HAV", "8:40-9:55", 42); + chem4071.setEnrolledStudentCount(29); + Course chem4102 = new Course("Dalibor Sames", "320 HAV", times[2], 28); + chem4102.setEnrolledStudentCount(27); - Department chem = new Department("CHEM", courses, "Laura J. Kaufman", 250); - mapping.put("CHEM", chem); + courses = new HashMap<>(); + courses.put("1403", chem1403); + courses.put("1500", chem1500); + courses.put("2045", chem2045); + courses.put("2444", chem2444); + courses.put("2494", chem2494); + courses.put("3080", chem3080); + courses.put("4071", chem4071); + courses.put("4102", chem4102); - //data for phys dept - Course phys1001 = new Course("Szabolcs Marka", "301 PUP", times[3], 150); - phys1001.setEnrolledStudentCount(131); - Course phys1201 = new Course("Eric Raymer", "428 PUP", times[3], 145); - phys1201.setEnrolledStudentCount(130); - Course phys1602 = new Course("Kerstin M Perez", "428 PUP", times[2], 140); - phys1602.setEnrolledStudentCount(77); - Course phys2802 = new Course("Yury Levin", "329 PUP", "10:10-12:00", 60); - phys2802.setEnrolledStudentCount(23); - Course phys3008 = new Course("William A Zajc", "329 PUP", times[2], 75); - phys3008.setEnrolledStudentCount(60); - Course phys4003 = new Course("Frederik Denef", "214 PUP", times[1], 50); - phys4003.setEnrolledStudentCount(19); - Course phys4018 = new Course("James W McIver", "307 PUP", times[3], 30); - phys4018.setEnrolledStudentCount(18); - Course phys4040 = new Course("James C Hill", "214 PUP", times[1], 50); - phys4040.setEnrolledStudentCount(31); + Department chem = new Department("CHEM", courses, "Laura J. Kaufman", 250); + mapping.put("CHEM", chem); - courses = new HashMap<>(); - courses.put("2802", phys2802); - courses.put("3008", phys3008); - courses.put("4003", phys4003); - courses.put("4018", phys4018); - courses.put("4040", phys4040); - courses.put("1602", phys1602); - courses.put("1001", phys1001); - courses.put("1201", phys1201); + // data for phys dept + Course phys1001 = new Course("Szabolcs Marka", "301 PUP", times[3], 150); + phys1001.setEnrolledStudentCount(131); + Course phys1201 = new Course("Eric Raymer", "428 PUP", times[3], 145); + phys1201.setEnrolledStudentCount(130); + Course phys1602 = new Course("Kerstin M Perez", "428 PUP", times[2], 140); + phys1602.setEnrolledStudentCount(77); + Course phys2802 = new Course("Yury Levin", "329 PUP", "10:10-12:00", 60); + phys2802.setEnrolledStudentCount(23); + Course phys3008 = new Course("William A Zajc", "329 PUP", times[2], 75); + phys3008.setEnrolledStudentCount(60); + Course phys4003 = new Course("Frederik Denef", "214 PUP", times[1], 50); + phys4003.setEnrolledStudentCount(19); + Course phys4018 = new Course("James W McIver", "307 PUP", times[3], 30); + phys4018.setEnrolledStudentCount(18); + Course phys4040 = new Course("James C Hill", "214 PUP", times[1], 50); + phys4040.setEnrolledStudentCount(31); - Department phys = new Department("PHYS", courses, "Dmitri N. Basov", 43); - mapping.put("PHYS", phys); + courses = new HashMap<>(); + courses.put("2802", phys2802); + courses.put("3008", phys3008); + courses.put("4003", phys4003); + courses.put("4018", phys4018); + courses.put("4040", phys4040); + courses.put("1602", phys1602); + courses.put("1001", phys1001); + courses.put("1201", phys1201); - //data for elen dept - Course elen1201 = new Course("David G Vallancourt", "301 PUP", times[1], 120); - elen1201.setEnrolledStudentCount(108); - Course elen3082 = new Course("Kenneth Shepard", "1205 MUDD", "4:10-6:40", 32); - elen3082.setEnrolledStudentCount(30); - Course elen3331 = new Course("David G Vallancourt", "203 MATH", times[0], 80); - elen3331.setEnrolledStudentCount(54); - Course elen3401 = new Course("Keren Bergman", "829 MUDD", times[3], 40); - elen3401.setEnrolledStudentCount(25); - Course elen3701 = new Course("Irving Kalet", "333 URIS", times[3], 50); - elen3701.setEnrolledStudentCount(24); - Course elen4510 = new Course("Mohamed Kamaludeen", "903 SSW", "7:00-9:30", 30); - elen4510.setEnrolledStudentCount(22); - Course elen4702 = new Course("Alexei Ashikhmin", "332 URIS", "7:00-9:30", 50); - elen4702.setEnrolledStudentCount(5); - Course elen4830 = new Course("Christine P Hendon", "633 MUDD", "10:10-12:40", 60); - elen4830.setEnrolledStudentCount(22); + Department phys = new Department("PHYS", courses, "Dmitri N. Basov", 43); + mapping.put("PHYS", phys); - courses = new HashMap<>(); - courses.put("1201", elen1201); - courses.put("3082", elen3082); - courses.put("3331", elen3331); - courses.put("3401", elen3401); - courses.put("3701", elen3701); - courses.put("4510", elen4510); - courses.put("4702", elen4702); - courses.put("4830", elen4830); + // data for elen dept + Course elen1201 = new Course("David G Vallancourt", "301 PUP", times[1], 120); + elen1201.setEnrolledStudentCount(108); + Course elen3082 = new Course("Kenneth Shepard", "1205 MUDD", "4:10-6:40", 32); + elen3082.setEnrolledStudentCount(30); + Course elen3331 = new Course("David G Vallancourt", "203 MATH", times[0], 80); + elen3331.setEnrolledStudentCount(54); + Course elen3401 = new Course("Keren Bergman", "829 MUDD", times[3], 40); + elen3401.setEnrolledStudentCount(25); + Course elen3701 = new Course("Irving Kalet", "333 URIS", times[3], 50); + elen3701.setEnrolledStudentCount(24); + Course elen4510 = new Course("Mohamed Kamaludeen", "903 SSW", "7:00-9:30", 30); + elen4510.setEnrolledStudentCount(22); + Course elen4702 = new Course("Alexei Ashikhmin", "332 URIS", "7:00-9:30", 50); + elen4702.setEnrolledStudentCount(5); + Course elen4830 = new Course("Christine P Hendon", "633 MUDD", "10:10-12:40", 60); + elen4830.setEnrolledStudentCount(22); - Department elen = new Department("ELEN", courses, "Ioannis Kymissis", 250); - mapping.put("ELEN", elen); + courses = new HashMap<>(); + courses.put("1201", elen1201); + courses.put("3082", elen3082); + courses.put("3331", elen3331); + courses.put("3401", elen3401); + courses.put("3701", elen3701); + courses.put("4510", elen4510); + courses.put("4702", elen4702); + courses.put("4830", elen4830); - //data for psyc dept - Course psyc1001 = new Course("Patricia G Lindemann", "501 SCH", "1:10-2:25", 200); - psyc1001.setEnrolledStudentCount(191); - Course psyc1610 = new Course("Christopher Baldassano", "200 SCH", times[2], 45); - psyc1610.setEnrolledStudentCount(42); - Course psyc2235 = new Course("Katherine T Fox-Glassman", "501 SCH", times[0], 125); - psyc2235.setEnrolledStudentCount(128); - Course psyc2620 = new Course("Jeffrey M Cohen", "303 URIS", "1:10-3:40", 60); - psyc2620.setEnrolledStudentCount(55); - Course psyc3212 = new Course("Mayron Piccolo", "200 SCH", "2:10-4:00", 15); - psyc3212.setEnrolledStudentCount(15); - Course psyc3445 = new Course("Mariam Aly", "405 SCH", "2:10-4:00", 12); - psyc3445.setEnrolledStudentCount(12); - Course psyc4236 = new Course("Trenton Jerde", "405 SCH", "6:10-8:00", 18); - psyc4236.setEnrolledStudentCount(17); - Course psyc4493 = new Course("Jennifer Blaze", "200 SCH", "2:10-4:00", 15); - psyc4493.setEnrolledStudentCount(9); + Department elen = new Department("ELEN", courses, "Ioannis Kymissis", 250); + mapping.put("ELEN", elen); - courses = new HashMap<>(); - courses.put("1001", psyc1001); - courses.put("1610", psyc1610); - courses.put("2235", psyc2235); - courses.put("2620", psyc2620); - courses.put("3212", psyc3212); - courses.put("3445", psyc3445); - courses.put("4236", psyc4236); - courses.put("4493", psyc4493); + // data for psyc dept + Course psyc1001 = new Course("Patricia G Lindemann", "501 SCH", "1:10-2:25", 200); + psyc1001.setEnrolledStudentCount(191); + Course psyc1610 = new Course("Christopher Baldassano", "200 SCH", times[2], 45); + psyc1610.setEnrolledStudentCount(42); + Course psyc2235 = new Course("Katherine T Fox-Glassman", "501 SCH", times[0], 125); + psyc2235.setEnrolledStudentCount(128); + Course psyc2620 = new Course("Jeffrey M Cohen", "303 URIS", "1:10-3:40", 60); + psyc2620.setEnrolledStudentCount(55); + Course psyc3212 = new Course("Mayron Piccolo", "200 SCH", "2:10-4:00", 15); + psyc3212.setEnrolledStudentCount(15); + Course psyc3445 = new Course("Mariam Aly", "405 SCH", "2:10-4:00", 12); + psyc3445.setEnrolledStudentCount(12); + Course psyc4236 = new Course("Trenton Jerde", "405 SCH", "6:10-8:00", 18); + psyc4236.setEnrolledStudentCount(17); + Course psyc4493 = new Course("Jennifer Blaze", "200 SCH", "2:10-4:00", 15); + psyc4493.setEnrolledStudentCount(9); - Department psyc = new Department("PSYC", courses, "Nim Tottenham", 437); - mapping.put("PSYC", psyc); + courses = new HashMap<>(); + courses.put("1001", psyc1001); + courses.put("1610", psyc1610); + courses.put("2235", psyc2235); + courses.put("2620", psyc2620); + courses.put("3212", psyc3212); + courses.put("3445", psyc3445); + courses.put("4236", psyc4236); + courses.put("4493", psyc4493); - myFileDatabase.setMapping(mapping); - } + Department psyc = new Department("PSYC", courses, "Nim Tottenham", 437); + mapping.put("PSYC", psyc); - /** - * This contains all the overheading teardown logic, it will - * mainly be focused on saving all the created user data to a - * file, so it will be ready for the next setup. - */ - @PreDestroy - public void onTermination() { - System.out.println("Termination"); - if (saveData) { - myFileDatabase.saveContentsToFile(); - } - } + myFileDatabase.setMapping(mapping); + } + /** + * This contains all the overheading teardown logic, it will + * mainly be focused on saving all the created user data to a + * file, so it will be ready for the next setup. + */ + @PreDestroy + public void onTermination() { + // System.out.println("Termination"); + LOGGER.info("Termination"); + if (saveData) { + myFileDatabase.saveContentsToFile(); + } + } - //Database Instance - public static MyFileDatabase myFileDatabase; - private static boolean saveData = true; + // Database Instance + public static MyFileDatabase myFileDatabase; + private static boolean saveData = true; } diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java index 1f61f893..1f5c03b7 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java @@ -1,13 +1,24 @@ package dev.coms4156.project.individualproject; -import java.io.*; -import java.util.*; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.springframework.stereotype.Service; /** * This class represents a file-based database containing department mappings. */ +// @Service // Mark it as a Spring-managed service public class MyFileDatabase { + private static final Logger LOGGER = Logger.getLogger(MyFileDatabase.class.getName()); + /** * Constructs a MyFileDatabase object and loads up the data structure with * the contents of the file. @@ -27,7 +38,7 @@ public MyFileDatabase(int flag, String filePath) { * * @param mapping the mapping of department names to Department objects */ - public void setMapping(HashMap mapping) { + public void setMapping(Map mapping) { this.departmentMapping = mapping; } @@ -36,16 +47,16 @@ public void setMapping(HashMap mapping) { * * @return the deserialized department mapping */ - public HashMap deSerializeObjectFromFile() { + public Map deSerializeObjectFromFile() { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath))) { Object obj = in.readObject(); - if (obj instanceof HashMap) { - return (HashMap) obj; + if (obj instanceof Map) { + return (Map) obj; } else { throw new IllegalArgumentException("Invalid object type in file."); } } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); + LOGGER.log(Level.SEVERE, "An error occurred", e); return null; } } @@ -57,9 +68,9 @@ public HashMap deSerializeObjectFromFile() { public void saveContentsToFile() { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath))) { out.writeObject(departmentMapping); - System.out.println("Object serialized successfully."); + LOGGER.info("Object serialized successfully."); } catch (IOException e) { - e.printStackTrace(); + LOGGER.log(Level.SEVERE, "An error occurred", e); } } @@ -68,7 +79,7 @@ public void saveContentsToFile() { * * @return the department mapping */ - public HashMap getDepartmentMapping() { + public Map getDepartmentMapping() { return this.departmentMapping; } @@ -92,5 +103,5 @@ public String toString() { private String filePath; /** The mapping of department names to Department objects. */ - private HashMap departmentMapping; + private Map departmentMapping; } 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 09f504dc..f313eb87 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java @@ -1,8 +1,18 @@ package dev.coms4156.project.individualproject; -import java.util.HashMap; -import org.springframework.http.*; -import org.springframework.web.bind.annotation.*; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + + /** * This class contains all the API routes for the system. @@ -10,6 +20,25 @@ @RestController public class RouteController { + private static final Logger LOGGER = Logger.getLogger(RouteController.class.getName()); + + /* + Following 2 bolcks are for the dependency injection(mocking) + */ + // @Autowired + // private final MyFileDatabase myFileDatabase; + + // @Autowired + // public RouteController(MyFileDatabase myFileDatabase) { + // this.myFileDatabase = myFileDatabase; + // } + // private final MyFileDatabase myFileDatabase; + + // Use constructor injection + // @Autowired + // public RouteController(MyFileDatabase myFileDatabase) { + // this.myFileDatabase = myFileDatabase; + // } /** * Redirects to the homepage. * @@ -34,14 +63,15 @@ public String index() { @GetMapping(value = "/retrieveDept", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity retrieveDepartment(@RequestParam(value = "deptCode") String deptCode) { try { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - + // print mapping + // System.out.println(departmentMapping); 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) { @@ -64,23 +94,26 @@ public ResponseEntity retrieveDepartment(@RequestParam(value = "deptCode") St * proper response. */ @GetMapping(value = "/retrieveCourse", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity retrieveCourse(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity retrieveCourse( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; if (doesDepartmentExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); if (!coursesMapping.containsKey(Integer.toString(courseCode))) { return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND); } else { return new ResponseEntity<>(coursesMapping.get(Integer.toString(courseCode)).toString(), - HttpStatus.FORBIDDEN); + HttpStatus.OK); } } + LOGGER.info("dept not found"); return new ResponseEntity<>("Department Not Found", HttpStatus.NOT_FOUND); } catch (Exception e) { return handleException(e); @@ -101,15 +134,17 @@ public ResponseEntity retrieveCourse(@RequestParam(value = "deptCode") String * response. */ @GetMapping(value = "/isCourseFull", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity isCourseFull(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity isCourseFull( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -138,12 +173,12 @@ public ResponseEntity getMajorCtFromDept(@RequestParam(value = "deptCode") St try { boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; if (doesDepartmentExists) { - HashMap departmentMapping; + Map 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); } @@ -164,7 +199,7 @@ public ResponseEntity identifyDeptChair(@RequestParam(value = "deptCode") Str try { boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; if (doesDepartmentExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); return new ResponseEntity<>(departmentMapping.get(deptCode).getDepartmentChair() + " is " + "the department chair.", HttpStatus.OK); @@ -189,15 +224,17 @@ public ResponseEntity identifyDeptChair(@RequestParam(value = "deptCode") Str * proper response. */ @GetMapping(value = "/findCourseLocation", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity findCourseLocation(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity findCourseLocation( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -226,15 +263,17 @@ public ResponseEntity findCourseLocation(@RequestParam(value = "deptCode") St * response. */ @GetMapping(value = "/findCourseInstructor", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity findCourseInstructor(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity findCourseInstructor( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -263,19 +302,21 @@ public ResponseEntity findCourseInstructor(@RequestParam(value = "deptCode") * indicating the proper response. */ @GetMapping(value = "/findCourseTime", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity findCourseTime(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity findCourseTime( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; 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); @@ -299,7 +340,7 @@ public ResponseEntity addMajorToDept(@RequestParam(value = "deptCode") String try { boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; if (doesDepartmentExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); Department specifiedDept = departmentMapping.get(deptCode); @@ -326,7 +367,7 @@ public ResponseEntity removeMajorFromDept(@RequestParam(value = "deptCode") S try { boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; if (doesDepartmentExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); Department specifiedDept = departmentMapping.get(deptCode); @@ -351,19 +392,21 @@ public ResponseEntity removeMajorFromDept(@RequestParam(value = "deptCode") S * code in tune with what has happened. */ @PatchMapping(value = "/dropStudentFromCourse", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity dropStudent(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity dropStudent( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); - boolean isStudentDropped = requestedCourse.dropStudent(); + boolean isStudentDropped = requestedCourse.dropStudent(courseCode); if (isStudentDropped) { return new ResponseEntity<>("Student has been dropped.", HttpStatus.OK); @@ -378,17 +421,29 @@ public ResponseEntity dropStudent(@RequestParam(value = "deptCode") String de } } - + /** + * Attempts to set a student from the specified course. + * + * @param deptCode A {@code String} representing the department. + * @param courseCode A {@code int} representing the course within the department. + * + * @return A {@code ResponseEntity} object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. + */ @PatchMapping(value = "/setEnrollmentCount", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity setEnrollmentCount(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "count") int count) { + public ResponseEntity setEnrollmentCount( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "count") int count) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -415,15 +470,18 @@ public ResponseEntity setEnrollmentCount(@RequestParam(value = "deptCode") St * successful, or an error message if the course is not found */ @PatchMapping(value = "/changeCourseTime", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity changeCourseTime(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "time") String time) { + public ResponseEntity changeCourseTime( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "time") String time) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -451,15 +509,18 @@ public ResponseEntity changeCourseTime(@RequestParam(value = "deptCode") Stri * successful, or an error message if the course is not found */ @PatchMapping(value = "/changeCourseTeacher", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity changeCourseTeacher(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "teacher") String teacher) { + public ResponseEntity changeCourseTeacher( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "teacher") String teacher) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -473,17 +534,28 @@ public ResponseEntity changeCourseTeacher(@RequestParam(value = "deptCode") S } } - + /** + * Def of ResponseEntity. + * + * @param deptCode A {@code String} representing the department. + * @param courseCode A {@code int} representing the course within the department. + * @param location A {@code String} representing the course location. + * + * @return A {@code ResponseEntity} object containing an HTTP 200. + */ @PatchMapping(value = "/changeCourseLocation", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity changeCourseLocation(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "location") String location) { + public ResponseEntity changeCourseLocation( + @RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "location") String location) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; if (doesCourseExists) { - HashMap departmentMapping; + Map departmentMapping; departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping(); - HashMap coursesMapping; + Map coursesMapping; coursesMapping = departmentMapping.get(deptCode).getCourseSelection(); Course requestedCourse = coursesMapping.get(Integer.toString(courseCode)); @@ -498,7 +570,7 @@ public ResponseEntity changeCourseLocation(@RequestParam(value = "deptCode") } private ResponseEntity handleException(Exception e) { - System.out.println(e.toString()); + LOGGER.log(Level.SEVERE, "An error occurred", e); return new ResponseEntity<>("An Error has occurred", HttpStatus.OK); } 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 4edd00f9..7a1b1ae7 100644 --- a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java @@ -1,18 +1,32 @@ package dev.coms4156.project.individualproject; -import org.junit.jupiter.api.*; +// import org.junit.jupiter.api.*; +// import static org.junit.jupiter.api.Assertions.*; +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 org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; -import static org.junit.jupiter.api.Assertions.*; - +/** + * Unit tests for the Course class. +*/ @SpringBootTest @ContextConfiguration public class CourseUnitTests { + /** + * setupCourseForTesting. + */ @BeforeAll public static void setupCourseForTesting() { testCourse = new Course("Griffin Newbold", "417 IAB", "11:40-12:55", 250); + testCourse.setEnrolledStudentCount(100); + testCourse2 = new Course("Griffin Newbold", "417 IAB", "11:40-12:55", 250); + testCourse2.setEnrolledStudentCount(250); } @@ -22,7 +36,22 @@ public void toStringTest() { assertEquals(expectedResult, testCourse.toString()); } + @Test + public void testEnrollStudent() { + boolean result; + result = testCourse.enrollStudent(); + assertTrue(result); + } + + @Test + public void testEnrollStudentFalse() { + testCourse2.setEnrolledStudentCount(250); + boolean result; + result = testCourse2.enrollStudent(); + assertFalse(result); + } /** The test course instance used for testing. */ + public static Course testCourse; -} - + public static Course testCourse2; +} \ No newline at end of file diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java new file mode 100644 index 00000000..3810e95b --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java @@ -0,0 +1,49 @@ +package dev.coms4156.project.individualproject; + +// import org.junit.jupiter.api.*; +// import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashMap; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; + + +/** + * DepartmentUnitTests. + */ +@SpringBootTest +@ContextConfiguration +public class DepartmentUnitTests { + + @Test + public void testCreateCourse() { + + // Create test data + String[] times = { "11:40-12:55" }; + String[] locations = { "417 IAB" }; + + Course course1 = new Course("Andrew", locations[0], times[0], 400); + + HashMap courses1 = new HashMap<>(); + courses1.put("1001", course1); + + + Department department = new Department("CS", courses1, "Andrew", 500); + + + // Create test data2 + String[] times2 = { "11:40-12:52" }; + String[] locations2 = { "417 IAC" }; + + Course course2 = new Course("Andrew", locations[0], times[0], 400); + + HashMap courses2 = new HashMap<>(); + courses2.put("1002", course1); + + department.createCourse("1002", "Griffin Newbold", "417 IAC", "11:40-12:52", 500); + } + +} \ No newline at end of file diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/IndividualProjectAppicationUnitTest.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/IndividualProjectAppicationUnitTest.java new file mode 100644 index 00000000..c7a64955 --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/IndividualProjectAppicationUnitTest.java @@ -0,0 +1,76 @@ +package dev.coms4156.project.individualproject; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + +class IndividualProjectAppicationUnitTest { + + private IndividualProjectApplication application; + + @BeforeEach + void setUp() { + // Initialize the application + application = new IndividualProjectApplication(); + application.myFileDatabase = new MyFileDatabase(1, "testfile.txt"); // Dummy path for test + } + + @Test + void testResetDataFile() { + application.resetDataFile(); + + Map departmentMapping = application + .myFileDatabase.getDepartmentMapping(); + + assertNotNull(departmentMapping); + + // Verify that the correct departments were added + assertTrue(departmentMapping.containsKey("COMS")); + assertTrue(departmentMapping.containsKey("ECON")); + assertTrue(departmentMapping.containsKey("IEOR")); + assertTrue(departmentMapping.containsKey("CHEM")); + assertTrue(departmentMapping.containsKey("PHYS")); + assertTrue(departmentMapping.containsKey("ELEN")); + assertTrue(departmentMapping.containsKey("PSYC")); + + // // Verify some course details for one department (e.g., COMS) + // Department compSci = departmentMapping.get("COMS"); + // assertEquals("Luca Carloni", compSci.getChair()); + // assertEquals(2700, compSci.getNumberOfStudents()); + + // // Verify that a specific course exists in the COMS department + // Course coms1004 = compSci.getCourses().get("1004"); + // assertNotNull(coms1004); + // assertEquals("Adam Cannon", coms1004.getInstructor()); + // assertEquals(249, coms1004.getEnrolledStudentCount()); + } + + @Test + void testRun() { + String[] args = new String[]{"setup"}; + application.run(args); + } + + @Test + void testMain() { + String[] args = new String[]{"setup"}; + application.main(args); + } + + @Test + void testonTermination() { + // application.saveData = true; + application.onTermination(); + } + + @Test + void testOverrideDatabase() { + application.overrideDatabase(application.myFileDatabase); + } + +} diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyfileDatabaseUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyfileDatabaseUnitTests.java new file mode 100644 index 00000000..29859b8f --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyfileDatabaseUnitTests.java @@ -0,0 +1,90 @@ +package dev.coms4156.project.individualproject; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.HashMap; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; + + +/** + * def of MyfileDatabaseUnitTests. +*/ +@SpringBootTest +@ContextConfiguration +public class MyfileDatabaseUnitTests { + + @Test + public void testToString() { + + // Create test data + String[] times = { "11:40-12:55" }; + String[] locations = { "417 IAB" }; + + Course course1 = new Course("Andrew", locations[0], times[0], 400); + + HashMap courses1 = new HashMap<>(); + courses1.put("1001", course1); + + + Department department1 = new Department("CS", courses1, "Andrew", 500); + + HashMap departmentMapping = new HashMap<>(); + departmentMapping.put("CS", department1); + + // Create the MyFileDatabase instance and set the mapping + MyFileDatabase db = new MyFileDatabase(1, "dummyPath"); + // The filePath is not used in this test + db.setMapping(departmentMapping); + + // Expected string representation + String expectedString = + "For the CS department: \n" + + "CS 1001: \n" + + "Instructor: Andrew; Location: 417 IAB; Time: 11:40-12:55\n"; + + System.err.println(db.toString()); + // Assert that the toString() output matches the expected result + assertEquals(expectedString, db.toString()); + } + + @Test + public void testSaveContentsToFile() { + + // Create test data + String[] times = { "11:40-12:55" }; + String[] locations = { "417 IAB" }; + + Course course1 = new Course("Andrew", locations[0], times[0], 400); + + HashMap courses1 = new HashMap<>(); + courses1.put("1001", course1); + + + Department department1 = new Department("CS", courses1, "Andrew", 500); + + HashMap departmentMapping = new HashMap<>(); + departmentMapping.put("CS", department1); + + // Create the MyFileDatabase instance and set the mapping + MyFileDatabase db = new MyFileDatabase(1, "dummyPath"); // The filePath is not used in this test + + db.saveContentsToFile(); + } + + @Test + public void testDeSerializeObjectFromFile() { + // Create the MyFileDatabase instance and set the mapping + MyFileDatabase db = new MyFileDatabase(1, "dummyPath"); // The filePath is not used in this test + + // db.deSerializeObjectFromFile(); + // Assert that the method throws IllegalArgumentException + assertThrows(IllegalArgumentException.class, () -> { + db.deSerializeObjectFromFile(); + }); + } + +} diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java new file mode 100644 index 00000000..419018a3 --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java @@ -0,0 +1,383 @@ +package dev.coms4156.project.individualproject; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.HashMap; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + + + + + +/** + * def of RouteControllerUnitTests. + */ +@ExtendWith(MockitoExtension.class) +@SpringJUnitConfig +@WebMvcTest(RouteController.class) // This will only load the web layer +public class RouteControllerUnitTests { + + @Autowired + private MockMvc mockMvc; + + // @MockBean + // private MyFileDatabase myFileDatabase; + + // // Mock the IndividualProjectApplication or methods if needed + // @MockBean + // private IndividualProjectApplication individualProjectApplication; + + + // @Test + // public void testRetrieveDepartmentFound2() throws Exception { + // // Mock the behavior of the database service + // String[] times = { "11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55" }; + // String[] locations = { "417 IAB", "309 HAV", "301 URIS" }; + // Course coms9999 = new Course("Andrew", locations[0], times[0], 400); + // coms9999.setEnrolledStudentCount(249); + // HashMap courses = new HashMap<>(); + // courses.put("9999", coms9999); + // Department mockCompSci = new Department("COMS",courses, "Andrew", 1000); + // HashMap mockDepartmentMapping = new HashMap<>(); + // mockDepartmentMapping.put("COMS", mockCompSci); + + // when(myFileDatabase.getDepartmentMapping()).thenReturn(mockDepartmentMapping); + + // mockMvc.perform(get("/retrieveDept").param("deptCode", "COMS")) + // .andExpect(status().isOk()) + // .andExpect(content().string("COMS 1004: \nInstructor: + // Daniel Rubenstein; Location: 207 Math; Time: 10:10-11:25\n")); + // } + + @Test + public void testIndexEndpoint() throws Exception { + // Expected response from the index() method + String expectedResponse = + "Welcome, in order to make an API call direct your browser or Postman to an endpoint " + + "\n\n This can be done using the following format: \n\n http:127.0.0" + + ".1:8080/endpoint?arg=value"; + + // Perform the GET request and capture the result + MvcResult result = mockMvc.perform(get("/")) + .andExpect(status().isOk()) // Check if the status is OK (200) + .andReturn(); // Return the result + + // Print the response content + String content = result.getResponse().getContentAsString(); + System.out.println("Response : " + content); + + // Perform GET request to "/", "/index", or "/home" + mockMvc.perform(get("/")) + .andExpect(status().isOk()) // Check if HTTP status is 200 OK + .andExpect(content().string(expectedResponse)); // Check if response body matches + } + + @Test + public void testRetrieveDepartmentNotFound() throws Exception { + mockMvc.perform(get("/retrieveDept").param("deptCode", "NonExistingDept")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Department Not Found")); + } + + @Test + public void testRetrieveDepartmentFound() throws Exception { + mockMvc.perform(get("/retrieveDept").param("deptCode", "COMS")) + .andExpect(status().isOk()) + .andExpect(content().string("COMS 3827: \nInstructor: " + + "Daniel Rubenstein; Location: " + + "207 Math; Time: 10:10-11:25\nCOMS 1004: \nInstructor:" + + " Adam Cannon; Location: 417 IAB;" + + " Time: 11:40-12:55\nCOMS 3203: \nInstructor: Ansaf " + + "Salleb-Aouissi; Location: 301 URIS;" + + " Time: 10:10-11:25\nCOMS 4156: \nInstructor: Gail Kaiser;" + + " Location: 501 NWC; Time: " + + "10:10-11:25\nCOMS 3157: \nInstructor: Jae Lee; Location: " + + "417 IAB; Time: 4:10-5:25\nCOMS" + + " 3134: \nInstructor: Brian Borowski; Location: " + + "301 URIS; Time: 4:10-5:25\nCOMS 3251: \n" + + "Instructor: Tony Dear; Location: 402 CHANDLER; " + + "Time: 1:10-3:40\nCOMS 3261: \nInstructor:" + + " Josh Alman; Location: 417 IAB; Time: 2:40-3:55\n")); + } + + + + + @Test + public void testRetrieveCourseFound() throws Exception { + + mockMvc.perform(get("/retrieveCourse").param("deptCode", "COMS") + .param("courseCode", "4156")) + .andExpect(status().isOk()) + .andExpect(content() + .string("\nInstructor: Gail Kaiser; Location: 501 NWC; Time: 10:10-11:25")); + } + + @Test + public void testRetrieveCourseNotFound() throws Exception { + // Perform the GET request and capture the result + MvcResult result = mockMvc.perform(get("/retrieveCourse") + .param("deptCode", "COMS").param("courseCode", "41")) + .andReturn(); // Return the result + // Print the response content + String content = result.getResponse().getContentAsString(); + System.out.println("Response : " + content); + + mockMvc.perform(get("/retrieveCourse").param("deptCode", "COMS") + .param("courseCode", "41")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + + @Test + public void testRetrieveCourseDeptNotFound() throws Exception { + // Perform the GET request and capture the result + MvcResult result = mockMvc.perform(get("/retrieveCourse") + .param("deptCode", "COS").param("courseCode", "41")) + .andReturn(); // Return the result + // Print the response content + String content = result.getResponse().getContentAsString(); + System.out.println("Response : " + content); + + mockMvc.perform(get("/retrieveCourse").param("deptCode", "COS") + .param("courseCode", "41")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Department Not Found")); + } + + @Test + public void testIsCourseFull() throws Exception { + mockMvc.perform(get("/isCourseFull").param("deptCode", "COMS") + .param("courseCode", "4156")) + .andExpect(status().isOk()) + .andExpect(content().string("true")); + } + + @Test + public void testIsCourseFullNotFound() throws Exception { + mockMvc.perform(get("/isCourseFull").param("deptCode", "COMS") + .param("courseCode", "416")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + + @Test + public void testgetMajorCtFromDept() throws Exception { + mockMvc.perform(get("/getMajorCountFromDept").param("deptCode", "COMS")) + .andExpect(status().isOk()) + .andExpect(content().string("There are: 2700 majors in the department")); + } + + @Test + public void testgetMajorCtFromDeptNotFound() throws Exception { + mockMvc.perform(get("/getMajorCountFromDept").param("deptCode", "nonExisting")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Department Not Found")); + } + + @Test + public void testidDeptChair() throws Exception { + mockMvc.perform(get("/idDeptChair").param("deptCode", "COMS")) + .andExpect(status().isOk()) + .andExpect(content().string("Luca Carloni is the department chair.")); + } + + @Test + public void testidDeptChairNotFound() throws Exception { + mockMvc.perform(get("/idDeptChair").param("deptCode", "nonExisting")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Department Not Found")); + } + + + @Test + public void testFindCourseLocation() throws Exception { + mockMvc.perform(get("/findCourseLocation").param("deptCode", "COMS") + .param("courseCode", "4156")) + .andExpect(status().isOk()) + .andExpect(content().string("501 NWC is where the course is located.")); + } + + @Test + public void testFindCourseLocationNotFound() throws Exception { + mockMvc.perform(get("/findCourseLocation").param("deptCode", "COMS") + .param("courseCode", "0")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + @Test + public void testFindCourseInstructor() throws Exception { + mockMvc.perform(get("/findCourseInstructor").param("deptCode", "COMS") + .param("courseCode", "4156")) + .andExpect(status().isOk()) + .andExpect(content().string("Gail Kaiser is the instructor for the course.")); + } + + @Test + public void testFindCourseInstructorNotFound() throws Exception { + mockMvc.perform(get("/findCourseInstructor").param("deptCode", "COMS") + .param("courseCode", "0")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + @Test + public void testFindCourseTimer() throws Exception { + mockMvc.perform(get("/findCourseTime").param("deptCode", "COMS").param("courseCode", "4156")) + .andExpect(status().isOk()) + .andExpect(content().string("The course meets at: 10:10-11:25")); + } + + @Test + public void testFindCourseTimeNotFound() throws Exception { + mockMvc.perform(get("/findCourseTime").param("deptCode", "COMS").param("courseCode", "0")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + /* + * start modifying the database (IEOR), need to mock the database + */ + + @Test + public void testAddMajorToDept() throws Exception { + mockMvc.perform(patch("/addMajorToDept").param("deptCode", "IEOR")) + .andExpect(status().isOk()) + .andExpect(content().string("Attribute was updated successfully")); + } + + @Test + public void testAddMajorToDeptNotFound() throws Exception { + mockMvc.perform(patch("/addMajorToDept").param("deptCode", "nonExisting")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Department Not Found")); + } + + @Test + public void testRemoveMajorFromDept() throws Exception { + mockMvc.perform(patch("/removeMajorFromDept").param("deptCode", "IEOR")) + .andExpect(status().isOk()) + .andExpect(content().string("Attribute was updated or is at minimum")); + } + + @Test + public void testRemoveMajorFromDeptNotFound() throws Exception { + mockMvc.perform(patch("/removeMajorFromDept").param("deptCode", "nonExisting")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Department Not Found")); + } + + @Test + public void testDropStudentFromCourset() throws Exception { + mockMvc.perform(patch("/dropStudentFromCourse").param("deptCode", "IEOR") + .param("courseCode", "2500")) + .andExpect(status().isOk()) + .andExpect(content().string("Student has been dropped.")); + } + + @Test + public void testDropStudentFromCoursetFail() throws Exception { + mockMvc.perform(patch("/dropStudentFromCourse").param("deptCode", "IEOR") + .param("courseCode", "3404")) + .andExpect(status().isBadRequest()) + .andExpect(content().string("Student has not been dropped.")); + } + + @Test + public void testDropStudentFromCourseNotFound() throws Exception { + mockMvc.perform(patch("/dropStudentFromCourse").param("deptCode", "nonExisting") + .param("courseCode", "2500")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + @Test + public void testSetEnrollmentCount() throws Exception { + mockMvc.perform(patch("/setEnrollmentCount").param("deptCode", "IEOR") + .param("courseCode", "2500").param("count", "2")) + .andExpect(status().isOk()) + .andExpect(content().string("Attributed was updated successfully.")); + } + + @Test + public void testSetEnrollmentCountNotFound() throws Exception { + mockMvc.perform(patch("/setEnrollmentCount").param("deptCode", "nonExisting") + .param("courseCode", "2500").param("count", "2")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + @Test + public void testChangeCourseTime() throws Exception { + mockMvc.perform(patch("/changeCourseTime").param("deptCode", "IEOR") + .param("courseCode", "2500").param("time", "9:00-11:30")) + .andExpect(status().isOk()) + .andExpect(content().string("Attributed was updated successfully.")); + } + + @Test + public void testChangeCourseTimeNotFound() throws Exception { + mockMvc.perform(patch("/changeCourseTime").param("deptCode", "nonExisting") + .param("courseCode", "2500").param("time", "9:00-11:30")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + @Test + public void testChangeCourseTeacher() throws Exception { + mockMvc.perform(patch("/changeCourseTeacher").param("deptCode", "IEOR") + .param("courseCode", "2500").param("teacher", "Michael Robbins")) + .andExpect(status().isOk()) + .andExpect(content().string("Attributed was updated successfully.")); + } + + @Test + public void testChangeCourseTeacherNotFound() throws Exception { + mockMvc.perform(patch("/changeCourseTeacher").param("deptCode", "nonExisting") + .param("courseCode", "2500").param("teacher", "Michael Robbins")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + @Test + public void testChangeCourseLocation() throws Exception { + mockMvc.perform(patch("/changeCourseLocation").param("deptCode", "IEOR") + .param("courseCode", "2500").param("location", "633 MUDD")) + .andExpect(status().isOk()) + .andExpect(content().string("Attributed was updated successfully.")); + } + + @Test + public void testChangeCourseLocationNotFound() throws Exception { + mockMvc.perform(patch("/changeCourseLocation").param("deptCode", "nonExisting") + .param("courseCode", "2500").param("location", "633 MUDD")) + .andExpect(status().isNotFound()) + .andExpect(content().string("Course Not Found")); + } + + //exception + // @Test + // public void testException() throws Exception { + // when(myFileDatabase.getDepartmentMapping()) + // .thenThrow(new RuntimeException("Database error")); + // mockMvc.perform(patch("/changeCourseLocation").param("deptCode", " + // nonExisting").param("courseCode", "2500ddd").param("location", "633 MUDD")) + // .andExpect(status().isOk()) + // .andExpect(content().string("An Error has occurred")); + // } + +} diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouterControllerExceptionUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouterControllerExceptionUnitTests.java new file mode 100644 index 00000000..878d47c6 --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouterControllerExceptionUnitTests.java @@ -0,0 +1,45 @@ +// package dev.coms4156.project.individualproject; + +// import org.junit.jupiter.api.Test; +// import org.junit.jupiter.api.extension.ExtendWith; +// import org.mockito.junit.jupiter.MockitoExtension; +// import org.springframework.beans.factory.annotation.Autowired; +// import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +// import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +// import org.springframework.test.web.servlet.MockMvc; +// import org.springframework.test.web.servlet.MvcResult; +// import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +// import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +// import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +// import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +// import org.springframework.boot.test.mock.mockito.MockBean; +// import static org.mockito.Mockito.doThrow; +// import static org.mockito.Mockito.when; + +// @ExtendWith(MockitoExtension.class) +// @SpringJUnitConfig +// @WebMvcTest(RouteController.class) // This will only load the web layer +// public class RouterControllerExceptionUnitTests { + +// @Autowired +// private MockMvc mockMvc; + +// @MockBean +// private MyFileDatabase myFileDatabase; + +// // Mock the IndividualProjectApplication or methods if needed +// @MockBean +// private IndividualProjectApplication individualProjectApplication; + +// //exception +// @Test +// public void testException() throws Exception { +// when(myFileDatabase.getDepartmentMapping()). +// thenThrow(new RuntimeException("Database error")); +// mockMvc.perform(patch("/changeCourseLocation").param("deptCode", "nonExisting") +// .param("courseCode", "2500ddd").param("location", "633 MUDD")) +// .andExpect(status().isOk()) +// .andExpect(content().string("An Error has occurred")); +// } + +// } diff --git a/IndividualProject/testfile.txt b/IndividualProject/testfile.txt new file mode 100644 index 00000000..fafc4f3b Binary files /dev/null and b/IndividualProject/testfile.txt differ