Commit 9331faaa authored by Patrick Chen's avatar Patrick Chen

simply construct tester type

parent 47440fc8
*.swp
*.o
build
......@@ -5,22 +5,35 @@ SUBMOD_DIR=$(REPO_DIR)/submodule
BUILD_DIR=$(REPO_DIR)/build
CXX=g++
CXX=clang++
CXX_FLAGS=-g -std=c++14 \
-I/usr/local/include \
-I$(INCLUDE_DIR) \
-I$(SUBMOD_DIR)/json/single_include
LD_FLAGS=-L/usr/local/lib \
-lexchange_server
-lboost_system \
-lboost_filesystem \
-l:libexchange_server.a \
-l:libomniDynamic4.a \
-l:libCOSNotify4.a \
-l:libCOS4.a \
-l:libomniZIOP4.a \
-l:libomniCodeSets4.a \
-l:libomniORB4.a \
-l:libomnithread.a \
-lz \
-lpthread
XSTEST_HDRS=$(INCLUDE_DIR)/Config.h
XSTEST_SRCS=$(SRC_DIR)/xs-test.cc
XSTEST_HDRS=$(INCLUDE_DIR)/test/Config.h $(INCLUDE_DIR)/test/Service.h $(INCLUDE_DIR)/test/Tester.h
XSTEST_SRCS=$(SRC_DIR)/xs-test.cc $(SRC_DIR)/test/Config.cc $(SRC_DIR)/test/Service.cc $(SRC_DIR)/test/Tester.cc
XSTEST_OBJS=$(subst $(REPO_DIR),$(BUILD_DIR),$(XSTEST_SRCS:.cc=.o))
all: $(BUILD_DIR)/xs-test
$(BUILD_DIR)/xs-test: $(XSTEST_OBJS)
$(CXX) -o $@ $(LD_FLAGS) $(XSTEST_OBJS)
$(CXX) -o $@ $(XSTEST_OBJS) $(LD_FLAGS)
$(BUILD_DIR)/%.o: $(REPO_DIR)/%.cc $(XSTEST_HDRS)
mkdir -p $(dir $@)
......@@ -29,3 +42,6 @@ $(BUILD_DIR)/%.o: $(REPO_DIR)/%.cc $(XSTEST_HDRS)
clean:
rm -rf $(BUILD_DIR)/xs-test
rm -rf $(XSTEST_OBJS)
print-var:
echo $(XSTEST_OBJS)
#ifndef XS_TEST_CONFIG_H_INCLUDED
#define XS_TEST_CONFIG_H_INCLUDED
#include "nlohmann/json.hpp"
#include <memory>
namespace xs { namespace test
{
class Config final
{
public:
static std::unique_ptr<Config> parse(const std::string& text) noexcept;
private:
};
} }
#endif
#ifndef XS_TEST_CONFIG_H_INCLUDED
#define XS_TEST_CONFIG_H_INCLUDED
#include <deque>
#include <memory>
#include <boost/filesystem.hpp>
#include <nlohmann/json.hpp>
#include "test/Service.h"
namespace xs { namespace test
{
class Config final
{
public:
Config() = delete;
explicit Config(const boost::filesystem::path& file);
Config(const Config&) = delete;
Config(Config&&) = delete;
~Config() noexcept = default;
Config& operator=(const Config&) = delete;
Config& operator=(Config&&) = delete;
private:
static bool verify(const nlohmann::json& object) noexcept;
public:
std::deque<Service> services_;
};
} }
#endif
#ifndef XS_TEST_SERVER_H_INCLUDED
#define XS_TEST_SERVER_H_INCLUDED
#include <nlohmann/json.hpp>
#include <memory>
#include <string>
namespace xs { namespace test
{
class Service final
{
public:
enum class Type {
COMMAND, EVENT
};
public:
Service() = delete;
Service(const Service&) = delete;
Service(Service&&) noexcept = default;
~Service() noexcept = default;
Service& operator=(const Service&) = delete;
Service& operator=(Service&&) = delete;
explicit Service(const nlohmann::json& object);
static bool verify(const nlohmann::json& object) noexcept;
private:
Service(Type type, std::string name, nlohmann::json response) noexcept;
public:
Type type_;
std::string name_;
nlohmann::json response_;
};
} }
#endif
#ifndef XS_TEST_TESTER_H_INCLUDED
#define XS_TEST_TESTER_H_INCLUDED
#include <memory>
#include <boost/filesystem.hpp>
#include "positivegrid/exchange_server.h"
#include "test/Config.h"
namespace xs { namespace test
{
class Tester final
{
public:
Tester() = delete;
~Tester() noexcept = default;
Tester(const Tester&) = delete;
Tester(Tester&&) = delete;
Tester& operator=(const Tester&) = delete;
Tester& operator=(Tester&&) = delete;
explicit Tester(const boost::filesystem::path& file);
private:
public:
Config config_;
ExchangeServer server_;
};
} }
#endif
#include "test/Config.h"
#include <boost/filesystem.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#include <memory>
#include <sstream>
#include <stdexcept>
namespace xs { namespace test {
namespace detail
{
std::string readAsString(const boost::filesystem::path& file)
{
std::ifstream stream(file.string());
if (!stream) {
throw std::runtime_error("cannot open file: " + file.string());
}
std::ostringstream oss;
oss << stream.rdbuf();
return oss.str();
}
}
Config::Config(const boost::filesystem::path& file)
{
const auto object = nlohmann::json::parse(
detail::readAsString(file)
);
if (!verify(object)) {
throw std::runtime_error("invalid config content");
}
for (const auto element : object["mockData"]) {
services_.emplace_back(Service(element));
}
}
bool Config::verify(const nlohmann::json& object) noexcept
{
if (!(object.count("mockData") && object["mockData"].is_array())) {
return false;
}
for (const auto element : object["mockData"]) {
if (!Service::verify(element)) {
return false;
}
}
return true;
}
} }
#include "test/Service.h"
#include <nlohmann/json.hpp>
#include <memory>
namespace xs { namespace test
{
Service::Service(const nlohmann::json& object)
: Service(
object.count("command") ? Type::COMMAND : Type::EVENT,
object[object.count("command") ? "command" : "event"].get<std::string>(),
object["response"]
)
{
/// TODO: validate the response string here
}
bool Service::verify(const nlohmann::json& object) noexcept
{
if (!(object.is_object()
&& (object.count("command") || object.count("event"))
&& object.count("response"))) {
return false;
}
const bool isCommand = object.count("command");
return (
(
(isCommand && object["command"].is_string())
|| (!isCommand && object["event"].is_string())
)
&& (
object["response"].is_string() || object["response"].is_object()
)
);
}
Service::Service(Type type, std::string name, nlohmann::json response) noexcept
: type_(type)
, name_(std::move(name))
, response_(std::move(response))
{ }
} }
#include "test/Tester.h"
namespace xs { namespace test {
Tester::Tester(const boost::filesystem::path& file)
: config_(file)
, server_(
"xs-test", { }, { }, 0, nullptr
)
{ }
} }
#include <cstdlib>
#include <iostream>
#include "Config.h"
#include <boost/filesystem.hpp>
#include "test/Tester.h"
#include "nlohmann/json.hpp"
#include "positivegrid/exchange_server.h"
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "usage: " << argv[0] << " config" << std::endl;
return EXIT_SUCCESS;
}
int main() {
namespace fs = boost::filesystem;
const auto configFile = fs::absolute(argv[1]);
if (!fs::is_regular_file(configFile)) {
std::cerr << "file \"" << argv[1] << "\" is not exist" << std::endl;
return EXIT_FAILURE;
}
using namespace xs::test;
const Tester tester{configFile};
for (const auto& service : tester.config_.services_) {
if (service.type_ == Service::Type::COMMAND) {
std::cout << "command: " << service.name_;
} else {
std::cout << "event: " << service.name_;
}
std::cout << ", response: " << service.response_ << std::endl;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment