Skip to content

Commit 47f50aa

Browse files
authored
feat: step2 구현 (#98)
1 parent 8531d53 commit 47f50aa

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package camp.nextstep.edu.github.data.network
2+
3+
import camp.nextstep.edu.github.domain.GithubRepository
4+
import retrofit2.http.GET
5+
6+
interface GitHubService {
7+
@GET("/repositories")
8+
suspend fun getRepositories(): List<GithubRepository>
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package camp.nextstep.edu.github.data.network
2+
3+
import camp.nextstep.edu.github.domain.GithubRepository
4+
import camp.nextstep.edu.github.domain.network.GitHubDataSource
5+
6+
internal class GithubRepositoriesDataSource(
7+
private val gitHubService: GitHubService
8+
) : GitHubDataSource {
9+
override suspend fun fetchRepositories(): List<GithubRepository> {
10+
return gitHubService.getRepositories()
11+
}
12+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package camp.nextstep.edu.github.data.network
2+
3+
import camp.nextstep.edu.github.domain.GithubRepository
4+
import com.google.common.truth.Truth.assertThat
5+
import kotlinx.coroutines.test.runTest
6+
import okhttp3.OkHttpClient
7+
import okhttp3.mockwebserver.MockResponse
8+
import okhttp3.mockwebserver.MockWebServer
9+
import org.junit.After
10+
import org.junit.Before
11+
import org.junit.Test
12+
import retrofit2.Retrofit
13+
import retrofit2.converter.gson.GsonConverterFactory
14+
import java.io.File
15+
import java.util.concurrent.TimeUnit
16+
17+
class GithubRepositoriesDataSourceTest {
18+
private lateinit var mockWebServer: MockWebServer
19+
private lateinit var api: GitHubService
20+
private lateinit var dataSource: GithubRepositoriesDataSource
21+
22+
private val client = OkHttpClient.Builder()
23+
.connectTimeout(1, TimeUnit.SECONDS)
24+
.readTimeout(1, TimeUnit.SECONDS)
25+
.writeTimeout(1, TimeUnit.SECONDS)
26+
.build()
27+
28+
@Before
29+
fun setUp() {
30+
mockWebServer = MockWebServer()
31+
}
32+
33+
@After
34+
fun tearDown() {
35+
mockWebServer.shutdown()
36+
}
37+
38+
@Test
39+
fun `GitHub repository들을 불러옴`() = runTest {
40+
api = Retrofit.Builder()
41+
.baseUrl(mockWebServer.url(""))
42+
.client(client)
43+
.addConverterFactory(GsonConverterFactory.create())
44+
.build()
45+
.create(GitHubService::class.java)
46+
val body = File("src/test/resources/response.json").readText()
47+
val response = MockResponse().setBody(body).setResponseCode(200)
48+
mockWebServer.enqueue(response)
49+
dataSource = GithubRepositoriesDataSource(api)
50+
val actual = dataSource.fetchRepositories()[0]
51+
val expected = GithubRepository("nextStep", "android-github")
52+
assertThat(actual).isEqualTo(expected)
53+
}
54+
55+
@Test
56+
fun `GitHub repository 실제 데이터 체크`() = runTest {
57+
api = Retrofit.Builder()
58+
.baseUrl(mockWebServer.url("https://api.github.com/"))
59+
.client(client)
60+
.addConverterFactory(GsonConverterFactory.create())
61+
.build()
62+
.create(GitHubService::class.java)
63+
dataSource = GithubRepositoriesDataSource(api)
64+
val actual = dataSource.fetchRepositories()
65+
assertThat(actual).hasSize(100)
66+
}
67+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[{
2+
"full_name": "nextStep",
3+
"description": "android-github"
4+
}]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package camp.nextstep.edu.github.domain
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class GithubRepository(
6+
@SerializedName("full_name")
7+
val fullName: String,
8+
val description: String
9+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package camp.nextstep.edu.github.domain.network
2+
3+
import camp.nextstep.edu.github.domain.GithubRepository
4+
5+
interface GitHubDataSource {
6+
suspend fun fetchRepositories(): List<GithubRepository>
7+
}

0 commit comments

Comments
 (0)