Skip to content

Commit

Permalink
Payload interface moved into model package
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 6, 2024
1 parent c26ee7b commit 1a1e28a
Show file tree
Hide file tree
Showing 40 changed files with 209 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import network.xyo.client.node.client.DiscoverPayload
import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository
import network.xyo.client.lib.JsonSerializable
import network.xyo.client.node.client.NodeClient
import network.xyo.client.payload.XyoPayload
import network.xyo.client.payload.Payload
import org.json.JSONObject
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.*

data class RequestDependencies(val client: NodeClient, val query: XyoPayload, val payloads: List<XyoPayload>)
data class RequestDependencies(val client: NodeClient, val query: Payload, val payloads: List<Payload>)

class XyoBoundWitnessTest {
class BoundWitnessTest {

val apiDomainBeta = "https://beta.api.archivist.xyo.network"
val apiDomainLocal = "http://10.0.2.2:8080"
Expand All @@ -47,7 +47,7 @@ class XyoBoundWitnessTest {
val account = Account.random()
val client = NodeClient(nodeUrl, account, appContext)
val query = DiscoverPayload()
val payloads = mutableListOf<XyoPayload>()
val payloads = mutableListOf<Payload>()
payloads.add(TestPayload1())
return RequestDependencies(client, query, payloads)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import network.xyo.client.payload.Payload

class IdPayload(val salt: String): Payload {
class IdPayload(val salt: String): Payload("network.xyo.id") {
constructor(salt: UInt) : this("${salt}")
override var schema = "network.xyo.id"
}

data class BoundWitnessSequenceTestCase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class TestPayload1SubObject {
var string_value = "yo"
}

class TestPayload1: Payload {
override var schema ="network.xyo.test"
class TestPayload1: Payload("network.xyo.test") {
var timestamp = 1_618_603_439_107
var number_field = 1
var object_field = TestPayload1SubObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class TestPayload2SubObject {
var optional_field: String? = null
}

class TestPayload2: Payload {
override var schema ="network.xyo.test"
class TestPayload2: Payload("network.xyo.test") {
var timestamp = 1_618_603_439_107
var object_field = TestPayload2SubObject()
var string_field = "there"
Expand Down
18 changes: 9 additions & 9 deletions sdk/src/androidTest/java/network/xyo/client/lib/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package network.xyo.client.lib

import network.xyo.client.account.Account
import network.xyo.client.payload.XyoPayload
import network.xyo.client.payload.Payload

class DebugPayload(val nonce: Int) : XyoPayload() {
override var schema: String
get() = "network.xyo.debug"
set(value) = Unit
class DebugPayload(val nonce: Int) : Payload(SCHEMA) {
companion object {
const val SCHEMA = "network.xyo.debug"
}
}

class BasicPayload : XyoPayload() {
override var schema: String
get() = "network.xyo.basic"
set(value) = Unit
class BasicPayload : Payload(SCHEMA) {
companion object {
const val SCHEMA = "network.xyo.basic"
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import kotlinx.coroutines.runBlocking
import network.xyo.client.lib.TestConstants
import network.xyo.client.account.Account
import network.xyo.client.archivist.wrapper.ArchivistWrapper
import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson
import network.xyo.client.payload.XyoPayload
import network.xyo.client.boundwitness.BoundWitnessBodyJson
import network.xyo.client.payload.Payload
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.*

class DiscoverPayload : XyoPayload() {
override var schema = "network.xyo.query.module.discover"
class DiscoverPayload : Payload(SCHEMA) {
companion object {
const val SCHEMA = "network.xyo.query.module.discover"
}
}

class NodeClientTest {
Expand Down Expand Up @@ -69,7 +71,7 @@ class NodeClientTest {
assertEquals(errors, null)

if (response != null) {
assertEquals(response.bw?.schema, XyoBoundWitnessBodyJson.schema)
assertEquals(response.bw?.schema, BoundWitnessBodyJson.SCHEMA)
} else {
throw(Error("Response should not be null"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package network.xyo.client.payload

import com.squareup.moshi.JsonClass
import java.util.*

@JsonClass(generateAdapter = true)
open class EventPayload(val event: String): Payload(SCHEMA) {
val timestamp = Date().time

companion object {
const val SCHEMA = "network.xyo.event"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.*

open class TestSubjectPayload: XyoPayload() {
override var schema = "network.xyo.test"
open class TestSubjectPayload: Payload("network.xyo.test") {

}

class TestPayload1SubObject {
Expand Down Expand Up @@ -55,7 +55,7 @@ class TestInvalidSchemaPayload: TestSubjectPayload() {
val knownAddress = Account.fromPrivateKey(ByteArray(32) {index -> index.toByte()})
const val knownHash = "6e173bbfc0577ebde66b44b090316eca5ecad8ecdb5c51886211d805c769d2ea"

class XyoPayloadTest {
class PayloadTest {

@Rule
@JvmField
Expand Down Expand Up @@ -96,7 +96,7 @@ class XyoPayloadTest {
@Test
fun testRoundTripPanel() {
val address = Account.fromPrivateKey("5a95531488b4d0d3645aea49678297ae9e2034879ce0389b80eb788e8b533592")
val witness = XyoWitness(address, fun(_: Context): List<XyoPayload> {
val witness = XyoWitness(address, fun(_: Context): List<Payload> {
return listOf(BasicPayload())
})

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import network.xyo.client.payload.XyoEventPayload
import network.xyo.client.payload.EventPayload
import network.xyo.client.witness.XyoPanel
import network.xyo.client.account.model.AccountInstance
import org.junit.Before
Expand Down Expand Up @@ -69,8 +69,8 @@ class XyoSdkTest {
val instance = XyoSdk.getInstance(appContext)

val testAccount = instance.getAccount(appContext)
val panel = XyoPanel(appContext, testAccount, fun(_:Context): List<XyoEventPayload> {
return listOf(XyoEventPayload("test_event"))
val panel = XyoPanel(appContext, testAccount, fun(_:Context): List<EventPayload> {
return listOf(EventPayload("test_event"))
})
val result = panel.reportAsyncQuery()
val bw = result.bw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import kotlinx.coroutines.runBlocking
import network.xyo.client.account.Account
import network.xyo.client.lib.TestConstants
import network.xyo.client.witness.location.info.LocationActivity
import network.xyo.client.witness.location.info.XyoLocationPayload
import network.xyo.client.witness.location.info.XyoLocationPayloadRaw
import network.xyo.client.witness.location.info.LocationPayload
import network.xyo.client.witness.location.info.LocationPayloadRaw
import network.xyo.client.witness.location.info.XyoLocationWitness
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -43,15 +43,15 @@ class LocationWitnessTest {
val witness = XyoLocationWitness()
val locationPayload = witness.observe(appContext)?.first()

assertInstanceOf<XyoLocationPayload>(locationPayload)
assert(locationPayload.schema == XyoLocationPayload.schema)
assertInstanceOf<LocationPayload>(locationPayload)
assert(locationPayload.schema == LocationPayload.SCHEMA)
assert(locationPayload.currentLocation !== null)
assert(locationPayload.currentLocation?.coords?.latitude !== null)
assert(locationPayload.currentLocation?.coords?.longitude !== null)

val locationRawPayload = witness.observe(appContext)?.get(1)
assertInstanceOf<XyoLocationPayloadRaw>(locationRawPayload)
assert(locationRawPayload.schema == XyoLocationPayloadRaw.schema)
assertInstanceOf<LocationPayloadRaw>(locationRawPayload)
assert(locationRawPayload.schema == LocationPayloadRaw.SCHEMA)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import network.xyo.client.witness.system.info.XyoSystemInfoPayload
import network.xyo.client.witness.system.info.SystemInfoPayload
import network.xyo.client.witness.system.info.XyoSystemInfoWitness
import org.junit.Test
import org.junit.jupiter.api.assertInstanceOf
Expand All @@ -20,7 +20,7 @@ class SystemInfoWitnessTest {
val witness = XyoSystemInfoWitness()
val payload = witness.observe(context)?.first()

assertInstanceOf<XyoSystemInfoPayload>(payload)
assertInstanceOf<SystemInfoPayload>(payload)
assert(payload.os != null)
assert(payload.device != null)
assert(payload.network != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import androidx.test.rule.GrantPermissionRule
import kotlinx.coroutines.runBlocking
import network.xyo.client.lib.TestConstants
import network.xyo.client.account.Account
import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.boundwitness.BoundWitnessBodyJson
import network.xyo.client.boundwitness.BoundWitnessJson
import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository
import network.xyo.client.witness.types.WitnessResult
import network.xyo.client.payload.XyoPayload
import network.xyo.client.payload.Payload
import network.xyo.client.witness.location.info.LocationActivity
import network.xyo.client.witness.location.info.XyoLocationPayload
import network.xyo.client.witness.location.info.XyoLocationPayloadRaw
import network.xyo.client.witness.location.info.LocationPayload
import network.xyo.client.witness.location.info.LocationPayloadRaw
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -49,28 +49,28 @@ class WitnessLocationHandlerTest {
@Test
fun testObserve() {
runBlocking {
var firstBw: XyoBoundWitnessJson? = null
var firstBw: BoundWitnessJson? = null
val result1 = WitnessLocationHandler().witness(appContext.applicationContext, arrayListOf(Pair(apiDomainBeta, null)))
when (result1) {
is WitnessResult.Success<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> -> {
is WitnessResult.Success<Triple<BoundWitnessJson?, Payload?, Payload?>> -> {
firstBw = result1.data.first
assertInstanceOf<XyoBoundWitnessBodyJson>(firstBw)
assertInstanceOf<XyoLocationPayload>(result1.data.second)
assertInstanceOf<XyoLocationPayloadRaw>(result1.data.third)
assertInstanceOf<BoundWitnessBodyJson>(firstBw)
assertInstanceOf<LocationPayload>(result1.data.second)
assertInstanceOf<LocationPayloadRaw>(result1.data.third)
}
is WitnessResult.Error -> {
assert(result1.exception.size > 0)
}
}

var secondBw: XyoBoundWitnessJson? = null
var secondBw: BoundWitnessJson? = null
val result2 = WitnessLocationHandler().witness(appContext.applicationContext, arrayListOf(Pair(apiDomainBeta, null)))
when (result2) {
is WitnessResult.Success<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> -> {
is WitnessResult.Success<Triple<BoundWitnessJson?, Payload?, Payload?>> -> {
secondBw = result2.data.first
assertInstanceOf<XyoBoundWitnessJson>(secondBw)
assertInstanceOf<XyoLocationPayload>(result2.data.second)
assertInstanceOf<XyoLocationPayloadRaw>(result2.data.third)
assertInstanceOf<BoundWitnessJson>(secondBw)
assertInstanceOf<LocationPayload>(result2.data.second)
assertInstanceOf<LocationPayloadRaw>(result2.data.third)

}
is WitnessResult.Error -> {
Expand Down
20 changes: 10 additions & 10 deletions sdk/src/androidTest/java/network/xyo/client/witness/XyoPanelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import network.xyo.client.lib.BasicPayload
import network.xyo.client.lib.TestConstants
import network.xyo.client.payload.XyoEventPayload
import network.xyo.client.payload.EventPayload
import network.xyo.client.account.Account
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.boundwitness.BoundWitnessJson
import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository
import network.xyo.client.payload.XyoPayload
import network.xyo.client.payload.Payload
import network.xyo.client.witness.location.info.LocationActivity
import network.xyo.client.witness.location.info.XyoLocationWitness
import network.xyo.client.witness.system.info.XyoSystemInfoPayload
import network.xyo.client.witness.system.info.SystemInfoPayload
import network.xyo.client.witness.system.info.XyoSystemInfoWitness
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -54,7 +54,7 @@ class XyoPanelTest {
}

private fun testCreatePanel(nodeUrl: String) {
val witness = XyoWitness<XyoPayload>(Account.random())
val witness = XyoWitness<Payload>(Account.random())
val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair(nodeUrl, Account.random())), listOf(witness))
assertNotNull(panel)
}
Expand All @@ -74,7 +74,7 @@ class XyoPanelTest {
runBlocking {
val witnessAccount = Account.fromPrivateKey("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
val witness2Account = Account.fromPrivateKey("5a95531488b4d0d3645aea49678297ae9e2034879ce0389b80eb788e8b533592")
val witness = XyoWitness(witnessAccount, fun(_: Context): List<XyoPayload> {
val witness = XyoWitness(witnessAccount, fun(_: Context): List<Payload> {
return listOf(BasicPayload())
})
val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair(nodeUrl, Account.random())), listOf(witness, XyoSystemInfoWitness(witness2Account), XyoLocationWitness()))
Expand Down Expand Up @@ -104,8 +104,8 @@ class XyoPanelTest {
fun testSimplePanelReport() {
runBlocking {
val testAccount = Account.random()
val panel = XyoPanel(appContext, testAccount, fun(_:Context): List<XyoEventPayload> {
return listOf(XyoEventPayload("test_event"))
val panel = XyoPanel(appContext, testAccount, fun(_:Context): List<EventPayload> {
return listOf(EventPayload("test_event"))
})
val result = panel.reportAsyncQuery()
if (result.apiResults === null) throw NullPointerException("apiResults should not be null")
Expand Down Expand Up @@ -134,8 +134,8 @@ class XyoPanelTest {
runBlocking {
val panel = XyoPanel(appContext, Account.random(), arrayListOf(), listOf(XyoSystemInfoWitness()))
val results = panel.reportAsyncQuery()
assertInstanceOf<XyoBoundWitnessJson>(results.bw)
assertInstanceOf<XyoSystemInfoPayload>(results.payloads?.first())
assertInstanceOf<BoundWitnessJson>(results.bw)
assertInstanceOf<SystemInfoPayload>(results.payloads?.first())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package network.xyo.client.archivist.wrapper

import network.xyo.client.payload.XyoPayload

open class ArchivistGetQueryPayload(val hashes: List<String>): XyoPayload() {
override var schema = ArchivistGetQueryPayload.schema
import com.squareup.moshi.JsonClass
import network.xyo.client.payload.Payload

@JsonClass(generateAdapter = true)
open class ArchivistGetQueryPayload(val hashes: List<String>): Payload(SCHEMA) {
companion object {
val schema = "network.xyo.query.archivist.get"
const val SCHEMA = "network.xyo.query.archivist.get"
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package network.xyo.client.archivist.wrapper

import network.xyo.client.payload.XyoPayload


class ArchivistInsertQueryPayload : XyoPayload() {
override var schema = ArchivistInsertQueryPayload.schema
import com.squareup.moshi.JsonClass
import network.xyo.client.payload.Payload

@JsonClass(generateAdapter = true)
class ArchivistInsertQueryPayload : Payload(SCHEMA) {
companion object {
val schema = "network.xyo.query.archivist.insert"
const val SCHEMA = "network.xyo.query.archivist.insert"
}
}
Loading

0 comments on commit 1a1e28a

Please sign in to comment.