🔀 Merge branch 'feature/add-jenkinsfile'
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / states / JsonStateTest.kt
1 package net.pterodactylus.rhynodge.states
2
3 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4 import com.spotify.hamcrest.jackson.JsonMatchers.jsonObject
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.junit.Test
7 import java.io.StringWriter
8
9 class JsonStateTest {
10
11         @Test
12         fun `json state can be serialized`() {
13                 val jsonState = JsonState(jsonObject)
14                 StringWriter().use { stringWriter ->
15                         objectMapper.writeValue(stringWriter, jsonState)
16                         val deserializedState = objectMapper.readValue(stringWriter.toString(), AbstractState::class.java) as JsonState
17                         assertThat(deserializedState.jsonNode, jsonObject(jsonObject))
18                 }
19         }
20
21         private val jsonObject = objectMapper.createObjectNode()
22                 .put("state", "foo")
23                 .also {
24                         it.putArray("list")
25                                 .add("value1")
26                                 .add("value2")
27                 }.also {
28                         it.putObject("object")
29                                 .put("foo", true)
30                                 .put("bar", "yes")
31                                 .put("baz", 1.5)
32                                 .putNull("qux")
33                 }
34
35 }
36
37 private val objectMapper = jacksonObjectMapper()