Skip to content

Commit

Permalink
Merge pull request #87 from whatever-mentoring/feature/betting-contro…
Browse files Browse the repository at this point in the history
…ller-test

Write test code of BettingController
  • Loading branch information
mkSpace authored Mar 27, 2024
2 parents 5dfc671 + 990d601 commit bd8d3ec
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep
@RestControllerAdvice
class ApiExceptionHandler {

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException::class)
private fun handlerMethodArgumentNotValidException(
exception: MethodArgumentNotValidException
Expand All @@ -26,6 +27,7 @@ class ApiExceptionHandler {
)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException::class)
private fun handlerConstraintViolationException(
exception: ConstraintViolationException,
Expand All @@ -36,6 +38,7 @@ class ApiExceptionHandler {
)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentTypeMismatchException::class)
private fun handleMethodArgumentTypeMismatchException(
exception: MethodArgumentTypeMismatchException,
Expand All @@ -46,6 +49,7 @@ class ApiExceptionHandler {
)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException::class)
private fun handleMissingServletRequestParameterException(
exception: MissingServletRequestParameterException,
Expand All @@ -56,6 +60,7 @@ class ApiExceptionHandler {
)
}

@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException::class)
private fun httpRequestMethodNotSupportedException(
exception: HttpRequestMethodNotSupportedException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.whatever.raisedragon.controller.betting

import com.whatever.raisedragon.applicationservice.betting.BettingApplicationService
import com.whatever.raisedragon.applicationservice.betting.dto.BettingCreateUpdateResponse
import com.whatever.raisedragon.applicationservice.betting.dto.BettingRetrieveResponse
import com.whatever.raisedragon.common.Response
import com.whatever.raisedragon.security.authentication.UserInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import com.whatever.raisedragon.applicationservice.betting.dto.BettingCreateServ
import com.whatever.raisedragon.applicationservice.betting.dto.BettingUpdateServiceRequest
import com.whatever.raisedragon.domain.betting.BettingPredictionType
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Positive

@Schema(description = "[Request] 배팅 생성")
data class BettingCreateRequest(
@Schema(description = "Goal Id")
@field:Positive(message = "올바른 goalId를 입력해야합니다.")
val goalId: Long,

@Schema(description = "배팅 타입 [SUCCESS, FAIL]")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.whatever.raisedragon.controller.betting

import com.whatever.raisedragon.ControllerTestSupport
import com.whatever.raisedragon.domain.betting.BettingPredictionType
import com.whatever.raisedragon.security.WithCustomUser
import org.assertj.core.api.Assertions.*
import org.hamcrest.core.IsNull.notNullValue
import org.hamcrest.core.IsNull.nullValue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.Mockito.*
import org.springframework.http.MediaType
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@WithCustomUser(id = 1L, nickname = "User")
class BettingControllerTest : ControllerTestSupport() {

@DisplayName("Betting을 생성한다.")
@Test
fun create1() {
// given
val request = BettingCreateRequest(goalId = 1L, predictionType = BettingPredictionType.FAIL)

// when // then
mockMvc
.perform(
post("/v1/betting")
.with(csrf())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(::print)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.errorResponse").value(nullValue()))
}

@DisplayName("Betting을 생성할 때 goalId는 양수이다.")
@Test
fun create2() {
// given
val request = BettingCreateRequest(goalId = -1, predictionType = BettingPredictionType.FAIL)

// when // then
mockMvc
.perform(
post("/v1/betting")
.with(csrf())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(::print)
.andExpect(status().isBadRequest)
.andExpect(jsonPath("$.isSuccess").value(false))
.andExpect(jsonPath("$.errorResponse").value(notNullValue()))
.andExpect(jsonPath("$.errorResponse.detailMessage").value("올바른 goalId를 입력해야합니다."))
.andExpect(jsonPath("$.data").isEmpty)
}

@DisplayName("단건 베팅을 조회합니다.")
@Test
fun retrieve() {
// given
val bettingId = 1L

// when // then
mockMvc
.perform(
get("/v1/betting/$bettingId")
)
.andDo(::print)
.andExpect(status().isOk)
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.errorResponse").value(nullValue()))
}

@DisplayName("베팅을 업데이트 합니다.")
@Test
fun update() {
// given
val request = BettingUpdateRequest(bettingId = 1L, predictionType = BettingPredictionType.SUCCESS)

// when // then
mockMvc
.perform(
put("/v1/betting")
.with(csrf())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(::print)
.andExpect(status().isOk)
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.errorResponse").value(nullValue()))
}
}

0 comments on commit bd8d3ec

Please sign in to comment.