Skip to content

Commit

Permalink
Merge pull request #51 from TeamDMU/test/api-old
Browse files Browse the repository at this point in the history
[Test] 구버전 API 테스트 작성
  • Loading branch information
GiJungPark authored Nov 4, 2024
2 parents 1560b6d + 18d20de commit a6ffcdb
Show file tree
Hide file tree
Showing 11 changed files with 455 additions and 7 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ subprojects {
"**/*Config*",
"**/*Dto*",
"**/*Error*",
"**/request/**",
"**/response/**",
"**/*Request*",
"**/*Response*",
"**/*Interceptor*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dmforu.admin.scheduler.crawling

import com.dmforu.crawling.parser.DietParser
import com.dmforu.domain.diet.Diet
import com.dmforu.domain.diet.DietWriter
import com.dmforu.domain.schedule.Schedule
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any

@ExtendWith(MockitoExtension::class)
class DietCrawlingServiceTest {

@Mock
private lateinit var dietParser: DietParser

@Mock
private lateinit var dietWriter: DietWriter

@InjectMocks
private lateinit var dietCrawlingService: DietCrawlingService

@DisplayName("새로운 식단표를 덮어쓴다.")
@Test
fun updateToRecentSchedule() {
// given
val parseResult: List<Diet> = mock()
given(dietParser.parse()).willReturn(parseResult)

// when
dietCrawlingService.updateToRecentDiet()

// then
verify(dietParser).parse()
verify(dietWriter).overwrite(any())

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dmforu.admin.scheduler.crawling

import com.dmforu.crawling.parser.ScheduleParser
import com.dmforu.domain.schedule.Schedule
import com.dmforu.domain.schedule.ScheduleWriter
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any

@ExtendWith(MockitoExtension::class)
class ScheduleCrawlingServiceTest {

@Mock
private lateinit var scheduleParser: ScheduleParser

@Mock
private lateinit var scheduleWriter: ScheduleWriter

@InjectMocks
private lateinit var scheduleCrawlingService: ScheduleCrawlingService

@DisplayName("새로운 학사 일정을 덮어쓴다.")
@Test
fun updateToRecentSchedule() {
// given
val parseResult: List<Schedule.Year> = mock()
given(scheduleParser.parse(any())).willReturn(parseResult)

// when
scheduleCrawlingService.updateToRecentSchedule()

// then
verify(scheduleParser).parse(any())
verify(scheduleWriter).overwrite(any())

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ class ApiControllerAdvice {
return ResponseEntity(ErrorResponse.error(ErrorType.NOT_FOUND_ERROR), ErrorType.NOT_FOUND_ERROR.status)
}

@ExceptionHandler(NoResourceFoundException::class)
fun handleNoResourceFoundException(e: NoResourceFoundException): ResponseEntity<ApiResponse<ErrorMessage>> {
return ResponseEntity(ErrorResponse.error(ErrorType.BAD_REQUEST_ERROR), ErrorType.BAD_REQUEST_ERROR.status)
}

@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleRequestBodyException(e: MethodArgumentNotValidException): ResponseEntity<ApiResponse<ErrorMessage>> {
log.error("MethodArgumentNotValidException : {}", e.message, e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.dmforu.api

import com.dmforu.api.controller.old.OldDietController
import com.dmforu.api.controller.old.OldNoticeController
import com.dmforu.api.controller.old.OldScheduleController
import com.dmforu.api.controller.old.OldSubscribeController
import com.dmforu.api.controller.v1.DietController
import com.dmforu.api.controller.v1.NoticeController
import com.dmforu.api.controller.v1.ScheduleController
import com.dmforu.api.controller.v1.SubscribeController
import com.dmforu.domain.diet.DietReader
import com.dmforu.domain.notice.NoticeReader
import com.dmforu.domain.schedule.ScheduleReader
import com.dmforu.domain.subscribe.OldSubscribeUpdater
import com.dmforu.domain.subscribe.SubscribeReader
import com.dmforu.domain.subscribe.SubscribeUpdater
import com.dmforu.domain.subscribe.SubscribeWriter
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.web.servlet.MockMvc

@WebMvcTest(controllers = [OldDietController::class, OldNoticeController::class, OldScheduleController::class, OldSubscribeController::class])
abstract class OldControllerTestSupport {

@Autowired
protected lateinit var mockMvc: MockMvc

@Autowired
protected lateinit var objectMapper: ObjectMapper

@MockBean
protected lateinit var dietReader: DietReader

@MockBean
protected lateinit var noticeReader: NoticeReader

@MockBean
protected lateinit var scheduleReader: ScheduleReader

@MockBean
protected lateinit var subscribeWriter: SubscribeWriter

@MockBean
protected lateinit var subscribeUpdater: OldSubscribeUpdater
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dmforu.api.controller.old

import com.dmforu.api.OldControllerTestSupport
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class OldDietControllerTest : OldControllerTestSupport() {

@DisplayName("식단을 불러온다.")
@Test
fun readDiet() {
mockMvc.perform(
get("/api/v1/dmu/cafeteria")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.dmforu.api.controller.old

import com.dmforu.api.OldControllerTestSupport
import com.dmforu.api.support.error.ErrorCode
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
import org.mockito.BDDMockito.given
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class OldNoticeControllerTest : OldControllerTestSupport() {

@DisplayName("대학 공지를 불러온다.")
@Test
fun getUniversityNotice() {
// given
given(noticeReader.readUniversityNotice(anyInt(), anyInt())).willReturn(listOf())

// when then
mockMvc.perform(
get("/api/v1/dmu/notice/universityNotice")
.param("page", "1")
.param("size", "20")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)
}

@DisplayName("학과 공지를 불러온다.")
@Test
fun getDepartmentNotice() {
// given
given(noticeReader.readDepartmentNotice(anyString(), anyInt(), anyInt())).willReturn(listOf())

// when then
mockMvc.perform(
get("/api/v1/dmu/departmentNotice/{department}", "컴퓨터소프트웨어공학과")
.param("page", "1")
.param("size", "20")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)
}

@DisplayName("학과 공지를 불러올 때, 학과는 필수 값이다.")
@Test
fun getDepartmentNoticeWhenDepartmentIsEmpty() {
mockMvc.perform(
get("/api/v1/dmu/departmentNotice/")
.param("page", "1")
.param("size", "20")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isNotFound)
.andExpect(jsonPath("$.error.code").value(ErrorCode.E404.name))
.andExpect(jsonPath("$.error.message").value("잘못된 URL로 요청을 하였습니다."))
.andExpect(jsonPath("$.error.data").isEmpty)
}


@DisplayName("공지를 검색한다.")
@Test
fun getNoticeByKeyword() {
// given
given(noticeReader.searchNotice(anyString(), anyString(), anyInt(), anyInt())).willReturn(listOf())

// when then
mockMvc.perform(
get("/api/v1/dmu/notice/{searchWord}", "검색어")
.param("department", "컴퓨터소프트웨어공학과")
.param("page", "1")
.param("size", "20")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)
}

@DisplayName("공지를 검색할 때, 학과는 필수 값이다.")
@Test
fun getNoticeByKeywordWhen() {
mockMvc.perform(
get("/api/v1/dmu/notice/{searchWord}", "검색어")
.param("page", "1")
.param("size", "20")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isBadRequest)
.andExpect(jsonPath("$.error.code").value(ErrorCode.E400.name))
.andExpect(jsonPath("$.error.message").value("잘못된 요청을 하였습니다."))
.andExpect(jsonPath("$.error.data").isEmpty)
}

@DisplayName("공지를 검색할 때, 검색어는 필수 값이다. ")
@Test
fun getNoticeByKeywordWhenPathValueIsEmpty() {
mockMvc.perform(
get("/api/v1/dmu/notice/")
.param("department", "컴퓨터소프트웨어공학과")
.param("page", "1")
.param("size", "20")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isNotFound)
.andExpect(jsonPath("$.error.code").value(ErrorCode.E404.name))
.andExpect(jsonPath("$.error.message").value("잘못된 URL로 요청을 하였습니다."))
.andExpect(jsonPath("$.error.data").isEmpty)
}

@DisplayName("공지를 불러올 때, 페이지네이션 값이 빠져있다면 페이지 1, 사이즈 20으로 자동 설정된다.")
@Test
fun getNoticeWithoutPagination() {
// given
given(noticeReader.readUniversityNotice(anyInt(), anyInt())).willReturn(listOf())
given(noticeReader.readDepartmentNotice(anyString(), anyInt(), anyInt())).willReturn(listOf())
given(noticeReader.searchNotice(anyString(), anyString(), anyInt(), anyInt())).willReturn(listOf())

// when then
mockMvc.perform(
get("/api/v1/dmu/notice/universityNotice")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)

mockMvc.perform(
get("/api/v1/dmu/notice/departmentNotice")
.param("department", "컴퓨터소프트웨어공학과")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)

mockMvc.perform(
get("/api/v1/dmu/notice/{searchWord}", "검색어")
.param("department", "컴퓨터소프트웨어공학과")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.dmforu.api.controller.old

import com.dmforu.api.OldControllerTestSupport
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.BDDMockito.given
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class OldScheduleControllerTest : OldControllerTestSupport() {

@DisplayName("학사 일정을 불러온다.")
@Test
fun readSchedule() {
// given
given(scheduleReader.read()).willReturn(listOf())

// when // then
mockMvc.perform(
get("/api/v1/dmu/schedule")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray)
}
}
Loading

0 comments on commit a6ffcdb

Please sign in to comment.