Use a single state and trigger for weather services
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / webpages / weather / WeatherTriggerTest.kt
1 package net.pterodactylus.rhynodge.webpages.weather
2
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
8 import org.junit.Test
9 import org.mockito.Mockito.mock
10 import java.io.File
11 import java.time.ZoneId.of
12 import java.time.ZonedDateTime
13
14 /**
15  * Unit test for [WeatherTrigger].
16
17  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
18  */
19 class WeatherTriggerTest {
20
21     private val now = ZonedDateTime.now(of("Europe/Berlin"))
22     private val previousState = WeatherState("Weather", now.minusDays(1))
23     private val trigger = WeatherTrigger()
24
25     @Test
26     fun currentStateIsAlwaysReturned() {
27         val currentState = WeatherState("Weather", now)
28         assertThat(trigger.mergeStates(previousState, currentState), `is`(currentState as State))
29     }
30
31     @Test
32     fun triggerDoesNotTriggerIfStateHasNotChanged() {
33         val currentState = WeatherState("Weather", now.minusDays(1))
34         trigger.mergeStates(previousState, currentState)
35         assertThat(trigger.triggers(), `is`(false))
36     }
37
38     @Test
39     fun triggerDoesTriggerIfStateHasChanged() {
40         val currentState = WeatherState("Weather", now)
41         trigger.mergeStates(previousState, currentState)
42         assertThat(trigger.triggers(), `is`(true))
43     }
44
45     @Test
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"))
52     }
53
54     @Test
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"))
86     }
87
88 }