c58656f145013e28d01fc3d68ad063358b08db5d
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / webpages / weather / wettercom / WetterComFilterTest.kt
1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
2
3 import net.pterodactylus.rhynodge.State
4 import net.pterodactylus.rhynodge.filters.ResourceLoader.loadDocument
5 import net.pterodactylus.rhynodge.states.FailedState
6 import net.pterodactylus.rhynodge.states.HtmlState
7 import net.pterodactylus.rhynodge.webpages.weather.HourState
8 import net.pterodactylus.rhynodge.webpages.weather.WeatherState
9 import net.pterodactylus.rhynodge.webpages.weather.WindDirection
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.`is`
12 import org.hamcrest.Matchers.contains
13 import org.junit.Rule
14 import org.junit.Test
15 import org.junit.rules.ExpectedException
16 import org.mockito.Mockito.`when`
17 import org.mockito.Mockito.mock
18 import java.time.LocalDateTime
19 import java.time.Month
20 import java.time.ZoneId
21
22 /**
23  * Unit test for [WetterComFilter].
24  *
25  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
26  */
27 class WetterComFilterTest {
28
29     @Rule @JvmField val expectedException = ExpectedException.none()
30
31     private val url = "http://www.wetter.com/wetter_aktuell/wettervorhersage/heute/deutschland/hamburg/DE0004130.html"
32     private val filter = WetterComFilter()
33
34     @Test
35     fun filterReturnsFailedStateWhenGivenFailedState() {
36         val exception = mock(Exception::class.java)
37         val failedState = FailedState(exception)
38         val newState = filter.filter(failedState)
39         assertThat(newState.success(), `is`(false))
40         assertThat(newState.exception(), `is`(exception as Throwable))
41     }
42
43     @Test
44     fun filterThrowsWhenASuccessfulNonHtmlStateIsGiven() {
45         val state = mock(State::class.java)
46         `when`(state.success()).thenReturn(true)
47         expectedException.expect(IllegalArgumentException::class.java)
48         filter.filter(state)
49     }
50
51     @Test
52     fun filterParsesHtmlCorrectly() {
53         val document = loadDocument(javaClass, "wetter.com.html", url)
54         val htmlState = HtmlState(url, document)
55         val newState = filter.filter(htmlState) as WeatherState
56         assertThat(newState.dateTime, `is`(LocalDateTime.of(2016, Month.MAY, 23, 5, 0).atZone(ZoneId.of("Europe/Berlin"))))
57         assertThat(newState.hours, contains(
58                 HourState(0, 15, null, 0.65, 0.8, WindDirection.NORTH, 5, null, null, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"),
59                 HourState(1, 15, null, 0.7, 0.9, WindDirection.NONE, 5, null, null, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"),
60                 HourState(2, 17, null, 0.75, 1.0, WindDirection.NORTHWEST, 5, null, null, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"),
61                 HourState(3, 17, null, 0.85, 0.3, WindDirection.NORTHWEST, 5, null, null, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"),
62                 HourState(4, 19, null, 0.9, 0.3, WindDirection.SOUTHWEST, 5, null, null, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"),
63                 HourState(5, 20, null, 0.85, 0.3, WindDirection.SOUTHWEST, 7, null, null, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"),
64                 HourState(6, 20, null, 0.75, 0.3, WindDirection.SOUTHWEST, 11, null, null, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"),
65                 HourState(7, 20, null, 0.70, 0.3, WindDirection.WEST, 11, null, null, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"),
66                 HourState(8, 20, null, 0.70, 0.2, WindDirection.WEST, 9, null, null, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"),
67                 HourState(9, 20, null, 0.70, 0.4, WindDirection.WEST, 5, null, null, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"),
68                 HourState(10, 20, null, 0.70, 0.4, WindDirection.WEST, 7, null, null, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"),
69                 HourState(11, 19, null, 0.70, 0.4, WindDirection.WEST, 11, null, null, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"),
70                 HourState(12, 18, null, 0.70, 0.4, WindDirection.NORTHWEST, 12, null, null, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"),
71                 HourState(13, 17, null, 0.70, 0.4, WindDirection.NORTHWEST, 11, null, null, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"),
72                 HourState(14, 16, null, 0.75, 0.4, WindDirection.NORTHWEST, 12, null, null, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"),
73                 HourState(15, 15, null, 0.85, 0.2, WindDirection.WEST, 12, null, null, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"),
74                 HourState(16, 14, null, 0.9, 0.2, WindDirection.WEST, 14, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
75                 HourState(17, 14, null, 0.9, 0.0, WindDirection.NORTHWEST, 12, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
76                 HourState(18, 14, null, 0.9, 0.2, WindDirection.WEST, 12, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
77                 HourState(19, 13, null, 0.85, 0.1, WindDirection.NORTHWEST, 11, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
78                 HourState(20, 13, null, 0.8, 0.01, WindDirection.WEST, 11, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
79                 HourState(21, 12, null, 0.75, 0.2, WindDirection.WEST, 12, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
80                 HourState(22, 12, null, 0.75, 0.2, WindDirection.NORTHWEST, 12, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"),
81                 HourState(23, 11, null, 0.75, 0.2, WindDirection.NORTHWEST, 12, null, null, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518")
82         ))
83     }
84
85 }