Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved tests using custom DSL
  • Loading branch information
ubiratansoares committed Jul 23, 2019
1 parent 67c6cdc commit b7963e1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 43 deletions.
2 changes: 2 additions & 0 deletions networking/build.gradle.kts
Expand Up @@ -29,4 +29,6 @@ dependencies {
unitTest {
forEachDependency { testImplementation(it) }
}

testImplementation(project(ModuleNames.CoroutinesTestUtils))
}
@@ -0,0 +1,30 @@
package io.dotanuki.norris.networking

import io.dotanuki.coroutines.testutils.unwrapError
import io.dotanuki.norris.domain.errors.ErrorTransformer
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

class CheckErrorTransformation(
private val original: Throwable,
private val transformer: ErrorTransformer
) {
private fun runCheck(check: (Throwable) -> Unit) =
runBlocking {
val result = runCatching { errorAtSuspendableOperation(original) }
val unwrapped = unwrapError(result)
val transformed = transformer.transform(unwrapped)
check(transformed)
}

private suspend fun errorAtSuspendableOperation(error: Throwable) =
suspendCoroutine<Unit> { continuation ->
continuation.resumeWithException(error)
}

companion object {
fun checkTransformation(from: Throwable, using: ErrorTransformer, check: (Throwable) -> Unit) =
CheckErrorTransformation(from, using).runCheck { check.invoke(it) }
}
}
Expand Up @@ -4,8 +4,10 @@ import io.dotanuki.burster.using
import io.dotanuki.norris.domain.errors.RemoteServiceIntegrationError
import io.dotanuki.norris.domain.errors.RemoteServiceIntegrationError.ClientOrigin
import io.dotanuki.norris.domain.errors.RemoteServiceIntegrationError.RemoteSystem
import io.dotanuki.norris.networking.CheckErrorTransformation.Companion.checkTransformation
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.ResponseBody.Companion.toResponseBody
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import retrofit2.HttpException
import retrofit2.Response
Expand All @@ -22,21 +24,22 @@ class HttpIntegrationErrorTransformerTests {
}

thenWith { status, message, expected ->
assertTransformed(
checkTransformation(
from = httpException<Any>(status, message),
to = expected,
using = HttpIntegrationErrorTransformer
using = HttpIntegrationErrorTransformer,
check = { transformed -> assertThat(transformed).isEqualTo(expected) }
)
}
}
}

@Test fun `should propagate any other error`() {
val otherError = IllegalStateException("Houston, we have a problem!")
assertTransformed(

checkTransformation(
from = otherError,
to = otherError,
using = HttpIntegrationErrorTransformer
using = HttpIntegrationErrorTransformer,
check = { transformed -> assertThat(transformed).isEqualTo(otherError) }
)
}

Expand Down
Expand Up @@ -6,6 +6,8 @@ import io.dotanuki.norris.domain.errors.NetworkingError.ConnectionSpike
import io.dotanuki.norris.domain.errors.NetworkingError.HostUnreachable
import io.dotanuki.norris.domain.errors.NetworkingError.OperationTimeout
import io.dotanuki.norris.domain.errors.RemoteServiceIntegrationError
import io.dotanuki.norris.networking.CheckErrorTransformation.Companion.checkTransformation
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.io.IOException
import java.net.ConnectException
Expand All @@ -27,10 +29,10 @@ class NetworkingErrorTransformerTests {
}

thenWith { incoming, expected ->
assertTransformed(
checkTransformation(
from = incoming,
to = expected,
using = NetworkingErrorTransformer
using = NetworkingErrorTransformer,
check = { transformed -> assertThat(transformed).isEqualTo(expected) }
)
}
}
Expand All @@ -39,10 +41,10 @@ class NetworkingErrorTransformerTests {
@Test fun `should propagate any other error`() {
val otherError = RemoteServiceIntegrationError.RemoteSystem

assertTransformed(
checkTransformation(
from = otherError,
to = otherError,
using = NetworkingErrorTransformer
using = NetworkingErrorTransformer,
check = { transformed -> assertThat(transformed).isEqualTo(otherError) }
)
}
}
@@ -1,26 +1,30 @@
package io.dotanuki.norris.networking

import io.dotanuki.norris.domain.errors.RemoteServiceIntegrationError
import io.dotanuki.norris.networking.CheckErrorTransformation.Companion.checkTransformation
import kotlinx.serialization.SerializationException
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class SerializationErrorTransformerTests {

@Test fun `should handle serialization errors`() {
val parseError = SerializationException("Found comments inside this JSON")
assertTransformed(
val expected = RemoteServiceIntegrationError.UnexpectedResponse

checkTransformation(
from = parseError,
to = RemoteServiceIntegrationError.UnexpectedResponse,
using = SerializationErrorTransformer
using = SerializationErrorTransformer,
check = { transformed -> assertThat(transformed).isEqualTo(expected) }
)
}

@Test fun `should not handle any other errors`() {
val toBePropagated = IllegalStateException("Something broke here ...")
assertTransformed(
checkTransformation(
from = toBePropagated,
to = toBePropagated,
using = SerializationErrorTransformer
using = SerializationErrorTransformer,
check = { transformed -> assertThat(transformed).isEqualTo(toBePropagated) }
)
}
}
25 changes: 0 additions & 25 deletions networking/src/test/java/io/dotanuki/norris/networking/util.kt

This file was deleted.

0 comments on commit b7963e1

Please sign in to comment.