@@ -5,39 +5,67 @@ package com.featurevisor.sdk
5
5
6
6
import com.featurevisor.types.DatafileContent
7
7
import io.kotest.matchers.shouldBe
8
+ import io.mockk.coEvery
9
+ import io.mockk.spyk
10
+ import io.mockk.verify
8
11
import org.junit.jupiter.api.Test
9
12
10
13
class InstanceTest {
11
-
14
+ private val datafileUrl = " https://www.testmock.com"
15
+ private val fetchHandler = object : (String ) -> Result <DatafileContent > {
16
+ override fun invoke (param : String ): Result <DatafileContent > = Result .failure(Throwable ())
17
+ }
18
+ private val mockDatafileFetchHandler: DatafileFetchHandler = spyk(fetchHandler)
19
+ private val datafileContent = DatafileContent (
20
+ schemaVersion = " 0" ,
21
+ revision = " 0" ,
22
+ attributes = listOf (),
23
+ segments = listOf (),
24
+ features = listOf ()
25
+ )
26
+ private var instanceOptions = InstanceOptions (
27
+ bucketKeySeparator = " " ,
28
+ configureBucketKey = null ,
29
+ configureBucketValue = null ,
30
+ datafile = datafileContent,
31
+ datafileUrl = null ,
32
+ handleDatafileFetch = null ,
33
+ initialFeatures = mapOf (),
34
+ interceptContext = null ,
35
+ logger = null ,
36
+ onActivation = {},
37
+ onReady = {},
38
+ onRefresh = {},
39
+ onUpdate = {},
40
+ refreshInterval = null ,
41
+ stickyFeatures = mapOf (),
42
+ onError = {},
43
+ )
12
44
private val systemUnderTest = FeaturevisorInstance .createInstance(
13
- options = InstanceOptions (
14
- bucketKeySeparator = " " ,
15
- configureBucketKey = null ,
16
- configureBucketValue = null ,
17
- datafile = DatafileContent (
18
- schemaVersion = " 0" ,
19
- revision = " 0" ,
20
- attributes = listOf (),
21
- segments = listOf (),
22
- features = listOf ()
23
- ),
24
- datafileUrl = null ,
25
- handleDatafileFetch = null ,
26
- initialFeatures = mapOf (),
27
- interceptContext = null ,
28
- logger = null ,
29
- onActivation = {},
30
- onReady = {},
31
- onRefresh = {},
32
- onUpdate = {},
33
- refreshInterval = null ,
34
- stickyFeatures = mapOf (),
35
- onError = {},
36
- )
45
+ options = instanceOptions
37
46
)
38
47
39
48
@Test
40
49
fun `instance initialised properly` () {
41
50
systemUnderTest.statuses.ready shouldBe true
42
51
}
52
+
53
+ @Test
54
+ fun `instance fetches data using handleDatafileFetch` () {
55
+ coEvery { mockDatafileFetchHandler(datafileUrl) } returns Result .success(datafileContent)
56
+ instanceOptions = instanceOptions.copy(
57
+ datafileUrl = datafileUrl,
58
+ datafile = null ,
59
+ handleDatafileFetch = mockDatafileFetchHandler,
60
+ )
61
+
62
+ FeaturevisorInstance .createInstance(
63
+ options = instanceOptions
64
+ )
65
+
66
+ verify(exactly = 1 ) {
67
+ mockDatafileFetchHandler(datafileUrl)
68
+ }
69
+ systemUnderTest.statuses.ready shouldBe true
70
+ }
43
71
}
0 commit comments