From aa7d1be53962f707e11992f32b248784e0242402 Mon Sep 17 00:00:00 2001 From: Wojtek Zieba Date: Fri, 27 Oct 2023 23:55:25 +0200 Subject: [PATCH] tests: assert the correct payload size of triggered HTTP request. --- .../parsely/parselyandroid/FunctionalTests.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/parsely/src/androidTest/java/com/parsely/parselyandroid/FunctionalTests.kt b/parsely/src/androidTest/java/com/parsely/parselyandroid/FunctionalTests.kt index 0545345e..52d592d2 100644 --- a/parsely/src/androidTest/java/com/parsely/parselyandroid/FunctionalTests.kt +++ b/parsely/src/androidTest/java/com/parsely/parselyandroid/FunctionalTests.kt @@ -3,6 +3,10 @@ package com.parsely.parselyandroid import android.app.Activity import androidx.test.core.app.ActivityScenario import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper import java.io.File import java.io.FileInputStream import java.io.ObjectInputStream @@ -16,6 +20,7 @@ import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.yield import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.fail import org.junit.Test @@ -57,7 +62,8 @@ class FunctionalTests { } // Waits for the SDK to send events (flush interval passes) - server.takeRequest() + val requestPayload = server.takeRequest().toMap() + assertThat(requestPayload["events"]).hasSize(51) runBlocking { withTimeoutOrNull(500.milliseconds) { @@ -69,10 +75,21 @@ class FunctionalTests { } } ?: fail("Local storage file is not empty!") } - } } + private fun RecordedRequest.toMap(): Map> { + val listType: TypeReference>> = + object : TypeReference>>() {} + + return ObjectMapper().readValue(body.readUtf8(), listType) + } + + @JsonIgnoreProperties(ignoreUnknown = true) + data class Event ( + @JsonProperty("idsite") var idsite: String, + ) + private val locallyStoredEvents get() = FileInputStream(File("$appsFiles/parsely-events.ser")).use { ObjectInputStream(it).use { objectInputStream ->