Skip to content

Commit 65e4200

Browse files
committed
remote tests
1 parent 557807e commit 65e4200

7 files changed

Lines changed: 169 additions & 17 deletions

File tree

0 Bytes
Binary file not shown.
126 Bytes
Binary file not shown.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.vrgsoft.remote
2+
3+
import com.nhaarman.mockitokotlin2.doReturn
4+
import com.nhaarman.mockitokotlin2.mock
5+
import com.vrgsoft.core.remote.BaseRepository
6+
import com.vrgsoft.core.remote.error.ConnectionError
7+
import com.vrgsoft.core.remote.error.NetworkError
8+
import com.vrgsoft.core.remote.result.ErrorResult
9+
import com.vrgsoft.core.remote.result.SuccessResult
10+
import kotlinx.coroutines.ExperimentalCoroutinesApi
11+
import kotlinx.coroutines.GlobalScope
12+
import kotlinx.coroutines.async
13+
import kotlinx.coroutines.test.runBlockingTest
14+
import org.junit.Assert.assertEquals
15+
import org.junit.Test
16+
import retrofit2.Response
17+
18+
@ExperimentalCoroutinesApi
19+
internal class BaseRepositoryTest {
20+
lateinit var repository: BaseRepository
21+
lateinit var api: TestApi
22+
23+
private var isSuccess = true
24+
25+
fun setUp() {
26+
repository = TestRepository()
27+
28+
val response = mock<Response<String>> {
29+
on { isSuccessful } doReturn isSuccess
30+
on { body() } doReturn "1"
31+
on { code() } doReturn 401
32+
on { message() } doReturn "Unauthorized"
33+
}
34+
35+
api = mock {
36+
on { call() } doReturn response.toDeferred()
37+
}
38+
}
39+
40+
@Test
41+
fun transform() {
42+
setUp()
43+
val result = SuccessResult("1")
44+
45+
val transformed = with(repository) {
46+
result.transform { it.toInt() }
47+
}
48+
49+
assertEquals(1, transformed.data)
50+
}
51+
52+
@Test
53+
fun transformIsSuccess() {
54+
setUp()
55+
val result = SuccessResult("1")
56+
57+
val transformed = with(repository) {
58+
result.transformIsSuccess { it.toInt() }
59+
}
60+
61+
require(transformed is SuccessResult<Int>)
62+
assertEquals(1, transformed.data)
63+
}
64+
65+
@Test
66+
fun transformIsConnectionError() {
67+
setUp()
68+
val result = ErrorResult<String>(ConnectionError())
69+
70+
val transformed = with(repository) {
71+
result.transformIsSuccess { it.toInt() }
72+
}
73+
74+
require(transformed is ErrorResult<Int>)
75+
require(transformed.error is ConnectionError)
76+
}
77+
78+
@Test
79+
fun transformIsNetworkError() {
80+
setUp()
81+
val code = 401
82+
val message = "Unauthorized"
83+
val result = ErrorResult<String>(NetworkError(code, message))
84+
85+
val transformed = with(repository) {
86+
result.transformIsSuccess { it.toInt() }
87+
}
88+
89+
require(transformed is ErrorResult<Int>)
90+
91+
with(transformed) {
92+
require(error is NetworkError)
93+
assertEquals(code, (error as NetworkError).code)
94+
assertEquals(message, (error as NetworkError).message)
95+
}
96+
}
97+
98+
@Test
99+
fun executeSuccess() {
100+
isSuccess = true
101+
setUp()
102+
val data = "1"
103+
104+
runBlockingTest {
105+
val transformed = with(repository) {
106+
execute { api.call() }
107+
}
108+
109+
require(transformed is SuccessResult<String>)
110+
assertEquals(data, transformed.data)
111+
}
112+
}
113+
114+
@Test
115+
fun executeError() {
116+
isSuccess = false
117+
setUp()
118+
val code = 401
119+
val message = "Unauthorized"
120+
121+
runBlockingTest {
122+
val transformed = with(repository) {
123+
execute { api.call() }
124+
}
125+
126+
require(transformed is ErrorResult<String>)
127+
require(transformed.error is NetworkError)
128+
129+
with(transformed.error as NetworkError) {
130+
assertEquals(code, this.code)
131+
assertEquals(message, this.message)
132+
}
133+
}
134+
}
135+
136+
@Test
137+
fun executeThrow() {
138+
isSuccess = true
139+
setUp()
140+
141+
runBlockingTest {
142+
val transformed = with(repository) {
143+
execute {
144+
throw IllegalArgumentException()
145+
api.call()
146+
}
147+
}
148+
149+
require(transformed is ErrorResult)
150+
require(transformed.error is ConnectionError)
151+
}
152+
}
153+
154+
fun <T> T.toDeferred() = GlobalScope.async { this@toDeferred }
155+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.vrgsoft.remote
2+
3+
import kotlinx.coroutines.Deferred
4+
import retrofit2.Response
5+
6+
interface TestApi {
7+
fun call(): Deferred<Response<String>>
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.vrgsoft.remote
2+
3+
import com.vrgsoft.core.remote.BaseRepository
4+
5+
class TestRepository : BaseRepository()

remote/src/test/java/com/vrgsoft/retrofit/ExampleUnitTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)