-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from snuhcs-course/test/remote-sources
Remote Source 테스트
- Loading branch information
Showing
4 changed files
with
382 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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()) | ||
|
||
|
@@ -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") | ||
|
@@ -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) } | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
frontend/app/src/test/java/com/example/speechbuddy/data/remote/MySymbolRemoteSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |
172 changes: 172 additions & 0 deletions
172
frontend/app/src/test/java/com/example/speechbuddy/data/remote/SettingsRemoteSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
|
||
} |
Oops, something went wrong.