Skip to content

Commit

Permalink
refactor: introduce RestClient interface
Browse files Browse the repository at this point in the history
Makes creating fake objects less problematic by removing a need to call real object constructor.
  • Loading branch information
wzieba committed Nov 17, 2023
1 parent be74b28 commit a13485c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package com.parsely.parselyandroid

import java.net.HttpURLConnection
import java.net.URL
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

internal open class ParselyAPIConnection(private val url: String) {
open suspend fun send(payload: String): Result<Unit> {
internal interface RestClient {
suspend fun send(payload: String): Result<Unit>
}

internal class ParselyAPIConnection(private val url: String) : RestClient {
override suspend fun send(payload: String): Result<Unit> {
var connection: HttpURLConnection? = null
try {
connection = URL(url).openConnection() as HttpURLConnection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.sync.withLock
internal class SendEvents(
private val flushManager: FlushManager,
private val repository: QueueRepository,
private val parselyAPIConnection: ParselyAPIConnection,
private val restClient: RestClient,
private val scope: CoroutineScope
) {

Expand All @@ -35,7 +35,7 @@ internal class SendEvents(
repository.remove(eventsToSend)
} else {
ParselyTracker.PLog("Requested %s", ParselyTracker.ROOT_URL)
parselyAPIConnection.send(jsonPayload)
restClient.send(jsonPayload)
.fold(
onSuccess = {
ParselyTracker.PLog("Pixel request success")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SendEventsTest {
sut = SendEvents(
FakeFlushManager(),
FakeRepository(),
FakeParselyAPIConnection(),
FakeRestClient(),
this
)

Expand All @@ -40,7 +40,7 @@ class SendEventsTest {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
val parselyAPIConnection = FakeRestClient().apply {
nextResult = Result.success(Unit)
}
sut = SendEvents(
Expand Down Expand Up @@ -68,7 +68,7 @@ class SendEventsTest {
sut = SendEvents(
FakeFlushManager(),
repository,
FakeParselyAPIConnection(),
FakeRestClient(),
this
)

Expand All @@ -87,7 +87,7 @@ class SendEventsTest {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
val parselyAPIConnection = FakeRestClient().apply {
nextResult = Result.failure(Exception())
}
sut = SendEvents(
Expand All @@ -113,7 +113,7 @@ class SendEventsTest {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
val parselyAPIConnection = FakeRestClient().apply {
nextResult = Result.success(Unit)
}
sut = SendEvents(
Expand All @@ -139,7 +139,7 @@ class SendEventsTest {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
val parselyAPIConnection = FakeRestClient().apply {
nextResult = Result.failure(Exception())
}
sut = SendEvents(
Expand Down Expand Up @@ -167,7 +167,7 @@ class SendEventsTest {
return ArrayList(listOf(mapOf("test" to 123)))
}
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
val parselyAPIConnection = FakeRestClient().apply {
nextResult = Result.success(Unit)
}
sut = SendEvents(
Expand All @@ -192,7 +192,7 @@ class SendEventsTest {
sut = SendEvents(
flushManager,
FakeRepository(),
FakeParselyAPIConnection(),
FakeRestClient(),
this
)

Expand Down Expand Up @@ -236,7 +236,7 @@ class SendEventsTest {
}
}

private class FakeParselyAPIConnection : ParselyAPIConnection("") {
private class FakeRestClient : RestClient {

var nextResult: Result<Unit>? = null

Expand Down

0 comments on commit a13485c

Please sign in to comment.