From 68897303fc86efa708c3cdd45e35fde19b5f9ea5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <xpokorn8@fi.muni.cz>
Date: Sat, 29 Apr 2023 23:34:07 +0200
Subject: [PATCH] Lecture fixes

---
 .../lecture/LectureController.java            |  25 +++--
 .../lecture/LectureFacade.java                |  22 ++--
 .../lecture/LectureControllerTest.java        | 100 +++++++++---------
 .../lecture/LectureFacadeTest.java            |  16 +--
 4 files changed, 85 insertions(+), 78 deletions(-)

diff --git a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java
index 80b4f8e1..8ace2521 100644
--- a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java
+++ b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java
@@ -7,7 +7,6 @@ import io.swagger.annotations.ApiResponses;
 import jakarta.validation.Valid;
 import org.fuseri.model.dto.lecture.LectureCreateDto;
 import org.fuseri.model.dto.lecture.LectureDto;
-import org.fuseri.model.dto.user.UserDto;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -119,8 +118,8 @@ public class LectureController {
     /**
      * Adds lecturer to the existing lecture resource
      *
-     * @param id          id of lecture to update
-     * @param lecturerDto UserDto for the course lecturer
+     * @param id         id of lecture to update
+     * @param lecturerId UserDto for the course lecturer
      * @return the LectureDto representing the updated lecture
      */
     @ApiOperation(value = "Add lecturer to the existing lecture")
@@ -130,15 +129,15 @@ public class LectureController {
             @ApiResponse(code = 400, message = "The request body is invalid"),
             @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
     })
-    public ResponseEntity<LectureDto> setLecturer(@PathVariable Long id, @RequestBody UserDto lecturerDto) {
-        return ResponseEntity.ok(lectureFacade.setLecturer(id, lecturerDto));
+    public ResponseEntity<LectureDto> setLecturer(@PathVariable Long id, @RequestParam Long lecturerId) {
+        return ResponseEntity.ok(lectureFacade.setLecturer(id, lecturerId));
     }
 
     /**
      * Adds student to the existing lecture resource
      *
-     * @param id      id of lecture to update
-     * @param student UserDto for the course student
+     * @param id        id of lecture to update
+     * @param studentId id for the course student
      * @return the LectureDto representing the updated lecture
      */
     @ApiOperation(value = "Add student to the existing lecture")
@@ -148,15 +147,15 @@ public class LectureController {
             @ApiResponse(code = 400, message = "The request body is invalid"),
             @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
     })
-    public ResponseEntity<LectureDto> enrol(@PathVariable Long id, @RequestBody UserDto student) {
-        return ResponseEntity.ok(lectureFacade.enrol(id, student));
+    public ResponseEntity<LectureDto> enrol(@PathVariable Long id, @RequestParam Long studentId) {
+        return ResponseEntity.ok(lectureFacade.enrol(id, studentId));
     }
 
     /**
      * Removes student from the existing lecture resource
      *
-     * @param id      id of lecture to update
-     * @param student UserDto for the course student
+     * @param id        id of lecture to update
+     * @param studentId id for the course student
      * @return the LectureDto representing the updated lecture
      */
     @ApiOperation(value = "Remove student from the existing lecture")
@@ -166,7 +165,7 @@ public class LectureController {
             @ApiResponse(code = 400, message = "The request body is invalid"),
             @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
     })
-    public ResponseEntity<LectureDto> expel(@PathVariable Long id, @RequestBody UserDto student) {
-        return ResponseEntity.ok(lectureFacade.expel(id, student));
+    public ResponseEntity<LectureDto> expel(@PathVariable Long id, @RequestParam Long studentId) {
+        return ResponseEntity.ok(lectureFacade.expel(id, studentId));
     }
 }
\ No newline at end of file
diff --git a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureFacade.java b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureFacade.java
index ce9214dd..dbd9359d 100644
--- a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureFacade.java
+++ b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureFacade.java
@@ -4,11 +4,11 @@ import org.fuseri.model.dto.course.LanguageTypeDto;
 import org.fuseri.model.dto.course.ProficiencyLevelDto;
 import org.fuseri.model.dto.lecture.LectureCreateDto;
 import org.fuseri.model.dto.lecture.LectureDto;
-import org.fuseri.model.dto.user.UserDto;
 import org.fuseri.modulelanguageschool.course.CourseService;
 import org.fuseri.modulelanguageschool.course.Language;
 import org.fuseri.modulelanguageschool.course.ProficiencyLevel;
 import org.fuseri.modulelanguageschool.user.UserMapper;
+import org.fuseri.modulelanguageschool.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
@@ -21,13 +21,16 @@ import java.util.List;
 public class LectureFacade {
 
     private final LectureService lectureService;
+
+    private final UserService userService;
     private final LectureMapper lectureMapper;
     private final UserMapper userMapper;
     private final CourseService courseService;
 
     @Autowired
-    public LectureFacade(LectureService lectureService, LectureMapper lectureMapper, UserMapper userMapper, CourseService courseService) {
+    public LectureFacade(LectureService lectureService, UserService userService, LectureMapper lectureMapper, UserMapper userMapper, CourseService courseService) {
         this.lectureService = lectureService;
+        this.userService = userService;
         this.lectureMapper = lectureMapper;
         this.userMapper = userMapper;
         this.courseService = courseService;
@@ -67,15 +70,18 @@ public class LectureFacade {
         return lectureMapper.mapToList(lectureService.findAll(Language.valueOf(lang.name()), ProficiencyLevel.valueOf(prof.name())));
     }
 
-    public LectureDto enrol(Long id, UserDto student) {
-        return lectureMapper.mapToDto(lectureService.enrol(id, userMapper.fromDto(student)));
+    public LectureDto enrol(Long id, long studentId) {
+        var student = userService.find(studentId);
+        return lectureMapper.mapToDto(lectureService.enrol(id, student));
     }
 
-    public LectureDto expel(Long id, UserDto student) {
-        return lectureMapper.mapToDto(lectureService.expel(id, userMapper.fromDto(student)));
+    public LectureDto expel(Long id, Long studentId) {
+        var student = userService.find(studentId);
+        return lectureMapper.mapToDto(lectureService.expel(id, student));
     }
 
-    public LectureDto setLecturer(Long id, UserDto lecturerDto) {
-        return lectureMapper.mapToDto(lectureService.setLecturer(id, userMapper.fromDto(lecturerDto)));
+    public LectureDto setLecturer(Long id, Long lecturerId) {
+        var lecturer = userService.find(lecturerId);
+        return lectureMapper.mapToDto(lectureService.setLecturer(id, lecturer));
     }
 }
diff --git a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureControllerTest.java b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureControllerTest.java
index a6177a7f..b20ba4d8 100644
--- a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureControllerTest.java
+++ b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureControllerTest.java
@@ -7,6 +7,7 @@ import org.fuseri.model.dto.lecture.LectureDto;
 import org.fuseri.model.dto.user.AddressDto;
 import org.fuseri.model.dto.user.UserDto;
 import org.fuseri.model.dto.user.UserType;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.mockito.ArgumentMatchers;
 import org.mockito.Mockito;
@@ -20,6 +21,7 @@ import org.springframework.test.web.servlet.MockMvc;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
 
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -31,23 +33,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 public class LectureControllerTest {
 
     private final LocalDateTime now = LocalDateTime.now();
-    private final LectureCreateDto lectureCreateDto = new LectureCreateDto(
-            now.plusDays(2),
-            now.plusDays(2).plusHours(2),
-            "Learning how to spell deprecated",
-            10, 0L);
-    private final LectureDto lectureDto = new LectureDto(
-            now.plusDays(2),
-            now.plusDays(2).plusHours(2),
-            "Learning how to spell deprecated",
-            10, 0L, 0L, Collections.emptyList());
-
     @Autowired
     ObjectMapper objectMapper;
-
+    private LectureCreateDto lectureCreateDto;
+    private LectureDto lectureDto;
+    private LectureDto lectureDtoWithStudent;
+    private UserDto student;
+    private UserDto lecturer;
     @Autowired
     private MockMvc mockMvc;
-
     @MockBean
     private LectureFacade lectureFacade;
 
@@ -61,6 +55,32 @@ public class LectureControllerTest {
         }
     }
 
+    @BeforeEach
+    void setup() {
+        student = new UserDto("novakovat", "novakovat@gamil.com", "Tereza",
+                "Nováková", new AddressDto(), UserType.STUDENT);
+        lecturer = new UserDto("novakoval", "novakoval@gamil.com", "Lucka",
+                "Nováková", new AddressDto(), UserType.LECTURER);
+        student.setId(1L);
+        lecturer.setId(2L);
+
+        lectureCreateDto = new LectureCreateDto(
+                now.plusDays(2),
+                now.plusDays(2).plusHours(2),
+                "Learning how to spell deprecated",
+                10, 0L);
+        lectureDto = new LectureDto(
+                now.plusDays(2),
+                now.plusDays(2).plusHours(2),
+                "Learning how to spell deprecated",
+                10, lecturer.getId(), 0L, Collections.emptyList());
+        lectureDtoWithStudent = new LectureDto(
+                now.plusDays(2),
+                now.plusDays(2).plusHours(2),
+                "Learning how to spell deprecated",
+                10, lecturer.getId(), 0L, List.of(student.getId()));
+    }
+
     @Test
     void createLecture() throws Exception {
         Mockito.when(lectureFacade.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto);
@@ -72,7 +92,7 @@ public class LectureControllerTest {
                 .andExpect(jsonPath("$.lectureTo").isNotEmpty())
                 .andExpect(jsonPath("$.topic").value("Learning how to spell deprecated"))
                 .andExpect(jsonPath("$.capacity").value("10"))
-                .andExpect(jsonPath("$.lecturerId").value("0"))
+                .andExpect(jsonPath("$.lecturerId").value("2"))
                 .andExpect(jsonPath("$.courseId").value("0"));
     }
 
@@ -102,7 +122,7 @@ public class LectureControllerTest {
                 .andExpect(jsonPath("$.lectureTo").isNotEmpty())
                 .andExpect(jsonPath("$.topic").value("Learning how to spell deprecated"))
                 .andExpect(jsonPath("$.capacity").value("10"))
-                .andExpect(jsonPath("$.lecturerId").value("0"))
+                .andExpect(jsonPath("$.lecturerId").value("2"))
                 .andExpect(jsonPath("$.courseId").value("0"));
     }
 
@@ -126,7 +146,7 @@ public class LectureControllerTest {
     @Test
     void findLecturesByCourseWithoutParameter() throws Exception {
         mockMvc.perform(get("/lectures/findByCourse"))
-                .andExpect(status().is4xxClientError());
+                .andExpect(status().is5xxServerError());
     }
 
     @Test
@@ -143,7 +163,7 @@ public class LectureControllerTest {
                 .andExpect(jsonPath("$.lectureTo").isNotEmpty())
                 .andExpect(jsonPath("$.topic").value("Learning how to spell deprecated"))
                 .andExpect(jsonPath("$.capacity").value("10"))
-                .andExpect(jsonPath("$.lecturerId").value("0"))
+                .andExpect(jsonPath("$.lecturerId").value("2"))
                 .andExpect(jsonPath("$.courseId").value("0"));
     }
 
@@ -171,20 +191,14 @@ public class LectureControllerTest {
     void setLecturerForLecture() throws Exception {
         Long id = 0L;
         Mockito.when(lectureFacade.setLecturer(ArgumentMatchers.eq(id),
-                        ArgumentMatchers.isA(UserDto.class)))
+                        ArgumentMatchers.isA(Long.class)))
                 .thenReturn(lectureDto);
-        UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza",
-                "Nováková", new AddressDto(), UserType.STUDENT);
-        mockMvc.perform(patch("/lectures/setLecturer/" + id)
-                        .content(asJsonString(student))
+        student.setId(0L);
+        mockMvc.perform(patch("/lectures/setLecturer/" + id + "?lecturerId=" + lecturer.getId())
+                        .content(asJsonString(student.getId()))
                         .contentType(MediaType.APPLICATION_JSON))
                 .andExpect(status().isOk())
-                .andExpect(jsonPath("$.lectureFrom").isNotEmpty())
-                .andExpect(jsonPath("$.lectureTo").isNotEmpty())
-                .andExpect(jsonPath("$.topic").value("Learning how to spell deprecated"))
-                .andExpect(jsonPath("$.capacity").value("10"))
-                .andExpect(jsonPath("$.lecturerId").value("0"))
-                .andExpect(jsonPath("$.courseId").value("0"));
+                .andExpect(jsonPath("$.lecturerId").value("2"));
     }
 
     @Test
@@ -197,20 +211,12 @@ public class LectureControllerTest {
     void enrolLecture() throws Exception {
         Long id = 0L;
         Mockito.when(lectureFacade.enrol(ArgumentMatchers.eq(id),
-                        ArgumentMatchers.isA(UserDto.class)))
-                .thenReturn(lectureDto);
-        UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza",
-                "Nováková", new AddressDto(),UserType.STUDENT );
-        mockMvc.perform(patch("/lectures/enrol/" + id)
-                        .content(asJsonString(student))
+                        ArgumentMatchers.isA(Long.class)))
+                .thenReturn(lectureDtoWithStudent);
+        mockMvc.perform(patch("/lectures/enrol/" + id + "?studentId=" + student.getId())
                         .contentType(MediaType.APPLICATION_JSON))
                 .andExpect(status().isOk())
-                .andExpect(jsonPath("$.lectureFrom").isNotEmpty())
-                .andExpect(jsonPath("$.lectureTo").isNotEmpty())
-                .andExpect(jsonPath("$.topic").value("Learning how to spell deprecated"))
-                .andExpect(jsonPath("$.capacity").value("10"))
-                .andExpect(jsonPath("$.lecturerId").value("0"))
-                .andExpect(jsonPath("$.courseId").value("0"));
+                .andExpect(jsonPath("$.students").isNotEmpty());
     }
 
     @Test
@@ -223,20 +229,12 @@ public class LectureControllerTest {
     void expelLecture() throws Exception {
         Long id = 0L;
         Mockito.when(lectureFacade.expel(ArgumentMatchers.eq(id),
-                        ArgumentMatchers.isA(UserDto.class)))
+                        ArgumentMatchers.isA(Long.class)))
                 .thenReturn(lectureDto);
-        UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza",
-                "Nováková", new AddressDto(), UserType.STUDENT);
-        mockMvc.perform(patch("/lectures/expel/" + id)
-                        .content(asJsonString(student))
+        mockMvc.perform(patch("/lectures/expel/" + id + "?studentId=" + student.getId())
                         .contentType(MediaType.APPLICATION_JSON))
                 .andExpect(status().isOk())
-                .andExpect(jsonPath("$.lectureFrom").isNotEmpty())
-                .andExpect(jsonPath("$.lectureTo").isNotEmpty())
-                .andExpect(jsonPath("$.topic").value("Learning how to spell deprecated"))
-                .andExpect(jsonPath("$.capacity").value("10"))
-                .andExpect(jsonPath("$.lecturerId").value("0"))
-                .andExpect(jsonPath("$.courseId").value("0"));
+                .andExpect(jsonPath("$.students").isEmpty());
     }
 
     @Test
diff --git a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureFacadeTest.java b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureFacadeTest.java
index e970e966..a74977d6 100644
--- a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureFacadeTest.java
+++ b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureFacadeTest.java
@@ -13,6 +13,7 @@ import org.fuseri.modulelanguageschool.course.Language;
 import org.fuseri.modulelanguageschool.course.ProficiencyLevel;
 import org.fuseri.modulelanguageschool.user.User;
 import org.fuseri.modulelanguageschool.user.UserMapper;
+import org.fuseri.modulelanguageschool.user.UserService;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -67,6 +68,9 @@ final class LectureFacadeTest {
     @MockBean
     private UserMapper userMapper;
 
+    @MockBean
+    private UserService userService;
+
     @Autowired
     private CourseService courseService;
 
@@ -149,10 +153,10 @@ final class LectureFacadeTest {
     void testEnrol() {
         Long id = 0L;
         when(lectureMapper.mapToDto(lecture)).thenReturn(lectureDto);
-        when(userMapper.fromDto(USER)).thenReturn(user);
+        when(userService.find(0L)).thenReturn(user);
         when(lectureService.enrol(anyLong(), any(User.class))).thenReturn(lecture);
 
-        LectureDto actualDto = lectureFacade.enrol(id, USER);
+        LectureDto actualDto = lectureFacade.enrol(id, 0L);
 
         assertNotNull(actualDto);
         assertEquals(lectureDto, actualDto);
@@ -162,10 +166,10 @@ final class LectureFacadeTest {
     void testExpel() {
         Long id = 0L;
         when(lectureMapper.mapToDto(lecture)).thenReturn(lectureDto);
-        when(userMapper.fromDto(USER)).thenReturn(user);
+        when(userService.find(1L)).thenReturn(user);
         when(lectureService.expel(anyLong(), any(User.class))).thenReturn(lecture);
 
-        LectureDto actualDto = lectureFacade.expel(id, USER);
+        LectureDto actualDto = lectureFacade.expel(id, 1L);
 
         assertNotNull(actualDto);
         assertEquals(lectureDto, actualDto);
@@ -175,10 +179,10 @@ final class LectureFacadeTest {
     void testSetLecturer() {
         Long id = 0L;
         when(lectureMapper.mapToDto(lecture)).thenReturn(lectureDto);
-        when(userMapper.fromDto(USER)).thenReturn(user);
+        when(userService.find(1L)).thenReturn(user);
         when(lectureService.setLecturer(anyLong(), any(User.class))).thenReturn(lecture);
 
-        LectureDto actualDto = lectureFacade.setLecturer(id, USER);
+        LectureDto actualDto = lectureFacade.setLecturer(id, 1L);
 
         assertNotNull(actualDto);
         assertEquals(lectureDto, actualDto);
-- 
GitLab