Skip to content

Commit 58e1b8a

Browse files
authored
Use datafile handler while featurevisor initialisation (#24)
1 parent 42740b8 commit 58e1b8a

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

src/main/kotlin/com/featurevisor/sdk/Instance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
100100

101101
datafileUrl != null -> {
102102
datafileReader = DatafileReader(options.datafile?: emptyDatafile)
103-
fetchDatafileContent(datafileUrl) { result ->
103+
fetchDatafileContent(datafileUrl, handleDatafileFetch) { result ->
104104
if (result.isSuccess) {
105105
datafileReader = DatafileReader(result.getOrThrow())
106106
statuses.ready = true

src/test/kotlin/com/featurevisor/sdk/InstanceTest.kt

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,67 @@ package com.featurevisor.sdk
55

66
import com.featurevisor.types.DatafileContent
77
import io.kotest.matchers.shouldBe
8+
import io.mockk.coEvery
9+
import io.mockk.spyk
10+
import io.mockk.verify
811
import org.junit.jupiter.api.Test
912

1013
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+
)
1244
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
3746
)
3847

3948
@Test
4049
fun `instance initialised properly`() {
4150
systemUnderTest.statuses.ready shouldBe true
4251
}
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+
}
4371
}

0 commit comments

Comments
 (0)