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

Merge branch 'main' into 'M3-data-initialization'

# Conflicts:
#   application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/course/Course.java
parents 45b43706 8cb05668
No related branches found
No related tags found
1 merge request!34M3 data initialization
Pipeline #
......@@ -21,22 +21,21 @@ import static org.mockito.Mockito.*;
@SpringBootTest
final class CourseServiceTest {
@MockBean
private CourseRepository courseRepository;
@Autowired
private CourseService courseService;
private final Course course = new Course("AJ1", 10,
Language.ENGLISH, ProficiencyLevel.A1, new ArrayList<>(), false);
private final Course course = new Course("AJ1", 10,
Language.ENGLISH, ProficiencyLevel.A1);
private final User user = new User("novakovat", UserType.STUDENT,
"novakova@gamil.com", "password", "Tereza",
"novakova@gamil.com", "password", "Tereza",
"Nováková", new Address(), new HashSet<>(), new HashMap<>());
private final Course courseWithEnrolledStudent = new Course("AJ1", 10,
Language.ENGLISH, ProficiencyLevel.A1, new ArrayList<>(List.of(user)), false);
private final List<Course> courses = List.of(course, course);
@MockBean
private CourseRepository courseRepository;
@Autowired
private CourseService courseService;
@Test
void save() {
when(courseRepository.save(course)).thenReturn(course);
......
......@@ -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