forked from SurvivalCodingCampus/winter_kotlin_study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoDataSourceTest.kt
105 lines (92 loc) · 3.42 KB
/
TodoDataSourceTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package day16.mocking.repository
import day16.util.createMockClient
import io.ktor.client.engine.mock.*
import io.ktor.client.network.sockets.*
import io.ktor.http.*
import kotlinx.coroutines.runBlocking
import org.hyunjung.day16.mocking.datasource.TodoDataSource
import org.hyunjung.day16.mocking.datasource.TodoDataSourceImpl
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Test
class TodoDataSourceTest {
@Test
fun `getTodos 성공 케이스`() = runBlocking {
val mockClient = createMockClient { request ->
if (request.url.encodedPath == "/todos") {
respond(
content = """
[
{"id": 1, "title": "Todo 1", "completed": false},
{"id": 2, "title": "Todo 2", "completed": true}
]
""",
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString())
)
} else {
error("Unhandled request")
}
}
val dataSource = TodoDataSourceImpl(mockClient)
val todos = dataSource.getTodos()
assertEquals(2, todos.size)
assertEquals("Todo 1", todos[0].title)
assertEquals(false, todos[0].completed)
}
@Test
fun `getTodo 성공 케이스`() = runBlocking {
val mockClient = createMockClient { request ->
if (request.url.encodedPath == "/todos/1") {
respond(
content = """
{"id": 1, "title": "Todo 1", "completed": false}
""",
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString())
)
} else {
error("Unhandled request")
}
}
val dataSource: TodoDataSource = TodoDataSourceImpl(mockClient)
val todo = dataSource.getTodo(1)
assertEquals(1, todo.id)
assertEquals("Todo 1", todo.title)
assertEquals(false, todo.completed)
}
@Test
fun `네트워크 에러 케이스`() = runBlocking {
val mockClient = createMockClient {
throw ConnectTimeoutException("Connection timeout")
}
val dataSource: TodoDataSource = TodoDataSourceImpl(mockClient)
try {
dataSource.getTodos()
fail("Expected exception not thrown")
} catch (e: ConnectTimeoutException) {
assertEquals("Connection timeout", e.message)
}
}
@Test
fun `존재하지 않는 ID 요청 케이스`() = runBlocking {
val mockClient = createMockClient { request ->
if (request.url.encodedPath == "/todos/0") {
respond(
content = "{}",
status = HttpStatusCode.NotFound,
headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString())
)
} else {
error("Unhandled request")
}
}
val dataSource: TodoDataSource = TodoDataSourceImpl(mockClient)
try {
dataSource.getTodo(0)
fail("Expected exception not thrown")
} catch (e: IllegalArgumentException) {
assertEquals("Todo with ID 0 not found", e.message)
}
}
}