Skip to content
Snippets Groups Projects
Commit 68897303 authored by Jan Pokorný's avatar Jan Pokorný :lifter_tone2:
Browse files

Lecture fixes

parent 6a69715a
No related branches found
No related tags found
1 merge request!35M2 fix language school
......@@ -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
......@@ -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));
}
}
......@@ -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
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment