Skip to content

Commit

Permalink
feat(board): 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hwanvely committed Aug 13, 2024
1 parent ec8c42f commit fbd1563
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import javax.persistence.Converter
@Converter(autoApply = true)
class BoardConverter : AttributeConverter<Board, String> {

companion object {
private val OBJECT_MAPPER = ObjectMapper().apply {
setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
}
}

override fun convertToDatabaseColumn(board: Board?): String {
override fun convertToDatabaseColumn(board: Board): String {
return try {
OBJECT_MAPPER.writeValueAsString(board)
} catch (e: JsonProcessingException) {
throw RuntimeException(e)
}
}

override fun convertToEntityAttribute(dbJson: String?): Board? {
override fun convertToEntityAttribute(dbJson: String): Board {
return try {
OBJECT_MAPPER.readValue(dbJson, Board::class.java)
} catch (e: JsonProcessingException) {
throw RuntimeException(e)
}
}

companion object {
private val OBJECT_MAPPER = ObjectMapper().apply {
setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.snackgame.server.game.snackgame.domain


import com.snackgame.server.game.snackgame.fixture.TestFixture
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class BoardConverterTest() {

private val converter = BoardConverter()

@Test
fun `게임판을 Json으로 저장할 수 있다`() {
val board = TestFixture.TWO_BY_FOUR()

val converted = converter.convertToDatabaseColumn(board)

assertThat(converted).isEqualTo(TestFixture.TWO_BY_FOUR_AS_JSON)
}

@Test
fun `Json을 스낵으로 변환할 수 있다`() {
val board = TestFixture.TWO_BY_FOUR()

var deserialized = converter.convertToEntityAttribute(TestFixture.TWO_BY_FOUR_AS_JSON)

assertThat(deserialized).usingRecursiveComparison().isEqualTo(board)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.snackgame.server.game.snackgame.snack.PlainSnack
object TestFixture {

const val TWO_BY_FOUR_AS_JSON =
"{\"apples\":[[{\"number\":1,\"isGolden\":false},{\"number\":1,\"isGolden\":false},{\"number\":0,\"isGolden\":false},{\"number\":5,\"isGolden\":false}],[{\"number\":9,\"isGolden\":false},{\"number\":2,\"isGolden\":false},{\"number\":0,\"isGolden\":false},{\"number\":2,\"isGolden\":false}]]}"
const val TWO_BY_TWO_WITH_GOLDEN_APPLE_AS_JSON =
"{\"apples\":[[{\"number\":1,\"isGolden\":false},{\"number\":8,\"isGolden\":false}],[{\"number\":9,\"isGolden\":true},{\"number\":2,\"isGolden\":false}]]}"
"{\"snacks\":[[{\"number\":1,\"isGolden\":false},{\"number\":1,\"isGolden\":false},{\"number\":0,\"isGolden\":false},{\"number\":5,\"isGolden\":false}],[{\"number\":9,\"isGolden\":false},{\"number\":2,\"isGolden\":false},{\"number\":0,\"isGolden\":false},{\"number\":2,\"isGolden\":false}]]}"
const val TWO_BY_TWO_WITH_GOLDEN_SNACK_AS_JSON =
"{\"snacks\":[[{\"number\":1,\"isGolden\":false},{\"number\":8,\"isGolden\":false}],[{\"number\":9,\"isGolden\":true},{\"number\":2,\"isGolden\":false}]]}"

/**
* <p>[1, 1, _, 5]</p>
Expand Down

0 comments on commit fbd1563

Please sign in to comment.