1 package net.pterodactylus.rhynodge.webpages.weather
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 [WeatherState].
14 * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
16 const val SERVICE_NAME = "Weather Service"
18 class WeatherStateTest {
20 private val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
23 fun stateRetainsServiceName() {
24 val state = WeatherState(SERVICE_NAME, now)
25 assertThat(state.service, `is`(SERVICE_NAME))
29 fun statesWithoutHoursEqualOneAnother() {
31 val firstState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
32 val secondState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
33 assertThat(firstState, `is`(secondState))
37 fun statesWithTheSameHoursAreEqual() {
39 val firstState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
40 firstState += HourState(0, 10, null, 0.05, 0.0, WindDirection.NORTH, 5, null, null, "Fine", "http://1")
41 firstState += HourState(1, 12, null, 0.1, 2.0, WindDirection.WEST, 8, null, null, "Superb", "http://2")
42 val secondState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
43 secondState += HourState(0, 10, null, 0.05, 0.0, WindDirection.NORTH, 5, null, null, "Fine", "http://1")
44 secondState += HourState(1, 12, null, 0.1, 2.0, WindDirection.WEST, 8, null, null, "Superb", "http://2")
45 assertThat(firstState, `is`(secondState))
49 fun iteratingDeliversHourStates() {
51 val firstState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
52 firstState += HourState(0, 10, null, 0.05, 0.0, WindDirection.NORTH, 5, null, null, "Fine", "http://1")
53 firstState += HourState(1, 12, null, 0.1, 2.0, WindDirection.WEST, 8, null, null, "Superb", "http://2")
54 assertThat(firstState.iterator().asSequence().toList(), `is`(firstState.hours as Iterable<HourState>))
58 fun stateIsSerializableAsJson() {
59 val objectMapper = ObjectMapper()
61 val originalState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
62 originalState += HourState(0, 10, null, 0.05, 0.0, WindDirection.NORTH, 5, null, null, "Fine", "http://1")
63 originalState += HourState(1, 12, null, 0.1, 2.0, WindDirection.WEST, 8, null, null, "Superb", "http://2")
64 val json = objectMapper.writeValueAsString(originalState)
66 val parsedState = objectMapper.readValue(json, WeatherState::class.java)
67 assertThat(parsedState, `is`(originalState))