11package com.github.hannotify.classyfire.ui.statemachine
22
33import com.github.hannotify.classyfire.data.category.CategoryRepository
4- import com.github.hannotify.classyfire.data.classification.ClassificationRepository
54import com.github.hannotify.classyfire.data.transaction.TransactionRepository
65import com.github.hannotify.classyfire.process.ClassificationService
6+ import com.github.hannotify.classyfire.testdata.Categories
77import com.github.hannotify.classyfire.ui.statemachine.states.*
8+ import io.mockk.every
9+ import io.mockk.mockkStatic
810import org.assertj.core.api.Assertions.*
11+ import org.junit.jupiter.api.AfterEach
912import org.junit.jupiter.api.BeforeEach
13+ import org.junit.jupiter.api.Disabled
1014import org.junit.jupiter.api.Test
15+ import org.junit.jupiter.params.ParameterizedTest
16+ import org.junit.jupiter.params.provider.CsvSource
17+ import java.io.File
1118import java.nio.file.Path
1219
1320internal class StateContextTest {
21+ private val categoryRepository = CategoryRepository (Path .of(" src/test/resources/categories/categories.txt" ))
1422 lateinit var stateContext: StateContext
1523
1624 @BeforeEach
1725 internal fun setup () {
26+ categoryRepository.saveAll(Categories .allCategories)
27+ categoryRepository.persist()
28+
1829 stateContext = StateContext (
1930 CategoryRepository (Path .of(" src/test/resources/categories/categories.txt" )),
2031 TransactionRepository (Path .of(" src/test/resources/transactions/test.csv" )),
2132 ClassificationService (Path .of(" src/test/resources/classifications/test-output.txt" )))
2233 }
2334
24- @Test
25- internal fun initialState_shouldBeRetrieveCategoriesState () {
26- assertThat(stateContext.state).isInstanceOf(RetrieveCategoriesState ::class .java)
27- }
35+ @ParameterizedTest(name = " State {0} should be {1}." )
36+ @CsvSource(textBlock = """
37+ 0, RetrieveCategoriesState,
38+ 1, RetrieveTransactionsState,
39+ 2, ProcessTrainingDataState
40+ 3, ProcessIncomeTransactionsState,
41+ 4, ProcessTrainingDataState
42+ 5, ProcessExpensesTransactionsState,
43+ 6, PersistClassificationsState""" )
44+ internal fun assertStateOrder (stateNumber : Int , expectedStateName : String ) {
45+ mockkStatic(::readLine)
46+ every { readLine() } returns " 0"
2847
29- @Test
30- internal fun secondState_shouldBeRetrieveTransactionsState () {
31- stateContext.nextState()
32- assertThat(stateContext.state).isInstanceOf(RetrieveTransactionsState ::class .java)
33- }
48+ repeat(stateNumber) { stateContext.nextState() }
3449
35- @Test
36- internal fun thirdState_shouldBeProcessIncomeTransactionsState () {
37- stateContext.nextState()
38- stateContext.nextState()
39- assertThat(stateContext.state).isInstanceOf(ProcessIncomeTransactionsState ::class .java)
50+ assertThat(stateContext.state?.javaClass?.simpleName).isEqualTo(expectedStateName)
4051 }
4152
4253 @Test
43- internal fun fourthState_shouldBeProcessExpensesTransactionsState () {
44- stateContext.nextState()
45- stateContext.nextState()
46- stateContext.nextState()
47- assertThat(stateContext.state).isInstanceOf(ProcessExpensesTransactionsState ::class .java)
48- }
54+ internal fun thereShouldBeNoSeventhState () {
55+ mockkStatic(::readLine)
56+ every { readLine() } returns " 0"
4957
50- @Test
51- internal fun fifthState_shouldBeProcessExpensesTransactionsState () {
52- stateContext.nextState()
53- stateContext.nextState()
54- stateContext.nextState()
55- stateContext.nextState()
56- assertThat(stateContext.state).isInstanceOf(ProcessExpensesTransactionsState ::class .java)
57- }
58+ repeat(7 ) { stateContext.nextState() }
5859
59- @Test
60- internal fun sixthState_shouldBePersistClassificationsState () {
61- stateContext.nextState()
62- stateContext.nextState()
63- stateContext.nextState()
64- stateContext.nextState()
65- stateContext.nextState()
66- assertThat(stateContext.state).isInstanceOf(PersistClassificationsState ::class .java)
60+ assertThat(stateContext.state).isNull()
6761 }
6862
69- @Test
70- internal fun thereShouldBeNoSeventhState () {
71- stateContext.nextState()
72- stateContext.nextState()
73- stateContext.nextState()
74- stateContext.nextState()
75- stateContext.nextState()
76- stateContext.nextState()
77- assertThat(stateContext.state).isNull()
63+ @AfterEach
64+ internal fun tearDown () {
65+ File (categoryRepository.storageLocation().toString()).delete()
7866 }
7967}
0 commit comments