feat(data): add DeviceConfig model, flexible R2ConfigParser, and DeviceRepository with unit tests

This commit is contained in:
2026-11-18 12:58:28 +05:30
parent 6a7821b766
commit be97c341bf
7 changed files with 267 additions and 0 deletions
@@ -0,0 +1,65 @@
package com.expapp.trigger.data.parser
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class R2ConfigParserTest {
@Test
fun parse_standardLowercaseKeys_returnsEndpoints() {
val json = """
{
"poweron": "https://cosmos.linuxguy.space/poweron",
"poweroff": "https://cosmos.linuxguy.space/poweroff",
"reboot": "https://cosmos.linuxguy.space/reboot"
}
""".trimIndent()
val result = R2ConfigParser.parse(json)
assertTrue(result.isSuccess)
val endpoints = result.getOrThrow()
assertEquals("https://cosmos.linuxguy.space/poweron", endpoints.powerOn)
assertEquals("https://cosmos.linuxguy.space/poweroff", endpoints.powerOff)
assertEquals("https://cosmos.linuxguy.space/reboot", endpoints.reboot)
}
@Test
fun parse_camelCaseAndUnderscoreKeys_returnsEndpoints() {
val json = """
{
"power_on": "https://cosmos.linuxguy.space/poweron",
"powerOff": "https://cosmos.linuxguy.space/poweroff",
"restart": "https://cosmos.linuxguy.space/reboot"
}
""".trimIndent()
val result = R2ConfigParser.parse(json)
assertTrue(result.isSuccess)
val endpoints = result.getOrThrow()
assertEquals("https://cosmos.linuxguy.space/poweron", endpoints.powerOn)
assertEquals("https://cosmos.linuxguy.space/poweroff", endpoints.powerOff)
assertEquals("https://cosmos.linuxguy.space/reboot", endpoints.reboot)
}
@Test
fun parse_missingRequiredKey_returnsFailure() {
val json = """
{
"poweron": "https://cosmos.linuxguy.space/poweron",
"reboot": "https://cosmos.linuxguy.space/reboot"
}
""".trimIndent()
val result = R2ConfigParser.parse(json)
assertTrue(result.isFailure)
assertTrue(result.exceptionOrNull()?.message?.contains("poweroff", ignoreCase = true) == true)
}
@Test
fun parse_invalidJson_returnsFailure() {
val invalid = "{ invalid json"
val result = R2ConfigParser.parse(invalid)
assertTrue(result.isFailure)
}
}
@@ -0,0 +1,67 @@
package com.expapp.trigger.data.repository
import com.expapp.trigger.data.model.DeviceConfig
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
class DeviceRepositoryTest {
private class FakeKeyValueStorage : KeyValueStorage {
private val data = mutableMapOf<String, String?>()
override fun getString(key: String, defaultValue: String?): String? = data[key] ?: defaultValue
override fun putString(key: String, value: String?) {
data[key] = value
}
}
private lateinit var storage: FakeKeyValueStorage
private lateinit var repository: DeviceRepository
@Before
fun setup() {
storage = FakeKeyValueStorage()
repository = DeviceRepository(storage)
}
@Test
fun getDevices_whenEmpty_returnsEmptyList() {
val devices = repository.getDevices()
assertTrue(devices.isEmpty())
}
@Test
fun addDevice_savesAndReturnsDevice() {
val device = DeviceConfig(
id = "dev-1",
name = "Debian Server",
node = "debian",
secretKey = "Carbon@12",
r2Url = "https://r2.example.com/config.json",
powerOnUrl = "https://cosmos.linuxguy.space/poweron",
powerOffUrl = "https://cosmos.linuxguy.space/poweroff",
rebootUrl = "https://cosmos.linuxguy.space/reboot"
)
repository.addDevice(device)
val devices = repository.getDevices()
assertEquals(1, devices.size)
assertEquals(device, devices[0])
}
@Test
fun deleteDevice_removesDeviceById() {
val d1 = DeviceConfig(id = "1", name = "D1", node = "n1", secretKey = "k1", r2Url = "", powerOnUrl = "", powerOffUrl = "", rebootUrl = "")
val d2 = DeviceConfig(id = "2", name = "D2", node = "n2", secretKey = "k2", r2Url = "", powerOnUrl = "", powerOffUrl = "", rebootUrl = "")
repository.addDevice(d1)
repository.addDevice(d2)
assertEquals(2, repository.getDevices().size)
repository.deleteDevice("1")
val remaining = repository.getDevices()
assertEquals(1, remaining.size)
assertEquals("2", remaining[0].id)
}
}