Skip to content
Snippets Groups Projects

M2 course tests

Merged Ester Vilímková requested to merge M2-course-tests into M2
1 file
+ 1
1
Compare changes
  • Side-by-side
  • Inline
@@ -15,57 +15,70 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDateTime;
import java.util.ArrayList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
public class LectureTest {
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;
@Autowired
private MockMvc mockMvc;
@MockBean
private LectureController lectureController;
private LectureFacade lectureFacade;
private final LectureCreateDto lectureCreateDto = new LectureCreateDto(
LocalDateTime.now().plusDays(2),
LocalDateTime.now().plusDays(2).plusHours(2),
"Learning how to spell deprecated",
10, 0L);
private final LectureDto lectureDto = new LectureDto(
LocalDateTime.now().plusDays(2),
LocalDateTime.now().plusDays(2).plusHours(2),
"Learning how to spell deprecated",
10, 0L, 0L);
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(lectureFacade.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto);
mockMvc.perform(post("/lectures")
.content(asJsonString(lectureCreateDto))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.from").isNotEmpty())
.andExpect(jsonPath("$.to").isNotEmpty())
.andExpect(status().is2xxSuccessful())
.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"))
.andReturn().getResponse().getContentAsString();
.andExpect(jsonPath("$.courseId").value("0"));
}
@Test
void createInvalidLecture() throws Exception {
LectureCreateDto invalidLectureCreateDto =
new LectureCreateDto(null, null, null, null, null);
Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto);
mockMvc.perform(post("/lectures")
.content(asJsonString(invalidLectureCreateDto))
.contentType(MediaType.APPLICATION_JSON))
@@ -74,7 +87,6 @@ public class LectureTest {
@Test
void createLectureWithoutParameter() throws Exception {
Mockito.when(lectureController.create(ArgumentMatchers.isA(LectureCreateDto.class))).thenReturn(lectureDto);
mockMvc.perform(post("/lectures"))
.andExpect(status().is4xxClientError());
}
@@ -82,21 +94,19 @@ public class LectureTest {
@Test
void findLecture() throws Exception {
Long id = 0L;
Mockito.when(lectureController.find(id)).thenReturn(lectureDto);
Mockito.when(lectureFacade.findById(id)).thenReturn(lectureDto);
mockMvc.perform(get("/lectures/find/" + id))
.andExpect(status().isOk())
.andExpect(jsonPath("$.from").isNotEmpty())
.andExpect(jsonPath("$.to").isNotEmpty())
.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"))
.andReturn().getResponse().getContentAsString();
.andExpect(jsonPath("$.courseId").value("0"));
}
@Test
void findLectureWithoutId() throws Exception {
Mockito.when(lectureController.find(ArgumentMatchers.anyLong())).thenReturn(lectureDto);
mockMvc.perform(get("/lectures/find/"))
.andExpect(status().is4xxClientError());
}
@@ -104,18 +114,16 @@ public class LectureTest {
@Test
void findLecturesByCourse() throws Exception {
Long id = 0L;
Mockito.when(lectureController.findByCourse(id)).thenReturn(new ArrayList<>());
Mockito.when(lectureFacade.findAll(id)).thenReturn(new ArrayList<>());
String response = mockMvc.perform(get("/lectures/findByCourse")
.param("courseId", String.valueOf(id)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertThat("response", response, is("[]"));
assertTrue(response.endsWith("[]"));
}
@Test
void findLecturesByCourseWithoutParameter() throws Exception {
Mockito.when(lectureController.findByCourse(ArgumentMatchers.anyLong())).thenReturn(new ArrayList<>());
mockMvc.perform(get("/lectures/findByCourse"))
.andExpect(status().is4xxClientError());
}
@@ -123,27 +131,23 @@ public class LectureTest {
@Test
void updateLecture() throws Exception {
Long id = 0L;
Mockito.when(lectureController.update(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(LectureCreateDto.class)))
Mockito.when(lectureFacade.update(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(LectureCreateDto.class)))
.thenReturn(lectureDto);
mockMvc.perform(put("/lectures/update/" + id)
.content(asJsonString(lectureCreateDto))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.from").isNotEmpty())
.andExpect(jsonPath("$.to").isNotEmpty())
.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"))
.andReturn().getResponse().getContentAsString();
.andExpect(jsonPath("$.courseId").value("0"));
}
@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());
}
@@ -151,14 +155,13 @@ public class LectureTest {
@Test
void deleteLecture() throws Exception {
Long id = 0L;
Mockito.doNothing().when(lectureController).delete(id);
Mockito.doNothing().when(lectureFacade).delete(id);
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());
}
@@ -166,29 +169,25 @@ public class LectureTest {
@Test
void setLecturerForLecture() throws Exception {
Long id = 0L;
Mockito.when(lectureController.setLecturer(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(UserDto.class)))
Mockito.when(lectureFacade.setLecturer(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(UserDto.class)))
.thenReturn(lectureDto);
UserDto student = new UserDto("novakovat","novakova@gamil.com", "Tereza",
UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza",
"Nováková", new AddressDto());
mockMvc.perform(patch("/lectures/setLecturer/" + id)
.content(asJsonString(student))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.from").isNotEmpty())
.andExpect(jsonPath("$.to").isNotEmpty())
.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"))
.andReturn().getResponse().getContentAsString();
.andExpect(jsonPath("$.courseId").value("0"));
}
@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());
}
@@ -196,29 +195,25 @@ public class LectureTest {
@Test
void enrolLecture() throws Exception {
Long id = 0L;
Mockito.when(lectureController.enrol(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(UserDto.class)))
Mockito.when(lectureFacade.enrol(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(UserDto.class)))
.thenReturn(lectureDto);
UserDto student = new UserDto("novakovat","novakova@gamil.com", "Tereza",
UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza",
"Nováková", new AddressDto());
mockMvc.perform(patch("/lectures/enrol/" + id)
.content(asJsonString(student))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.from").isNotEmpty())
.andExpect(jsonPath("$.to").isNotEmpty())
.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"))
.andReturn().getResponse().getContentAsString();
.andExpect(jsonPath("$.courseId").value("0"));
}
@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());
}
@@ -226,41 +221,27 @@ public class LectureTest {
@Test
void expelLecture() throws Exception {
Long id = 0L;
Mockito.when(lectureController.expel(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(UserDto.class)))
Mockito.when(lectureFacade.expel(ArgumentMatchers.eq(id),
ArgumentMatchers.isA(UserDto.class)))
.thenReturn(lectureDto);
UserDto student = new UserDto("novakovat","novakova@gamil.com", "Tereza",
UserDto student = new UserDto("novakovat", "novakova@gamil.com", "Tereza",
"Nováková", new AddressDto());
mockMvc.perform(patch("/lectures/expel/" + id)
.content(asJsonString(student))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.from").isNotEmpty())
.andExpect(jsonPath("$.to").isNotEmpty())
.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"))
.andReturn().getResponse().getContentAsString();
.andExpect(jsonPath("$.courseId").value("0"));
}
@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);
}
}
}
Loading