Use a single hour state for all weather services
[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.WindDirection
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.`is`
11 import org.hamcrest.Matchers.contains
12 import org.junit.Rule
13 import org.junit.Test
14 import org.junit.rules.ExpectedException
15 import org.mockito.Mockito.`when`
16 import org.mockito.Mockito.mock
17 import java.time.LocalDateTime
18 import java.time.Month
19 import java.time.ZoneId
20
21 /**
22  * Unit test for [WetterComFilter].
23  *
24  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
25  */
26 class WetterComFilterTest {
27
28     @Rule @JvmField val expectedException = ExpectedException.none()
29
30     private val url = "http://www.wetter.com/wetter_aktuell/wettervorhersage/heute/deutschland/hamburg/DE0004130.html"
31     private val filter = WetterComFilter()
32
33     @Test
34     fun filterReturnsFailedStateWhenGivenFailedState() {
35         val exception = mock(Exception::class.java)
36         val failedState = FailedState(exception)
37         val newState = filter.filter(failedState)
38         assertThat(newState.success(), `is`(false))
39         assertThat(newState.exception(), `is`(exception as Throwable))
40     }
41
42     @Test
43     fun filterThrowsWhenASuccessfulNonHtmlStateIsGiven() {
44         val state = mock(State::class.java)
45         `when`(state.success()).thenReturn(true)
46         expectedException.expect(IllegalArgumentException::class.java)
47         filter.filter(state)
48     }
49
50     @Test
51     fun filterParsesHtmlCorrectly() {
52         val document = loadDocument(javaClass, "wetter.com.html", url)
53         val htmlState = HtmlState(url, document)
54         val newState = filter.filter(htmlState) as WetterComState
55         assertThat(newState.dateTime, `is`(LocalDateTime.of(2016, Month.MAY, 23, 5, 0).atZone(ZoneId.of("Europe/Berlin"))))
56         assertThat(newState.hours, contains(
57                 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"),
58                 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"),
59                 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"),
60                 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"),
61                 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"),
62                 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"),
63                 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"),
64                 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"),
65                 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"),
66                 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"),
67                 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"),
68                 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"),
69                 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"),
70                 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"),
71                 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"),
72                 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"),
73                 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"),
74                 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"),
75                 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"),
76                 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"),
77                 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"),
78                 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"),
79                 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"),
80                 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")
81         ))
82     }
83
84 }