-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* NetworkRequestHandler created with its test * Kt3k coveralls commented * Dependencies added * Commented lines were added * Documentation added * Identation was fixed * Changes in documentation
- Loading branch information
Showing
4 changed files
with
122 additions
and
2 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
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
54 changes: 54 additions & 0 deletions
54
...ing/src/main/java/ar/com/wolox/wolmo/networking/retrofit/handler/NetworkRequestHandler.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,54 @@ | ||
package ar.com.wolox.wolmo.networking.retrofit.handler | ||
|
||
import retrofit2.Response | ||
|
||
/** | ||
* An adapter that converts Retrofit's responses to other, more specific, ones | ||
* depending on network status codes or failures when using Kotlin's Coroutines. | ||
* It will return a [NetworkResponse] object indicating operation result. | ||
*/ | ||
object NetworkRequestHandler { | ||
|
||
/** | ||
* Static method that allows to execute requests from a [suspend] function of [Response] type | ||
* and returns a [NetworkResponse] object depending on HTTP response. | ||
*/ | ||
suspend fun <T : Response<*>> safeApiCall(block: suspend () -> T): NetworkResponse<T> = | ||
try { | ||
val response = block.invoke() | ||
if (response.isSuccessful) { | ||
NetworkResponse.Success(response) | ||
} else { | ||
NetworkResponse.Error(response) | ||
} | ||
} catch (t: Throwable) { | ||
NetworkResponse.Failure(t) | ||
} | ||
} | ||
|
||
sealed class NetworkResponse<T> { | ||
|
||
/** | ||
* Successful HTTP response from the server. | ||
* The server received the request, answered it and the response is not of an error type. | ||
* It will return a [T] object, the API JSON response converted to a Java/Kotlin object, | ||
* which includes the API response code. | ||
*/ | ||
data class Success<T>(val response: T) : NetworkResponse<T>() | ||
|
||
/** | ||
* Successful HTTP response from the server, but has an error body. | ||
* The server received the request, answered it and reported an error. | ||
* It will return a [T], the API JSON response converted to a Java/Kotlin object, | ||
* which includes the API response code. | ||
*/ | ||
data class Error<T>(val response: T) : NetworkResponse<T>() | ||
|
||
/** | ||
* The HTTP request to the server failed on the local device, no data was transmitted. | ||
* Invoked when a network or unexpected exception occurred during the HTTP request, meaning | ||
* that the request couldn't be executed. The cause of the failure will be given through a | ||
* [Throwable] object | ||
*/ | ||
data class Failure<T>(val t: Throwable) : NetworkResponse<T>() | ||
} |
57 changes: 57 additions & 0 deletions
57
...src/test/java/ar/com/wolox/wolmo/networking/retrofit/handler/NetworkRequestHandlerTest.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,57 @@ | ||
package ar.com.wolox.wolmo.networking.retrofit.handler | ||
|
||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.Mockito.mock | ||
import org.mockito.MockitoAnnotations | ||
import org.mockito.stubbing.Answer | ||
import retrofit2.Response | ||
|
||
@ExperimentalCoroutinesApi | ||
class NetworkRequestHandlerTest { | ||
|
||
@Before | ||
fun setup() { | ||
MockitoAnnotations.initMocks(this) | ||
} | ||
|
||
@Test | ||
fun `given a succesfull response with code between 200 and 300, NetworkResponse Success state is called`() = runBlocking { | ||
// GIVEN | ||
val apiResponse = mock(Response::class.java) as Response<Any> | ||
|
||
// WHEN | ||
`when`(apiResponse.isSuccessful).thenReturn(true) | ||
|
||
// THEN | ||
val networkResponse = NetworkRequestHandler.safeApiCall { apiResponse } | ||
assert(networkResponse is NetworkResponse.Success) | ||
} | ||
|
||
@Test | ||
fun `given a succesfull response with code above 300, NetworkResponse Error state is called`() = runBlocking { | ||
// GIVEN | ||
val apiResponse = mock(Response::class.java) as Response<Any> | ||
|
||
// WHEN | ||
`when`(apiResponse.isSuccessful).thenReturn(false) | ||
|
||
// THEN | ||
val networkResponse = NetworkRequestHandler.safeApiCall { apiResponse } | ||
assert(networkResponse is NetworkResponse.Error) | ||
} | ||
|
||
@Test | ||
fun `given a non-succesfull response, NetworkResponse Failure state is called`() = runBlocking { | ||
// GIVEN | ||
val answer: Answer<Exception> = Answer { Exception() } | ||
val apiResponse = mock (Response::class.java, answer) as Response<Any> | ||
|
||
// THEN | ||
val networkResponse = NetworkRequestHandler.safeApiCall { apiResponse } | ||
assert(networkResponse is NetworkResponse.Failure) | ||
} | ||
} |