|
| 1 | +package dev.coms4156.project.individualproject; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | + |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.Paths; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +/** |
| 20 | + * This class is built to test the MyFileDatabase class, which we have created for storing |
| 21 | + * the courses and departments in a file. |
| 22 | + */ |
| 23 | +@SpringBootTest |
| 24 | +@ContextConfiguration |
| 25 | +public class MyFileDatabaseUnitTest { |
| 26 | + |
| 27 | + @BeforeAll |
| 28 | + public static void setupMyFileDatabaseForTesting() { |
| 29 | + |
| 30 | + testMyFileDatabase = new MyFileDatabase(0, "data.txt"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void getMappingTest() { |
| 35 | + assertEquals(7, testMyFileDatabase.getDepartmentMapping().size()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void setMappingTest() { |
| 40 | + HashMap<String, Department> currentMapping = testMyFileDatabase.getDepartmentMapping(); |
| 41 | + |
| 42 | + Course testCourse = new Course("Griffin Newbold", "417 IAB", "11:40-12:55", 250); |
| 43 | + HashMap<String, Course> testCourses = new HashMap<String, Course>(); |
| 44 | + testCourses.put("4156", testCourse); |
| 45 | + |
| 46 | + Department testDepartment = new Department("COMS", testCourses, "Griffin Newbold", 2700); |
| 47 | + HashMap<String, Department> testDepartmentMapping = new HashMap<String, Department>(); |
| 48 | + testDepartmentMapping.put("COMS", testDepartment); |
| 49 | + |
| 50 | + testMyFileDatabase.setMapping(testDepartmentMapping); |
| 51 | + |
| 52 | + assertEquals(1, testMyFileDatabase.getDepartmentMapping().size()); |
| 53 | + |
| 54 | + testMyFileDatabase.setMapping(currentMapping); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void toStringTest() { |
| 60 | + System.out.println(testMyFileDatabase.toString()); |
| 61 | + assertEquals(0, 0); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void deSerializeObjectFromFileTest() { |
| 66 | + assertEquals(7, testMyFileDatabase.deSerializeObjectFromFile().size()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void saveContentsToFileTest() { |
| 71 | + try { |
| 72 | + MyFileDatabase writingMyFileDatabase = new MyFileDatabase(1, "writing_data.txt"); |
| 73 | + |
| 74 | + Course testCourse = new Course("Griffin Newbold", "417 IAB", "11:40-12:55", 250); |
| 75 | + HashMap<String, Course> testCourses = new HashMap<String, Course>(); |
| 76 | + testCourses.put("4156", testCourse); |
| 77 | + |
| 78 | + Department testDepartment = new Department("COMS", testCourses, "Griffin Newbold", 2700); |
| 79 | + HashMap<String, Department> testDepartmentMapping = new HashMap<String, Department>(); |
| 80 | + testDepartmentMapping.put("COMS", testDepartment); |
| 81 | + |
| 82 | + writingMyFileDatabase.setMapping(testDepartmentMapping); |
| 83 | + writingMyFileDatabase.saveContentsToFile(); |
| 84 | + |
| 85 | + MyFileDatabase readingMyFileDatabase = new MyFileDatabase(0, "writing_data.txt"); |
| 86 | + assertEquals(1, readingMyFileDatabase.getDepartmentMapping().size()); |
| 87 | + |
| 88 | + // clean up (delete the file) |
| 89 | + Path path = Paths.get("writing_data.txt"); |
| 90 | + Files.deleteIfExists(path); |
| 91 | + } catch (IOException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + fail("Failed to delete the file or another IO error occurred."); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + private static MyFileDatabase testMyFileDatabase; |
| 100 | +} |
0 commit comments