1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
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`
9 import java.time.Instant
10 import java.time.ZoneId
11 import java.time.ZonedDateTime
14 * Unit test for [WetterComState].
16 * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
18 class WetterComStateTest {
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))
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))
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>))
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)
58 val parsedState = objectMapper.readValue(json, WetterComState::class.java)
59 assertThat(parsedState, `is`(originalState))