rippled
GetCounts_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <test/jtx.h>
21 #include <ripple/beast/unit_test.h>
22 #include <ripple/protocol/jss.h>
23 #include <ripple/protocol/SField.h>
24 #include <ripple/basics/CountedObject.h>
25 
26 namespace ripple {
27 
28 class GetCounts_test : public beast::unit_test::suite
29 {
31  {
32  using namespace test::jtx;
33  Env env(*this);
34 
35  Json::Value result;
36  {
37  // check counts with no transactions posted
38  result = env.rpc("get_counts")[jss::result];
39  BEAST_EXPECT(result[jss::status] == "success");
40  BEAST_EXPECT(! result.isMember("Transaction"));
41  BEAST_EXPECT(! result.isMember("STObject"));
42  BEAST_EXPECT(! result.isMember("HashRouterEntry"));
43  BEAST_EXPECT(
44  result.isMember(jss::uptime) &&
45  ! result[jss::uptime].asString().empty());
46  BEAST_EXPECT(
47  result.isMember(jss::dbKBTotal) &&
48  result[jss::dbKBTotal].asInt() > 0);
49  }
50 
51  // create some transactions
52  env.close();
53  Account alice {"alice"};
54  Account bob {"bob"};
55  env.fund (XRP(10000), alice, bob);
56  env.trust (alice["USD"](1000), bob);
57  for(auto i=0; i<20; ++i)
58  {
59  env (pay (alice, bob, alice["USD"](5)));
60  env.close();
61  }
62 
63  {
64  // check counts, default params
65  result = env.rpc("get_counts")[jss::result];
66  BEAST_EXPECT(result[jss::status] == "success");
67  // compare with values reported by CountedObjects
68  auto const& objectCounts = CountedObjects::getInstance ().getCounts (10);
69  for (auto const& it : objectCounts)
70  {
71  BEAST_EXPECTS(result.isMember(it.first), it.first);
72  BEAST_EXPECTS(result[it.first].asInt() == it.second, it.first);
73  }
74  BEAST_EXPECT(! result.isMember(jss::local_txs));
75  }
76 
77  {
78  // make request with min threshold 100 and verify
79  // that only STObject and NodeObject are reported
80  result = env.rpc("get_counts", "100")[jss::result];
81  BEAST_EXPECT(result[jss::status] == "success");
82 
83  // compare with values reported by CountedObjects
84  auto const& objectCounts = CountedObjects::getInstance ().getCounts (100);
85  for (auto const& it : objectCounts)
86  {
87  BEAST_EXPECTS(result.isMember(it.first), it.first);
88  BEAST_EXPECTS(result[it.first].asInt() == it.second, it.first);
89  }
90  BEAST_EXPECT(! result.isMember("Transaction"));
91  BEAST_EXPECT(! result.isMember("STTx"));
92  BEAST_EXPECT(! result.isMember("STArray"));
93  BEAST_EXPECT(! result.isMember("HashRouterEntry"));
94  BEAST_EXPECT(! result.isMember("STLedgerEntry"));
95  }
96 
97  {
98  // local_txs field will exist when there are open Txs
99  env (pay (alice, bob, alice["USD"](5)));
100  result = env.rpc("get_counts")[jss::result];
101  // deliberately don't call close so we have open Tx
102  BEAST_EXPECT(
103  result.isMember(jss::local_txs) &&
104  result[jss::local_txs].asInt() > 0);
105  }
106  }
107 
108 public:
109  void run () override
110  {
111  testGetCounts();
112  }
113 };
114 
115 BEAST_DEFINE_TESTSUITE(GetCounts,rpc,ripple);
116 
117 } // ripple
118 
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::GetCounts_test::run
void run() override
Definition: GetCounts_test.cpp:109
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:25
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:961
ripple::GetCounts_test
Definition: GetCounts_test.cpp:28
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:38
std::string::empty
T empty(T... args)
ripple::GetCounts_test::testGetCounts
void testGetCounts()
Definition: GetCounts_test.cpp:30
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:516
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482