1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
3 import com.fasterxml.jackson.databind.ObjectMapper
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.`is`
7 import java.time.Instant
8 import java.time.ZoneId
9 import java.time.ZonedDateTime
12 * Unit test for [WetterComState].
14 * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
16 class WetterComStateTest {
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))
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))
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>))
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)
56 val parsedState = objectMapper.readValue(json, WetterComState::class.java)
57 assertThat(parsedState, `is`(originalState))