Skip to content

Commit

Permalink
Reduce code duplication in tx response builder tests (hyperledger-iro…
Browse files Browse the repository at this point in the history
…ha#1359)

* Reduce code duplication in tx response builder tests

* Remove verifyType

Signed-off-by: Andrei Lebedev <[email protected]>
  • Loading branch information
lebdron authored May 31, 2018
1 parent df116c5 commit 0a4908d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "module/shared_model/builders/protobuf/test_block_builder.hpp"
#include "module/shared_model/builders/protobuf/test_proposal_builder.hpp"
#include "module/shared_model/builders/protobuf/test_transaction_builder.hpp"
#include "module/shared_model/builders/transaction_responses/transaction_builders_common.hpp"
#include "torii/processor/transaction_processor_impl.hpp"

using namespace iroha;
Expand Down Expand Up @@ -70,7 +69,9 @@ class TransactionProcessorTest : public ::testing::Test {
for (const auto &tx : transactions) {
auto tx_status = status_map.find(tx.hash());
ASSERT_NE(tx_status, status_map.end());
boost::apply_visitor(verifyType<Status>(), tx_status->second->get());
ASSERT_NO_THROW(boost::apply_visitor(
shared_model::interface::SpecifiedVisitor<Status>(),
tx_status->second->get()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,160 +17,69 @@

#include <gtest/gtest.h>
#include "builders/protobuf/transaction_responses/proto_transaction_status_builder.hpp"
#include "module/shared_model/builders/transaction_responses/transaction_builders_common.hpp"
#include "framework/specified_visitor.hpp"

using shared_model::proto::TransactionStatusBuilder;

/**
* @given expected transaction stateless invalid status and hash
* @when model object is built using these status and hash
* @then built object has expected status and hash
*/
TEST(ProtoTransactionStatusBuilderTest, TestStatelessFailedStatus) {
using StatelessFailedStatusType =
shared_model::interface::StatelessFailedTxResponse;

auto expected_status = iroha::protocol::STATELESS_VALIDATION_FAILED;
auto expected_hash = std::string(32, '1');

auto stateless_invalid_response =
TransactionStatusBuilder()
.statelessValidationFailed()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();

boost::apply_visitor(verifyType<StatelessFailedStatusType>(),
stateless_invalid_response.get());

auto proto_status = stateless_invalid_response.getTransport();
ASSERT_EQ(proto_status.tx_status(), expected_status);
ASSERT_EQ(proto_status.tx_hash(), expected_hash);
}
template <typename T>
class ProtoTransactionStatusBuilderTest : public ::testing::Test {};

template <typename Iface,
TransactionStatusBuilder (TransactionStatusBuilder::*Member)(),
iroha::protocol::TxStatus Status>
struct TransactionResponseBuilderTestCase {
using IfaceType = Iface;
static constexpr auto member = Member;
static constexpr auto status = Status;
};

using TransactionResponsTypes =
::testing::Types<TransactionResponseBuilderTestCase<
shared_model::interface::StatelessFailedTxResponse,
&TransactionStatusBuilder::statelessValidationFailed,
iroha::protocol::STATELESS_VALIDATION_FAILED>,
TransactionResponseBuilderTestCase<
shared_model::interface::StatelessValidTxResponse,
&TransactionStatusBuilder::statelessValidationSuccess,
iroha::protocol::STATELESS_VALIDATION_SUCCESS>,
TransactionResponseBuilderTestCase<
shared_model::interface::StatefulFailedTxResponse,
&TransactionStatusBuilder::statefulValidationFailed,
iroha::protocol::STATEFUL_VALIDATION_FAILED>,
TransactionResponseBuilderTestCase<
shared_model::interface::StatefulValidTxResponse,
&TransactionStatusBuilder::statefulValidationSuccess,
iroha::protocol::STATEFUL_VALIDATION_SUCCESS>,
TransactionResponseBuilderTestCase<
shared_model::interface::CommittedTxResponse,
&TransactionStatusBuilder::committed,
iroha::protocol::COMMITTED>,
TransactionResponseBuilderTestCase<
shared_model::interface::NotReceivedTxResponse,
&TransactionStatusBuilder::notReceived,
iroha::protocol::NOT_RECEIVED> >;
TYPED_TEST_CASE(ProtoTransactionStatusBuilderTest, TransactionResponsTypes);

/**
* @given expected transaction stateless valid status and hash
* @given expected transaction status and hash
* @when model object is built using these status and hash
* @then built object has expected status and hash
*/
TEST(ProtoTransactionStatusBuilderTest, TestStatelessValidStatus) {
using StatelessValidStatusType =
shared_model::interface::StatelessValidTxResponse;

auto expected_status = iroha::protocol::STATELESS_VALIDATION_SUCCESS;
auto expected_hash = std::string(32, '1');

auto stateless_invalid_response =
TransactionStatusBuilder()
.statelessValidationSuccess()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();

boost::apply_visitor(verifyType<StatelessValidStatusType>(),
stateless_invalid_response.get());

auto proto_status = stateless_invalid_response.getTransport();
ASSERT_EQ(proto_status.tx_status(), expected_status);
ASSERT_EQ(proto_status.tx_hash(), expected_hash);
}

/**
* @given expected transaction stateful invalid status and hash
* @when model object is built using these status and hash
* @then built object has expected status and hash
*/
TEST(ProtoTransactionStatusBuilderTest, TestStatefulFailedStatus) {
using StatefulFailedStatusType =
shared_model::interface::StatefulFailedTxResponse;

auto expected_status = iroha::protocol::STATEFUL_VALIDATION_FAILED;
auto expected_hash = std::string(32, '1');

auto stateful_invalid_response =
TransactionStatusBuilder()
.statefulValidationFailed()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();

boost::apply_visitor(verifyType<StatefulFailedStatusType>(),
stateful_invalid_response.get());

auto proto_status = stateful_invalid_response.getTransport();
ASSERT_EQ(proto_status.tx_status(), expected_status);
ASSERT_EQ(proto_status.tx_hash(), expected_hash);
}
TYPED_TEST(ProtoTransactionStatusBuilderTest, TestStatusType) {
using StatusType = typename TypeParam::IfaceType;

/**
* @given expected transaction stateful valid status and hash
* @when model object is built using these status and hash
* @then built object has expected status and hash
*/
TEST(ProtoTransactionStatusBuilderTest, TestStatefulValidStatus) {
using StatefulValidStatusType =
shared_model::interface::StatefulValidTxResponse;

auto expected_status = iroha::protocol::STATEFUL_VALIDATION_SUCCESS;
auto expected_hash = std::string(32, '1');

auto stateful_invalid_response =
TransactionStatusBuilder()
.statefulValidationSuccess()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();

boost::apply_visitor(verifyType<StatefulValidStatusType>(),
stateful_invalid_response.get());

auto proto_status = stateful_invalid_response.getTransport();
ASSERT_EQ(proto_status.tx_status(), expected_status);
ASSERT_EQ(proto_status.tx_hash(), expected_hash);
}

/**
* @given expected transaction commit status and hash
* @when model object is built using these status and hash
* @then built object has expected status and hash
*/
TEST(ProtoTransactionStatusBuilderTest, TestCommittedStatus) {
using CommittedStatusType = shared_model::interface::CommittedTxResponse;

auto expected_status = iroha::protocol::COMMITTED;
auto expected_hash = std::string(32, '1');

auto committed_response =
TransactionStatusBuilder()
.committed()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();

boost::apply_visitor(verifyType<CommittedStatusType>(),
committed_response.get());

auto proto_status = committed_response.getTransport();
ASSERT_EQ(proto_status.tx_status(), expected_status);
ASSERT_EQ(proto_status.tx_hash(), expected_hash);
}

/**
* @given expected transaction not received status and hash
* @when model object is built using these status and hash
* @then built object has expected status and hash
*/
TEST(ProtoTransactionStatusBuilderTest, TestNotReceivedStatus) {
using NotReceivedStatusType = shared_model::interface::NotReceivedTxResponse;

auto expected_status = iroha::protocol::NOT_RECEIVED;
auto expected_status = TypeParam::status;
auto expected_hash = std::string(32, '1');

auto not_received_response =
TransactionStatusBuilder()
.notReceived()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();
auto response = (TransactionStatusBuilder().*TypeParam::member)()
.txHash(shared_model::crypto::Hash(expected_hash))
.build();

boost::apply_visitor(verifyType<NotReceivedStatusType>(),
not_received_response.get());
ASSERT_NO_THROW(boost::apply_visitor(
shared_model::interface::SpecifiedVisitor<StatusType>(),
response.get()));

auto proto_status = not_received_response.getTransport();
auto proto_status = response.getTransport();
ASSERT_EQ(proto_status.tx_status(), expected_status);
ASSERT_EQ(proto_status.tx_hash(), expected_hash);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 0a4908d

Please sign in to comment.