From 1e4af503395b358b05d5c3939b5e623904bf29d6 Mon Sep 17 00:00:00 2001 From: erik-lance Date: Sat, 11 Mar 2023 22:41:22 +0800 Subject: [PATCH] Added code from Integration Testing 1 Video until PostgreSQL/Docker part --- .../controllers/EnlistControllerIT.java | 54 ++++++++++++++++++- .../controllers/EnlistController.java | 3 +- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/integrationTest/java/com/orangeandbronze/enlistment/controllers/EnlistControllerIT.java b/src/integrationTest/java/com/orangeandbronze/enlistment/controllers/EnlistControllerIT.java index 759b3f7..3e35ff3 100644 --- a/src/integrationTest/java/com/orangeandbronze/enlistment/controllers/EnlistControllerIT.java +++ b/src/integrationTest/java/com/orangeandbronze/enlistment/controllers/EnlistControllerIT.java @@ -7,7 +7,10 @@ import org.springframework.boot.test.context.*; import org.springframework.jdbc.core.*; import org.springframework.test.annotation.*; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.*; +import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.*; import java.time.*; @@ -38,13 +41,60 @@ class EnlistControllerIT { @Test void enlist_student_in_section() throws Exception { - + // Given in the DB: a student & a section + jdbcTemplate.update("INSERT INTO student(student_number, firstname, lastname) VALUES (?, ?, ?)", + DEFAULT_STUDENT_NUMBER, "firstname", "lastname"); + final String roomName = "defaultRoom"; + jdbcTemplate.update("INSERT INTO room (name, capacity) VALUES (?, ?)", roomName, 10); + jdbcTemplate.update("INSERT INTO subject (subject_id) VALUES (?)", DEFAULT_SUBJECT.toString()); + jdbcTemplate.update("INSERT INTO section (section_id, number_of_students, days, start_time, end_time, room_name, subject_subject_id)" + + " VALUES (?, ?, ?, ?, ?, ?, ?)", + DEFAULT_SECTION_ID, 0, Days.MTH.ordinal(), LocalTime.of(9, 0), LocalTime.of(10, 0), roomName, DEFAULT_SUBJECT.toString()); + // When the POST method on path "/enlist" is invoked, with + // parameters for sectionId matching the record in the DB, and UserAction "ENLIST" + // with a student object in session corresponding to the student record in the DB + Student student = studentRepository.findById(DEFAULT_STUDENT_NUMBER).orElseThrow(() -> + new NoSuchElementException("No student w/ student num " + DEFAULT_STUDENT_NUMBER + " found in DB.")); + mockMvc.perform(post("/enlist").sessionAttr("student", student).param("sectionId", DEFAULT_SECTION_ID) + .param("userAction", ENLIST.name())); + // Then a new record in the student_sections, containing the corresponding studentNumber & sectionId + int count = jdbcTemplate.queryForObject( + "SELECT COUNT(*) FROM student_sections WHERE student_student_number = ? AND sections_section_id = ?", + Integer.class, DEFAULT_STUDENT_NUMBER, DEFAULT_SECTION_ID + ); + + assertEquals(1, count); } @Test void cancel_student_in_section() throws Exception { - + // Given in the DB: a student & a section, and a enlistment record existing in student_sections + jdbcTemplate.update("INSERT INTO student(student_number, firstname, lastname) VALUES (?, ?, ?)", + DEFAULT_STUDENT_NUMBER, "firstname", "lastname"); + final String roomName = "defaultRoom"; + jdbcTemplate.update("INSERT INTO room (name, capacity) VALUES (?, ?)", roomName, 10); + jdbcTemplate.update("INSERT INTO subject (subject_id) VALUES (?)", DEFAULT_SUBJECT.toString()); + jdbcTemplate.update("INSERT INTO section (section_id, number_of_students, days, start_time, end_time, room_name, subject_subject_id)" + + " VALUES (?, ?, ?, ?, ?, ?, ?)", + DEFAULT_SECTION_ID, 0, Days.MTH.ordinal(), LocalTime.of(9, 0), LocalTime.of(10, 0), roomName, DEFAULT_SUBJECT.toString()); + jdbcTemplate.update("INSERT INTO student_sections (student_student_number, sections_section_id)" + + "VALUES (?, ?)", DEFAULT_STUDENT_NUMBER, DEFAULT_SECTION_ID); + + // When the POST method on path "/enlist" is invoked, with + // parameters for sectionId matching the record in the DB, and UserAction "ENLIST" + // with a student object in session corresponding to the student record in the DB + Student student = studentRepository.findById(DEFAULT_STUDENT_NUMBER).orElseThrow(() -> + new NoSuchElementException("No student w/ student num " + DEFAULT_STUDENT_NUMBER + " found in DB.")); + mockMvc.perform(post("/enlist").sessionAttr("student", student).param("sectionId", DEFAULT_SECTION_ID) + .param("userAction", CANCEL.name())); + // Then a new record in the student_sections, containing the corresponding studentNumber & sectionId + int count = jdbcTemplate.queryForObject( + "SELECT COUNT(*) FROM student_sections WHERE student_student_number = ? AND sections_section_id = ?", + Integer.class, DEFAULT_STUDENT_NUMBER, DEFAULT_SECTION_ID + ); + + assertEquals(0, count); } private final static int FIRST_STUDENT_ID = 11; diff --git a/src/main/java/com/orangeandbronze/enlistment/controllers/EnlistController.java b/src/main/java/com/orangeandbronze/enlistment/controllers/EnlistController.java index 6979136..6c49753 100644 --- a/src/main/java/com/orangeandbronze/enlistment/controllers/EnlistController.java +++ b/src/main/java/com/orangeandbronze/enlistment/controllers/EnlistController.java @@ -74,8 +74,7 @@ public String enlistOrCancel(@ModelAttribute Student student, @RequestParam Stri Session session = entityManager.unwrap(Session.class); notNull(session); session.update(student); - - student.enlist(section); + userAction.act(student, section); studentRepo.save(student); sectionRepo.save(section); return "redirect:enlist";