Skip to content

Commit

Permalink
[#27] 강의 및 일정 조회 테스트 (#39)
Browse files Browse the repository at this point in the history
* test : [스마트 캠퍼스 API] 강의 및 일정 조희, 생성 테스트

* test : [스마트 캠퍼스 API] Integration Test, Controller Test

* chore: 테스트 데이터 밀어넣는 방식 변경

* test: 테스트 정상적으로 돌아가도록 수정

* chore: jacksonObjectMapper 추가

* refactor: develop merge

* refactor: 잘못 머지한 문장 삭제

* refactor: 중복 문장 제거, 데이터 삽입 형식 변경

---------

Co-authored-by: kkanggu <[email protected]>
  • Loading branch information
swdevsw98 and kkanggu authored Sep 8, 2023
1 parent 6839ee3 commit 4fa6c6e
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema


@Schema(name = "과제 등록 API Request Body")
class AssignmentRequest(
data class AssignmentRequest(
@Schema(description = "강의 id")
val courseId: Long,
@Schema(description = "과제 이름")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.example.daitssuapi.common.enums.CalendarType
import io.swagger.v3.oas.annotations.media.Schema

@Schema(name = "일정 추가 API Request Body")
class CalendarRequest(
data class CalendarRequest(
@Schema(description = "일정의 종류")
val type: CalendarType,
@Schema(description = "일정의 분류")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.daitssuapi.domain.course.dto.request

import org.jetbrains.annotations.NotNull

class CourseRequest(
data class CourseRequest(
@NotNull
val name: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.daitssuapi.domain.course.dto.request
import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "강의 등록 API Request Body입니다. ")
class VideoRequest(
data class VideoRequest(
@Schema(description = "강의 id")
val courseId: Long,
@Schema(description = "강의 이름")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.daitssuapi.domain.course.dto.response

import java.time.LocalDateTime

class AssignmentResponse(
data class AssignmentResponse(
val id: Long,
val name: String,
val dueAt: LocalDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.daitssuapi.domain.course.dto.response
import com.example.daitssuapi.common.enums.CalendarType
import java.time.LocalDateTime

class CalendarResponse(
data class CalendarResponse(
val type: CalendarType,
val dueAt: LocalDateTime,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.daitssuapi.domain.course.dto.response

class CourseResponse(
data class CourseResponse(
val name: String,
val videos: List<VideoResponse> = emptyList(),
val assignments: List<AssignmentResponse> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.daitssuapi.domain.course.dto.response

import java.time.LocalDateTime

class VideoResponse(
data class VideoResponse(
val id: Long,
val name: String,
val dueAt: LocalDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package com.example.daitssuapi.domain.course.model.entity
import com.example.daitssuapi.common.audit.BaseEntity
import com.example.daitssuapi.common.enums.CalendarType
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.Table
import java.time.LocalDateTime

@Entity
@Table(schema = "course")
class Calendar(
@Enumerated(EnumType.STRING)
val type: CalendarType,
val course: String,
val dueAt: LocalDateTime,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.example.daitssuapi.domain.course.controller

import com.example.daitssuapi.common.enums.CalendarType
import com.example.daitssuapi.common.enums.ErrorCode
import com.example.daitssuapi.domain.course.dto.request.CalendarRequest
import com.example.daitssuapi.domain.course.model.repository.UserCourseRelationRepository
import com.example.daitssuapi.utils.ControllerTest
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
Expand All @@ -19,6 +24,7 @@ class CourseControllerTest(
private lateinit var mockMvc: MockMvc

private val url = "/course/user"
private val courseUrl = "/course"

@Test
@DisplayName("성공_올바른 userId를 이용하여 과목 조회 시_1개 이상의 과목이 조회될 수 있다")
Expand All @@ -38,4 +44,117 @@ class CourseControllerTest(
.andExpect(status().isOk)
.andExpect(jsonPath("$.data").isEmpty)
}

@Test
@DisplayName("과목 리스트 조회시_모든 과목이 조회된다")
fun get_course_list() {
val response = mockMvc.perform(get("$courseUrl"))
.andExpect(status().isOk)
.andReturn().response

assertThat(jacksonObjectMapper().readTree(response.contentAsString)["data"].isEmpty).isFalse()
}

@Test
@DisplayName("올바른 courseId로 과목 조회시_과목에 대한 정보가 조회된다")
fun get_course_with_course_id () {
val courseId = 1L
val response = mockMvc.perform(get("$courseUrl/$courseId"))
.andExpect(status().isOk)
.andReturn().response

assertThat(jacksonObjectMapper().readTree(response.contentAsString)["data"].isEmpty).isFalse()
}

@Test
@DisplayName("올바르지 않은 courseId로 과목 조회시_에러가 발생한다")
fun get_course_with_wrong_course_id () {
val wrongCourseId = 0L
val response = mockMvc.perform(get("$courseUrl/$wrongCourseId"))
.andExpect(status().isBadRequest)
.andReturn().response

val responseBody = jacksonObjectMapper().readTree(response.contentAsByteArray)

assertAll(
{ assertThat(responseBody["code"].intValue()).isEqualTo(ErrorCode.COURSE_NOT_FOUND.code) },
{ assertThat(responseBody["message"].textValue()).isEqualTo(ErrorCode.COURSE_NOT_FOUND.message) },
{ assertThat(responseBody["data"].isNull).isTrue() }
)
}

@Test
@DisplayName("올바른 date로 캘린더 조회시_캘린더에 대한 정보가 조회된다")
fun get_calendar_with_date() {
val date = "2023-07-31 23:59:59"
val response = mockMvc.perform(get("$courseUrl/calendar/$date"))
.andExpect(status().isOk)
.andReturn().response

assertThat(jacksonObjectMapper().readTree(response.contentAsString).isEmpty).isFalse()
}

@Test
@DisplayName("잘못된 date로 캘린더 조회시_에러가 출력된다")
fun get_calendar_with_wrong_date () {
val wrongDate = "2023-07-31"
val response = mockMvc.perform(get("$courseUrl/calendar/$wrongDate"))
.andExpect(status().isBadRequest)
.andReturn().response

val responseBody = jacksonObjectMapper().readTree(response.contentAsByteArray)

assertAll(
{ assertThat(responseBody["code"].intValue()).isEqualTo(ErrorCode.INVALID_DATE_FORMAT.code) },
{ assertThat(responseBody["message"].textValue()).isEqualTo(ErrorCode.INVALID_DATE_FORMAT.message) },
{ assertThat(responseBody["data"].isNull).isTrue() }
)
}

@Test
@DisplayName("올바른 date가 포함된 calendarRequest로 post시_캘린더가 생성된다")
fun post_create_calendar_with_calendar_request() {
val calendarRequest = CalendarRequest(
name = "숙제 마감일",
type = CalendarType.ASSIGNMENT,
course = "do it",
dueAt = "2023-07-27 23:59:59"
)
val response = mockMvc.perform(
post("$courseUrl/calendar")
.contentType(MediaType.APPLICATION_JSON)
.content(jacksonObjectMapper().writeValueAsString(calendarRequest))
)
.andExpect(status().isOk)
.andReturn().response

assertThat(jacksonObjectMapper().readTree(response.contentAsString)["data"].isEmpty).isFalse()
}

@Test
@DisplayName("잘못된 date가 포함된 calendarRequest로 post시_에러가 발생한다")
fun post_create_calendar_with_wrong_calendar_request() {
val calendarRequest = CalendarRequest(
name = "강의 출석 마감일",
type = CalendarType.VIDEO,
course = "choco",
dueAt = "2023-07-27"
)
val response = mockMvc.perform(
post("$courseUrl/calendar")
.contentType(MediaType.APPLICATION_JSON)
.content(jacksonObjectMapper().writeValueAsString(calendarRequest))
)
.andExpect(status().isBadRequest)
.andReturn().response

val responseBody = jacksonObjectMapper().readTree(response.contentAsByteArray)

assertAll(
{ assertThat(responseBody["code"].intValue()).isEqualTo(ErrorCode.INVALID_DATE_FORMAT.code) },
{ assertThat(responseBody["message"].textValue()).isEqualTo(ErrorCode.INVALID_DATE_FORMAT.message) },
{ assertThat(responseBody["data"].isNull).isTrue() }
)

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.example.daitssuapi.domain.course.service

import com.example.daitssuapi.common.enums.CalendarType
import com.example.daitssuapi.common.enums.RegisterStatus
import com.example.daitssuapi.common.exception.DefaultException
import com.example.daitssuapi.domain.course.dto.request.CalendarRequest
import com.example.daitssuapi.domain.course.model.repository.CourseRepository
import com.example.daitssuapi.domain.course.model.repository.UserCourseRelationRepository
import com.example.daitssuapi.domain.main.model.repository.UserRepository
import com.example.daitssuapi.utils.IntegrationTest
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.data.repository.findByIdOrNull

@IntegrationTest
class CourseServiceTest(
private val courseService: CourseService,
private val userRepository: UserRepository,
private val courseRepository: CourseRepository,
private val userCourseRelationRepository: UserCourseRelationRepository
) {
@Test
Expand Down Expand Up @@ -43,4 +48,110 @@ class CourseServiceTest(
{ assertThat(findCourses).isEmpty() }
)
}

@Test
@DisplayName("과목 리스트 조회 시_과목 리스트가 출력된다")
fun get_course_list () {
val courses = courseRepository.findAll()
val findCourses = courseService.getCourseList()

assertAll(
{ assertThat(findCourses).isNotEmpty },
{ assertThat(courses.size).isEqualTo(findCourses.size) }
)
}

@Test
@DisplayName("올바른 courseId를 이용하여 조회시_과목이 조회된다")
fun get_course_with_course_id () {
val courseId = 1L
val course = courseRepository.findByIdOrNull(courseId)
val findCourse = courseService.getCourse(courseId)

assertAll(
{ assertThat(findCourse.name).isEqualTo(course?.name) },
{ assertThat(findCourse.term).isEqualTo(course?.term) },
{ assertThat(findCourse.videos.size).isEqualTo(course?.videos?.size) },
{ assertThat(findCourse.assignments.size).isEqualTo(course?.videos?.size) }
)
}

@Test
@DisplayName("잘못된 courseId를 이용하여 조회시_에러가 출력된다")
fun get_course_with_wrong_course_id() {
val wrongCourseId = 0L

org.junit.jupiter.api.assertThrows<DefaultException> {
courseService.getCourse(wrongCourseId)
}

}

@Test
@DisplayName("올바른 date 형식으로 캘린더 조회시_일정이 조회된다")
fun get_calendar_with_date () {
// case 1. 조회가 잘되는지 확인
val date = "2023-07-01 11:00:00"
val name = "eat paper"
val findCalendar = courseService.getCalendar(date)

assertAll(
{ assertThat(findCalendar.keys).contains(name) },
{ assertThat(findCalendar[name]?.size).isEqualTo(2) }
)

// case2 경계값
val date2 = "2023-05-01 00:00:00"
val name2 = "choco"
val findCalendar2 = courseService.getCalendar(date2)

assertAll(
{ assertThat(findCalendar2.keys).contains(name2) },
{ assertThat(findCalendar2[name2]?.size).isEqualTo(1) }
)
}

@Test
@DisplayName("잘못된 date 형식으로 캘린더 조회시_에러가 발생한다")
fun get_calendar_with_wrong_date() {
val date = "2023-07-27"

org.junit.jupiter.api.assertThrows<DefaultException> {
courseService.getCalendar(date)
}
}

@Test
@DisplayName("올바른 calendarRequest 요청시_캘린더가 생성된다")
fun post_create_calendar_with_calendar_request() {
val calendarRequest = CalendarRequest(
type = CalendarType.VIDEO,
course = "do it",
dueAt = "2023-07-27 23:59:59",
name = "과제 꼭 하기"
)
val findCalendar = courseService.postCalendar(calendarRequest)

assertAll(
{ assertThat(findCalendar.type).isEqualTo(calendarRequest.type) },
{ assertThat(findCalendar.name).isEqualTo(calendarRequest.name) },
{ assertThat(findCalendar.dueAt).isEqualTo("2023-07-27T23:59:59") }
)
}

@Test
@DisplayName("잘못된 calendarRequest 요청시_에러가 발생한다")
fun post_create_calendar_with_wrong_calendar_request() {
val calendarRequest = CalendarRequest(
type = CalendarType.VIDEO,
course = "do it",
dueAt = "2023-07-27",
name = "과제 꼭 하기"
)

org.junit.jupiter.api.assertThrows<DefaultException> {
courseService.postCalendar(calendarRequest)
}
}

}
Loading

0 comments on commit 4fa6c6e

Please sign in to comment.