🎨 Replace Sinfest filter with Kotlin version
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / webpages / weather / WeatherStateTest.kt
1 package net.pterodactylus.rhynodge.webpages.weather
2
3 import com.fasterxml.jackson.databind.ObjectMapper
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.`is`
6 import org.junit.Test
7 import java.time.Instant
8 import java.time.ZoneId
9 import java.time.ZonedDateTime
10
11 /**
12  * Unit test for [WeatherState].
13
14  * @author [David â€˜Bombe’ Roden](mailto:bombe@pterodactylus.net)
15  */
16 const val SERVICE_NAME = "Weather Service"
17
18 class WeatherStateTest {
19
20     private val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
21
22     @Test
23     fun stateRetainsServiceName() {
24         val state = WeatherState(SERVICE_NAME, now)
25         assertThat(state.service, `is`(SERVICE_NAME))
26     }
27
28     @Test
29     fun statesWithoutHoursEqualOneAnother() {
30         val now = now
31         val firstState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
32         val secondState = WeatherState(SERVICE_NAME, ZonedDateTime.from(now))
33         assertThat(firstState, `is`(secondState))
34     }
35
36     @Test
37     fun statesWithTheSameHoursAreEqual() {
38         val now = now
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))
46     }
47
48     @Test
49     fun iteratingDeliversHourStates() {
50         val now = now
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>))
55     }
56
57     @Test
58     fun stateIsSerializableAsJson() {
59         val objectMapper = ObjectMapper()
60         val now = now
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)
65         println(json)
66         val parsedState = objectMapper.readValue(json, WeatherState::class.java)
67         assertThat(parsedState, `is`(originalState))
68     }
69
70 }