Skip to content

Commit

Permalink
feat: get top の実装 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinari321 authored Jan 13, 2023
1 parent 600197e commit 9df6c5e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.sunabak0.akiyadego.presentation

import dev.sunabak0.akiyadego.openapi.generated.controller.DefaultApi
import dev.sunabak0.akiyadego.openapi.generated.model.Post
import dev.sunabak0.akiyadego.openapi.generated.model.PostResponse
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
Expand All @@ -14,4 +16,24 @@ class ApplicationController : DefaultApi {
override fun rootGet(): ResponseEntity<Unit> {
return ResponseEntity(Unit, HttpStatus.OK)
}

override fun allPost(): ResponseEntity<PostResponse> {
return ResponseEntity(
PostResponse(
posts = listOf(
Post(
id = 1,
imagePath = "",
title = "",
category = "",
prefecture = "",
description = "",
userId = "",
createdAt = ""
),
)
),
HttpStatus.OK
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
Expand Down Expand Up @@ -34,10 +36,58 @@ class ApplicationTest(@Autowired val mockMvc: MockMvc) {

/**
* then:
* - ステータスコードを比較
* - レスポンスボディを比較
*/
val expectedStatus = HttpStatus.OK.value()
val expectedResponseBody = "{}"
assertThat(actualStatus).isEqualTo(expectedStatus)
assertThat(actualResponseBody).isEqualTo(expectedResponseBody)
}

@Test
@DisplayName("GET /top のテスト")
fun allPostTest() {
/**
* given:
*/

/**
* when:
*/
val response = mockMvc.get("/top") {
contentType = MediaType.APPLICATION_JSON
}.andReturn().response
val actualStatus = response.status
val actualResponseBody = response.contentAsString

/**
* then:
* - ステータスコードを比較
* - レスポンスボディを比較
*/
val expectedStatus = HttpStatus.OK.value()
val expectedResponseBody = """
{
"posts":[
{
"id":1,
"imagePath":"",
"title":"",
"category":"",
"prefecture":"",
"description":"",
"userId":"",
"createdAt":""
}
],
}
""".trimIndent()
assertThat(actualStatus).isEqualTo(expectedStatus)
JSONAssert.assertEquals(
expectedResponseBody,
actualResponseBody,
JSONCompareMode.NON_EXTENSIBLE
)
}
}

0 comments on commit 9df6c5e

Please sign in to comment.