Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote Source 테스트 #101

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import com.example.speechbuddy.data.remote.requests.AuthLoginRequest
import com.example.speechbuddy.data.remote.requests.AuthVerifyEmailRequest
import com.example.speechbuddy.service.AuthService
import com.example.speechbuddy.utils.ResponseHandler
import io.mockk.clearAllMocks
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
Expand All @@ -24,6 +26,9 @@ class AuthTokenRemoteSourceTest {
private lateinit var authTokenRemoteSource: AuthTokenRemoteSource
private val responseHandler = ResponseHandler()

private val internetErrorResponseBody =
"{\"code\": 600, \"message\": \"No Internet Connection\"}".toResponseBody()

private val errorResponseBody =
"{\"error\":\"Something went wrong\"}".toResponseBody("application/json".toMediaType())

Expand All @@ -33,6 +38,11 @@ class AuthTokenRemoteSourceTest {
authTokenRemoteSource = AuthTokenRemoteSource(authService, responseHandler)
}

@After
fun tearDown() {
clearAllMocks()
}

@Test
fun `should return auth token when request is valid for login`() = runBlocking {
val request = AuthLoginRequest(email = "[email protected]", password = "password123")
Expand Down Expand Up @@ -81,4 +91,29 @@ class AuthTokenRemoteSourceTest {
coVerify(exactly = 1) { authService.verifyEmailForResetPassword(request) }
}

@Test
fun `should return internet error when exception happens for login`(): Unit = runBlocking {
val request = AuthLoginRequest(email = "[email protected]", password = "password123")
val errorResponse = Response.error<AuthTokenDto>(600, internetErrorResponseBody)
coEvery { authService.login(request) } throws Exception()

authTokenRemoteSource.loginAuthToken(request).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) { authService.login(request) }
}


@Test
fun `should return internet error when exception happens for verifyEmailForResetPasswordAuthToken`(): Unit = runBlocking {
val request = AuthVerifyEmailRequest(email = "[email protected]", code = "123456")
val errorResponse = Response.error<AccessTokenDto>(600, internetErrorResponseBody)
coEvery { authService.verifyEmailForResetPassword(request) } throws Exception()

authTokenRemoteSource.verifyEmailForResetPasswordAuthToken(request).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) { authService.verifyEmailForResetPassword(request) }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.example.speechbuddy.data.remote

import com.example.speechbuddy.data.remote.models.MySymbolDto
import com.example.speechbuddy.service.SymbolCreationService
import com.example.speechbuddy.utils.ResponseHandler
import io.mockk.clearAllMocks
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.runBlocking
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.After
import org.junit.Before
import org.junit.Test
import retrofit2.Response

class MySymbolRemoteSourceTest {
private lateinit var symbolCreationService: SymbolCreationService
private lateinit var mySymbolRemoteSource: MySymbolRemoteSource
private val responseHandler = ResponseHandler()

private val mockAccessToken = "testAccessToken"
private val mockAuthHeader = "Bearer $mockAccessToken"

private val internetErrorResponseBody =
"{\"code\": 600, \"message\": \"No Internet Connection\"}".toResponseBody()

private val errorResponseBody =
"{\"error\":\"Something went wrong\"}".toResponseBody("application/json".toMediaType())

@Before
fun setUp() {
symbolCreationService = mockk()
mySymbolRemoteSource = MySymbolRemoteSource(symbolCreationService, responseHandler)
}

@After
fun tearDown() {
clearAllMocks()
}

@Test
fun `should return MySymbolDto when request is valid for createSymbolBackup`() = runBlocking {
val symbolText = "Test Symbol"
val categoryId = 1
val image = mockk<MultipartBody.Part>()

val mySymbolDto = MySymbolDto(
id = 1,
imageUrl = "sampleUrl"
)

val expectedResponse = Response.success<MySymbolDto>(200, mySymbolDto)
coEvery {
symbolCreationService.createSymbolBackup(mockAuthHeader, any(), any(), image)
} returns expectedResponse

mySymbolRemoteSource.createSymbolBackup(mockAuthHeader, symbolText, categoryId, image).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) {
symbolCreationService.createSymbolBackup(mockAuthHeader, any(), any(), image)
}
}

@Test
fun `should return error when request is invalid`() = runBlocking {
val symbolText = "Test Symbol"
val categoryId = 1
val image = mockk<MultipartBody.Part>()
val expectedResponse = Response.error<MySymbolDto>(400, errorResponseBody)
coEvery {
symbolCreationService.createSymbolBackup(mockAuthHeader, any(), any(), image)
} returns expectedResponse

mySymbolRemoteSource.createSymbolBackup(mockAuthHeader, symbolText, categoryId, image).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) {
symbolCreationService.createSymbolBackup(mockAuthHeader, any(), any(), image)
}
}

@Test
fun `should return internet error when exception happens`() = runBlocking {
val symbolText = "Test Symbol"
val categoryId = 1
val image = mockk<MultipartBody.Part>()
val errorResponse = Response.error<MySymbolDto>(600, internetErrorResponseBody)
coEvery {
symbolCreationService.createSymbolBackup(mockAuthHeader, any(), any(), image)
} throws Exception()

mySymbolRemoteSource.createSymbolBackup(mockAuthHeader, symbolText, categoryId, image).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) {
symbolCreationService.createSymbolBackup(mockAuthHeader, any(), any(), image)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.example.speechbuddy.data.remote

import com.example.speechbuddy.data.remote.models.FavoritesListDto
import com.example.speechbuddy.data.remote.models.SettingsBackupDto
import com.example.speechbuddy.data.remote.models.SymbolListDto
import com.example.speechbuddy.data.remote.requests.BackupWeightTableRequest
import com.example.speechbuddy.service.BackupService
import com.example.speechbuddy.utils.ResponseHandler
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.runBlocking
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import retrofit2.Response

class SettingsRemoteSourceTest {

private lateinit var backupService: BackupService
private lateinit var settingsRemoteSource: SettingsRemoteSource
private val responseHandler = ResponseHandler()

private val mockAccessToken = "testAccessToken"
private val mockAuthHeader = "Bearer $mockAccessToken"

private val internetErrorResponseBody =
"{\"code\": 600, \"message\": \"No Internet Connection\"}".toResponseBody()

private val errorResponseBody =
"{\"error\":\"Something went wrong\"}".toResponseBody()

@Before
fun setUp() {
backupService = mockk()
settingsRemoteSource = SettingsRemoteSource(backupService, responseHandler)
}

@Test
fun `should return SettingsBackupDto when request for display settings is valid`() = runBlocking {
val expectedResponse: Response<SettingsBackupDto> = mockk(relaxed = true)
coEvery { backupService.getDisplaySettings(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getDisplaySettings(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getDisplaySettings(mockAuthHeader) }
}

@Test
fun `should return SymbolListDto when request for symbol list is valid`() = runBlocking {
val expectedResponse: Response<SymbolListDto> = mockk(relaxed = true)
coEvery { backupService.getSymbolList(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getSymbolList(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getSymbolList(mockAuthHeader) }
}

@Test
fun `should return FavoritesListDto when request for favorites list is valid`() = runBlocking {
val expectedResponse: Response<FavoritesListDto> = mockk(relaxed = true)
coEvery { backupService.getFavoriteSymbolList(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getFavoritesList(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getFavoriteSymbolList(mockAuthHeader) }
}

@Test
fun `should return BackupWeightTableRequest when request for weight table is valid`() = runBlocking {
val expectedResponse: Response<BackupWeightTableRequest> = mockk(relaxed = true)
coEvery { backupService.getWeightTable(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getWeightTable(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getWeightTable(mockAuthHeader) }
}

@Test
fun `should return error when request for display settings is invalid`() = runBlocking {
val expectedResponse = Response.error<SettingsBackupDto>(400, errorResponseBody)
coEvery { backupService.getDisplaySettings(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getDisplaySettings(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getDisplaySettings(mockAuthHeader) }
}

@Test
fun `should return error when request for symbol list is invalid`() = runBlocking {
val expectedResponse = Response.error<SymbolListDto>(400, errorResponseBody)
coEvery { backupService.getSymbolList(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getSymbolList(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getSymbolList(mockAuthHeader) }
}

@Test
fun `should return error when request for favorites list is invalid`() = runBlocking {
val expectedResponse = Response.error<FavoritesListDto>(400, errorResponseBody)
coEvery { backupService.getFavoriteSymbolList(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getFavoritesList(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getFavoriteSymbolList(mockAuthHeader) }
}

@Test
fun `should return error when request for weight table is invalid`() = runBlocking {
val expectedResponse = Response.error<BackupWeightTableRequest>(400, errorResponseBody)
coEvery { backupService.getWeightTable(mockAuthHeader) } returns expectedResponse

settingsRemoteSource.getWeightTable(mockAuthHeader).collect { result ->
assertEquals(expectedResponse, result)
}
coVerify(exactly = 1) { backupService.getWeightTable(mockAuthHeader) }
}

@Test
fun `should return internet error when exception happens for symbol list`() = runBlocking {
val errorResponse = Response.error<SymbolListDto>(600, internetErrorResponseBody)
coEvery { backupService.getSymbolList(mockAuthHeader) } throws Exception()

settingsRemoteSource.getSymbolList(mockAuthHeader).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) { backupService.getSymbolList(mockAuthHeader) }
}

@Test
fun `should return internet error when exception happens for display settings`() = runBlocking {
val errorResponse = Response.error<SettingsBackupDto>(600, internetErrorResponseBody)
coEvery { backupService.getDisplaySettings(mockAuthHeader) } throws Exception()

settingsRemoteSource.getDisplaySettings(mockAuthHeader).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) { backupService.getDisplaySettings(mockAuthHeader) }
}

@Test
fun `should return internet error when exception happens for favorites list`() = runBlocking {
val errorResponse = Response.error<FavoritesListDto>(600, internetErrorResponseBody)
coEvery { backupService.getFavoriteSymbolList(mockAuthHeader) } throws Exception()

settingsRemoteSource.getFavoritesList(mockAuthHeader).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) { backupService.getFavoriteSymbolList(mockAuthHeader) }
}

@Test
fun `should return internet error when exception happens for weight table`() = runBlocking {
val errorResponse = Response.error<BackupWeightTableRequest>(600, internetErrorResponseBody)
coEvery { backupService.getWeightTable(mockAuthHeader) } throws Exception()

settingsRemoteSource.getWeightTable(mockAuthHeader).collect { result ->
assertEquals(errorResponse.code(), result.code())
}
coVerify(exactly = 1) { backupService.getWeightTable(mockAuthHeader) }
}

}
Loading