Skip to content

Commit

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

* Update ArrowEitherCallAdapterTest.kt
  • Loading branch information
Tejas Mate authored Nov 8, 2023
1 parent cc9bd47 commit 4ba933e
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ 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.Test
import kotlinx.coroutines.test.runTest

class ArrowEitherCallAdapterTest : StringSpec({
class ArrowEitherCallAdapterTest {

lateinit var server: MockWebServer
lateinit var service: SuspendApiTestClient

beforeAny {
@Test fun beforeAny = runTime {
server = MockWebServer()
server.start()
service = Retrofit.Builder()
Expand All @@ -29,61 +30,61 @@ class ArrowEitherCallAdapterTest : StringSpec({
.create(SuspendApiTestClient::class.java)
}

afterAny { server.shutdown() }
@Test fun afterAny = runTime { server.shutdown() }

"should return ResponseMock for 200 with valid JSON" {
@Test fun shouldReturnResponseMockFor200WithValidJson() = runTest {
server.enqueue(MockResponse().setBody("""{"response":"Arrow rocks"}"""))

val body = service.getEither()

body shouldBe ResponseMock("Arrow rocks").right()
}

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

val body = service.postSomething("Sample string")

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 body = service.postSomething("Sample string")

body shouldBe Unit.right()
}

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

val body = service.getEither()

body shouldBe ErrorMock(666).left()
}

"should throw for 200 with invalid JSON" {
@Test fun shouldThrowFor200WithInvalidJson() = runTest {
server.enqueue(MockResponse().setBody("""not a valid JSON"""))

val body = runCatching { service.getEither() }

body.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 body = runCatching { service.getEither() }

body.isFailure shouldBe true
}

"should throw when server disconnects" {
@Test fun shouldThrowWhenServerDisconnects() = runTest {
server.enqueue(MockResponse().apply { socketPolicy = SocketPolicy.DISCONNECT_AFTER_REQUEST })

val body = runCatching { service.getEither() }

body.isFailure shouldBe true
}
})
}

0 comments on commit 4ba933e

Please sign in to comment.