2205dbbbcb939b4d88ed6c453948beb72103044a
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / webpages / weather / wettercom / WetterComTriggerTest.kt
1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
2
3 import net.pterodactylus.rhynodge.Reaction
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.`is`
6 import org.hamcrest.Matchers.containsString
7 import org.junit.Test
8 import org.mockito.Mockito
9 import java.time.ZoneId
10 import java.time.ZonedDateTime
11
12 /**
13  * Unit test for [WetterComTrigger].
14
15  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
16  */
17 class WetterComTriggerTest {
18
19     private val trigger = WetterComTrigger()
20     private val now = ZonedDateTime.now()
21     private val previousState = WetterComState(now)
22
23     @Test
24     fun equalStatesAreNotMerged() {
25         val currentState = WetterComState(now)
26         val newState = trigger.mergeStates(previousState, currentState) as WetterComState
27         assertThat(newState, `is`(previousState))
28         assertThat(newState, `is`(currentState))
29     }
30
31     @Test
32     fun currentStateIsReturnedIfDifferentFromPreviousState() {
33         val currentState = WetterComState(now.minusDays(1))
34         val newState = trigger.mergeStates(previousState, currentState) as WetterComState
35         assertThat(newState, `is`(currentState))
36     }
37
38     @Test
39     fun mergingEqualStatesDoesNotTrigger() {
40         val currentState = WetterComState(now)
41         trigger.mergeStates(previousState, currentState) as WetterComState
42         assertThat(trigger.triggers(), `is`(false))
43     }
44
45     @Test
46     fun mergingDifferentStatesDoesTrigger() {
47         val currentState = WetterComState(now.minusDays(1))
48         trigger.mergeStates(previousState, currentState) as WetterComState
49         assertThat(trigger.triggers(), `is`(true))
50     }
51
52     @Test
53     fun outputSummaryContainsCorrectDate() {
54         val currentState = WetterComState(ZonedDateTime.of(2000, 1, 1, 5, 0, 0, 0, ZoneId.of("Europe/Berlin")))
55         trigger.mergeStates(previousState, currentState) as WetterComState
56         val reaction = Mockito.mock(Reaction::class.java)
57         val output = trigger.output(reaction)
58         assertThat(output.summary(), `is`("The weather (according to wetter.com) on January 1, 2000"))
59     }
60
61     @Test
62     fun outputHtmlContainsHourStates() {
63         val currentState = WetterComState(now.minusDays(1))
64         currentState.addHour(HourState(0, 10.0, 0.11, 12.0, WindDirection.NORTH, 13.0, "Rain", "http://1"))
65         currentState.addHour(HourState(1, 14.0, 0.15, 16.0, WindDirection.SOUTH, 17.0, "Sun", "http://2"))
66         trigger.mergeStates(previousState, currentState) as WetterComState
67         val reaction = Mockito.mock(Reaction::class.java)
68         val output = trigger.output(reaction)
69         val htmlText = output.text("text/html", -1)
70         assertThat(htmlText, containsString("10 °C"))
71         assertThat(htmlText, containsString("11%"))
72         assertThat(htmlText, containsString("12 l/m"))
73         assertThat(htmlText, containsString("13 km/h"))
74         assertThat(htmlText, containsString("Rain"))
75         assertThat(htmlText, containsString("http://1"))
76         assertThat(htmlText, containsString("14 °C"))
77         assertThat(htmlText, containsString("15%"))
78         assertThat(htmlText, containsString("16 l/m"))
79         assertThat(htmlText, containsString("17 km/h"))
80         assertThat(htmlText, containsString("Sun"))
81         assertThat(htmlText, containsString("http://2"))
82     }
83
84 }