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