Make wetter.com state JSON-serializable
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / webpages / weather / wettercom / WetterComStateTest.kt
1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
2
3 import com.fasterxml.jackson.databind.ObjectMapper
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.`is`
6 import org.junit.Test
7 import java.time.Instant
8 import java.time.ZoneId
9 import java.time.ZonedDateTime
10
11 /**
12  * Unit test for [WetterComState].
13
14  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
15  */
16 class WetterComStateTest {
17
18     @Test
19     fun statesWithoutHoursEqualOneAnother() {
20         val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
21         val firstState = WetterComState(ZonedDateTime.from(now))
22         val secondState = WetterComState(ZonedDateTime.from(now))
23         assertThat(firstState, `is`(secondState))
24     }
25
26     @Test
27     fun statesWithTheSameHoursAreEqual() {
28         val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
29         val firstState = WetterComState(ZonedDateTime.from(now))
30         firstState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1"))
31         firstState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2"))
32         val secondState = WetterComState(ZonedDateTime.from(now))
33         secondState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1"))
34         secondState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2"))
35         assertThat(firstState, `is`(secondState))
36     }
37
38     @Test
39     fun iteratingDeliversHourStates() {
40         val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
41         val firstState = WetterComState(ZonedDateTime.from(now))
42         firstState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1"))
43         firstState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2"))
44         assertThat(firstState.iterator().asSequence().toList(), `is`(firstState.hours as Iterable<HourState>))
45     }
46
47     @Test
48     fun stateIsSerializableAsJson() {
49         val objectMapper = ObjectMapper()
50         val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
51         val originalState = WetterComState(ZonedDateTime.from(now))
52         originalState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1"))
53         originalState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2"))
54         val json = objectMapper.writeValueAsString(originalState)
55         println(json)
56         val parsedState = objectMapper.readValue(json, WetterComState::class.java)
57         assertThat(parsedState, `is`(originalState))
58     }
59
60 }