Skip to content

Commit

Permalink
Add query serializer test:
Browse files Browse the repository at this point in the history
- Refactor query serializer class
  • Loading branch information
grimadas committed Aug 6, 2017
1 parent dd5fa43 commit a1c61bc
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 10 deletions.
23 changes: 13 additions & 10 deletions irohad/ametsuchi/impl/query_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ namespace iroha {
using namespace rapidjson;

QuerySerializer::QuerySerializer() {
deserializers_["get_account"] = &QuerySerializer::deserializeGetAccount;
deserializers_["get_account_assets"] =
deserializers_["GetAccount"] = &QuerySerializer::deserializeGetAccount;
deserializers_["GetAccountAssets"] =
&QuerySerializer::deserializeGetAccountAssets;
deserializers_["get_account_asset_transactions"] =
deserializers_["GetAccountAssetTransactions"] =
&QuerySerializer::deserializeGetAccountAssetTransactions;
deserializers_["get_account_transactions"] =
deserializers_["GetAccountTransactions"] =
&QuerySerializer::deserializeGetAccountTransactions;
deserializers_["get_account_signatories"] =
deserializers_["GetAccountSignatories"] =
&QuerySerializer::deserializeGetSignatories;
}

Expand All @@ -46,15 +46,16 @@ namespace iroha {

// check if all necessary fields are there
auto obj_query = doc.GetObject();
auto req_fields = {"signature", "creator_account_id", "created_ts",
"query_hash", "query_counter", "query_type"};
auto req_fields = {"signature", "creator_account_id", "created_ts",
"query_counter", "query_type"};
if (std::any_of(req_fields.begin(), req_fields.end(),
[&obj_query](auto &&field) {
return not obj_query.HasMember(field);
})) {
return nonstd::nullopt;
}


auto sig = obj_query["signature"].GetObject();

// check if signature has all needed fields
Expand All @@ -80,7 +81,8 @@ namespace iroha {
auto query_type = obj_query["query_type"].GetString();

auto it = deserializers_.find(query_type);
if (it != deserializers_.end() and (this->*it->second)(obj_query, pb_query)) {
if (it != deserializers_.end() and
(this->*it->second)(obj_query, pb_query)) {
return pb_query;
} else {
return nonstd::nullopt;
Expand Down Expand Up @@ -151,6 +153,7 @@ namespace iroha {
// check if all fields exist
if (not(obj_query.HasMember("account_id") &&
obj_query.HasMember("asset_id"))) {

return false;
}
auto pb_get_account_assets = pb_query.mutable_get_account_assets();
Expand All @@ -160,5 +163,5 @@ namespace iroha {

return true;
}
}
}
} // namespace ametsuchi
} // namespace iroha
12 changes: 12 additions & 0 deletions test/module/irohad/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ set(CMAKE_BUILD_TYPE Debug)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/test_bin)

addtest(block_serializer_test block_serializer_test.cpp)
target_link_libraries(block_serializer_test
model
ametsuchi
)

addtest(query_serializer_test query_serializer_test.cpp)
target_link_libraries(query_serializer_test
model
ametsuchi
)

addtest(ametsuchi_test ametsuchi_test.cpp)
target_link_libraries(ametsuchi_test
ametsuchi
Expand Down
104 changes: 104 additions & 0 deletions test/module/irohad/ametsuchi/query_serializer_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ametsuchi/query_serializer.hpp"
#include <gmock/gmock.h>
#include <gtest/gtest.h>

using namespace iroha::ametsuchi;

TEST(QuerySerializerTest, SerializeGetAccountWhenValid) {
QuerySerializer querySerializer;

auto json_query =
"{\"signature\": {\n"
" \"pubkey\": "
"\"2323232323232323232323232323232323232323232323232323232323232323\",\n"
" \"signature\": "
"\"2323232323232323232323232323232323232323232323232323232323232323232323"
"2323232323232323232323232323232323232323232323232323232323\"\n"
" }, \n"
" \"created_ts\": 0,\n"
" \"creator_account_id\": \"123\",\n"
" \"query_counter\": 0,\n"
" \"query_type\": \"GetAccount\",\n"
" \"account_id\": \"test@test\"\n"
" }";
auto res = querySerializer.deserialize(json_query);
ASSERT_TRUE(res.has_value());
ASSERT_TRUE(res.value().has_get_account());
ASSERT_EQ(res.value().get_account().account_id(), "test@test");
}

TEST(QuerySerializerTest, SerializeGetAccountWhenInvalid) {
QuerySerializer querySerializer;
auto json_query =
" {\"created_ts\": 0,\n"
" \"creator_account_id\": \"123\",\n"
" \"query_counter\": 0,\n"
" \"query_type\": \"GetAccount\"\n"
" }";
auto res = querySerializer.deserialize(json_query);
ASSERT_FALSE(res.has_value());
}


TEST(QuerySerializerTest, SerializeGetAccountAssetsWhenValid) {
QuerySerializer querySerializer;
auto json_query =
"{\"signature\": {\n"
" \"pubkey\": "
"\"2323232323232323232323232323232323232323232323232323232323232323\",\n"
" \"signature\": "
"\"2323232323232323232323232323232323232323232323232323232323232323232323"
"2323232323232323232323232323232323232323232323232323232323\"\n"
" }, \n"
" \"created_ts\": 0,\n"
" \"creator_account_id\": \"123\",\n"
" \"query_counter\": 0,\n"
" \"query_type\": \"GetAccountAssets\",\n"
" \"account_id\": \"test@test\",\n"
" \"asset_id\": \"coin#test\"\n"
" }";
auto res = querySerializer.deserialize(json_query);
ASSERT_TRUE(res.has_value());
ASSERT_TRUE(res.value().has_get_account_assets());
ASSERT_EQ(res.value().get_account_assets().account_id(), "test@test");
ASSERT_EQ(res.value().get_account_assets().asset_id(), "coin#test");
}


TEST(QuerySerializerTest, SerializeWhenUnknownType) {
QuerySerializer querySerializer;
auto json_query =
"{\"signature\": {\n"
" \"pubkey\": "
"\"2323232323232323232323232323232323232323232323232323232323232323\",\n"
" \"signature\": "
"\"2323232323232323232323232323232323232323232323232323232323232323232323"
"2323232323232323232323232323232323232323232323232323232323\"\n"
" }, \n"
" \"created_ts\": 0,\n"
" \"creator_account_id\": \"123\",\n"
" \"query_counter\": 0,\n"
" \"query_type\": \"GetSomething\",\n"
" \"account_id\": \"test@test\",\n"
" \"asset_id\": \"coin#test\"\n"
" }";
auto res = querySerializer.deserialize(json_query);
ASSERT_FALSE(res.has_value());
}

0 comments on commit a1c61bc

Please sign in to comment.