Commit 977e708e authored by Patrick Chen's avatar Patrick Chen

handle command with diff params

parent e1b7d7de
......@@ -47,8 +47,6 @@ namespace xs { namespace test
private:
Config config_;
ExchangeServer server_;
std::unordered_map<std::string, const Service*> acceptCommands_;
std::unordered_map<std::string, const Service*> acceptEvents_;
std::unordered_map<std::string, unsigned> receivedCommandCount_;
std::unordered_map<std::string, unsigned> receivedEventCount_;
};
......
......@@ -32,9 +32,6 @@ Tester::Tester(const boost::filesystem::path& file)
void Tester::registerServices() noexcept
{
acceptCommands_.clear();
acceptEvents_.clear();
using namespace std::placeholders;
for (const auto& service : config_.getServices()) {
if (service.isCommand()) {
......@@ -42,13 +39,11 @@ void Tester::registerServices() noexcept
service.name_,
std::bind(&Tester::onCommand, this, _1, _2)
);
acceptCommands_[service.name_] = &service;
} else {
server_.onEvent(
service.name_,
std::bind(&Tester::onEvent, this, _1, _2)
);
acceptEvents_[service.name_] = &service;
}
}
}
......@@ -95,13 +90,38 @@ Tester::Report Tester::createReport(const std::string& response, const Expectati
std::string Tester::onCommand(const std::string& name, const std::string& params) noexcept
{
const auto found = acceptCommands_.find(name);
const auto* service = std::get<1>(*found);
const auto& response = service->response_;
using namespace boost::adaptors;
using namespace std::placeholders;
const auto handlers = config_.getServices()
| filtered(std::bind(&Service::isCommand, _1));
for (const auto& handler : handlers) {
if (handler.name_ != name) {
continue;
}
if (!handler.condition_) {
receivedCommandCount_[name]++;
return detail::toString(*handler.response_);
}
auto& receiveCount = receivedCommandCount_[name];
const auto& condition = *handler.condition_;
const bool isString = condition.is_string();
if (isString) {
if (params == condition.get<std::string>()) {
receiveCount++;
return detail::toString(*handler.response_);
}
} else {
if (nlohmann::json::parse(params) == condition) {
receiveCount++;
return detail::toString(*handler.response_);
}
}
}
return detail::toString(*response);
return "";
}
void Tester::onEvent(const std::string& name, const std::string& params) noexcept
......
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