Skip to content

Commit

Permalink
tests: assert the correct payload size
Browse files Browse the repository at this point in the history
of triggered HTTP request.
  • Loading branch information
wzieba committed Oct 27, 2023
1 parent 8d4a0d3 commit aa7d1be
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -69,10 +75,21 @@ class FunctionalTests {
}
} ?: fail("Local storage file is not empty!")
}

}
}

private fun RecordedRequest.toMap(): Map<String, List<Event>> {
val listType: TypeReference<Map<String, List<Event>>> =
object : TypeReference<Map<String, List<Event>>>() {}

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 ->
Expand Down

0 comments on commit aa7d1be

Please sign in to comment.