Commit 7ad112c4 authored by Patrick Chen's avatar Patrick Chen

let event accept no response attribute

parent 6ca724e2
#ifndef XS_TEST_SERVER_H_INCLUDED #ifndef XS_TEST_SERVER_H_INCLUDED
#define XS_TEST_SERVER_H_INCLUDED #define XS_TEST_SERVER_H_INCLUDED
#include <boost/optional.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
...@@ -10,11 +11,6 @@ namespace xs { namespace test ...@@ -10,11 +11,6 @@ namespace xs { namespace test
{ {
class Service final class Service final
{ {
public:
enum class Type {
COMMAND, EVENT
};
public: public:
Service() = delete; Service() = delete;
Service(const Service&) = delete; Service(const Service&) = delete;
...@@ -31,12 +27,12 @@ namespace xs { namespace test ...@@ -31,12 +27,12 @@ namespace xs { namespace test
bool isEvent() const noexcept; bool isEvent() const noexcept;
private: private:
Service(Type type, std::string name, nlohmann::json response) noexcept; Service(std::string name, nlohmann::json response) noexcept;
Service(std::string name) noexcept;
public: public:
Type type_;
std::string name_; std::string name_;
nlohmann::json response_; boost::optional<nlohmann::json> response_;
}; };
} } } }
......
...@@ -9,48 +9,43 @@ namespace xs { namespace test ...@@ -9,48 +9,43 @@ namespace xs { namespace test
{ {
Service::Service(const nlohmann::json& object) Service::Service(const nlohmann::json& object)
: Service( : Service(
object.count("command") ? Type::COMMAND : Type::EVENT, object[object.count("command") ? "command" : "event"].get<std::string>()
object[object.count("command") ? "command" : "event"].get<std::string>(),
object["response"]
) )
{ {
/// TODO: validate the response string here if (object.count("command")) {
response_ = object["response"];
}
} }
bool Service::verify(const nlohmann::json& object) noexcept bool Service::verify(const nlohmann::json& object) noexcept
{ {
if (!(object.is_object() if (!object.is_object()) {
&& (object.count("command") || object.count("event"))
&& object.count("response"))) {
return false; return false;
} }
const bool isCommand = object.count("command"); if (object.count("command")) {
return object["command"].is_string()
return ( && (object["response"].is_string() || object["response"].is_object())
( ;
(isCommand && object["command"].is_string()) } else if (object.count("event")) {
|| (!isCommand && object["event"].is_string()) return object["event"].is_string();
) }
&& (
object["response"].is_string() || object["response"].is_object() return false;
)
);
} }
Service::Service(Type type, std::string name, nlohmann::json response) noexcept Service::Service(std::string name) noexcept
: type_(type) : name_(std::move(name))
, name_(std::move(name)) , response_(boost::none)
, response_(std::move(response))
{ } { }
bool Service::isCommand() const noexcept bool Service::isCommand() const noexcept
{ {
return type_ == Type::COMMAND; return static_cast<bool>(response_);
} }
bool Service::isEvent() const noexcept bool Service::isEvent() const noexcept
{ {
return type_ == Type::EVENT; return !isCommand();
} }
} } } }
...@@ -101,14 +101,13 @@ std::string Tester::onCommand(const std::string& name, const std::string& params ...@@ -101,14 +101,13 @@ std::string Tester::onCommand(const std::string& name, const std::string& params
receivedCommandCount_[name]++; receivedCommandCount_[name]++;
return detail::toString(response); return detail::toString(*response);
} }
void Tester::onEvent(const std::string& name, const std::string& params) noexcept void Tester::onEvent(const std::string& name, const std::string& params) noexcept
{ {
const auto found = acceptEvents_.find(name); const auto found = acceptEvents_.find(name);
const auto* service = std::get<1>(*found); const auto* service = std::get<1>(*found);
const auto& response = service->response_;
receivedEventCount_[name]++; receivedEventCount_[name]++;
} }
......
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