diff --git a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseMapperTest.java b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseMapperTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6fda3c0b30a284864c5cf4d33da2081a2fe8fdb3 --- /dev/null +++ b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/course/CourseMapperTest.java @@ -0,0 +1,143 @@ +package org.fuseri.modulelanguageschool.course; + +import org.fuseri.model.dto.course.CourseCreateDto; +import org.fuseri.model.dto.course.CourseDto; +import org.fuseri.model.dto.course.LanguageTypeDto; +import org.fuseri.model.dto.course.ProficiencyLevelDto; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + +import java.util.Collections; +import java.util.List; + +@SpringBootTest +final class CourseMapperTest { + + private final CourseDto courseDto = new CourseDto("AJ1", 10, + LanguageTypeDto.ENGLISH, ProficiencyLevelDto.A1); + private final CourseCreateDto courseCreateDto = new CourseCreateDto("AJ1", 10, + LanguageTypeDto.ENGLISH, ProficiencyLevelDto.A1); + private final Course course = new Course("AJ1", 10, + Language.ENGLISH, ProficiencyLevel.A1, null); + @Autowired + private CourseMapper CourseMapper; + + @Test + void mapNullToDto() { + var createdDto = CourseMapper.mapToDto(null); + + Assertions.assertNull(createdDto); + } + + @Test + void mapToDto() { + var createdDto = CourseMapper.mapToDto(course); + + Assertions.assertEquals(courseDto.getId(), createdDto.getId()); + Assertions.assertEquals(courseDto.getName(), createdDto.getName()); + Assertions.assertEquals(courseDto.getCapacity(), createdDto.getCapacity()); + Assertions.assertEquals(courseDto.getLanguage(), createdDto.getLanguage()); + Assertions.assertEquals(courseDto.getProficiency(), createdDto.getProficiency()); + Assertions.assertEquals(courseDto.getStudentIds(), createdDto.getStudentIds()); + } + + @Test + void mapNullToCourseCourseDto() { + var createdCourse = CourseMapper.mapToCourse((CourseDto) null); + + Assertions.assertNull(createdCourse); + } + + @Test + void mapToCourseCourseDto() { + var createdCourse = CourseMapper.mapToCourse(courseDto); + + Assertions.assertEquals(course.getId(), createdCourse.getId()); + Assertions.assertEquals(course.getName(), createdCourse.getName()); + Assertions.assertEquals(course.getCapacity(), createdCourse.getCapacity()); + Assertions.assertEquals(course.getStudents(), createdCourse.getStudents()); + Assertions.assertEquals(course.getProficiency(), createdCourse.getProficiency()); + Assertions.assertEquals(course.getLanguage(), createdCourse.getLanguage()); + } + + @Test + void mapNullToList() { + var courseDtos = CourseMapper.mapToList(null); + + Assertions.assertNull(courseDtos); + } + + @Test + void mapToEmptyList() { + var courseDtos = CourseMapper.mapToList(Collections.emptyList()); + + Assertions.assertEquals(courseDtos.size(), 0); + } + + @Test + void mapToList() { + var courseDtos = CourseMapper.mapToList(Collections.singletonList(course)); + + Assertions.assertEquals(courseDtos.size(), 1); + + + Assertions.assertEquals(courseDto.getId(), courseDtos.get(0).getId()); + Assertions.assertEquals(courseDto.getName(), courseDtos.get(0).getName()); + Assertions.assertEquals(courseDto.getCapacity(), courseDtos.get(0).getCapacity()); + Assertions.assertEquals(courseDto.getLanguage(), courseDtos.get(0).getLanguage()); + Assertions.assertEquals(courseDto.getProficiency(), courseDtos.get(0).getProficiency()); + Assertions.assertEquals(courseDto.getStudentIds(), courseDtos.get(0).getStudentIds()); + } + + @Test + void mapToEmptyPageDto() { + Page<CourseDto> pageDto = CourseMapper.mapToPageDto(Page.empty()); + + Assertions.assertEquals(1, pageDto.getTotalPages()); + } + + @Test + void mapToPageDto() { + List<Course> courses = List.of(course); + Page<Course> page = new PageImpl<>(courses, PageRequest.of(0, 1), courses.size()); + Page<CourseDto> pageDto = CourseMapper.mapToPageDto(page); + + Assertions.assertEquals(page.getTotalPages(), pageDto.getTotalPages()); + Assertions.assertEquals(page.getNumber(), pageDto.getNumber()); + Assertions.assertEquals(page.getNumberOfElements(), pageDto.getNumberOfElements()); + Assertions.assertEquals(page.getSize(), pageDto.getSize()); + Assertions.assertEquals(page.getTotalElements(), pageDto.getTotalElements()); + + Assertions.assertEquals(courseDto.getId(), pageDto.getContent().get(0).getId()); + Assertions.assertEquals(courseDto.getName(), pageDto.getContent().get(0).getName()); + Assertions.assertEquals(courseDto.getCapacity(), pageDto.getContent().get(0).getCapacity()); + Assertions.assertEquals(courseDto.getLanguage(), pageDto.getContent().get(0).getLanguage()); + Assertions.assertEquals(courseDto.getProficiency(), pageDto.getContent().get(0).getProficiency()); + Assertions.assertEquals(courseDto.getStudentIds(), pageDto.getContent().get(0).getStudentIds()); + } + + @Test + void mapNullToCourseCourseCreateDto() { + var createdCourse = CourseMapper.mapToCourse((CourseCreateDto) null); + + Assertions.assertNull(createdCourse); + } + + @Test + void mapToCourseCourseCreateDto() { + var createdCourse = CourseMapper.mapToCourse(courseCreateDto); + + Assertions.assertEquals(course.getId(), createdCourse.getId()); + Assertions.assertEquals(course.getName(), createdCourse.getName()); + Assertions.assertEquals(course.getCapacity(), createdCourse.getCapacity()); + Assertions.assertEquals(course.getStudents(), createdCourse.getStudents()); + Assertions.assertEquals(course.getProficiency(), createdCourse.getProficiency()); + Assertions.assertEquals(course.getLanguage(), createdCourse.getLanguage()); + } + +}