|
| 1 | +package io.csbroker.apiserver.controller.v1.problem |
| 2 | + |
| 3 | +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper |
| 4 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 5 | +import com.jayway.jsonpath.JsonPath |
| 6 | +import io.csbroker.apiserver.controller.IntegrationTest |
| 7 | +import io.csbroker.apiserver.controller.v2.problem.response.SubmitLongProblemResponseDto |
| 8 | +import io.csbroker.apiserver.dto.problem.longproblem.LongProblemAnswerDto |
| 9 | +import io.csbroker.apiserver.model.GradingHistory |
| 10 | +import io.csbroker.apiserver.model.LongProblem |
| 11 | +import io.csbroker.apiserver.model.StandardAnswer |
| 12 | +import io.kotest.matchers.collections.shouldContain |
| 13 | +import io.kotest.matchers.shouldBe |
| 14 | +import org.junit.jupiter.api.Test |
| 15 | +import org.springframework.http.HttpMethod |
| 16 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status |
| 17 | + |
| 18 | +class LongProblemIntegrationTest : IntegrationTest() { |
| 19 | + |
| 20 | + private val baseUrl = "/api/v1/problems/long" |
| 21 | + private val objectMapper = jacksonObjectMapper() |
| 22 | + |
| 23 | + @Test |
| 24 | + fun `서술형 문제 조회`() { |
| 25 | + // given & when |
| 26 | + |
| 27 | + val problem = getProblem() |
| 28 | + val response = request( |
| 29 | + method = HttpMethod.GET, |
| 30 | + url = "$baseUrl/${problem.id}", |
| 31 | + ) |
| 32 | + |
| 33 | + // then |
| 34 | + response.andExpect(status().isOk) |
| 35 | + .andDo { |
| 36 | + val title = JsonPath.read(it.response.contentAsString, "$.data.title") as String |
| 37 | + val description = JsonPath.read(it.response.contentAsString, "$.data.description") as String |
| 38 | + title shouldBe problem.title |
| 39 | + description shouldBe problem.description |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `서술형 문제 제출`() { |
| 45 | + val problem = getProblem() |
| 46 | + |
| 47 | + // given & when |
| 48 | + val preSubmissionCount = findAll<GradingHistory>( |
| 49 | + "SELECT gh From GradingHistory gh where gh.problem.id = :id", |
| 50 | + mapOf("id" to problem.id!!), |
| 51 | + ).size |
| 52 | + |
| 53 | + val preUserSubmissionCount = findAll<GradingHistory>( |
| 54 | + "SELECT gh From GradingHistory gh where gh.problem.id = :problemId AND gh.user.id = :userId", |
| 55 | + mapOf("problemId" to problem.id!!, "userId" to defaultUser.id!!), |
| 56 | + ).size |
| 57 | + |
| 58 | + val standardAnswers = findAll<StandardAnswer>( |
| 59 | + "SELECT sa From StandardAnswer sa where sa.longProblem.id = :id", |
| 60 | + mapOf("id" to problem.id!!), |
| 61 | + ) |
| 62 | + |
| 63 | + val userAnswer = "answer" |
| 64 | + val response = request( |
| 65 | + method = HttpMethod.POST, |
| 66 | + url = "$baseUrl/${problem.id}/submit", |
| 67 | + body = LongProblemAnswerDto(userAnswer), |
| 68 | + ) |
| 69 | + |
| 70 | + // userAnswer 생성 확인 |
| 71 | + |
| 72 | + response.andExpect(status().isOk) |
| 73 | + .andDo { |
| 74 | + val jsonNode = objectMapper.readTree(it.response.contentAsString) |
| 75 | + val dataNode = jsonNode.get("data") |
| 76 | + val dataAsString = dataNode.toString() |
| 77 | + val responseDto = objectMapper.readValue<SubmitLongProblemResponseDto>(dataAsString) |
| 78 | + responseDto.title shouldBe problem.title |
| 79 | + responseDto.description shouldBe problem.description |
| 80 | + responseDto.totalSubmissionCount shouldBe preSubmissionCount + 1 // 제출 수가 증가하는지 확인 |
| 81 | + responseDto.userSubmissionCount shouldBe preUserSubmissionCount + 1 // 제출 수가 증가하는지 확인 |
| 82 | + responseDto.userAnswer shouldBe userAnswer |
| 83 | + standardAnswers.map { sa -> sa.content } shouldContain responseDto.standardAnswer // 모법답안중 하나가 반환되는지 확인 |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fun getProblem(): LongProblem { |
| 88 | + val problem = save( |
| 89 | + LongProblem( |
| 90 | + title = "title", |
| 91 | + description = "description", |
| 92 | + creator = defaultUser, |
| 93 | + ), |
| 94 | + ) |
| 95 | + save(StandardAnswer(content = "standard answer1", longProblem = problem)) |
| 96 | + save(StandardAnswer(content = "standard answer2", longProblem = problem)) |
| 97 | + |
| 98 | + return problem |
| 99 | + } |
| 100 | +} |
0 commit comments