1 package net.pterodactylus.rhynodge.webpages.weather
3 import net.pterodactylus.rhynodge.Reaction
4 import net.pterodactylus.rhynodge.State
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.hamcrest.Matchers.`is`
7 import org.hamcrest.Matchers.containsString
9 import org.mockito.Mockito.mock
11 import java.time.ZoneId.of
12 import java.time.ZonedDateTime
15 * Unit test for [WeatherTrigger].
17 * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
19 class WeatherTriggerTest {
21 private val now = ZonedDateTime.now(of("Europe/Berlin"))
22 private val previousState = WeatherState("Weather", now.minusDays(1))
23 private val trigger = WeatherTrigger()
26 fun currentStateIsAlwaysReturned() {
27 val currentState = WeatherState("Weather", now)
28 assertThat(trigger.mergeStates(previousState, currentState), `is`(currentState as State))
32 fun triggerDoesNotTriggerIfStateHasNotChanged() {
33 val currentState = WeatherState("Weather", now.minusDays(1))
34 trigger.mergeStates(previousState, currentState)
35 assertThat(trigger.triggers(), `is`(false))
39 fun triggerDoesTriggerIfStateHasChanged() {
40 val currentState = WeatherState("Weather", now)
41 trigger.mergeStates(previousState, currentState)
42 assertThat(trigger.triggers(), `is`(true))
46 fun outputContainsCorrectSummary() {
47 val currentState = WeatherState("Weather", ZonedDateTime.of(2016, 5, 28, 0, 0, 0, 0, of("Europe/Berlin")))
48 trigger.mergeStates(previousState, currentState)
49 val reaction = mock(Reaction::class.java)
50 val output = trigger.output(reaction)
51 assertThat(output.summary(), `is`("The Weather (according to Weather) on May 28, 2016"))
55 fun outputContainsCorrectHourData() {
56 val currentState = WeatherState("Weather", ZonedDateTime.of(2016, 5, 28, 0, 0, 0, 0, of("Europe/Berlin")))
57 currentState += HourState(0, 10, 11, 0.12, 13.0, WindDirection.SOUTHSOUTHEAST, 14, 15, 0.16, "17", "http://18")
58 currentState += HourState(1, 20, 21, 0.22, 23.0, WindDirection.NORTHNORTHWEST, 24, 25, 0.26, "27", "http://28")
59 trigger.mergeStates(previousState, currentState)
60 val reaction = mock(Reaction::class.java)
61 val output = trigger.output(reaction)
62 val htmlText = output.text("text/html", -1)
63 File("/tmp/wetter.html").writer().use { it.write(htmlText) }
64 assertThat(htmlText, containsString("00:00"))
65 assertThat(htmlText, containsString("10 °C"))
66 assertThat(htmlText, containsString("(11 °C)"))
67 assertThat(htmlText, containsString("12%"))
68 assertThat(htmlText, containsString("13 l/m²"))
69 assertThat(htmlText, containsString("↖↑"))
70 assertThat(htmlText, containsString("14 km/h"))
71 assertThat(htmlText, containsString("15 km/h"))
72 assertThat(htmlText, containsString("16%"))
73 assertThat(htmlText, containsString("17"))
74 assertThat(htmlText, containsString("http://18"))
75 assertThat(htmlText, containsString("01:00"))
76 assertThat(htmlText, containsString("20 °C"))
77 assertThat(htmlText, containsString("(21 °C)"))
78 assertThat(htmlText, containsString("22%"))
79 assertThat(htmlText, containsString("23 l/m²"))
80 assertThat(htmlText, containsString("↘↓"))
81 assertThat(htmlText, containsString("24 km/h"))
82 assertThat(htmlText, containsString("25 km/h"))
83 assertThat(htmlText, containsString("26%"))
84 assertThat(htmlText, containsString("27"))
85 assertThat(htmlText, containsString("http://28"))