Skip to content

Commit

Permalink
Merge add-config-to-main into update-iroha-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l committed Jul 23, 2017
2 parents 7496a55 + 8857870 commit 8c6c85c
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 34 deletions.
70 changes: 37 additions & 33 deletions irohad/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,48 @@ limitations under the License.

namespace common {

namespace config {
using namespace rapidjson;
namespace config {
using namespace rapidjson;

ConfigLoader::ConfigLoader(const std::string& file_name){
std::ifstream ifs(file_name);
if(ifs.is_open()){
IStreamWrapper isw(ifs);
doc.ParseStream(isw);
if(doc.HasParseError()){
throw std::runtime_error("parse error");
}
}
ConfigLoader::ConfigLoader(const std::string& file_name) {
std::ifstream ifs(file_name);

if (ifs.is_open()) {

IStreamWrapper isw(ifs);
doc.ParseStream(isw);

if (doc.HasParseError()) {
// ToDo log failed to logger
doc.Parse("{}");
}
}
}

int ConfigLoader::getIntOrElse(const std::string& key, int def){
if(doc[key.c_str()].IsInt()){
return doc[key.c_str()].GetInt();
}else{
return def;
};
}
int ConfigLoader::getIntOrElse(const std::string& key, int def) {
if (doc.IsObject() && doc.HasMember(key.c_str()) && doc[key.c_str()].IsInt()){
return doc[key.c_str()].GetInt();
} else {
return def;
};
}

std::string ConfigLoader::getStringOrElse(const std::string& key, std::string def){
if(doc[key.c_str()].IsString()){
return doc[key.c_str()].GetString();
}else{
return def;
};
}
std::string ConfigLoader::getStringOrElse(const std::string& key, std::string def) {
if (doc.IsObject() && doc.HasMember(key.c_str()) && doc[key.c_str()].IsString()) {
return doc[key.c_str()].GetString();
} else {
return def;
};
}

bool ConfigLoader::getBoolOrElse(const std::string& key, bool def){
if(doc[key.c_str()].IsBool()){
return doc[key.c_str()].GetBool();
}else{
return def;
};
}
};
bool ConfigLoader::getBoolOrElse(const std::string& key, bool def) {
if (doc.IsObject() && doc.HasMember(key.c_str()) && doc[key.c_str()].IsBool()) {
return doc[key.c_str()].GetBool();
} else {
return def;
};
}
};
}; // namespace common

#endif
5 changes: 4 additions & 1 deletion irohad/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ target_link_libraries(irohad
add_executable(iroha-main iroha-main.cpp)
target_link_libraries(iroha-main
irohad
)
gflags
rapidjson
config
)
11 changes: 11 additions & 0 deletions irohad/main/iroha-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ limitations under the License.

#include <main/application.hpp>

#include <common/config.hpp>
#include <gflags/gflags.h>
#include <cstring>
#include "../../external/src/gflags_gflags-build/include/gflags/gflags.h"

DEFINE_string(config, "iroha.conf", "Iroha's initialize config path");

int main(int argc, char *argv[]) {

gflags::ParseCommandLineFlags(&argc, &argv, true);
gflags::ShutDownCommandLineFlags();
auto loader = common::config::ConfigLoader(FLAGS_config);

auto irohad = Irohad();
irohad.run();

Expand Down
1 change: 1 addition & 0 deletions test/module/irohad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(CMAKE_BUILD_TYPE Debug)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/test_bin)

# Reusable tests
add_subdirectory(common)
add_subdirectory(ametsuchi)
#add_subdirectory(api)
add_subdirectory(consensus)
Expand Down
12 changes: 12 additions & 0 deletions test/module/irohad/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

add_executable(config_loader_test
config_loader_test.cpp
)
target_link_libraries(config_loader_test
config
gtest
)
add_test(
NAME config_loader_test
COMMAND $<TARGET_FILE:config_loader_test>
)
63 changes: 63 additions & 0 deletions test/module/irohad/common/config_loader_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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 <gtest/gtest.h>
#include <common/config.hpp>

#include <fstream>
#include <cstdlib>
class ConfigLoaderTest : public testing::Test {
public:
virtual void SetUp() {
std::ofstream ofs("sample.conf");
ofs << "{\"name\":\"maruuchi\",\"age\":20, \"isRamen\": false}";
ofs.close();
}

virtual void TearDown() {
system("rm sample.conf");
}
};

TEST_F(ConfigLoaderTest, Normal) {
auto loader = common::config::ConfigLoader("sample.conf");
ASSERT_STREQ(loader.getStringOrElse("name","hibiya").c_str(), "maruuchi");
ASSERT_EQ(loader.getIntOrElse("age", 12), 20);
ASSERT_FALSE(loader.getBoolOrElse("isRamen", true));
}

TEST_F(ConfigLoaderTest, NoFile) {
auto loader = common::config::ConfigLoader("sample__.conf");
ASSERT_STREQ(loader.getStringOrElse("name","hibiya").c_str(), "hibiya");
ASSERT_EQ(loader.getIntOrElse("age", 12), 12);
ASSERT_FALSE(loader.getBoolOrElse("isRamen", false));
}

TEST_F(ConfigLoaderTest, WrongStrKeyName) {
auto loader = common::config::ConfigLoader("sample.conf");
ASSERT_STREQ(loader.getStringOrElse("names","hibiya").c_str(), "hibiya");
}

TEST_F(ConfigLoaderTest, WrongIntKeyName) {
auto loader = common::config::ConfigLoader("sample.conf");
ASSERT_EQ(loader.getIntOrElse("ago", 12), 12);
}

TEST_F(ConfigLoaderTest, WrongBoolKeyName) {
auto loader = common::config::ConfigLoader("sample.conf");
ASSERT_TRUE(loader.getBoolOrElse("isRamenSanro", true));
}

0 comments on commit 8c6c85c

Please sign in to comment.