Skip to content

Commit

Permalink
Update ArrowResponseEAdapterTest.kt (#3264)
Browse files Browse the repository at this point in the history
* Update ArrowResponseEAdapterTest.kt

* Update ArrowResponseEAdapterTest.kt

---------

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>
Co-authored-by: Alejandro Serrano <trupill@gmail.com>
  • Loading branch information
3 people authored Nov 8, 2023
1 parent ec6ff88 commit 99a8e49
Showing 1 changed file with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import arrow.core.right
import arrow.retrofit.adapter.mock.ErrorMock
import arrow.retrofit.adapter.mock.ResponseMock
import arrow.retrofit.adapter.retrofit.SuspendApiTestClient
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlinx.coroutines.test.runTest

class ArrowResponseEAdapterTest : StringSpec({
class ArrowResponseEAdapterTest {

lateinit var server: MockWebServer
lateinit var service: SuspendApiTestClient

beforeAny {
@BeforeTest
fun initialize() {
server = MockWebServer()
server.start()
service = Retrofit.Builder()
Expand All @@ -29,9 +33,13 @@ class ArrowResponseEAdapterTest : StringSpec({
.create(SuspendApiTestClient::class.java)
}

afterAny { server.shutdown() }

"should return ResponseMock for 200 with valid JSON" {
@AfterTest
fun shutdown() {
server.shutdown()
}

@Test
fun shouldReturnResponseMockFor200WithValidJson() = runTest {
server.enqueue(MockResponse().setBody("""{"response":"Arrow rocks"}"""))

val responseE = service.getResponseE()
Expand All @@ -42,7 +50,8 @@ class ArrowResponseEAdapterTest : StringSpec({
}
}

"should return Unit when service method returns Unit and null body received" {
@Test
fun shouldReturnUnitWhenServiceMethodReturnsUnitAndNullBodyReceived() = runTest {
server.enqueue(MockResponse().setResponseCode(204))

val responseE = service.postSomethingResponseE("Sample string")
Expand All @@ -52,8 +61,9 @@ class ArrowResponseEAdapterTest : StringSpec({
body shouldBe Unit.right()
}
}

"should return Unit when service method returns Unit and JSON body received" {

@Test
fun shouldReturnUnitWhenServiceMethodReturnsUnitAndJsonBodyReceived() = runTest {
server.enqueue(MockResponse().setBody("""{"response":"Arrow rocks"}"""))

val responseE = service.postSomethingResponseE("Sample string")
Expand All @@ -64,7 +74,8 @@ class ArrowResponseEAdapterTest : StringSpec({
}
}

"should return ErrorMock for 400 with valid JSON" {
@Test
fun shouldReturnErrorMockFor400WithValidJson() = runTest {
server.enqueue(MockResponse().setBody("""{"errorCode":42}""").setResponseCode(400))

val responseE = service.getResponseE()
Expand All @@ -74,28 +85,31 @@ class ArrowResponseEAdapterTest : StringSpec({
body shouldBe ErrorMock(42).left()
}
}

"should throw for 200 with invalid JSON" {

@Test
fun shouldThrowFor200WithInvalidJson() = runTest {
server.enqueue(MockResponse().setBody("""not a valid JSON"""))

val responseE = runCatching { service.getResponseE() }

responseE.isFailure shouldBe true
}

"should throw for 400 and invalid JSON" {

@Test
fun shouldThrowFor400AndInvalidJson() = runTest {
server.enqueue(MockResponse().setBody("""not a valid JSON""").setResponseCode(400))

val responseE = runCatching { service.getResponseE() }

responseE.isFailure shouldBe true
}

"should throw when server disconnects" {

@Test
fun shouldThrowWhenServerDisconnects() = runTest {
server.enqueue(MockResponse().apply { socketPolicy = SocketPolicy.DISCONNECT_AFTER_REQUEST })

val responseE = runCatching { service.getResponseE() }

responseE.isFailure shouldBe true
}
})
}

0 comments on commit 99a8e49

Please sign in to comment.