From 294ed10fae8bea9975bf2ac8a71619fe8e6ab2cf Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Mon, 4 Dec 2023 08:35:44 +0900 Subject: [PATCH 1/4] :white_check_mark: Enhnace AuthTokenRemoteSource Test --- .../data/remote/AuthTokenRemoteSourceTest.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt index 48c870e1..83ea6f0e 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt @@ -24,6 +24,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()) @@ -81,4 +84,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 = "test@example.com", password = "password123") + val errorResponse = Response.error(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 = "test@example.com", code = "123456") + val errorResponse = Response.error(600, internetErrorResponseBody) + coEvery { authService.verifyEmailForResetPassword(request) } throws Exception() + + authTokenRemoteSource.verifyEmailForResetPasswordAuthToken(request).collect { result -> + assertEquals(errorResponse.code(), result.code()) + } + coVerify(exactly = 1) { authService.verifyEmailForResetPassword(request) } + } + } From 7a2f05653b034774f08c1c88ffbd41715fb7cc78 Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Mon, 4 Dec 2023 08:48:38 +0900 Subject: [PATCH 2/4] :white_check_mark: Implement MySymbolRemoteSource Test --- .../data/remote/AuthTokenRemoteSourceTest.kt | 7 ++ .../data/remote/MySymbolRemoteSourceTest.kt | 105 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 frontend/app/src/test/java/com/example/speechbuddy/data/remote/MySymbolRemoteSourceTest.kt diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt index 83ea6f0e..51f1279a 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/AuthTokenRemoteSourceTest.kt @@ -6,6 +6,7 @@ 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 @@ -13,6 +14,7 @@ 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 @@ -36,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 = "test@example.com", password = "password123") diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/MySymbolRemoteSourceTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/MySymbolRemoteSourceTest.kt new file mode 100644 index 00000000..3ce944d1 --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/MySymbolRemoteSourceTest.kt @@ -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() + + val mySymbolDto = MySymbolDto( + id = 1, + imageUrl = "sampleUrl" + ) + + val expectedResponse = Response.success(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() + val expectedResponse = Response.error(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() + val errorResponse = Response.error(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) + } + } + +} \ No newline at end of file From bdc5744fdf667043059206c63c744015c1ed4dd2 Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Mon, 4 Dec 2023 08:54:34 +0900 Subject: [PATCH 3/4] :white_check_mark: Implement UserRemoteSource Test --- .../data/remote/UserRemoteSourceTest.kt | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 frontend/app/src/test/java/com/example/speechbuddy/data/remote/UserRemoteSourceTest.kt diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/UserRemoteSourceTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/UserRemoteSourceTest.kt new file mode 100644 index 00000000..91489033 --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/UserRemoteSourceTest.kt @@ -0,0 +1,70 @@ +package com.example.speechbuddy.data.remote + +import com.example.speechbuddy.data.remote.models.UserDto +import com.example.speechbuddy.service.UserService +import com.example.speechbuddy.utils.ResponseHandler +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import junit.framework.TestCase.assertEquals +import kotlinx.coroutines.runBlocking +import okhttp3.ResponseBody.Companion.toResponseBody +import org.junit.Before +import org.junit.Test +import retrofit2.Response + +class UserRemoteSourceTest { + + private lateinit var userService: UserService + private lateinit var userRemoteSource: UserRemoteSource + 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() { + userService = mockk() + userRemoteSource = UserRemoteSource(userService, responseHandler) + } + + @Test + fun `should return UserDto when request is valid`() = runBlocking { + val expectedResponse: Response = mockk(relaxed = true) + coEvery { userService.getMyInfo(mockAuthHeader) } returns expectedResponse + + userRemoteSource.getMyInfoUser(mockAuthHeader).collect { result -> + assertEquals(expectedResponse, result) + } + coVerify(exactly = 1) { userService.getMyInfo(mockAuthHeader) } + } + + @Test + fun `should return error when request is invalid`() = runBlocking { + val expectedResponse = Response.error(400, errorResponseBody) + coEvery { userService.getMyInfo(mockAuthHeader) } returns expectedResponse + + userRemoteSource.getMyInfoUser(mockAuthHeader).collect { result -> + assertEquals(expectedResponse, result) + } + coVerify(exactly = 1) { userService.getMyInfo(mockAuthHeader) } + } + + @Test + fun `should return internet error when exception happens`() = runBlocking { + val errorResponse = Response.error(600, internetErrorResponseBody) + coEvery { userService.getMyInfo(mockAuthHeader) } throws Exception() + + userRemoteSource.getMyInfoUser(mockAuthHeader).collect { result -> + assertEquals(errorResponse.code(), result.code()) + } + coVerify(exactly = 1) { userService.getMyInfo(mockAuthHeader) } + } + +} \ No newline at end of file From eeb913271cba69987c346cd5b53fa9bdc9f77112 Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Mon, 4 Dec 2023 09:08:22 +0900 Subject: [PATCH 4/4] :white_check_mark: Implement SettingsRemoteSource Test --- .../data/remote/SettingsRemoteSourceTest.kt | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 frontend/app/src/test/java/com/example/speechbuddy/data/remote/SettingsRemoteSourceTest.kt diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/SettingsRemoteSourceTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/SettingsRemoteSourceTest.kt new file mode 100644 index 00000000..b2f2c0c1 --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/SettingsRemoteSourceTest.kt @@ -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 = 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 = 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 = 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 = 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(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(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(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(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(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(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(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(600, internetErrorResponseBody) + coEvery { backupService.getWeightTable(mockAuthHeader) } throws Exception() + + settingsRemoteSource.getWeightTable(mockAuthHeader).collect { result -> + assertEquals(errorResponse.code(), result.code()) + } + coVerify(exactly = 1) { backupService.getWeightTable(mockAuthHeader) } + } + +} \ No newline at end of file