|
| 1 | +package jp.co.soramitsu.runtime.integration |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import org.junit.Assume.assumeTrue |
| 5 | +import org.junit.Test |
| 6 | +import java.io.BufferedReader |
| 7 | +import java.io.OutputStreamWriter |
| 8 | +import java.net.HttpURLConnection |
| 9 | +import java.net.URL |
| 10 | + |
| 11 | +/** |
| 12 | + * Balance integration logger for Polkadot (DOT). |
| 13 | + * This test logs the raw result of `state_getStorage` for a provided System.Account storage key. |
| 14 | + * |
| 15 | + * Usage: |
| 16 | + * - Provide a reachable RPC URL via system property or env var `DOT_RPC_URL`. |
| 17 | + * - Provide a precomputed storage key for System.Account(<SS58 accountId>) via `DOT_ACCOUNT_STORAGE_KEY`. |
| 18 | + * The key is SCALE-encoded and hashed as per substrate (pallet: "System", storage: "Account"). |
| 19 | + * - Example run: |
| 20 | + * ./gradlew \ |
| 21 | + * -Pandroid.testInstrumentationRunnerArguments.DOT_RPC_URL=https://polkadot-rpc.publicnode.com \ |
| 22 | + * -Pandroid.testInstrumentationRunnerArguments.DOT_ACCOUNT_STORAGE_KEY=0x26aa394eea5630e07c48ae0c9558cef702a5... |
| 23 | + * :runtime:connectedDebugAndroidTest |
| 24 | + */ |
| 25 | +class DotBalanceIntegrationTest { |
| 26 | + |
| 27 | + private fun hasNetwork(): Boolean = try { |
| 28 | + val url = URL("https://www.google.com/generate_204") |
| 29 | + (url.openConnection() as HttpURLConnection).run { |
| 30 | + connectTimeout = 2000 |
| 31 | + readTimeout = 2000 |
| 32 | + requestMethod = "GET" |
| 33 | + connect() |
| 34 | + val ok = responseCode in 200..399 |
| 35 | + disconnect() |
| 36 | + ok |
| 37 | + } |
| 38 | + } catch (_: Exception) { false } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun testPolkadotAccountStorage_logsResponseOrSkips() { |
| 42 | + assumeTrue("No network; skipping", hasNetwork()) |
| 43 | + |
| 44 | + val rpcUrl = System.getProperty("DOT_RPC_URL") |
| 45 | + ?: System.getenv("DOT_RPC_URL") |
| 46 | + ?: "https://polkadot-rpc.publicnode.com" |
| 47 | + |
| 48 | + val storageKey = System.getProperty("DOT_ACCOUNT_STORAGE_KEY") ?: System.getenv("DOT_ACCOUNT_STORAGE_KEY") |
| 49 | + assumeTrue("No DOT_ACCOUNT_STORAGE_KEY provided; skipping", !storageKey.isNullOrBlank()) |
| 50 | + |
| 51 | + val payload = """ |
| 52 | + {"jsonrpc":"2.0","method":"state_getStorage","params":["$storageKey"],"id":2} |
| 53 | + """.trimIndent() |
| 54 | + |
| 55 | + val response = httpPost(rpcUrl, payload) |
| 56 | + Log.i("DotBalanceIntegrationTest", "RPC URL: $rpcUrl") |
| 57 | + Log.i("DotBalanceIntegrationTest", "state_getStorage(Account) => $response") |
| 58 | + |
| 59 | + // Basic sanity: expect non-empty hex or null |
| 60 | + assumeTrue("Unexpected storage response", response.contains("result")) |
| 61 | + } |
| 62 | + |
| 63 | + private fun httpPost(urlStr: String, body: String): String { |
| 64 | + val url = URL(urlStr) |
| 65 | + val conn = (url.openConnection() as HttpURLConnection).apply { |
| 66 | + requestMethod = "POST" |
| 67 | + connectTimeout = 5000 |
| 68 | + readTimeout = 10000 |
| 69 | + doOutput = true |
| 70 | + setRequestProperty("Content-Type", "application/json") |
| 71 | + } |
| 72 | + |
| 73 | + OutputStreamWriter(conn.outputStream, Charsets.UTF_8).use { it.write(body) } |
| 74 | + |
| 75 | + val code = conn.responseCode |
| 76 | + val stream = if (code in 200..299) conn.inputStream else conn.errorStream |
| 77 | + val text = BufferedReader(stream.reader(Charsets.UTF_8)).use { it.readText() } |
| 78 | + conn.disconnect() |
| 79 | + return text |
| 80 | + } |
| 81 | +} |
| 82 | + |
0 commit comments