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