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

let event accept no response attribute

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