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