From a065e1db76de9b517566de01de3edee892722e58 Mon Sep 17 00:00:00 2001 From: Ugo Amanoh Date: Tue, 17 Dec 2019 11:48:30 +0100 Subject: [PATCH 1/5] feat: adding the functionality to search for a network using it's name --- .../main/java/me/uport/sdk/core/Networks.kt | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/me/uport/sdk/core/Networks.kt b/core/src/main/java/me/uport/sdk/core/Networks.kt index 1873faf..b4d067c 100644 --- a/core/src/main/java/me/uport/sdk/core/Networks.kt +++ b/core/src/main/java/me/uport/sdk/core/Networks.kt @@ -121,13 +121,26 @@ object Networks { } /** - * Gets an [EthNetwork] based on a [networkId] + * Gets an [EthNetwork] based on a [networkId] or [name] */ - fun get(networkId: String): EthNetwork { - val cleanNetId = cleanId(networkId) + fun get(nameOrId: String): EthNetwork { + val cleanNetId = cleanId(nameOrId) return NETWORK_CONFIG[cleanNetId] - ?: NETWORK_CONFIG[networkId] - ?: throw IllegalStateException("network [$networkId] not configured") + ?: NETWORK_CONFIG[nameOrId] + ?: getNetworkByName(nameOrId) + ?: throw IllegalStateException("network [$nameOrId] not configured") + } + + /** + * Searches for an [EthNetwork] based on a [name] + */ + private fun getNetworkByName(name: String): EthNetwork? { + for ((_, value) in NETWORK_CONFIG) { + if (value.name === name) { + return value + } + } + return null } private fun cleanId(id: String) = id.clean0xPrefix().trimStart('0').prepend0xPrefix() From c15b516dd403df4372cc8b598a50ceac58f2e752 Mon Sep 17 00:00:00 2001 From: Ugo Amanoh Date: Tue, 17 Dec 2019 11:55:49 +0100 Subject: [PATCH 2/5] test: added tests for finding a network using its name --- .../java/me/uport/sdk/core/NetworksTest.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/core/src/test/java/me/uport/sdk/core/NetworksTest.kt b/core/src/test/java/me/uport/sdk/core/NetworksTest.kt index a75e79a..f2c7b02 100644 --- a/core/src/test/java/me/uport/sdk/core/NetworksTest.kt +++ b/core/src/test/java/me/uport/sdk/core/NetworksTest.kt @@ -39,6 +39,36 @@ class NetworksTest { .isEqualTo(Networks.mainnet.ethrDidRegistry) } + @Test + fun `can find a registered network using the name`() { + + assertThat(Networks.get("mainnet")).isEqualTo(Networks.mainnet) + + assertThat(Networks.get("rinkeby")).isEqualTo(Networks.rinkeby) + + assertThat(Networks.get("kovan")).isEqualTo(Networks.kovan) + + assertThat(Networks.get("ropsten")).isEqualTo(Networks.ropsten) + } + + @Test + fun `throws error if the network name does not match`() { + assertThat { + Networks.get("manet") + }.thrownError { + isInstanceOf(IllegalStateException::class) + } + } + + @Test + fun `throws error if the network name cases does not match`() { + assertThat { + Networks.get("Mainnet") + }.thrownError { + isInstanceOf(IllegalStateException::class) + } + } + @Test fun `getting an unknown network throws`() { assertThat { From 0388ef40af4ec24b177b3a3b1aeed523506c0cc0 Mon Sep 17 00:00:00 2001 From: Ugo Amanoh Date: Tue, 17 Dec 2019 13:21:36 +0100 Subject: [PATCH 3/5] refactor: remove case-sensitivity during network name search --- core/src/main/java/me/uport/sdk/core/Networks.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/me/uport/sdk/core/Networks.kt b/core/src/main/java/me/uport/sdk/core/Networks.kt index b4d067c..a2f2c2d 100644 --- a/core/src/main/java/me/uport/sdk/core/Networks.kt +++ b/core/src/main/java/me/uport/sdk/core/Networks.kt @@ -136,7 +136,7 @@ object Networks { */ private fun getNetworkByName(name: String): EthNetwork? { for ((_, value) in NETWORK_CONFIG) { - if (value.name === name) { + if (value.name.toLowerCase() === name.toLowerCase()) { return value } } From 7ff682dcb585abb3e37a61a80c352f30e6f5120a Mon Sep 17 00:00:00 2001 From: Ugo Amanoh Date: Tue, 17 Dec 2019 13:28:13 +0100 Subject: [PATCH 4/5] test: added test to show network name case-insensitive search --- core/src/main/java/me/uport/sdk/core/Networks.kt | 2 +- .../test/java/me/uport/sdk/core/NetworksTest.kt | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/me/uport/sdk/core/Networks.kt b/core/src/main/java/me/uport/sdk/core/Networks.kt index a2f2c2d..78b2b3b 100644 --- a/core/src/main/java/me/uport/sdk/core/Networks.kt +++ b/core/src/main/java/me/uport/sdk/core/Networks.kt @@ -136,7 +136,7 @@ object Networks { */ private fun getNetworkByName(name: String): EthNetwork? { for ((_, value) in NETWORK_CONFIG) { - if (value.name.toLowerCase() === name.toLowerCase()) { + if (value.name.toLowerCase() == name.toLowerCase()) { return value } } diff --git a/core/src/test/java/me/uport/sdk/core/NetworksTest.kt b/core/src/test/java/me/uport/sdk/core/NetworksTest.kt index f2c7b02..ba9307d 100644 --- a/core/src/test/java/me/uport/sdk/core/NetworksTest.kt +++ b/core/src/test/java/me/uport/sdk/core/NetworksTest.kt @@ -52,18 +52,14 @@ class NetworksTest { } @Test - fun `throws error if the network name does not match`() { - assertThat { - Networks.get("manet") - }.thrownError { - isInstanceOf(IllegalStateException::class) - } + fun `can find a registered network using the different case name`() { + assertThat(Networks.get("MainNet")).isEqualTo(Networks.mainnet) } - @Test - fun `throws error if the network name cases does not match`() { + @Test + fun `throws error if the network name does not match`() { assertThat { - Networks.get("Mainnet") + Networks.get("manet") }.thrownError { isInstanceOf(IllegalStateException::class) } From 36c4831bd7553c7601673545f946d6e38929028d Mon Sep 17 00:00:00 2001 From: Ugo Amanoh Date: Tue, 17 Dec 2019 13:57:02 +0100 Subject: [PATCH 5/5] refactor: use find as opposed to forEach to search a map --- core/src/main/java/me/uport/sdk/core/Networks.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/me/uport/sdk/core/Networks.kt b/core/src/main/java/me/uport/sdk/core/Networks.kt index 78b2b3b..1fa9877 100644 --- a/core/src/main/java/me/uport/sdk/core/Networks.kt +++ b/core/src/main/java/me/uport/sdk/core/Networks.kt @@ -135,12 +135,8 @@ object Networks { * Searches for an [EthNetwork] based on a [name] */ private fun getNetworkByName(name: String): EthNetwork? { - for ((_, value) in NETWORK_CONFIG) { - if (value.name.toLowerCase() == name.toLowerCase()) { - return value - } - } - return null + val queryName = name.toLowerCase() + return NETWORK_CONFIG.values.find { it.name.toLowerCase() == queryName } } private fun cleanId(id: String) = id.clean0xPrefix().trimStart('0').prepend0xPrefix()