Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Improved tests using custom DSL
- Loading branch information
1 parent
67c6cdc
commit b7963e1
Showing
6 changed files
with
59 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
networking/src/test/java/io/dotanuki/norris/networking/CheckErrorTransformation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 10 additions & 6 deletions
16
networking/src/test/java/io/dotanuki/norris/networking/SerializationErrorTransformerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
25
networking/src/test/java/io/dotanuki/norris/networking/util.kt
This file was deleted.
Oops, something went wrong.