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