1 package net.pterodactylus.rhynodge.queries
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
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
21 * Unit test for [FallbackQuery].
23 * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
25 class FallbackQueryTest {
27 @JvmField @Rule val expectedException = ExpectedException.none()!!
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)
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)
41 fun `fallback query returns state of third query`() {
42 setupQueries(thirdState = successState)
43 assertThat(query.state(), sameInstance<State>(successState))
47 fun `fallback query calls all three queries`() {
48 setupQueries(thirdState = successState)
50 verify(firstQuery).state()
51 verify(secondQuery).state()
52 verify(thirdQuery).state()
56 fun `fallback query returns second state`() {
57 setupQueries(secondState = successState)
58 assertThat(query.state(), sameInstance<State>(successState))
62 fun `fallback query does not query third query`() {
63 setupQueries(secondState = successState)
65 verify(firstQuery).state()
66 verify(secondQuery).state()
67 verify(thirdQuery, never()).state()
71 fun `fallback query returns first state`() {
72 setupQueries(firstState = successState)
73 assertThat(query.state(), sameInstance<State>(successState))
77 fun `fallback query does not query second and third query`() {
78 setupQueries(firstState = successState)
80 verify(firstQuery).state()
81 verify(secondQuery, never()).state()
82 verify(thirdQuery, never()).state()
86 fun `fallback query returns failed state if no query succeeds`() {
88 assertThat(query.state().success(), `is`(false))
92 fun `fallback query can not be created without queries`() {
93 expectedException.expect(IllegalArgumentException::class.java)
99 private val successState: AbstractState = TestState()