dd2a3c183910dfad4ea3bd4c44b8bb05557997c7
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / queries / FallbackQueryTest.kt
1 package net.pterodactylus.rhynodge.queries
2
3 import net.pterodactylus.rhynodge.Query
4 import net.pterodactylus.rhynodge.State
5 import net.pterodactylus.rhynodge.states.AbstractState
6 import net.pterodactylus.rhynodge.states.FailedState
7 import net.pterodactylus.rhynodge.states.StateManagerTest
8 import net.pterodactylus.rhynodge.states.StateManagerTest.TestState
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.`is`
11 import org.hamcrest.Matchers.sameInstance
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 org.mockito.Mockito.never
18 import org.mockito.Mockito.verify
19
20 /**
21  * Unit test for [FallbackQuery].
22  *
23  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
24  */
25 class FallbackQueryTest {
26
27         @JvmField @Rule val expectedException = ExpectedException.none()!!
28
29         private val firstQuery = mock(Query::class.java)
30         private val secondQuery = mock(Query::class.java)
31         private val thirdQuery = mock(Query::class.java)
32         private val query = FallbackQuery(firstQuery, secondQuery, thirdQuery)
33
34         private fun setupQueries(firstState: State = FailedState(), secondState: State = FailedState(), thirdState: State = FailedState()) {
35                 `when`(firstQuery.state()).thenReturn(firstState)
36                 `when`(secondQuery.state()).thenReturn(secondState)
37                 `when`(thirdQuery.state()).thenReturn(thirdState)
38         }
39
40         @Test
41         fun `fallback query returns state of third query`() {
42                 setupQueries(thirdState = successState)
43                 assertThat(query.state(), sameInstance<State>(successState))
44         }
45
46         @Test
47         fun `fallback query calls all three queries`() {
48                 setupQueries(thirdState = successState)
49                 query.state()
50                 verify(firstQuery).state()
51                 verify(secondQuery).state()
52                 verify(thirdQuery).state()
53         }
54
55         @Test
56         fun `fallback query returns second state`() {
57                 setupQueries(secondState = successState)
58                 assertThat(query.state(), sameInstance<State>(successState))
59         }
60
61         @Test
62         fun `fallback query does not query third query`() {
63                 setupQueries(secondState = successState)
64                 query.state()
65                 verify(firstQuery).state()
66                 verify(secondQuery).state()
67                 verify(thirdQuery, never()).state()
68         }
69
70         @Test
71         fun `fallback query returns first state`() {
72                 setupQueries(firstState = successState)
73                 assertThat(query.state(), sameInstance<State>(successState))
74         }
75
76         @Test
77         fun `fallback query does not query second and third query`() {
78                 setupQueries(firstState = successState)
79                 query.state()
80                 verify(firstQuery).state()
81                 verify(secondQuery, never()).state()
82                 verify(thirdQuery, never()).state()
83         }
84
85         @Test
86         fun `fallback query returns failed state if no query succeeds`() {
87                 setupQueries()
88                 assertThat(query.state().success(), `is`(false))
89         }
90
91         @Test
92         fun `fallback query can not be created without queries`() {
93                 expectedException.expect(IllegalArgumentException::class.java)
94                 FallbackQuery()
95         }
96
97 }
98
99 private val successState: AbstractState = TestState()