|
| 1 | +package com.androidvip.sysctlgui.data.repository |
| 2 | + |
| 3 | +import com.androidvip.sysctlgui.domain.exceptions.EmptyFileException |
| 4 | +import com.androidvip.sysctlgui.domain.exceptions.MalformedLineException |
| 5 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 6 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 7 | +import kotlinx.coroutines.test.runTest |
| 8 | +import org.intellij.lang.annotations.Language |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | +import java.io.ByteArrayInputStream |
| 13 | + |
| 14 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 15 | +class PresetRepositoryImplTest { |
| 16 | + |
| 17 | + private lateinit var repository: PresetRepositoryImpl |
| 18 | + private val testDispatcher = StandardTestDispatcher() |
| 19 | + |
| 20 | + @Before |
| 21 | + fun setUp() { |
| 22 | + repository = PresetRepositoryImpl(testDispatcher) |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `given valid preset stream, when readPreset is called, then return kernel params`() = runTest(testDispatcher) { |
| 27 | + // Given |
| 28 | + val presetContent = "net.ipv4.ip_forward=1\nvm.swappiness=60" |
| 29 | + val inputStream = ByteArrayInputStream(presetContent.toByteArray()) |
| 30 | + |
| 31 | + // When |
| 32 | + val params = repository.readPreset(inputStream) |
| 33 | + |
| 34 | + // Then |
| 35 | + assertEquals(2, params.size) |
| 36 | + assertEquals("net.ipv4.ip_forward", params[0].name) |
| 37 | + assertEquals("1", params[0].value) |
| 38 | + assertEquals("vm.swappiness", params[1].name) |
| 39 | + assertEquals("60", params[1].value) |
| 40 | + } |
| 41 | + |
| 42 | + @Test(expected = EmptyFileException::class) |
| 43 | + fun `given empty preset stream, when readPreset is called, then throw EmptyFileException`() = runTest(testDispatcher) { |
| 44 | + // Given |
| 45 | + val inputStream = ByteArrayInputStream(byteArrayOf()) |
| 46 | + // When |
| 47 | + repository.readPreset(inputStream) |
| 48 | + } |
| 49 | + |
| 50 | + @Test(expected = MalformedLineException::class) |
| 51 | + fun `given preset with malformed line, when readPreset is called, then throw MalformedLineException`() = runTest(testDispatcher) { |
| 52 | + // Given |
| 53 | + @Language("conf") |
| 54 | + val presetContent = """ |
| 55 | + net.ipv4.ip_forward=1 |
| 56 | + vm.swappiness |
| 57 | + """.trimIndent() |
| 58 | + val inputStream = ByteArrayInputStream(presetContent.toByteArray()) |
| 59 | + |
| 60 | + // When |
| 61 | + repository.readPreset(inputStream) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `given preset with comments, when readPreset is called, then ignore comments`() = runTest(testDispatcher) { |
| 66 | + // Given |
| 67 | + @Language("conf") |
| 68 | + val presetContent = """ |
| 69 | + # This is a comment |
| 70 | + net.ipv4.ip_forward=1 |
| 71 | + ; another comment |
| 72 | + vm.swappiness=60 |
| 73 | + """.trimIndent() |
| 74 | + val inputStream = ByteArrayInputStream(presetContent.toByteArray()) |
| 75 | + |
| 76 | + // When |
| 77 | + val params = repository.readPreset(inputStream) |
| 78 | + |
| 79 | + // Then |
| 80 | + assertEquals(2, params.size) |
| 81 | + assertEquals("net.ipv4.ip_forward", params[0].name) |
| 82 | + assertEquals("1", params[0].value) |
| 83 | + assertEquals("vm.swappiness", params[1].name) |
| 84 | + assertEquals("60", params[1].value) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `given preset with empty lines, when readPreset is called, then ignore empty lines`() = runTest(testDispatcher) { |
| 89 | + // Given |
| 90 | + @Language("conf") |
| 91 | + val presetContent = """ |
| 92 | + |
| 93 | + net.ipv4.ip_forward=1 |
| 94 | + |
| 95 | + vm.swappiness=60 |
| 96 | +
|
| 97 | + """.trimIndent() |
| 98 | + val inputStream = ByteArrayInputStream(presetContent.toByteArray()) |
| 99 | + |
| 100 | + // When |
| 101 | + val params = repository.readPreset(inputStream) |
| 102 | + |
| 103 | + // Then |
| 104 | + assertEquals(2, params.size) |
| 105 | + } |
| 106 | +} |
0 commit comments