|
| 1 | +package io.customer.messagingpush |
| 2 | + |
| 3 | +import io.customer.commontest.config.TestConfig |
| 4 | +import io.customer.commontest.config.testConfigurationDefault |
| 5 | +import io.customer.messagingpush.network.HttpClient |
| 6 | +import io.customer.messagingpush.network.HttpRequestParams |
| 7 | +import io.customer.messagingpush.testutils.core.IntegrationTest |
| 8 | +import io.mockk.every |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.slot |
| 11 | +import io.mockk.verify |
| 12 | +import org.amshove.kluent.shouldBeEqualTo |
| 13 | +import org.amshove.kluent.shouldContain |
| 14 | +import org.amshove.kluent.shouldNotBe |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.robolectric.RobolectricTestRunner |
| 18 | + |
| 19 | +@RunWith(RobolectricTestRunner::class) |
| 20 | +class PushDeliveryTrackingTest : IntegrationTest() { |
| 21 | + |
| 22 | + private val httpClient: HttpClient = mockk(relaxed = true) |
| 23 | + private val pushDeliveryTracker = PushDeliveryTrackerImpl() |
| 24 | + |
| 25 | + override fun setup(testConfig: TestConfig) { |
| 26 | + super.setup( |
| 27 | + testConfigurationDefault { |
| 28 | + diGraph { |
| 29 | + sdk { |
| 30 | + // Override the httpClient in your DI so the SUT uses this mock. |
| 31 | + overrideDependency<HttpClient>(httpClient) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun trackMetric_givenValidInputs_expectCorrectPathAndSuccessCallback() { |
| 40 | + val token = "token123" |
| 41 | + val event = "OPENED" |
| 42 | + val deliveryId = "delivery_abc" |
| 43 | + |
| 44 | + val capturedParams = slot<HttpRequestParams>() |
| 45 | + |
| 46 | + every { |
| 47 | + httpClient.request(capture(capturedParams), any()) |
| 48 | + } answers { |
| 49 | + // Invoke the second arg (the callback) with success. |
| 50 | + secondArg<(Result<String>) -> Unit>().invoke(Result.success("Success")) |
| 51 | + } |
| 52 | + |
| 53 | + var callbackResult: Result<Unit>? = null |
| 54 | + |
| 55 | + pushDeliveryTracker.trackMetric(token, event, deliveryId) { result -> |
| 56 | + callbackResult = result |
| 57 | + // Return Unit to match the function signature |
| 58 | + Unit |
| 59 | + } |
| 60 | + |
| 61 | + // Assert #1: Confirm the correct path. |
| 62 | + capturedParams.captured.path shouldBeEqualTo "/track" |
| 63 | + |
| 64 | + // Assert #2: The body should not be null. |
| 65 | + val requestBody = capturedParams.captured.body |
| 66 | + requestBody shouldNotBe null |
| 67 | + |
| 68 | + // Optional substring checks to verify key fields exist (avoid org.json parsing): |
| 69 | + requestBody!! shouldContain token |
| 70 | + requestBody shouldContain event.lowercase() |
| 71 | + requestBody shouldContain deliveryId |
| 72 | + |
| 73 | + // Assert #3: The callback result is success. |
| 74 | + callbackResult!!.isSuccess.shouldBeEqualTo(true) |
| 75 | + |
| 76 | + // Ensure we only called once |
| 77 | + verify(exactly = 1) { httpClient.request(any(), any()) } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun trackMetric_givenHttpClientFails_expectCallbackFailure() { |
| 82 | + every { |
| 83 | + httpClient.request(any(), any()) |
| 84 | + } answers { |
| 85 | + // Simulate failure |
| 86 | + secondArg<(Result<String>) -> Unit>().invoke(Result.failure(Exception("Network error"))) |
| 87 | + } |
| 88 | + |
| 89 | + var callbackResult: Result<Unit>? = null |
| 90 | + |
| 91 | + pushDeliveryTracker.trackMetric("token", "OPENED", "deliveryId") { result -> |
| 92 | + callbackResult = result |
| 93 | + Unit |
| 94 | + } |
| 95 | + |
| 96 | + callbackResult!!.isFailure.shouldBeEqualTo(true) |
| 97 | + } |
| 98 | +} |
0 commit comments