|
| 1 | +package io.customer.datapipelines.plugins |
| 2 | + |
| 3 | +import com.segment.analytics.kotlin.core.HTTPClient |
| 4 | +import com.segment.analytics.kotlin.core.emptyJsonObject |
| 5 | +import io.customer.commontest.config.TestConfig |
| 6 | +import io.customer.datapipelines.testutils.core.JUnitTest |
| 7 | +import io.customer.datapipelines.testutils.core.testConfiguration |
| 8 | +import io.customer.datapipelines.testutils.utils.OutputReaderPlugin |
| 9 | +import io.customer.datapipelines.testutils.utils.trackEvents |
| 10 | +import io.mockk.every |
| 11 | +import io.mockk.mockkConstructor |
| 12 | +import okio.IOException |
| 13 | +import org.amshove.kluent.shouldBe |
| 14 | +import org.amshove.kluent.shouldNotBe |
| 15 | +import org.junit.jupiter.api.Test |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for CustomerIODestination plugin, focusing on behavior when network settings fetch fails. |
| 19 | + * These tests verify that even when the settings fetch fails, the plugin remains enabled |
| 20 | + * and can process events. |
| 21 | + */ |
| 22 | +class CustomerIODestinationTest : JUnitTest() { |
| 23 | + |
| 24 | + private lateinit var outputReaderPlugin: OutputReaderPlugin |
| 25 | + |
| 26 | + override fun setup(testConfig: TestConfig) { |
| 27 | + // Mock HTTP client to throw exception when fetching settings |
| 28 | + mockkConstructor(HTTPClient::class) |
| 29 | + every { anyConstructed<HTTPClient>().settings(any()) } throws IOException("Network error") |
| 30 | + // every { anyConstructed<HTTPClient>().settings("cdp.customer.io/v1") } throws IOException("Network error") |
| 31 | + // every { anyConstructed<HTTPClient>().settings("cdp-eu.customer.io/v1") } throws IOException("Network error") |
| 32 | + |
| 33 | + // Initialize test |
| 34 | + super.setup( |
| 35 | + testConfiguration { |
| 36 | + sdkConfig { |
| 37 | + // Explicitly enable CustomerIODestination plugin |
| 38 | + autoAddCustomerIODestination(true) |
| 39 | + } |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + // Add output reader plugin to capture events |
| 44 | + outputReaderPlugin = OutputReaderPlugin() |
| 45 | + analytics.add(outputReaderPlugin) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `CustomerIODestination plugin should be enabled by default even when fetching settings fails`() { |
| 50 | + // Verify plugin is present in analytics instance |
| 51 | + sdkInstance.analytics.find(CustomerIODestination::class) shouldNotBe null |
| 52 | + |
| 53 | + // Track an event to verify it flows through the pipeline |
| 54 | + sdkInstance.track("test_event") |
| 55 | + |
| 56 | + // Verify event was processed |
| 57 | + outputReaderPlugin.trackEvents.size shouldBe 1 |
| 58 | + outputReaderPlugin.trackEvents.first().event shouldBe "test_event" |
| 59 | + outputReaderPlugin.trackEvents.first().properties shouldBe emptyJsonObject |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `Default settings in analytics configuration should include CustomerIO destination`() { |
| 64 | + // Verify settings contain CustomerIO destination even with network failure |
| 65 | + val configuration = analytics.configuration |
| 66 | + configuration.defaultSettings shouldNotBe null |
| 67 | + val integrations = configuration.defaultSettings?.integrations |
| 68 | + integrations shouldNotBe null |
| 69 | + (integrations?.contains(CUSTOMER_IO_DATA_PIPELINES) ?: false) shouldBe true |
| 70 | + } |
| 71 | +} |
0 commit comments