Skip to content

Commit

Permalink
Added code from Integration Testing 1 Video until PostgreSQL/Docker part
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-lance committed Mar 11, 2023
1 parent 6627a5a commit 1e4af50
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 1e4af50

Please sign in to comment.