2023-11-06 12:36:50 +05:30
|
|
|
package com.example.hpostesting
|
|
|
|
|
|
2024-01-18 01:53:41 +05:30
|
|
|
import android.content.Context
|
2023-11-06 12:36:50 +05:30
|
|
|
import android.content.SharedPreferences
|
2024-01-18 12:08:03 +05:30
|
|
|
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
|
|
|
|
import androidx.lifecycle.Observer
|
|
|
|
|
//import androidx.test.core.app.ApplicationProvider
|
2024-01-18 01:53:41 +05:30
|
|
|
import com.example.hpostesting.data.dao.HemoCubeBufferDao
|
|
|
|
|
import com.example.hpostesting.data.dao.HemoCubeDao
|
|
|
|
|
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
2024-01-18 12:08:03 +05:30
|
|
|
import com.example.hpostesting.data.model.log.UploadLogsResponse
|
|
|
|
|
import com.example.hpostesting.data.model.login.LoginResponse
|
2024-01-18 01:53:41 +05:30
|
|
|
import com.example.hpostesting.data.repository.DatabaseRepository
|
|
|
|
|
import com.example.hpostesting.data.repository.Repository
|
|
|
|
|
import com.example.hpostesting.domain.LogFileManager
|
2023-11-06 12:36:50 +05:30
|
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeFragment
|
|
|
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
|
|
|
|
import com.example.hpostesting.presentation.hemocube.HemocubeActivity
|
2024-01-18 12:08:03 +05:30
|
|
|
import com.example.hpostesting.util.TestCoroutineRule
|
|
|
|
|
import com.google.common.base.Verify.verify
|
2023-11-06 12:36:50 +05:30
|
|
|
import `in`.sminnovations.hpostesting.databinding.FragmentHemoCubeReferenceBinding
|
2024-01-18 12:08:03 +05:30
|
|
|
import io.mockk.MockKAnnotations
|
|
|
|
|
import io.mockk.coEvery
|
|
|
|
|
import io.mockk.coVerify
|
|
|
|
|
import io.mockk.every
|
|
|
|
|
import io.mockk.impl.annotations.MockK
|
|
|
|
|
import io.mockk.mockk
|
|
|
|
|
import io.mockk.verify
|
2024-01-17 00:20:04 +05:30
|
|
|
import junit.framework.TestCase.assertEquals
|
2024-01-18 12:08:03 +05:30
|
|
|
import kotlinx.coroutines.test.runBlockingTest
|
|
|
|
|
import org.junit.After
|
2023-11-06 12:36:50 +05:30
|
|
|
import org.junit.Before
|
2024-01-18 12:08:03 +05:30
|
|
|
import org.junit.Rule
|
2023-11-06 12:36:50 +05:30
|
|
|
import org.junit.Test
|
2024-01-18 12:08:03 +05:30
|
|
|
import org.mockito.ArgumentMatchers
|
|
|
|
|
import org.mockito.ArgumentMatchers.any
|
2023-11-06 12:36:50 +05:30
|
|
|
import org.mockito.ArgumentMatchers.anyString
|
|
|
|
|
import org.mockito.Mock
|
2024-01-18 12:08:03 +05:30
|
|
|
import org.mockito.Mockito
|
2023-11-06 12:36:50 +05:30
|
|
|
import org.mockito.Mockito.`when`
|
|
|
|
|
import org.mockito.MockitoAnnotations
|
2024-01-18 12:08:03 +05:30
|
|
|
import java.io.File
|
|
|
|
|
import com.example.hpostesting.data.Result
|
|
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
|
import androidx.work.Configuration
|
|
|
|
|
//import androidx.work.testing.TestListenableWorkerBuilder
|
2023-11-06 12:36:50 +05:30
|
|
|
|
2024-01-18 01:53:41 +05:30
|
|
|
|
2024-01-18 12:08:03 +05:30
|
|
|
@ExperimentalCoroutinesApi
|
|
|
|
|
class HemocubeViewModelTest {
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockSharedPreferences: SharedPreferences
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockActivity: HemocubeActivity // Replace with your actual Activity class
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockBinding: FragmentHemoCubeReferenceBinding // Replace with your actual Binding class
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockContext: Context
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var hemoCubeDaoMock: HemoCubeDao
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockRepository: Repository
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockHemoCubeBufferDao: HemoCubeBufferDao
|
|
|
|
|
//
|
2024-01-18 01:53:41 +05:30
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockLogFileManager: LogFileManager
|
|
|
|
|
//
|
|
|
|
|
// @Mock
|
|
|
|
|
// private lateinit var mockLocalFileDataSource: LocalFileDataSource
|
2024-01-18 12:08:03 +05:30
|
|
|
//
|
|
|
|
|
// private lateinit var hemoCubeFragment: HemoCubeFragment
|
|
|
|
|
//
|
|
|
|
|
// private lateinit var hemoCubeViewModel: HemoCubeViewModel
|
|
|
|
|
//
|
|
|
|
|
// @Before
|
|
|
|
|
// fun setUp() {
|
|
|
|
|
// MockitoAnnotations.openMocks(this)
|
|
|
|
|
// hemoCubeFragment = HemoCubeFragment()
|
|
|
|
|
// Mockito.`when`(
|
|
|
|
|
// mockSharedPreferences.getString(
|
|
|
|
|
// ArgumentMatchers.anyString(),
|
|
|
|
|
// ArgumentMatchers.anyString()
|
|
|
|
|
// )
|
|
|
|
|
// ).thenReturn("dummy_value")
|
2024-01-18 01:53:41 +05:30
|
|
|
// hemoCubeViewModel = HemoCubeViewModel(hemoCubeDaoMock, mockHemoCubeBufferDao, mockRepository, mockLogFileManager, mockLocalFileDataSource, mockContext)
|
2024-01-18 12:08:03 +05:30
|
|
|
// }
|
2024-01-18 01:53:41 +05:30
|
|
|
//
|
2024-01-18 12:08:03 +05:30
|
|
|
// @Test
|
|
|
|
|
// fun `parseData test`() {
|
|
|
|
|
// assertEquals(1, 1)
|
|
|
|
|
// }
|
2024-01-18 01:53:41 +05:30
|
|
|
//
|
2024-01-18 12:08:03 +05:30
|
|
|
// @Test
|
|
|
|
|
// fun `getCurrentDate returns date`() {
|
|
|
|
|
//// `when`(hemoCubeViewModel.getCurrentDate()).thenReturn("1705429433")
|
|
|
|
|
////
|
|
|
|
|
//// val result = hemoCubeViewModel.getCurrentDate()
|
|
|
|
|
////
|
|
|
|
|
//// assertEquals("1705429433", hemoCubeViewModel.getCurrentDate())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `extractMiddleString to get device id`() {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// `when`(mockSharedPreferences.getString(anyString(), anyString())).thenReturn("dummy_value")
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// val deviceId = hemoCubeFragment.extractMiddleString("SNS HPP1-9000 SNE")
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// assertEquals("HPP1-9000", deviceId)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `updateDeviceId in shared pref`() {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// `when`(mockSharedPreferences.getString(anyString(), anyString())).thenReturn("HPP1-0001")
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// val deviceId = mockSharedPreferences.getString(anyString(), anyString())
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// assertEquals("HPP1-0001", deviceId)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `allReadingsComplete check`() {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// val repeatReadingCount = 1
|
|
|
|
|
// val readingsPerSample = 1
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// val result = hemoCubeFragment.allReadingsComplete(repeatReadingCount, readingsPerSample)
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// assertEquals(true, result)
|
|
|
|
|
// assertEquals(hemoCubeFragment.allReadingsComplete(0, 1), false)
|
|
|
|
|
// }
|
2024-01-17 02:44:33 +05:30
|
|
|
|
|
|
|
|
|
2023-11-06 12:36:50 +05:30
|
|
|
|
2024-01-17 02:44:33 +05:30
|
|
|
|
|
|
|
|
|
2024-01-18 12:08:03 +05:30
|
|
|
// @get:Rule
|
|
|
|
|
// val instantTaskExecutorRule = InstantTaskExecutorRule()
|
|
|
|
|
//
|
|
|
|
|
// @get:Rule
|
|
|
|
|
// val coroutineRule = TestCoroutineRule()
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var repository: Repository
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var logFileManager: LogFileManager
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var context: Context
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var observer: Observer<Result<LoginResponse>>
|
|
|
|
|
//
|
|
|
|
|
// private lateinit var viewModel: HemoCubeViewModel
|
|
|
|
|
//
|
|
|
|
|
// @Before
|
|
|
|
|
// fun setup() {
|
|
|
|
|
// MockKAnnotations.init(this)
|
|
|
|
|
//
|
|
|
|
|
// every { context.getSharedPreferences(any(), any()) } returns mockk()
|
|
|
|
|
//
|
|
|
|
|
// viewModel = HemoCubeViewModel(
|
|
|
|
|
// mockk(),
|
|
|
|
|
// mockk(),
|
|
|
|
|
// repository,
|
|
|
|
|
// logFileManager,
|
|
|
|
|
// mockk(),
|
|
|
|
|
// context
|
|
|
|
|
// )
|
|
|
|
|
// viewModel.loginResponse.observeForever(observer)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @After
|
|
|
|
|
// fun teardown() {
|
|
|
|
|
// viewModel.loginResponse.removeObserver(observer)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `login() should update LiveData with success`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Set up WorkManager with TestListenableWorkerBuilder
|
|
|
|
|
// val config = Configuration.Builder()
|
|
|
|
|
// .setMinimumLoggingLevel(android.util.Log.DEBUG)
|
|
|
|
|
// .build()
|
|
|
|
|
//
|
|
|
|
|
// val context = ApplicationProvider.getApplicationContext<Context>()
|
|
|
|
|
//
|
|
|
|
|
// WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// // Arrange
|
|
|
|
|
// coEvery { repository.login(any()) } returns Result.Success(mockk())
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.login(mockk())
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// coVerify { repository.login(any()) }
|
|
|
|
|
// verify { observer.onChanged(Result.Success(any())) }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `login() should update LiveData with error`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// coEvery { repository.login(any()) } returns Result.Error(Exception("Login failed"))
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.login(mockk())
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// coVerify { repository.login(any()) }
|
|
|
|
|
// verify { observer.onChanged(Result.Error(any())) }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `uploadLogs() should update LiveData with success`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// val file = mockk<File>(relaxed = true)
|
|
|
|
|
// val response = Result.Success(mockk<UploadLogsResponse>())
|
|
|
|
|
// every { logFileManager.createLogFile() } returns file
|
|
|
|
|
// coEvery { repository.uploadLogs(any()) } returns response
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.uploadLogs()
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// verify { observer.onChanged(response) }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `uploadLogs() should update LiveData with error`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// val file = mockk<File>(relaxed = true)
|
|
|
|
|
// val response = Result.Error(Exception("Upload failed"))
|
|
|
|
|
// every { logFileManager.createLogFile() } returns file
|
|
|
|
|
// coEvery { repository.uploadLogs(any()) } returns response
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.uploadLogs()
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// verify { observer.onChanged(response) }
|
|
|
|
|
// }
|
2023-11-06 12:36:50 +05:30
|
|
|
}
|