Skip to content

Commit

Permalink
Add create object helper (for transaction builder)
Browse files Browse the repository at this point in the history
  • Loading branch information
motxx committed Feb 22, 2017
1 parent 4092242 commit 5a075a4
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 86 deletions.
119 changes: 119 additions & 0 deletions core/transaction_builder/helper/create_objects_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
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.
*/

#ifndef CORE_TRANSACTION_BUILDER_CREATE_OBJECTS_HPP
#define CORE_TRANSACTION_BUILDER_CREATE_OBJECTS_HPP

#include <infra/protobuf/api.pb.h>
#include <util/exception.hpp>

namespace transaction {

/*
Primitives
*/

inline Api::BaseObject createValueString(std::string val) {
Api::BaseObject ret;
ret.set_valuestring(std::move(val));
return ret;
}

inline Api::BaseObject createValueInt(int val) {
Api::BaseObject ret;
ret.set_valueint(static_cast<::google::protobuf::int64>(val));
return ret;
}

inline Api::BaseObject createValueBool(bool val) {
Api::BaseObject ret;
ret.set_valueboolean(val);
return ret;
}

inline Api::BaseObject createValueDouble(double val) {
Api::BaseObject ret;
ret.set_valuedouble(val);
return ret;
}

inline Api::Trust createTrust(double value, bool isOk) {
Api::Trust ret;
ret.set_value(value);
ret.set_isok(isOk);
return ret;
}

/*
Map
*/

using Map = std::unordered_map<std::string, Api::BaseObject>;

/*
Assets
*/

inline Api::Domain createDomain(std::string ownerPublicKey, std::string name) {
Api::Domain ret;
ret.set_ownerpublickey(std::move(ownerPublicKey));
ret.set_name(std::move(name));
return ret;
}

inline Api::Account createAccount(std::string publicKey, std::string name,
std::vector<std::string> assets) {
Api::Account ret;
ret.set_publickey(std::move(publicKey));
ret.set_name(std::move(name));
*ret.mutable_assets() = ::google::protobuf::RepeatedPtrField<std::string>(
assets.begin(), assets.end());
return ret;
}

inline Api::Asset createAsset(std::string domain, std::string name,
::transaction::Map value,
std::string smartContractName) {
Api::Asset ret;
ret.set_domain(std::move(domain));
ret.set_name(std::move(name));
*ret.mutable_value() = ::google::protobuf::Map<std::string, Api::BaseObject>(
value.begin(), value.end());
ret.set_smartcontractname(std::move(smartContractName));
return ret;
}

inline Api::SimpleAsset createSimpleAsset(std::string domain, std::string name,
Api::BaseObject value,
std::string smartContractName) {
Api::SimpleAsset ret;
ret.set_domain(std::move(domain));
ret.set_name(std::move(name));
*ret.mutable_value() = std::move(value);
ret.set_smartcontractname(std::move(smartContractName));
return ret;
}

inline Api::Peer createPeer(std::string publicKey, std::string address,
Api::Trust trust) {
Api::Peer ret;
ret.set_publickey(std::move(publicKey));
ret.set_address(std::move(address));
*ret.mutable_trust() = std::move(trust);
return ret;
}
}
#endif
2 changes: 2 additions & 0 deletions core/transaction_builder/transaction_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
#ifndef CORE_MODEL_TRANSACTION_BUILDER_HPP
#define CORE_MODEL_TRANSACTION_BUILDER_HPP

#include "helper/create_objects_helper.hpp"

#include "add/add_domain_builder.hpp"
#include "add/add_account_builder.hpp"
#include "add/add_asset_builder.hpp"
Expand Down
7 changes: 7 additions & 0 deletions core/util/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ namespace exception {
std::invalid_argument("TODO: sorry [" + functionName + "] in " + filename + " is not yet implemented, would you like to contribute it?") {
}

BaseMethodException::BaseMethodException(
const std::string& functionName,
const std::string& filename
):
std::domain_error("BaseMethodException [" + functionName + "] in " + filename) {
}

ParseFromStringException::ParseFromStringException(
const std::string& filename
):
Expand Down
9 changes: 8 additions & 1 deletion core/util/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ namespace exception {

class NotImplementedException : public std::invalid_argument {
public: NotImplementedException(
const std::string& message,
const std::string& functionName,
const std::string& filename
);
};

class BaseMethodException : public std::domain_error {
public: BaseMethodException(
const std::string& functionName,
const std::string& filename
);
};
Expand Down
150 changes: 65 additions & 85 deletions test/transaction_builder/transaction_builder_add_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,162 +19,142 @@ limitations under the License.
#include <transaction_builder/transaction_builder.hpp>
#include <util/exception.hpp>

using transaction::TransactionBuilder;
namespace txbuilder = transaction;

using txbuilder::TransactionBuilder;
using type_signatures::Add;
using type_signatures::Domain;
using type_signatures::Account;
using type_signatures::Asset;
using type_signatures::SimpleAsset;
using type_signatures::Peer;

constexpr auto commandType = "Add";
constexpr auto senderPublicKey = "sender public key";

/***************************************************************************
Add
***************************************************************************/
TEST(transaction_builder, create_add_domain) {
Api::Domain domain;
domain.set_ownerpublickey("pubkey1");
domain.set_name("name");

const auto ownerPublicKey = "owner public key";
const auto name = "name";

auto txDomain = TransactionBuilder<Add<Domain>>()
.setSenderPublicKey("karin")
.setDomain(domain)
.setSenderPublicKey(senderPublicKey)
.setDomain(txbuilder::createDomain(ownerPublicKey, name))
.build();

ASSERT_STREQ(txDomain.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txDomain.type().c_str(), "Add");
ASSERT_STREQ(txDomain.senderpubkey().c_str(), senderPublicKey);
ASSERT_STREQ(txDomain.type().c_str(), commandType);

auto obj = txDomain.domain();
ASSERT_STREQ(obj.ownerpublickey().c_str(), "pubkey1");
ASSERT_STREQ(obj.name().c_str(), "name");
ASSERT_STREQ(obj.ownerpublickey().c_str(), ownerPublicKey);
ASSERT_STREQ(obj.name().c_str(), name);
}

TEST(transaction_builder, create_add_account) {

Api::Account account;
{
account.set_publickey("pubkey1");
account.set_name("name");
}

std::vector<std::string> assets = {
const auto publicKey = "public key";
const auto name = "name";
const std::vector<std::string> assets = {
"asset1",
"asset2"
};
for (auto&& e: assets) {
account.add_assets(e);
}

auto txAccount = TransactionBuilder<Add<Account>>()
.setSenderPublicKey("karin")
.setAccount(account)
.setSenderPublicKey(senderPublicKey)
.setAccount(txbuilder::createAccount(publicKey, name, assets))
.build();

ASSERT_STREQ(txAccount.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txAccount.type().c_str(), "Add");
ASSERT_STREQ(txAccount.senderpubkey().c_str(), senderPublicKey);
ASSERT_STREQ(txAccount.type().c_str(), commandType);

auto obj = txAccount.account();
ASSERT_STREQ(obj.publickey().c_str(), "pubkey1");
ASSERT_STREQ(obj.name().c_str(), "name");
ASSERT_STREQ(obj.publickey().c_str(), publicKey);
ASSERT_STREQ(obj.name().c_str(), name);
for (int i = 0; i < obj.assets_size(); i++) {
ASSERT_STREQ(obj.assets(i).c_str(), assets[i].c_str());
}
}

TEST(transaction_builder, create_add_asset) {
std::unordered_map<std::string, Api::BaseObject> value;
{
{
Api::BaseObject baseObject;
baseObject.set_valuestring("value1");
value.emplace("key1", baseObject);
}
{
Api::BaseObject baseObject;
baseObject.set_valueint(123456);
value.emplace("key2", baseObject);
}
}

// Needs create helper function?
Api::Asset asset;
const auto domainID = "domainID";
const auto name = "name";

txbuilder::Map value;
{
asset.set_domain("domainID");
asset.set_name("name");
google::protobuf::Map<std::string, Api::BaseObject> protoValue(value.begin(), value.end());
*asset.mutable_value() = protoValue;
asset.set_smartcontractname("contract_func");
value.emplace("key1", txbuilder::createValueString("value1"));
value.emplace("key2", txbuilder::createValueInt(123456));
}

const auto scName = "contract_func";

auto asset =
txbuilder::createAsset(domainID, name, value, scName);

auto txAsset = TransactionBuilder<Add<Asset>>()
.setSenderPublicKey("karin")
.setAsset(asset)
.build();
.setSenderPublicKey(senderPublicKey)
.setAsset(std::move(asset))
.build();

// Verify
ASSERT_STREQ(txAsset.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txAsset.type().c_str(), "Add");
ASSERT_STREQ(txAsset.senderpubkey().c_str(), senderPublicKey);
ASSERT_STREQ(txAsset.type().c_str(), commandType);

auto obj = txAsset.asset();
ASSERT_STREQ(obj.domain().c_str(), "domainID");
ASSERT_STREQ(obj.name().c_str(), "name");
ASSERT_STREQ(obj.domain().c_str(), domainID);
ASSERT_STREQ(obj.name().c_str(), name);

ASSERT_TRUE(obj.value().find("key1")->second.value_case() == Api::BaseObject::ValueCase::kValueString);
ASSERT_TRUE(obj.value().find("key2")->second.value_case() == Api::BaseObject::ValueCase::kValueInt);
ASSERT_STREQ(obj.value().find("key1")->second.valuestring().c_str(), "value1");
ASSERT_TRUE(obj.value().find("key2")->second.valueint() == 123456);
ASSERT_STREQ(obj.smartcontractname().c_str(), "contract_func");
ASSERT_STREQ(obj.smartcontractname().c_str(), scName);
}

TEST(transaction_builder, create_add_peer) {

Api::Trust trust;
{
trust.set_value(1.23);
}

Api::Peer peer;
{
peer.set_publickey("publickey");
peer.set_address("address");
*peer.mutable_trust() = trust;
}
auto peer = txbuilder::createPeer(
"publickey",
"address",
txbuilder::createTrust(1.23, true)
);

auto txPeer = TransactionBuilder<Add<Peer>>()
.setSenderPublicKey("karin")
.setPeer(peer)
.setSenderPublicKey(senderPublicKey)
.setPeer(std::move(peer))
.build();

// Verify
ASSERT_STREQ(txPeer.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txPeer.type().c_str(), "Add");
ASSERT_STREQ(txPeer.senderpubkey().c_str(), senderPublicKey);
ASSERT_STREQ(txPeer.type().c_str(), commandType);

auto obj = txPeer.peer();
ASSERT_STREQ(obj.publickey().c_str(), "publickey");
ASSERT_STREQ(obj.address().c_str(), "address");
ASSERT_TRUE(obj.trust().value() == 1.23);
ASSERT_TRUE(obj.trust().isok() == true);
}

TEST(transaction_builder, create_add_simpleasset) {

Api::BaseObject baseObject;
{
baseObject.set_valueint(123456);
}

Api::SimpleAsset simpleAsset;
{
simpleAsset.set_domain("domainID");
simpleAsset.set_name("name");
*simpleAsset.mutable_value() = baseObject;
simpleAsset.set_smartcontractname("contract_func");
}
auto simpleAsset = txbuilder::createSimpleAsset(
"domainID",
"name",
txbuilder::createValueInt(123456),
"contract_func"
);

auto txSimpleAsset = TransactionBuilder<Add<SimpleAsset>>()
.setSenderPublicKey("karin")
.setSimpleAsset(simpleAsset)
.setSenderPublicKey(senderPublicKey)
.setSimpleAsset(std::move(simpleAsset))
.build();

// Verify
ASSERT_STREQ(txSimpleAsset.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txSimpleAsset.type().c_str(), "Add");
ASSERT_STREQ(txSimpleAsset.senderpubkey().c_str(), senderPublicKey);
ASSERT_STREQ(txSimpleAsset.type().c_str(), commandType);

auto obj = txSimpleAsset.simpleasset();
ASSERT_STREQ(obj.domain().c_str(), "domainID");
Expand Down

0 comments on commit 5a075a4

Please sign in to comment.