Skip to content

Commit

Permalink
test: add backup and verification e2e scenarios (#170)
Browse files Browse the repository at this point in the history
Signed-off-by: Allain Magyar <allain.magyar@iohk.io>
Co-authored-by: Cristian G <cristian.castro@iohk.io>
  • Loading branch information
amagyar-iohk and cristianIOHK authored Sep 11, 2024
1 parent 778cb91 commit 576d0de
Show file tree
Hide file tree
Showing 26 changed files with 788 additions and 345 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
root = true

[*.{kt,kts}]
ij_kotlin_packages_to_use_import_on_demand = unset
ij_kotlin_name_count_to_use_star_import_for_members = 99
ij_kotlin_name_count_to_use_star_import = 99
ktlint_experimental = disabled
ktlint_code_style = intellij_idea
ktlint_standard_no_semi = disabled
Expand Down
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@goncalo-frade-iohk @yshyn-iohk @elribonazo @cristianIOHK
@elribonazo @cristianIOHK

# Test related:
/tests/ @amagyar-iohk
Expand Down
8 changes: 1 addition & 7 deletions edge-agent-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ kotlin {
jvmTarget = "17"
}
}
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
publishing {
publications {
withType<MavenPublication> {
Expand Down Expand Up @@ -145,10 +142,9 @@ kotlin {
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
implementation("io.ktor:ktor-client-mock:2.3.11")
implementation("junit:junit:4.13.2")
implementation("org.mockito:mockito-core:4.4.0")
implementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
}
Expand All @@ -172,10 +168,8 @@ kotlin {
val androidInstrumentedTest by getting {
dependencies {
dependsOn(commonTest)
implementation(kotlin("test"))
implementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("androidx.test.ext:junit:1.1.5")
implementation("junit:junit:4.13.2")
}
}
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ actual class DbConnectionImpl actual constructor() : DbConnection {
*/
actual val SqlDriver.isConnected: Boolean
get() {
try {
return this.execute(null, "SELECT 1", 0).value == 1L
return try {
this.execute(null, "SELECT 1", 0).value == 0L
// return this.executeQuery(null, "SELECT 1", 0).next()
} catch (ex: Exception) {
return false
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.hyperledger.identus.apollo.base64.base64UrlDecoded
import org.hyperledger.identus.walletsdk.domain.buildingblocks.Castor
Expand All @@ -25,7 +26,6 @@ import org.hyperledger.identus.walletsdk.edgeagent.protocols.ProtocolType
import org.hyperledger.identus.walletsdk.edgeagent.protocols.issueCredential.IssueCredential
import org.hyperledger.identus.walletsdk.edgeagent.protocols.revocation.RevocationNotification
import java.time.Duration
import kotlin.jvm.Throws

interface ConnectionManager : ConnectionsManager, DIDCommConnection {

Expand Down Expand Up @@ -106,10 +106,9 @@ class ConnectionManagerImpl(
}
}
}

// Fallback mechanism if no WebSocket service endpoint is available
if (serviceEndpoint == null) {
while (true) {
while (this.isActive) {
// Continuously await and process new messages
awaitMessages().collect { array ->
processMessages(array)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.hyperledger.identus.walletsdk.edgeagent

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.hyperledger.identus.apollo.base64.base64UrlEncoded
import org.hyperledger.identus.walletsdk.domain.buildingblocks.Castor
Expand Down Expand Up @@ -54,7 +54,8 @@ class ConnectionManagerTest {

lateinit var connectionManager: ConnectionManagerImpl

val testDispatcher = TestCoroutineDispatcher()
@OptIn(ExperimentalCoroutinesApi::class)
val testDispatcher = UnconfinedTestDispatcher()

@BeforeTest
fun setup() {
Expand Down Expand Up @@ -118,7 +119,7 @@ class ConnectionManagerTest {
`when`(castorMock.resolveDID(any())).thenReturn(didDoc)

connectionManager.startFetchingMessages()
assertNotNull((connectionManager as ConnectionManagerImpl).fetchingMessagesJob)
assertNotNull(connectionManager.fetchingMessagesJob)
verify(basicMediatorHandlerMock).listenUnreadMessages(any(), any())
}

Expand Down Expand Up @@ -220,91 +221,91 @@ class ConnectionManagerTest {
}

@Test
fun testStartFetchingMessages_whenServiceEndpointNotContainsWSS_thenUseAPIRequest() =
runBlockingTest {
`when`(basicMediatorHandlerMock.mediatorDID)
.thenReturn(DID("did:prism:b6c0c33d701ac1b9a262a14454d1bbde3d127d697a76950963c5fd930605:Cj8KPRI7CgdtYXN0ZXIwEAFKLgoJc2VmsxEiECSTjyV7sUfCr_ArpN9rvCwR9fRMAhcsr_S7ZRiJk4p5k"))

val vmAuthentication = DIDDocument.VerificationMethod(
id = DIDUrl(DID("2", "1", "0")),
controller = DID("2", "2", "0"),
type = Curve.ED25519.value,
publicKeyJwk = mapOf("crv" to Curve.ED25519.value, "x" to "")
)
fun testStartFetchingMessages_whenServiceEndpointNotContainsWSS_thenUseAPIRequest() = runTest {
`when`(basicMediatorHandlerMock.mediatorDID)
.thenReturn(DID("did:prism:b6c0c33d701ac1b9a262a14454d1bbde3d127d697a76950963c5fd930605:Cj8KPRI7CgdtYXN0ZXIwEAFKLgoJc2VmsxEiECSTjyV7sUfCr_ArpN9rvCwR9fRMAhcsr_S7ZRiJk4p5k"))

val vmKeyAgreement = DIDDocument.VerificationMethod(
id = DIDUrl(DID("3", "1", "0")),
controller = DID("3", "2", "0"),
type = Curve.X25519.value,
publicKeyJwk = mapOf("crv" to Curve.X25519.value, "x" to "")
)
val vmAuthentication = DIDDocument.VerificationMethod(
id = DIDUrl(DID("2", "1", "0")),
controller = DID("2", "2", "0"),
type = Curve.ED25519.value,
publicKeyJwk = mapOf("crv" to Curve.ED25519.value, "x" to "")
)

val vmService = DIDDocument.Service(
id = UUID.randomUUID().toString(),
type = emptyArray(),
serviceEndpoint = DIDDocument.ServiceEndpoint(
uri = "https://serviceEndpoint"
)
val vmKeyAgreement = DIDDocument.VerificationMethod(
id = DIDUrl(DID("3", "1", "0")),
controller = DID("3", "2", "0"),
type = Curve.X25519.value,
publicKeyJwk = mapOf("crv" to Curve.X25519.value, "x" to "")
)

val vmService = DIDDocument.Service(
id = UUID.randomUUID().toString(),
type = emptyArray(),
serviceEndpoint = DIDDocument.ServiceEndpoint(
uri = "https://serviceEndpoint"
)
)

val didDoc = DIDDocument(
id = DID("did:prism:asdfasdf"),
coreProperties = arrayOf(
DIDDocument.Authentication(
urls = emptyArray(),
verificationMethods = arrayOf(vmAuthentication)
),
DIDDocument.KeyAgreement(
urls = emptyArray(),
verificationMethods = arrayOf(vmKeyAgreement)
),
DIDDocument.Services(
values = arrayOf(vmService)
)
val didDoc = DIDDocument(
id = DID("did:prism:asdfasdf"),
coreProperties = arrayOf(
DIDDocument.Authentication(
urls = emptyArray(),
verificationMethods = arrayOf(vmAuthentication)
),
DIDDocument.KeyAgreement(
urls = emptyArray(),
verificationMethods = arrayOf(vmKeyAgreement)
),
DIDDocument.Services(
values = arrayOf(vmService)
)
)
)

`when`(castorMock.resolveDID(any())).thenReturn(didDoc)
val messages = arrayOf(Pair("1234", Message(piuri = "", body = "")))
`when`(basicMediatorHandlerMock.pickupUnreadMessages(any())).thenReturn(
flow {
emit(
messages
)
}
)
val attachments: Array<AttachmentDescriptor> =
arrayOf(
AttachmentDescriptor(
mediaType = "application/json",
format = CredentialType.JWT.type,
data = AttachmentBase64(base64 = "asdfasdfasdfasdfasdfasdfasdfasdfasdf".base64UrlEncoded)
)
`when`(castorMock.resolveDID(any())).thenReturn(didDoc)
val messages = arrayOf(Pair("1234", Message(piuri = "", body = "")))
`when`(basicMediatorHandlerMock.pickupUnreadMessages(any())).thenReturn(
flow {
emit(
messages
)
val listMessages = listOf(
Message(
piuri = ProtocolType.DidcommconnectionRequest.value,
body = ""
),
Message(
piuri = ProtocolType.DidcommIssueCredential.value,
thid = UUID.randomUUID().toString(),
from = DID("did:peer:asdf897a6sdf"),
to = DID("did:peer:f706sg678ha"),
attachments = attachments,
body = """{}"""
}
)
val attachments: Array<AttachmentDescriptor> =
arrayOf(
AttachmentDescriptor(
mediaType = "application/json",
format = CredentialType.JWT.type,
data = AttachmentBase64(base64 = "asdfasdfasdfasdfasdfasdfasdfasdfasdf".base64UrlEncoded)
)
)
val messageList: Flow<List<Message>> = flow {
emit(listMessages)
}
`when`(plutoMock.getAllMessages()).thenReturn(messageList)

connectionManager.startFetchingMessages()
assertNotNull((connectionManager as ConnectionManagerImpl).fetchingMessagesJob)
verify(basicMediatorHandlerMock).pickupUnreadMessages(10)
verify(basicMediatorHandlerMock).registerMessagesAsRead(arrayOf("1234"))
val listMessages = listOf(
Message(
piuri = ProtocolType.DidcommconnectionRequest.value,
body = ""
),
Message(
piuri = ProtocolType.DidcommIssueCredential.value,
thid = UUID.randomUUID().toString(),
from = DID("did:peer:asdf897a6sdf"),
to = DID("did:peer:f706sg678ha"),
attachments = attachments,
body = """{}"""
)
)
val messageList: Flow<List<Message>> = flow {
emit(listMessages)
}
`when`(plutoMock.getAllMessages()).thenReturn(messageList)

connectionManager.startFetchingMessages()
assertNotNull(connectionManager.fetchingMessagesJob)
assert(connectionManager.fetchingMessagesJob?.isActive == true)
verify(basicMediatorHandlerMock).pickupUnreadMessages(10)
verify(basicMediatorHandlerMock).registerMessagesAsRead(arrayOf("1234"))
}

@Test
fun testConnectionManager_whenProcessMessageRevoke_thenAllCorrect() = runTest {
Expand Down
Loading

0 comments on commit 576d0de

Please sign in to comment.