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 ...@@ -47,8 +47,6 @@ namespace xs { namespace test
private: private:
Config config_; Config config_;
ExchangeServer server_; 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> receivedCommandCount_;
std::unordered_map<std::string, unsigned> receivedEventCount_; std::unordered_map<std::string, unsigned> receivedEventCount_;
}; };
......
...@@ -32,9 +32,6 @@ Tester::Tester(const boost::filesystem::path& file) ...@@ -32,9 +32,6 @@ Tester::Tester(const boost::filesystem::path& file)
void Tester::registerServices() noexcept void Tester::registerServices() noexcept
{ {
acceptCommands_.clear();
acceptEvents_.clear();
using namespace std::placeholders; using namespace std::placeholders;
for (const auto& service : config_.getServices()) { for (const auto& service : config_.getServices()) {
if (service.isCommand()) { if (service.isCommand()) {
...@@ -42,13 +39,11 @@ void Tester::registerServices() noexcept ...@@ -42,13 +39,11 @@ void Tester::registerServices() noexcept
service.name_, service.name_,
std::bind(&Tester::onCommand, this, _1, _2) std::bind(&Tester::onCommand, this, _1, _2)
); );
acceptCommands_[service.name_] = &service;
} else { } else {
server_.onEvent( server_.onEvent(
service.name_, service.name_,
std::bind(&Tester::onEvent, this, _1, _2) 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 ...@@ -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 std::string Tester::onCommand(const std::string& name, const std::string& params) noexcept
{ {
const auto found = acceptCommands_.find(name); using namespace boost::adaptors;
const auto* service = std::get<1>(*found); using namespace std::placeholders;
const auto& response = service->response_; 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_);
}
receivedCommandCount_[name]++; 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 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