From a1c61bc5aa41c9101e79338d940bb968e973499b Mon Sep 17 00:00:00 2001 From: grimadas Date: Fri, 4 Aug 2017 17:16:57 +0300 Subject: [PATCH] Add query serializer test: - Refactor query serializer class --- irohad/ametsuchi/impl/query_serializer.cpp | 23 ++-- test/module/irohad/ametsuchi/CMakeLists.txt | 12 ++ .../ametsuchi/query_serializer_test.cpp | 104 ++++++++++++++++++ 3 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 test/module/irohad/ametsuchi/query_serializer_test.cpp diff --git a/irohad/ametsuchi/impl/query_serializer.cpp b/irohad/ametsuchi/impl/query_serializer.cpp index b57b4879df..edc860c6e5 100644 --- a/irohad/ametsuchi/impl/query_serializer.cpp +++ b/irohad/ametsuchi/impl/query_serializer.cpp @@ -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; } @@ -46,8 +46,8 @@ 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); @@ -55,6 +55,7 @@ namespace iroha { return nonstd::nullopt; } + auto sig = obj_query["signature"].GetObject(); // check if signature has all needed fields @@ -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; @@ -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(); @@ -160,5 +163,5 @@ namespace iroha { return true; } - } -} \ No newline at end of file + } // namespace ametsuchi +} // namespace iroha \ No newline at end of file diff --git a/test/module/irohad/ametsuchi/CMakeLists.txt b/test/module/irohad/ametsuchi/CMakeLists.txt index 5905fe621b..83a927713a 100644 --- a/test/module/irohad/ametsuchi/CMakeLists.txt +++ b/test/module/irohad/ametsuchi/CMakeLists.txt @@ -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 diff --git a/test/module/irohad/ametsuchi/query_serializer_test.cpp b/test/module/irohad/ametsuchi/query_serializer_test.cpp new file mode 100644 index 0000000000..66b161d781 --- /dev/null +++ b/test/module/irohad/ametsuchi/query_serializer_test.cpp @@ -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 +#include + +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()); +} \ No newline at end of file