Skip to content

Commit

Permalink
167: Add missing methods in Client. Add new test in ClientIntegration…
Browse files Browse the repository at this point in the history
…Test.

Signed-off-by: Deyan Zhekov <[email protected]>
  • Loading branch information
deyanzz committed Sep 28, 2023
1 parent 8c7659f commit de9ea0d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
8 changes: 8 additions & 0 deletions sdk/main/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ class Client
*/
void close();

/**
* Replace all nodes in this Client with a new set of nodes from the Hedera network.
*
* @param networkMap The map with string representation of node addresses with their corresponding accountId.
* @return A reference to this Client object with the newly-set operator account ID from the map.
*/
Client& setNetwork(const std::unordered_map<std::string, AccountId>& networkMap);

/**
* Set the length of time a request sent by this Client can be processed before it times out.
*
Expand Down
10 changes: 10 additions & 0 deletions sdk/main/src/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Hbar.h"
#include "PrivateKey.h"
#include "PublicKey.h"
#include "exceptions/UninitializedException.h"
#include "impl/MirrorNetwork.h"
#include "impl/Network.h"
#include "impl/ValuePtr.h"
Expand Down Expand Up @@ -171,6 +172,7 @@ Client Client::forNetwork(const std::unordered_map<std::string, AccountId>& netw
{
Client client;
client.mImpl->mNetwork = std::make_shared<internal::Network>(internal::Network::forNetwork(networkMap));
client.mImpl->mMirrorNetwork = nullptr;
return client;
}

Expand Down Expand Up @@ -206,6 +208,14 @@ void Client::close()
}
}

//-----
Client& Client::setNetwork(const std::unordered_map<std::string, AccountId>& networkMap)
{
mImpl->mNetwork = std::make_shared<internal::Network>(internal::Network::forNetwork(networkMap));
mImpl->mMirrorNetwork = nullptr;
return *this;
}

//-----
Client& Client::setRequestTimeout(const std::chrono::duration<double>& timeout)
{
Expand Down
66 changes: 65 additions & 1 deletion sdk/tests/integration/ClientIntegrationTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,41 @@
* limitations under the License.
*
*/
#include "AccountBalance.h"
#include "AccountBalanceQuery.h"
#include "AccountCreateTransaction.h"
#include "AccountId.h"
#include "AccountInfo.h"
#include "AccountInfoQuery.h"
#include "BaseIntegrationTest.h"
#include "Client.h"
#include "ED25519PrivateKey.h"
#include "Hbar.h"
#include "PublicKey.h"
#include "TransactionReceipt.h"
#include "TransactionRecord.h"
#include "TransactionResponse.h"
#include "TransferTransaction.h"
#include "exceptions/UninitializedException.h"
#include "impl/Utilities.h"

#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <string_view>
#include <unordered_map>

using json = nlohmann::json;
using namespace std;
using namespace Hedera;

class ClientIntegrationTest : public ::testing::Test
class ClientIntegrationTest : public BaseIntegrationTest
{
protected:
// [[nodiscard]] inline const Client& getTestClient() const { return mClient; }
[[nodiscard]] inline const std::string_view& getJsonNetworkTag() const { return mJsonNetworkTag; }
[[nodiscard]] inline const std::string_view& getJsonOperatorTag() const { return mJsonOperatorTag; }
[[nodiscard]] inline const std::string_view& getJsonAccountIdTag() const { return mJsonAccountIdTag; }
Expand All @@ -49,6 +62,8 @@ class ClientIntegrationTest : public ::testing::Test
[[nodiscard]] inline const std::string getPathToJSON() const { return mFilePath.string(); }

private:
// Client mClient;

const std::string_view mJsonNetworkTag = "network";
const std::string_view mJsonOperatorTag = "operator";
const std::string_view mJsonAccountIdTag = "accountId";
Expand Down Expand Up @@ -113,3 +128,52 @@ TEST_F(ClientIntegrationTest, ConnectToLocalNode)
EXPECT_NE(client.getOperatorPublicKey(), nullptr);
EXPECT_FALSE(newAccountId.toString().empty());
}

//-----
TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly)
{
// Given
const std::unique_ptr<PrivateKey> myPrivateKey = ED25519PrivateKey::fromString(
"302e020100300506032b6570042204202e000363977258a41f418cf84a7df9cf1e8ae98b72de86803e64846defa43054");
const AccountId accountId_3 = AccountId::fromString("0.0.3");
const AccountId accountId_4 = AccountId::fromString("0.0.4");
const AccountId accountId_5 = AccountId::fromString("0.0.5");

std::unordered_map<std::string, AccountId> testnetMap;
testnetMap.insert(std::pair<std::string, AccountId>("34.94.106.61:50211", accountId_3));
testnetMap.insert(std::pair<std::string, AccountId>("35.237.119.55:50211", accountId_4));

cout << "!!! Got accountId_3 & accountId_4 !!!" << endl << endl;

Client client = Client::forNetwork(testnetMap);

cout << "!!! START !!!" << endl << endl;

// Given
AccountBalance accountBalance_3;
AccountBalance accountBalance_4;
AccountBalance accountBalance_5;

ASSERT_NO_THROW(accountBalance_3 = AccountBalanceQuery().setAccountId(accountId_3).execute(client));
ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client));

cout << "Balance for Account 3: " << accountBalance_3.getBalance().toTinybars() << " tynibars." << endl;
cout << "Balance for Account 4: " << accountBalance_4.getBalance().toTinybars() << " tynibars." << endl << endl;

// When / Then
std::unordered_map<std::string, AccountId> newTestnetMap;
newTestnetMap.insert(std::pair<std::string, AccountId>("35.237.119.55:50211", accountId_4));
newTestnetMap.insert(std::pair<std::string, AccountId>("35.245.27.193:50211", accountId_5));

client.setNetwork(newTestnetMap);

cout << "!!! newTestnetMap was configured !!!" << endl << endl;

ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client));
ASSERT_NO_THROW(accountBalance_5 = AccountBalanceQuery().setAccountId(accountId_5).execute(client));

cout << "Balance for Account 4: " << accountBalance_4.getBalance().toTinybars() << " tynibars." << endl;
cout << "Balance for Account 5: " << accountBalance_5.getBalance().toTinybars() << " tynibars." << endl << endl;

cout << "!!! END !!!" << endl << endl;
}

0 comments on commit de9ea0d

Please sign in to comment.