Skip to content

Commit

Permalink
feat: step2 구현 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhyukseul authored Aug 27, 2023
1 parent 8531d53 commit 47f50aa
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package camp.nextstep.edu.github.data.network

import camp.nextstep.edu.github.domain.GithubRepository
import retrofit2.http.GET

interface GitHubService {
@GET("/repositories")
suspend fun getRepositories(): List<GithubRepository>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package camp.nextstep.edu.github.data.network

import camp.nextstep.edu.github.domain.GithubRepository
import camp.nextstep.edu.github.domain.network.GitHubDataSource

internal class GithubRepositoriesDataSource(
private val gitHubService: GitHubService
) : GitHubDataSource {
override suspend fun fetchRepositories(): List<GithubRepository> {
return gitHubService.getRepositories()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package camp.nextstep.edu.github.data.network

import camp.nextstep.edu.github.domain.GithubRepository
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

class GithubRepositoriesDataSourceTest {
private lateinit var mockWebServer: MockWebServer
private lateinit var api: GitHubService
private lateinit var dataSource: GithubRepositoriesDataSource

private val client = OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS)
.build()

@Before
fun setUp() {
mockWebServer = MockWebServer()
}

@After
fun tearDown() {
mockWebServer.shutdown()
}

@Test
fun `GitHub repository들을 불러옴`() = runTest {
api = Retrofit.Builder()
.baseUrl(mockWebServer.url(""))
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GitHubService::class.java)
val body = File("src/test/resources/response.json").readText()
val response = MockResponse().setBody(body).setResponseCode(200)
mockWebServer.enqueue(response)
dataSource = GithubRepositoriesDataSource(api)
val actual = dataSource.fetchRepositories()[0]
val expected = GithubRepository("nextStep", "android-github")
assertThat(actual).isEqualTo(expected)
}

@Test
fun `GitHub repository 실제 데이터 체크`() = runTest {
api = Retrofit.Builder()
.baseUrl(mockWebServer.url("https://api.github.com/"))
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GitHubService::class.java)
dataSource = GithubRepositoriesDataSource(api)
val actual = dataSource.fetchRepositories()
assertThat(actual).hasSize(100)
}
}
4 changes: 4 additions & 0 deletions core/data/src/test/resources/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[{
"full_name": "nextStep",
"description": "android-github"
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package camp.nextstep.edu.github.domain

import com.google.gson.annotations.SerializedName

data class GithubRepository(
@SerializedName("full_name")
val fullName: String,
val description: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package camp.nextstep.edu.github.domain.network

import camp.nextstep.edu.github.domain.GithubRepository

interface GitHubDataSource {
suspend fun fetchRepositories(): List<GithubRepository>
}

0 comments on commit 47f50aa

Please sign in to comment.