From 2b46d85ef65db21db68e51730ac8c98d173e4edb Mon Sep 17 00:00:00 2001 From: James Swan <122404367+swan-amazon@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:40:05 +0000 Subject: [PATCH] Updated to use test data from resources --- .../CHIPLedgerDetailsFragment.kt | 55 ++++++++----------- ...nses.json => mock_dcl_responses_json.json} | 0 2 files changed, 22 insertions(+), 33 deletions(-) rename examples/android/CHIPTool/app/src/main/res/raw/{mock_dcl_responses.json => mock_dcl_responses_json.json} (100%) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPLedgerDetailsFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPLedgerDetailsFragment.kt index ab6e0a2c7f4004..0f6fce6e95d85f 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPLedgerDetailsFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPLedgerDetailsFragment.kt @@ -68,45 +68,34 @@ class CHIPLedgerDetailsFragment : Fragment() { .toString() Log.d(TAG, "Dcl request Url: $url") - // Ledger API call - val responseRaw: String = """ - { - "vid": 65521, - "pid": 32769, - "deviceTypeId": 1, - "productName": "Harmony Home", - "productLabel": "Purifier", - "partNumber": "1", - "commissioningCustomFlow": 2, - "commissioningCustomFlowUrl": "https://example.com/matter/65521/32769/setup/1/index.html", - "commissioningModeInitialStepsHint": 0, - "commissioningModeInitialStepsInstruction": "", - "commissioningModeSecondaryStepsHint": 0, - "commissioningModeSecondaryStepsInstruction": "", - "userManualUrl": "https://example.com/matter/65521/32769/manual/1/index.html", - "supportUrl": "https://example.com/matter/65521/32769/support/1/index.html", - "productUrl": "https://example.com/matter/65521/32769/product/1/index.html", - "lsfUrl": "", - "lsfRevision": 0, - "creator": "cosmos1cj3f9jue3s389n2u6492nvyn86apey0x6hx03u", - "schemaVersion": 1, - "enhancedSetupFlowOptions": 1, - "enhancedSetupFlowTCUrl": "https://example.com/matter/65521/32769/tc/1/content.json", - "enhancedSetupFlowTCRevision": 1, - "enhancedSetupFlowTCDigest": "MTZlYjIwYWViYzU2ZWI0Nzc4Zjg0NTRiMmJjMjVlYWFlOTY5MjMzNGRiMzRhMjUxZjMwYmQ1Y2MzYzI1NGI2OA==", - "enhancedSetupFlowTCFileSize": 1024, - "maintenanceUrl": "https://example.com/matter/65521/32769/maintenance/1/index.html" - } - """ + val context = getContext()!! + val mockDclRequestsResponsesRaw = readRawResourceFile(R.raw.mock_dcl_responses_json) + val mockDclRequestsResponses: org.json.JSONArray = org.json.JSONArray(mockDclRequestsResponsesRaw) + + val mockDclRequestsResponseMap = HashMap() + for (i in 0 until mockDclRequestsResponses.length()) { + val mockDclRequestsResponse = mockDclRequestsResponses.getJSONObject(i) + + val mockDclRequest: org.json.JSONObject = mockDclRequestsResponse.getJSONObject("request") + val mockDclResponse: org.json.JSONObject = mockDclRequestsResponse.getJSONObject("response") + + val mockDclRequestURL = mockDclRequest.getString("url") + val mockDclResponseBody = mockDclResponse.getJSONObject("body") + + mockDclRequestsResponseMap.put(mockDclRequestURL, mockDclResponseBody) + Log.d(TAG, "mockDclRequestURL= $mockDclRequestURL") + Log.d(TAG, "mockDclResponseBody= $mockDclResponseBody") + } - val response: org.json.JSONObject = org.json.JSONObject(responseRaw) + Log.d(TAG, "Request url= $url") + val response: org.json.JSONObject = mockDclRequestsResponseMap.get(url.toString())!! Log.d(TAG, "Response from dcl $response") - Toast.makeText(getContext(), "Enhanced Setup Flow: $response", Toast.LENGTH_LONG).show() + Toast.makeText(context, "Enhanced Setup Flow: $response", Toast.LENGTH_LONG).show() val enhancedSetupFlowOptions = response.optInt("enhancedSetupFlowOptions", 0) if (0 != (enhancedSetupFlowOptions and 0b00000001)) { - Toast.makeText(getContext(), "Enhanced Setup Flow supported!", Toast.LENGTH_SHORT).show() + Toast.makeText(context, "Enhanced Setup Flow supported!", Toast.LENGTH_SHORT).show() binding.commissionBtn.setOnClickListener { FragmentUtil.getHost(this@CHIPLedgerDetailsFragment, Callback::class.java) diff --git a/examples/android/CHIPTool/app/src/main/res/raw/mock_dcl_responses.json b/examples/android/CHIPTool/app/src/main/res/raw/mock_dcl_responses_json.json similarity index 100% rename from examples/android/CHIPTool/app/src/main/res/raw/mock_dcl_responses.json rename to examples/android/CHIPTool/app/src/main/res/raw/mock_dcl_responses_json.json