From 676673d3dfcabadd42beb1df4216c410e8c18b1a Mon Sep 17 00:00:00 2001 From: ligi Date: Mon, 18 Jul 2022 00:19:58 +0200 Subject: [PATCH] Add test for parsing session update to validate PR #76 --- .../impls/MoshiPayloadAdapter.kt | 2 +- .../java/org/walletconnect/TheWCSession.kt | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/src/test/java/org/walletconnect/TheWCSession.kt diff --git a/lib/src/main/kotlin/org/walletconnect/impls/MoshiPayloadAdapter.kt b/lib/src/main/kotlin/org/walletconnect/impls/MoshiPayloadAdapter.kt index 06a4a14..816b1f1 100644 --- a/lib/src/main/kotlin/org/walletconnect/impls/MoshiPayloadAdapter.kt +++ b/lib/src/main/kotlin/org/walletconnect/impls/MoshiPayloadAdapter.kt @@ -93,7 +93,7 @@ class MoshiPayloadAdapter(moshi: Moshi) : Session.PayloadAdapter { /** * Convert FROM request bytes */ - private fun ByteArray.toMethodCall(): Session.MethodCall = + fun ByteArray.toMethodCall(): Session.MethodCall = String(this).let { json -> mapAdapter.fromJson(json)?.let { try { diff --git a/lib/src/test/java/org/walletconnect/TheWCSession.kt b/lib/src/test/java/org/walletconnect/TheWCSession.kt new file mode 100644 index 0000000..83c9a97 --- /dev/null +++ b/lib/src/test/java/org/walletconnect/TheWCSession.kt @@ -0,0 +1,39 @@ +package org.walletconnect + +import com.squareup.moshi.Moshi +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.walletconnect.impls.MoshiPayloadAdapter + + +class TheWCSession { + + @Test + fun canParseSessionUpdate() { + val adapter = MoshiPayloadAdapter(Moshi.Builder().build()) + with(adapter) { + val json = """{ + |"id":42, + |"result":{}, + |"method":"wc_sessionUpdate", + |"params":[{ + | "approved":true, + | "chainId":420, + | "accounts":["0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045","0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5"] + |}] + |}""".trimMargin() + val tested = json.toByteArray().toMethodCall() + assertThat(tested).isInstanceOf(Session.MethodCall.SessionUpdate::class.java) + assertThat(tested.id()).isEqualTo(42) + val testedAndTyped = tested as Session.MethodCall.SessionUpdate + + assertThat(testedAndTyped.params.approved).isTrue + assertThat(testedAndTyped.params.accounts).isEqualTo( + listOf("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", "0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5") + ) + assertThat(testedAndTyped.params.chainId).isEqualTo(420) + } + + } +} +