diff --git a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/course/CourseController.java b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/course/CourseController.java index 353dcd43d30b280c8cf683fe794faeb0c29d01b9..06be0f52abf6023422e5ca78524c8531be419128 100644 --- a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/course/CourseController.java +++ b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/course/CourseController.java @@ -12,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -40,8 +42,9 @@ public class CourseController { */ @ApiOperation(value = "Create a new course") @PostMapping - public CourseDto create(@Valid @RequestBody CourseCreateDto dto) { - return courseFacade.create(dto); + public ResponseEntity<CourseDto> create(@Valid @RequestBody CourseCreateDto dto) { + CourseDto courseDto = courseFacade.create(dto); + return ResponseEntity.status(HttpStatus.CREATED).body(courseDto); } /** @@ -52,10 +55,10 @@ public class CourseController { */ @ApiOperation(value = "Retrieve a course by ID") @GetMapping("/find/{id}") - public CourseDto find(@PathVariable Long id) { - return courseFacade.findById(id); + public ResponseEntity<CourseDto> find(@PathVariable Long id) { + CourseDto courseDto = courseFacade.findById(id); + return ResponseEntity.ok(courseDto); } - /** * Retrieves a paginated list of courses * @@ -64,8 +67,9 @@ public class CourseController { */ @ApiOperation(value = "Retrieve a paginated list of courses") @GetMapping("/findAll") - public Page<CourseDto> findAll(@RequestParam int page) { - return courseFacade.findAll(PageRequest.of(page, 10, Sort.by(Sort.Direction.ASC, "name"))); + public ResponseEntity<Page<CourseDto>> findAll(@RequestParam int page) { + Page<CourseDto> courseDtoPage = courseFacade.findAll(PageRequest.of(page, 10, Sort.by(Sort.Direction.ASC, "name"))); + return ResponseEntity.ok(courseDtoPage); } /** @@ -76,10 +80,10 @@ public class CourseController { */ @ApiOperation(value = "Retrieve a paginated list of courses of a given language") @GetMapping("/findAllByLang") - public List<CourseDto> findAll(@RequestParam LanguageTypeDto lang) { - return courseFacade.findAll(lang); + public ResponseEntity<List<CourseDto>> findAll(@RequestParam LanguageTypeDto lang) { + List<CourseDto> courseDtos = courseFacade.findAll(lang); + return ResponseEntity.ok(courseDtos); } - /** * Retrieves a paginated list of courses of a given language and proficiency * @@ -89,9 +93,10 @@ public class CourseController { */ @ApiOperation(value = "Retrieve a paginated list of courses of a given language and proficiency") @GetMapping("/findAllByLangProf") - public List<CourseDto> findAll(@RequestParam LanguageTypeDto lang, + public ResponseEntity<List<CourseDto>> findAll(@RequestParam LanguageTypeDto lang, @RequestParam ProficiencyLevelDto prof) { - return courseFacade.findAll(lang, prof); + List<CourseDto> courses = courseFacade.findAll(lang, prof); + return ResponseEntity.ok(courses); } /** @@ -103,8 +108,9 @@ public class CourseController { */ @ApiOperation(value = "Update an existing course") @PutMapping("/update/{id}") - public CourseDto update(@PathVariable Long id, @Valid @RequestBody CourseCreateDto dto) { - return courseFacade.update(id, dto); + public ResponseEntity<CourseDto> update(@PathVariable Long id, @Valid @RequestBody CourseCreateDto dto) { + CourseDto updatedCourse = courseFacade.update(id, dto); + return ResponseEntity.ok(updatedCourse); } /** @@ -114,11 +120,11 @@ public class CourseController { */ @ApiOperation(value = "Delete a course by ID") @DeleteMapping("/delete/{id}") - public void delete(@PathVariable Long id) { + public ResponseEntity<Void> delete(@PathVariable Long id) { courseFacade.delete(id); + return ResponseEntity.noContent().build(); } - /** * Adds student to the existing course * @@ -128,8 +134,9 @@ public class CourseController { */ @ApiOperation(value = "Add student to the existing course") @PatchMapping("/enrol/{id}") - public CourseDto enrol(@PathVariable Long id, @RequestBody UserDto student) { - return courseFacade.enrol(id, student); + public ResponseEntity<CourseDto> enrol(@PathVariable Long id, @RequestBody UserDto student) { + CourseDto updatedCourse = courseFacade.enrol(id, student); + return ResponseEntity.ok(updatedCourse); } /** @@ -141,8 +148,9 @@ public class CourseController { */ @ApiOperation(value = "Remove student from the existing course") @PatchMapping("/expel/{id}") - public CourseDto expel(@PathVariable Long id, @RequestBody UserDto student) { - return courseFacade.expel(id, student); + public ResponseEntity<CourseDto> expel(@PathVariable Long id, @RequestBody UserDto student) { + CourseDto updatedCourse = courseFacade.expel(id, student); + return ResponseEntity.ok(updatedCourse); } } diff --git a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseControllerTest.java b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseControllerTest.java index dec1fa4c9c8dec97c1e495f62c0af8396129e605..e0424615ecfc4e6211bc164cd943d3e8636a530f 100644 --- a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseControllerTest.java +++ b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseControllerTest.java @@ -15,7 +15,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.data.domain.PageImpl; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.test.web.servlet.MockMvc; import java.util.ArrayList; @@ -34,8 +36,10 @@ public class CourseControllerTest { private final CourseCreateDto courseCreateDto = new CourseCreateDto("english b2 course", 10, LanguageTypeDto.ENGLISH, ProficiencyLevelDto.B2); private final CourseDto courseDto = new CourseDto("english b2 course", 10, LanguageTypeDto.ENGLISH, ProficiencyLevelDto.B2); + @Autowired ObjectMapper objectMapper; + @Autowired private MockMvc mockMvc; @@ -52,7 +56,7 @@ public class CourseControllerTest { @Test void createCourse() throws Exception { - Mockito.when(courseController.create(ArgumentMatchers.isA(CourseCreateDto.class))).thenReturn(courseDto); + Mockito.when(courseController.create(ArgumentMatchers.isA(CourseCreateDto.class))).thenReturn(ResponseEntity.ok(courseDto)); mockMvc.perform(post("/courses") .content(asJsonString(courseCreateDto)) .contentType(MediaType.APPLICATION_JSON)) @@ -68,7 +72,7 @@ public class CourseControllerTest { void createInvalidCourse() throws Exception { CourseCreateDto invalidCourseCreateDto = new CourseCreateDto(null, null, null, null); - Mockito.when(courseController.create(ArgumentMatchers.isA(CourseCreateDto.class))).thenReturn(courseDto); + Mockito.when(courseController.create(ArgumentMatchers.isA(CourseCreateDto.class))).thenReturn(ResponseEntity.badRequest().build()); mockMvc.perform(post("/courses") .content(asJsonString(invalidCourseCreateDto)) .contentType(MediaType.APPLICATION_JSON)) @@ -77,15 +81,15 @@ public class CourseControllerTest { @Test void createCourseWithoutParameter() throws Exception { - Mockito.when(courseController.create(ArgumentMatchers.isA(CourseCreateDto.class))).thenReturn(courseDto); mockMvc.perform(post("/courses")) .andExpect(status().is4xxClientError()); } + @Test void findCourse() throws Exception { Long id = 0L; - Mockito.when(courseController.find(id)).thenReturn(courseDto); + Mockito.when(courseController.find(id)).thenReturn(ResponseEntity.ok(courseDto)); mockMvc.perform(get("/courses/find/" + id)) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("english b2 course")) @@ -97,7 +101,6 @@ public class CourseControllerTest { @Test void findCourseWithoutId() throws Exception { - Mockito.when(courseController.find(ArgumentMatchers.anyLong())).thenReturn(courseDto); mockMvc.perform(get("/courses/find/")) .andExpect(status().is4xxClientError()); } @@ -105,7 +108,7 @@ public class CourseControllerTest { @Test void findAll() throws Exception { int page = 0; - Mockito.when(courseController.findAll(page)).thenReturn(new PageImpl<>(new ArrayList<>())); + Mockito.when(courseController.findAll(page)).thenReturn(ResponseEntity.ok(new PageImpl<>(new ArrayList<>()))); String response = mockMvc.perform(get("/courses/findAll").param("page", Integer.toString(page))) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); @@ -115,16 +118,16 @@ public class CourseControllerTest { @Test void findAllWithoutPage() throws Exception { -// Mockito.when(courseController.findAll(ArgumentMatchers.anyInt())).thenReturn(new PageImpl<>(new ArrayList<>())); mockMvc.perform(get("/courses/findAll")) .andExpect(status().is4xxClientError()); } + @Test void findAllByLang() throws Exception { int page = 0; LanguageTypeDto lang = LanguageTypeDto.ENGLISH; - Mockito.when(courseController.findAll(lang)).thenReturn(new ArrayList<>()); + Mockito.when(courseController.findAll(lang)).thenReturn(ResponseEntity.ok(new ArrayList<>())); String response = mockMvc.perform(get("/courses/findAllByLang") .param("page", Integer.toString(page)) .param("lang", lang.toString())) @@ -144,7 +147,7 @@ public class CourseControllerTest { void findAllByLangProf() throws Exception { LanguageTypeDto lang = LanguageTypeDto.ENGLISH; ProficiencyLevelDto proficiencyLevel = ProficiencyLevelDto.A1; - Mockito.when(courseController.findAll(lang, proficiencyLevel)).thenReturn(new ArrayList<>()); + Mockito.when(courseController.findAll(lang, proficiencyLevel)).thenReturn(ResponseEntity.ok(new ArrayList<>())); String response = mockMvc.perform(get("/courses/findAllByLangProf") .param("lang", lang.toString()) .param("prof", proficiencyLevel.toString())) @@ -156,20 +159,12 @@ public class CourseControllerTest { @Test void findAllByLangProfWithoutParameters() throws Exception { - Mockito.when(courseController.findAll( - ArgumentMatchers.isA(LanguageTypeDto.class), - ArgumentMatchers.isA(ProficiencyLevelDto.class))) - .thenReturn(new ArrayList<>()); mockMvc.perform(get("/courses/findAllByLangProf")) .andExpect(status().is4xxClientError()); } @Test void findAllByLangProfWithoutLangProf() throws Exception { - Mockito.when(courseController.findAll( - ArgumentMatchers.isA(LanguageTypeDto.class), - ArgumentMatchers.isA(ProficiencyLevelDto.class))) - .thenReturn(new ArrayList<>()); String page = "0"; mockMvc.perform(get("/courses/findAllByLangProf").param("page", page)) .andExpect(status().is4xxClientError()); @@ -180,7 +175,7 @@ public class CourseControllerTest { Long id = 0L; Mockito.when(courseController.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(CourseCreateDto.class))) - .thenReturn(courseDto); + .thenReturn(ResponseEntity.ok(courseDto)); mockMvc.perform(put("/courses/update/" + id) .content(asJsonString(courseDto)) @@ -196,9 +191,6 @@ public class CourseControllerTest { @Test void updateCourseWithoutParameter() throws Exception { - Mockito.when(courseController.update(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(CourseCreateDto.class))) - .thenReturn(courseDto); mockMvc.perform(put("/courses/update")) .andExpect(status().is4xxClientError()); } @@ -206,16 +198,14 @@ public class CourseControllerTest { @Test void deleteCourse() throws Exception { Long id = 0L; - Mockito.doNothing().when(courseController).delete(id); + Mockito.when(courseController.delete(id)).thenReturn(ResponseEntity.noContent().build()); mockMvc.perform(delete("/courses/delete/" + id)) - .andExpect(status().isOk()); + .andExpect(status().is2xxSuccessful()); } @Test void deleteCourseWithoutParameter() throws Exception { - Mockito.doNothing().when(courseController).delete(ArgumentMatchers.anyLong()); - mockMvc.perform(delete("/courses/delete/")) .andExpect(status().is4xxClientError()); } @@ -230,7 +220,7 @@ public class CourseControllerTest { courseDtoWithStudent.setStudentIds(new ArrayList<>(List.of(student.getId()))); Mockito.when(courseController.enrol(ArgumentMatchers.eq(id), - ArgumentMatchers.isA(UserDto.class))).thenReturn(courseDtoWithStudent); + ArgumentMatchers.isA(UserDto.class))).thenReturn(ResponseEntity.ok(courseDtoWithStudent)); mockMvc.perform(patch("/courses/enrol/" + id) .content(asJsonString(student)) @@ -246,19 +236,13 @@ public class CourseControllerTest { @Test void enrolCourseWithoutUserParameter() throws Exception { - String id = "0"; - Mockito.when(courseController.enrol(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(courseDto); + Long id = 0L; mockMvc.perform(patch("/courses/enrol/" + id)) .andExpect(status().is4xxClientError()); } @Test void enrolCourseWithoutCourseIdParameter() throws Exception { - Mockito.when(courseController.enrol(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(courseDto); UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza", "Nováková", new AddressDto()); @@ -273,7 +257,7 @@ public class CourseControllerTest { Long id = 0L; Mockito.when(courseController.expel(ArgumentMatchers.eq(id), ArgumentMatchers.isA(UserDto.class))) - .thenReturn(courseDto); + .thenReturn(ResponseEntity.ok(courseDto)); UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza", "Nováková", new AddressDto()); @@ -293,9 +277,6 @@ public class CourseControllerTest { @Test void expelCourseWithoutUserParameter() throws Exception { Long id = 0L; - Mockito.when(courseController.expel(ArgumentMatchers.eq(id), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(courseDto); mockMvc.perform(patch("/courses/expel/" + id)) .andExpect(status().is4xxClientError()); @@ -303,9 +284,6 @@ public class CourseControllerTest { @Test void deleteCourseWithoutCourseIdParameter() throws Exception { - Mockito.when(courseController.expel(ArgumentMatchers.anyLong(), - ArgumentMatchers.isA(UserDto.class))) - .thenReturn(courseDto); UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza", "Nováková", new AddressDto());