<commit_before>#include "Utils/Testing.hpp"

#include "Messaging/MessageQueue.hpp"

using namespace Core;
using namespace Messaging;

using namespace fakeit;
using namespace std::placeholders;

namespace {

  class Content : public Core::IEntity {
    TYPE_INFO(Content, Core::IEntity, "content")
  };

  struct EventSink {
    virtual Core::IEntity::Unique onRequest(const Content&) = 0;
    virtual void onResponse(const Response&) = 0;
    virtual void onResponseContent(const Content&) = 0;
    virtual void onResponseStatus(const Status&) = 0;
    virtual void onEvent(const Event&) const = 0;
    virtual void onEventContent(const Content&) const = 0;
  };

}

TEST_CASE("message queue is routing an event to a generic client", "[MessageQueue]") {
  auto content = Content::makeShared();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onEvent)).Do([=](const Event& event) {
    REQUIRE(event.getEventType() == "created");
    REQUIRE(event.getResource() == "resource");
    REQUIRE(event.getContent() == content.get());
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createGenericClient("clientId");
  client->setOnEvent(std::bind(&EventSink::onEvent, &eventSink.get(), _1));

  auto event = Event::makeShared("created", "resource", content);
  queue->addEvent(event);
  queue->idle();

  Verify(Method(eventSink, onEvent));
}

TEST_CASE("message queue is not routing an event to a deleted generic client", "[MessageQueue]") {
  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));

  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});
  auto queue = MessageQueue::makeUnique(logger);

  {
    Mock<EventSink> eventSink;
    auto client = queue->createGenericClient("clientId");
    client->setOnEvent(std::bind(&EventSink::onEvent, &eventSink.get(), _1));
    client.reset();
  }

  queue->addEvent(Event::makeShared("created", "resource"));
  queue->idle();
}

TEST_CASE("message queue is routing a response to a generic client", "[MessageQueue]") {
  auto requestContent = Content::makeShared();
  auto responseContent = Content::makeUnique();
  auto responseContentPtr = responseContent.get();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onRequest)).Do([&](const Content& param) {
    REQUIRE(&param == requestContent.get());
    return std::move(responseContent);
  });

  When(Method(eventSink, onResponse)).Do([=](const Response& response) {
    REQUIRE(response.getRequestType() == "get");
    REQUIRE(response.getReceiver() == "clientId");
    REQUIRE(response.getResource() == "resource");
    REQUIRE(&response.getContent() == responseContentPtr);
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createGenericClient("clientId");
  client->setOnResponse(std::bind(&EventSink::onResponse, &eventSink.get(), _1));

  queue->createResourceController("resource_controller_before");
  auto controller = queue->createResourceController("resource");
  queue->createResourceController("resource_controller_after");

  controller->addOnRequest<Content>("get", std::bind(&EventSink::onRequest, &eventSink.get(), _1));

  client->sendRequest("get", "resource", requestContent);
  queue->idle();

  Verify(Method(eventSink, onRequest));
  Verify(Method(eventSink, onResponse));
}

TEST_CASE("message queue is routing an event to a resource client", "[MessageQueue]") {
  auto content = Content::makeShared();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onEventContent)).Do([=](const Content& param) {
    REQUIRE(&param == content.get());
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createResourceClient("clientId", "resource");
  client->addOnEvent<Content>("created", std::bind(&EventSink::onEventContent, &eventSink.get(), _1));

  auto event = Event::makeShared("created", "resource", content);
  queue->addEvent(event);
  queue->idle();

  Verify(Method(eventSink, onEventContent));
}

TEST_CASE("message queue is routing a response to a resource client", "[MessageQueue]") {
  auto requestContent = Content::makeShared();
  auto responseContent = Content::makeUnique();
  auto responseContentPtr = responseContent.get();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onRequest)).Do([&](const Content& param) {
    REQUIRE(&param == requestContent.get());
    return std::move(responseContent);
  });

  When(Method(eventSink, onResponseContent)).Do([=](const Content& param) {
    REQUIRE(&param == responseContentPtr);
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createResourceClient("clientId", "resource");
  client->addOnResponse<Content>("get", std::bind(&EventSink::onResponseContent, &eventSink.get(), _1));

  queue->createResourceController("resource_controller_before");
  auto controller = queue->createResourceController("resource");
  queue->createResourceController("resource_controller_after");

  controller->addOnRequest<Content>("get", std::bind(&EventSink::onRequest, &eventSink.get(), _1));

  client->sendRequest("get", requestContent);
  queue->idle();

  Verify(Method(eventSink, onRequest));
  Verify(Method(eventSink, onResponseContent));
}

TEST_CASE("message queue is failing to route a response to a deleted client", "[MessageQueue]") {
  auto requestContent = Content::makeShared();
  auto responseContent = Content::makeUnique();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onRequest)).Do([&](const Content&) {
    return std::move(responseContent);
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  Fake(Method(loggerInstanse, error));
  /*
  When(Method(loggerInstanse, message)).AlwaysDo([](const std::string& message) {
  });
  When(Method(loggerInstanse, error)).Do([](const std::string& error) {
  });
  */
  
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});
  auto queue = MessageQueue::makeUnique(logger);

  QueueResourceClient* clientPtr;
  {
    auto client = queue->createResourceClient("clientId", "resource");
    clientPtr = client.get();
  }

  auto controller = queue->createResourceController("resource");
  controller->addOnRequest<Content>("get", std::bind(&EventSink::onRequest, &eventSink.get(), _1));

  clientPtr->sendRequest("get", requestContent);
  queue->idle();

  Verify(Method(eventSink, onRequest));
  Verify(Method(loggerInstanse, message));
  Verify(Method(loggerInstanse, error));
}

TEST_CASE("message queue is failing to route a request if there is no controller to handle it", "[MessageQueue]") {
  auto requestContent = Content::makeShared();

  Mock<EventSink> eventSink;

  When(Method(eventSink, onResponseStatus)).Do([=](const Status& status) {<commit_msg>Removing commented out code<commit_after>#include "Utils/Testing.hpp"

#include "Messaging/MessageQueue.hpp"

using namespace Core;
using namespace Messaging;

using namespace fakeit;
using namespace std::placeholders;

namespace {

  class Content : public Core::IEntity {
    TYPE_INFO(Content, Core::IEntity, "content")
  };

  struct EventSink {
    virtual Core::IEntity::Unique onRequest(const Content&) = 0;
    virtual void onResponse(const Response&) = 0;
    virtual void onResponseContent(const Content&) = 0;
    virtual void onResponseStatus(const Status&) = 0;
    virtual void onEvent(const Event&) const = 0;
    virtual void onEventContent(const Content&) const = 0;
  };

}

TEST_CASE("message queue is routing an event to a generic client", "[MessageQueue]") {
  auto content = Content::makeShared();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onEvent)).Do([=](const Event& event) {
    REQUIRE(event.getEventType() == "created");
    REQUIRE(event.getResource() == "resource");
    REQUIRE(event.getContent() == content.get());
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createGenericClient("clientId");
  client->setOnEvent(std::bind(&EventSink::onEvent, &eventSink.get(), _1));

  auto event = Event::makeShared("created", "resource", content);
  queue->addEvent(event);
  queue->idle();

  Verify(Method(eventSink, onEvent));
}

TEST_CASE("message queue is not routing an event to a deleted generic client", "[MessageQueue]") {
  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));

  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});
  auto queue = MessageQueue::makeUnique(logger);

  {
    Mock<EventSink> eventSink;
    auto client = queue->createGenericClient("clientId");
    client->setOnEvent(std::bind(&EventSink::onEvent, &eventSink.get(), _1));
    client.reset();
  }

  queue->addEvent(Event::makeShared("created", "resource"));
  queue->idle();
}

TEST_CASE("message queue is routing a response to a generic client", "[MessageQueue]") {
  auto requestContent = Content::makeShared();
  auto responseContent = Content::makeUnique();
  auto responseContentPtr = responseContent.get();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onRequest)).Do([&](const Content& param) {
    REQUIRE(&param == requestContent.get());
    return std::move(responseContent);
  });

  When(Method(eventSink, onResponse)).Do([=](const Response& response) {
    REQUIRE(response.getRequestType() == "get");
    REQUIRE(response.getReceiver() == "clientId");
    REQUIRE(response.getResource() == "resource");
    REQUIRE(&response.getContent() == responseContentPtr);
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createGenericClient("clientId");
  client->setOnResponse(std::bind(&EventSink::onResponse, &eventSink.get(), _1));

  queue->createResourceController("resource_controller_before");
  auto controller = queue->createResourceController("resource");
  queue->createResourceController("resource_controller_after");

  controller->addOnRequest<Content>("get", std::bind(&EventSink::onRequest, &eventSink.get(), _1));

  client->sendRequest("get", "resource", requestContent);
  queue->idle();

  Verify(Method(eventSink, onRequest));
  Verify(Method(eventSink, onResponse));
}

TEST_CASE("message queue is routing an event to a resource client", "[MessageQueue]") {
  auto content = Content::makeShared();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onEventContent)).Do([=](const Content& param) {
    REQUIRE(&param == content.get());
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createResourceClient("clientId", "resource");
  client->addOnEvent<Content>("created", std::bind(&EventSink::onEventContent, &eventSink.get(), _1));

  auto event = Event::makeShared("created", "resource", content);
  queue->addEvent(event);
  queue->idle();

  Verify(Method(eventSink, onEventContent));
}

TEST_CASE("message queue is routing a response to a resource client", "[MessageQueue]") {
  auto requestContent = Content::makeShared();
  auto responseContent = Content::makeUnique();
  auto responseContentPtr = responseContent.get();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onRequest)).Do([&](const Content& param) {
    REQUIRE(&param == requestContent.get());
    return std::move(responseContent);
  });

  When(Method(eventSink, onResponseContent)).Do([=](const Content& param) {
    REQUIRE(&param == responseContentPtr);
    return Status::OK;
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});

  auto queue = MessageQueue::makeUnique(logger);

  auto client = queue->createResourceClient("clientId", "resource");
  client->addOnResponse<Content>("get", std::bind(&EventSink::onResponseContent, &eventSink.get(), _1));

  queue->createResourceController("resource_controller_before");
  auto controller = queue->createResourceController("resource");
  queue->createResourceController("resource_controller_after");

  controller->addOnRequest<Content>("get", std::bind(&EventSink::onRequest, &eventSink.get(), _1));

  client->sendRequest("get", requestContent);
  queue->idle();

  Verify(Method(eventSink, onRequest));
  Verify(Method(eventSink, onResponseContent));
}

TEST_CASE("message queue is failing to route a response to a deleted client", "[MessageQueue]") {
  auto requestContent = Content::makeShared();
  auto responseContent = Content::makeUnique();

  Mock<EventSink> eventSink;
  When(Method(eventSink, onRequest)).Do([&](const Content&) {
    return std::move(responseContent);
  });

  Mock<ILogger> loggerInstanse;
  Fake(Method(loggerInstanse, message));
  Fake(Method(loggerInstanse, error));
  
  auto logger = ILogger::Shared(&loggerInstanse.get(), [](...) {});
  auto queue = MessageQueue::makeUnique(logger);

  QueueResourceClient* clientPtr;
  {
    auto client = queue->createResourceClient("clientId", "resource");
    clientPtr = client.get();
  }

  auto controller = queue->createResourceController("resource");
  controller->addOnRequest<Content>("get", std::bind(&EventSink::onRequest, &eventSink.get(), _1));

  clientPtr->sendRequest("get", requestContent);
  queue->idle();

  Verify(Method(eventSink, onRequest));
  Verify(Method(loggerInstanse, message));
  Verify(Method(loggerInstanse, error));
}

TEST_CASE("message queue is failing to route a request if there is no controller to handle it", "[MessageQueue]") {
  auto requestContent = Content::makeShared();

  Mock<EventSink> eventSink;

  When(Method(eventSink, onResponseStatus)).Do([=](const Status& status) {
{'fraction_non_alphanumeric': 0.16243654822335024, 'fraction_numerical': 0.0019207024283166415, 'mean_word_length': 6.00240153698367, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '506441', 'n_tokens_mistral': 4347, 'n_tokens_neox': 4341, 'n_words': 879}