|
| 1 | +package com.androidvip.sysctlgui.data.repository |
| 2 | + |
| 3 | +import com.androidvip.sysctlgui.data.source.DocumentationDataSource |
| 4 | +import com.androidvip.sysctlgui.domain.models.KernelParam |
| 5 | +import com.androidvip.sysctlgui.domain.models.ParamDocumentation |
| 6 | +import io.mockk.called |
| 7 | +import io.mockk.coEvery |
| 8 | +import io.mockk.coVerify |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.verify |
| 11 | +import kotlinx.coroutines.delay |
| 12 | +import kotlinx.coroutines.test.runTest |
| 13 | +import org.junit.Assert |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.Test |
| 16 | + |
| 17 | +class DocumentationRepositoryImplTest { |
| 18 | + private val offlineDataSource: DocumentationDataSource = mockk(relaxed = true) |
| 19 | + private val onlineDataSource: DocumentationDataSource = mockk(relaxed = true) |
| 20 | + private lateinit var repository: DocumentationRepositoryImpl |
| 21 | + |
| 22 | + private val testParam = KernelParam( |
| 23 | + name = "vm.swappiness", |
| 24 | + path = "/proc/sys/vm/swappiness", |
| 25 | + value = "100" |
| 26 | + ) |
| 27 | + |
| 28 | + private val expectedDocumentation = ParamDocumentation( |
| 29 | + title = "Swappiness", |
| 30 | + documentationText = "Controls swappiness", |
| 31 | + url = "https://docs.kernel.org/admin-guide/sysctl/vm.html#swappiness" |
| 32 | + ) |
| 33 | + |
| 34 | + @Before |
| 35 | + fun setUp() { |
| 36 | + repository = DocumentationRepositoryImpl(offlineDataSource, onlineDataSource) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `getDocumentation when online is true and online source succeeds then return online documentation`() = |
| 41 | + runTest { |
| 42 | + // Given |
| 43 | + coEvery { onlineDataSource.getDocumentation(testParam) } returns expectedDocumentation |
| 44 | + |
| 45 | + // When |
| 46 | + val result = repository.getDocumentation(testParam, online = true) |
| 47 | + |
| 48 | + // Then |
| 49 | + coVerify(exactly = 1) { onlineDataSource.getDocumentation(testParam) } |
| 50 | + verify { offlineDataSource wasNot called } |
| 51 | + |
| 52 | + Assert.assertEquals(expectedDocumentation, result) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `getDocumentation when online is true and online source times out then return offline documentation`() = runTest { |
| 57 | + // Given |
| 58 | + coEvery { onlineDataSource.getDocumentation(testParam) } coAnswers { |
| 59 | + delay(DocumentationRepositoryImpl.REQUEST_TIMEOUT_MS + 100) |
| 60 | + null |
| 61 | + } |
| 62 | + |
| 63 | + coEvery { onlineDataSource.getDocumentation(testParam) } returns null |
| 64 | + coEvery { offlineDataSource.getDocumentation(testParam) } returns expectedDocumentation |
| 65 | + |
| 66 | + // When |
| 67 | + val result = repository.getDocumentation(testParam, online = true) |
| 68 | + |
| 69 | + // Then |
| 70 | + coVerify(exactly = 1) { onlineDataSource.getDocumentation(testParam) } |
| 71 | + coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) } |
| 72 | + Assert.assertEquals(expectedDocumentation, result) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `getDocumentation when online is false then return offline documentation`() = runTest { |
| 77 | + // Given |
| 78 | + coEvery { offlineDataSource.getDocumentation(testParam) } returns expectedDocumentation |
| 79 | + |
| 80 | + // When |
| 81 | + val result = repository.getDocumentation(testParam, online = false) |
| 82 | + |
| 83 | + // Then |
| 84 | + coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) } |
| 85 | + verify { onlineDataSource wasNot called } |
| 86 | + Assert.assertEquals(expectedDocumentation, result) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `getDocumentation when online is true and both sources return null then return null`() = runTest { |
| 91 | + // Given |
| 92 | + coEvery { onlineDataSource.getDocumentation(testParam) } returns null |
| 93 | + coEvery { offlineDataSource.getDocumentation(testParam) } returns null |
| 94 | + |
| 95 | + // When |
| 96 | + val result = repository.getDocumentation(testParam, online = true) |
| 97 | + |
| 98 | + // Then |
| 99 | + coVerify(exactly = 1) { onlineDataSource.getDocumentation(testParam) } |
| 100 | + coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) } |
| 101 | + Assert.assertNull(result) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `getDocumentation when online is false and offline source returns null then return null`() = runTest { |
| 106 | + // Given |
| 107 | + coEvery { offlineDataSource.getDocumentation(testParam) } returns null |
| 108 | + |
| 109 | + // When |
| 110 | + val result = repository.getDocumentation(testParam, online = false) |
| 111 | + |
| 112 | + // Then |
| 113 | + coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) } |
| 114 | + verify { onlineDataSource wasNot called } |
| 115 | + Assert.assertNull(result) |
| 116 | + } |
| 117 | +} |
0 commit comments