feat(data): add DeviceConfig model, flexible R2ConfigParser, and DeviceRepository with unit tests
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
package com.expapp.trigger.data.model
|
||||||
|
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class DeviceConfig(
|
||||||
|
val id: String = UUID.randomUUID().toString(),
|
||||||
|
val name: String,
|
||||||
|
val node: String,
|
||||||
|
val secretKey: String,
|
||||||
|
val r2Url: String,
|
||||||
|
val powerOnUrl: String,
|
||||||
|
val powerOffUrl: String,
|
||||||
|
val rebootUrl: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.expapp.trigger.data.parser
|
||||||
|
|
||||||
|
import com.google.gson.JsonParser
|
||||||
|
|
||||||
|
data class ParsedEndpoints(
|
||||||
|
val powerOn: String,
|
||||||
|
val powerOff: String,
|
||||||
|
val reboot: String
|
||||||
|
)
|
||||||
|
|
||||||
|
object R2ConfigParser {
|
||||||
|
fun parse(jsonString: String): Result<ParsedEndpoints> {
|
||||||
|
return runCatching {
|
||||||
|
val jsonElement = JsonParser.parseString(jsonString)
|
||||||
|
if (!jsonElement.isJsonObject) {
|
||||||
|
throw IllegalArgumentException("Expected JSON object")
|
||||||
|
}
|
||||||
|
val jsonObject = jsonElement.asJsonObject
|
||||||
|
|
||||||
|
// Case-insensitive & variant normalization
|
||||||
|
val normalizedMap = mutableMapOf<String, String>()
|
||||||
|
for ((key, value) in jsonObject.entrySet()) {
|
||||||
|
val cleanKey = key.lowercase().replace("_", "").replace("-", "")
|
||||||
|
if (value.isJsonPrimitive && value.asJsonPrimitive.isString) {
|
||||||
|
normalizedMap[cleanKey] = value.asString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val powerOn = normalizedMap["poweron"]
|
||||||
|
?: normalizedMap["on"]
|
||||||
|
?: normalizedMap["start"]
|
||||||
|
?: throw IllegalArgumentException("Missing required endpoint key: 'poweron'")
|
||||||
|
|
||||||
|
val powerOff = normalizedMap["poweroff"]
|
||||||
|
?: normalizedMap["off"]
|
||||||
|
?: normalizedMap["shutdown"]
|
||||||
|
?: throw IllegalArgumentException("Missing required endpoint key: 'poweroff'")
|
||||||
|
|
||||||
|
val reboot = normalizedMap["reboot"]
|
||||||
|
?: normalizedMap["restart"]
|
||||||
|
?: normalizedMap["reset"]
|
||||||
|
?: throw IllegalArgumentException("Missing required endpoint key: 'reboot'")
|
||||||
|
|
||||||
|
ParsedEndpoints(
|
||||||
|
powerOn = powerOn,
|
||||||
|
powerOff = powerOff,
|
||||||
|
reboot = reboot
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.expapp.trigger.data.repository
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import com.expapp.trigger.data.model.DeviceConfig
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
|
|
||||||
|
interface KeyValueStorage {
|
||||||
|
fun getString(key: String, defaultValue: String?): String?
|
||||||
|
fun putString(key: String, value: String?)
|
||||||
|
}
|
||||||
|
|
||||||
|
class SharedPrefsStorage(context: Context) : KeyValueStorage {
|
||||||
|
private val prefs: SharedPreferences =
|
||||||
|
context.getSharedPreferences("device_trigger_prefs", Context.MODE_PRIVATE)
|
||||||
|
|
||||||
|
override fun getString(key: String, defaultValue: String?): String? =
|
||||||
|
prefs.getString(key, defaultValue)
|
||||||
|
|
||||||
|
override fun putString(key: String, value: String?) {
|
||||||
|
prefs.edit().putString(key, value).apply()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeviceRepository(private val storage: KeyValueStorage) {
|
||||||
|
private val gson = Gson()
|
||||||
|
private val storageKey = "saved_devices_json"
|
||||||
|
|
||||||
|
fun getDevices(): List<DeviceConfig> {
|
||||||
|
val json = storage.getString(storageKey, null) ?: return emptyList()
|
||||||
|
val type = object : TypeToken<List<DeviceConfig>>() {}.type
|
||||||
|
return try {
|
||||||
|
gson.fromJson<List<DeviceConfig>>(json, type) ?: emptyList()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addDevice(device: DeviceConfig) {
|
||||||
|
val current = getDevices().toMutableList()
|
||||||
|
current.removeAll { it.id == device.id }
|
||||||
|
current.add(device)
|
||||||
|
saveDevices(current)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteDevice(id: String) {
|
||||||
|
val current = getDevices().toMutableList()
|
||||||
|
current.removeAll { it.id == id }
|
||||||
|
saveDevices(current)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateDevice(device: DeviceConfig) {
|
||||||
|
addDevice(device)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveDevices(devices: List<DeviceConfig>) {
|
||||||
|
val json = gson.toJson(devices)
|
||||||
|
storage.putString(storageKey, json)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<resources>
|
||||||
|
<string name="app_name">Device Trigger</string>
|
||||||
|
</resources>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<resources>
|
||||||
|
<style name="Theme.ExpApp" parent="android:Theme.Material.NoActionBar">
|
||||||
|
<item name="android:statusBarColor">#121212</item>
|
||||||
|
<item name="android:navigationBarColor">#121212</item>
|
||||||
|
</style>
|
||||||
|
</resources>
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user