From 8da675b7ee61569f8563b14a01dc02105ed22196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <xpokorn8@fi.muni.cz> Date: Sat, 15 Apr 2023 07:56:26 +0200 Subject: [PATCH] transformed LectureController to return responseEntity --- .../lecture/LectureController.java | 35 ++++---- .../lecture/LectureControllerTest.java | 86 ++++++++----------- 2 files changed, 55 insertions(+), 66 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 0922a5e2..d0b58998 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,6 +7,8 @@ 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; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -36,8 +38,9 @@ public class LectureController { */ @ApiOperation(value = "Create a new lecture") @PostMapping - public LectureDto create(@Valid @RequestBody LectureCreateDto lecture) { - return lectureFacade.create(lecture); + public ResponseEntity<LectureDto> create(@Valid @RequestBody LectureCreateDto lecture) { + LectureDto lectureDto = lectureFacade.create(lecture); + return ResponseEntity.status(HttpStatus.CREATED).body(lectureDto); } /** @@ -48,8 +51,9 @@ public class LectureController { */ @ApiOperation(value = "Retrieve a lecture by its ID") @GetMapping("find/{courseId}") - public LectureDto find(@PathVariable Long courseId) { - return lectureFacade.findById(courseId); + public ResponseEntity<LectureDto> find(@PathVariable Long courseId) { + LectureDto lectureDto = lectureFacade.findById(courseId); + return ResponseEntity.ok(lectureDto); } /** @@ -60,8 +64,8 @@ public class LectureController { */ @ApiOperation(value = "Retrieve a list of lectures for the corresponding course") @GetMapping("/findByCourse") - public List<LectureDto> findByCourse(@Valid @RequestParam Long courseId) { - return lectureFacade.findAll(courseId); + public ResponseEntity<List<LectureDto>> findByCourse(@Valid @RequestParam Long courseId) { + return ResponseEntity.ok(lectureFacade.findAll(courseId)); } /** @@ -72,8 +76,8 @@ public class LectureController { */ @ApiOperation(value = "Update an existing lecture") @PutMapping("/update/{id}") - public LectureDto update(@PathVariable Long id, @Valid @RequestBody LectureCreateDto lecture) { - return lectureFacade.update(id, lecture); + public ResponseEntity<LectureDto> update(@PathVariable Long id, @Valid @RequestBody LectureCreateDto lecture) { + return ResponseEntity.ok(lectureFacade.update(id, lecture)); } /** @@ -83,8 +87,9 @@ public class LectureController { */ @ApiOperation(value = "Delete a lecture by its ID") @DeleteMapping("/delete/{id}") - public void delete(@PathVariable Long id) { + public ResponseEntity<Void> delete(@PathVariable Long id) { lectureFacade.delete(id); + return ResponseEntity.noContent().build(); } @@ -97,8 +102,8 @@ public class LectureController { */ @ApiOperation(value = "Add lecturer to the existing lecture") @PatchMapping("/setLecturer/{id}") - public LectureDto setLecturer(@PathVariable Long id, @RequestBody UserDto lecturerDto) { - return lectureFacade.setLecturer(id, lecturerDto); + public ResponseEntity<LectureDto> setLecturer(@PathVariable Long id, @RequestBody UserDto lecturerDto) { + return ResponseEntity.ok(lectureFacade.setLecturer(id, lecturerDto)); } /** @@ -110,8 +115,8 @@ public class LectureController { */ @ApiOperation(value = "Add student to the existing lecture") @PatchMapping("/enrol/{id}") - public LectureDto enrol(@PathVariable Long id, @RequestBody UserDto student) { - return lectureFacade.enrol(id, student); + public ResponseEntity<LectureDto> enrol(@PathVariable Long id, @RequestBody UserDto student) { + return ResponseEntity.ok(lectureFacade.enrol(id, student)); } /** @@ -123,7 +128,7 @@ public class LectureController { */ @ApiOperation(value = "Remove student from the existing lecture") @PatchMapping("/expel/{id}") - public LectureDto expel(@PathVariable Long id, @RequestBody UserDto student) { - return lectureFacade.expel(id, student); + public ResponseEntity<LectureDto> expel(@PathVariable Long id, @RequestBody UserDto student) { + return ResponseEntity.ok(lectureFacade.expel(id, student)); } } \ No newline at end of file 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 d4f02844..8da28910 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 @@ -14,7 +14,9 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.test.web.servlet.MockMvc; + import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; @@ -28,16 +30,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc public class LectureControllerTest { - @Autowired - ObjectMapper objectMapper; - @Autowired - private MockMvc mockMvc; - - @MockBean - private LectureController lectureController; private final LocalDateTime now = LocalDateTime.now(); - private final LectureCreateDto lectureCreateDto = new LectureCreateDto( now.plusDays(2), now.plusDays(2).plusHours(2), @@ -48,10 +42,26 @@ public class LectureControllerTest { now.plusDays(2).plusHours(2), "Learning how to spell deprecated", 10, 0L, 0L, Collections.emptyList()); + @Autowired + ObjectMapper objectMapper; + @Autowired + private MockMvc mockMvc; + @MockBean + private LectureController lectureController; + + public static String asJsonString(final Object obj) { + try { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + return mapper.writeValueAsString(obj); + } catch (Exception e) { + throw new RuntimeException(e); + } + } @Test void createLecture() throws Exception { - Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto); + Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(ResponseEntity.ok(lectureDto)); mockMvc.perform(post("/lectures") .content(asJsonString(lectureCreateDto)) .contentType(MediaType.APPLICATION_JSON)) @@ -69,7 +79,7 @@ public class LectureControllerTest { void createInvalidLecture() throws Exception { LectureCreateDto invalidLectureCreateDto = new LectureCreateDto(null, null, null, null, null); - Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto); + Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(ResponseEntity.badRequest().build()); mockMvc.perform(post("/lectures") .content(asJsonString(invalidLectureCreateDto)) .contentType(MediaType.APPLICATION_JSON)) @@ -78,7 +88,6 @@ public class LectureControllerTest { @Test void createLectureWithoutParameter() throws Exception { - Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto); mockMvc.perform(post("/lectures")) .andExpect(status().is4xxClientError()); } @@ -86,7 +95,7 @@ public class LectureControllerTest { @Test void findLecture() throws Exception { Long id = 0L; - Mockito.when(lectureController.find(id)).thenReturn(lectureDto); + Mockito.when(lectureController.find(id)).thenReturn(ResponseEntity.ok(lectureDto)); mockMvc.perform(get("/lectures/find/" + id)) .andExpect(status().isOk()) .andExpect(jsonPath("$.lectureFrom").isNotEmpty()) @@ -100,7 +109,6 @@ public class LectureControllerTest { @Test void findLectureWithoutId() throws Exception { - Mockito.when(lectureController.find(ArgumentMatchers.anyLong())).thenReturn(lectureDto); mockMvc.perform(get("/lectures/find/")) .andExpect(status().is4xxClientError()); } @@ -108,7 +116,7 @@ public class LectureControllerTest { @Test void findLecturesByCourse() throws Exception { Long id = 0L; - Mockito.when(lectureController.findByCourse(id)).thenReturn(new ArrayList<>()); + Mockito.when(lectureController.findByCourse(id)).thenReturn(ResponseEntity.ok(new ArrayList<>())); String response = mockMvc.perform(get("/lectures/findByCourse") .param("courseId", String.valueOf(id))) .andExpect(status().isOk()) @@ -119,7 +127,6 @@ public class LectureControllerTest { @Test void findLecturesByCourseWithoutParameter() throws Exception { - Mockito.when(lectureController.findByCourse(ArgumentMatchers.anyLong())).thenReturn(new ArrayList<>()); mockMvc.perform(get("/lectures/findByCourse")) .andExpect(status().is4xxClientError()); } @@ -128,8 +135,8 @@ public class LectureControllerTest { void updateLecture() throws Exception { Long id = 0L; Mockito.when(lectureController.update(ArgumentMatchers.eq(id), - ArgumentMatchers.isA(LectureCreateDto.class))) - .thenReturn(lectureDto); + ArgumentMatchers.isA(LectureCreateDto.class))) + .thenReturn(ResponseEntity.ok(lectureDto)); mockMvc.perform(put("/lectures/update/" + id) .content(asJsonString(lectureCreateDto)) .contentType(MediaType.APPLICATION_JSON)) @@ -145,9 +152,6 @@ public class LectureControllerTest { @Test void updateLectureWithoutParameter() throws Exception { - Mockito.when(lectureController. - update(ArgumentMatchers.anyLong(), ArgumentMatchers.isA(LectureCreateDto.class))) - .thenReturn(lectureDto); mockMvc.perform(put("/lectures/update")) .andExpect(status().is4xxClientError()); } @@ -155,14 +159,13 @@ public class LectureControllerTest { @Test void deleteLecture() throws Exception { Long id = 0L; - Mockito.doNothing().when(lectureController).delete(id); + Mockito.when(lectureController.delete(id)).thenReturn(ResponseEntity.noContent().build()); mockMvc.perform(delete("/lectures/delete/" + id)) - .andExpect(status().isOk()); + .andExpect(status().is2xxSuccessful()); } @Test void deleteCourseWithoutParameter() throws Exception { - Mockito.doNothing().when(lectureController).delete(ArgumentMatchers.anyLong()); mockMvc.perform(delete("/lectures/delete/")) .andExpect(status().is4xxClientError()); } @@ -171,9 +174,9 @@ public class LectureControllerTest { void setLecturerForLecture() throws Exception { Long id = 0L; Mockito.when(lectureController.setLecturer(ArgumentMatchers.eq(id), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(lectureDto); - UserDto student = new UserDto("novakovat","novakova@gamil.com", "Tereza", + ArgumentMatchers.isA(UserDto.class))) + .thenReturn(ResponseEntity.ok(lectureDto)); + UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza", "Nováková", new AddressDto()); mockMvc.perform(patch("/lectures/setLecturer/" + id) .content(asJsonString(student)) @@ -190,9 +193,6 @@ public class LectureControllerTest { @Test void setLecturerForLectureWithoutParameters() throws Exception { - Mockito.when(lectureController.setLecturer(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(lectureDto); mockMvc.perform(patch("/lectures/setLecturer")) .andExpect(status().is4xxClientError()); } @@ -201,9 +201,9 @@ public class LectureControllerTest { void enrolLecture() throws Exception { Long id = 0L; Mockito.when(lectureController.enrol(ArgumentMatchers.eq(id), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(lectureDto); - UserDto student = new UserDto("novakovat","novakova@gamil.com", "Tereza", + ArgumentMatchers.isA(UserDto.class))) + .thenReturn(ResponseEntity.ok(lectureDto)); + UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza", "Nováková", new AddressDto()); mockMvc.perform(patch("/lectures/enrol/" + id) .content(asJsonString(student)) @@ -220,9 +220,6 @@ public class LectureControllerTest { @Test void enrolCourseWithoutUserParameters() throws Exception { - Mockito.when(lectureController.enrol(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(lectureDto); mockMvc.perform(patch("/lectures/enrol")) .andExpect(status().is4xxClientError()); } @@ -231,9 +228,9 @@ public class LectureControllerTest { void expelLecture() throws Exception { Long id = 0L; Mockito.when(lectureController.expel(ArgumentMatchers.eq(id), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(lectureDto); - UserDto student = new UserDto("novakovat","novakova@gamil.com", "Tereza", + ArgumentMatchers.isA(UserDto.class))) + .thenReturn(ResponseEntity.ok(lectureDto)); + UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza", "Nováková", new AddressDto()); mockMvc.perform(patch("/lectures/expel/" + id) .content(asJsonString(student)) @@ -250,21 +247,8 @@ public class LectureControllerTest { @Test void expelCourseWithoutUserParameters() throws Exception { - Mockito.when(lectureController.expel(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(lectureDto); mockMvc.perform(patch("/lectures/expel")) .andExpect(status().is4xxClientError()); } - public static String asJsonString(final Object obj) { - try { - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(new JavaTimeModule()); - return mapper.writeValueAsString(obj); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } -- GitLab