Skip to content

Commit

Permalink
fix: properly trigger events after push events
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyTWF committed Oct 5, 2022
1 parent 4b021f8 commit 25382df
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.cpp
@@ -1,14 +1,15 @@
#include "PrenoteAcknowledgedPushEventHandler.h"
#include "PrenoteMessage.h"
#include "PrenoteMessageCollection.h"
#include "PrenoteMessageEventHandlerCollection.h"
#include "push/PushEventSubscription.h"

using UKControllerPlugin::Push::PushEventSubscription;

namespace UKControllerPlugin::Prenote {
PrenoteAcknowledgedPushEventHandler::PrenoteAcknowledgedPushEventHandler(
std::shared_ptr<PrenoteMessageCollection> prenotes)
: prenotes(std::move(prenotes))
std::shared_ptr<PrenoteMessageCollection> prenotes, const PrenoteMessageEventHandlerCollection& eventHandlers)
: prenotes(std::move(prenotes)), eventHandlers(eventHandlers)
{
}

Expand All @@ -21,7 +22,9 @@ namespace UKControllerPlugin::Prenote {
}

int prenoteId = messageData.at("id").get<int>();
this->prenotes->GetById(prenoteId)->Acknowledge();
const auto prenote = prenotes->GetById(prenoteId);
prenote->Acknowledge();
eventHandlers.MessageAcknowledged(*prenote);
LogInfo("Acknowledged prenote id " + std::to_string(prenoteId));
}

Expand Down
8 changes: 7 additions & 1 deletion src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.h
Expand Up @@ -3,11 +3,14 @@

namespace UKControllerPlugin::Prenote {
class PrenoteMessageCollection;
class PrenoteMessageEventHandlerCollection;

class PrenoteAcknowledgedPushEventHandler : public UKControllerPlugin::Push::PushEventProcessorInterface
{
public:
explicit PrenoteAcknowledgedPushEventHandler(std::shared_ptr<PrenoteMessageCollection> prenotes);
explicit PrenoteAcknowledgedPushEventHandler(
std::shared_ptr<PrenoteMessageCollection> prenotes,
const PrenoteMessageEventHandlerCollection& eventHandlers);
void ProcessPushEvent(const Push::PushEvent& message) override;
[[nodiscard]] auto GetPushEventSubscriptions() const
-> std::set<UKControllerPlugin::Push::PushEventSubscription> override;
Expand All @@ -17,5 +20,8 @@ namespace UKControllerPlugin::Prenote {

// All the prenotes
const std::shared_ptr<PrenoteMessageCollection> prenotes;

// Handles prenote events
const PrenoteMessageEventHandlerCollection& eventHandlers;
};
} // namespace UKControllerPlugin::Prenote
9 changes: 6 additions & 3 deletions src/plugin/prenote/PrenoteDeletedPushEventHandler.cpp
@@ -1,12 +1,14 @@
#include "PrenoteDeletedPushEventHandler.h"
#include "PrenoteMessage.h"
#include "PrenoteMessageCollection.h"
#include "PrenoteMessageEventHandlerCollection.h"

using UKControllerPlugin::Push::PushEventSubscription;

namespace UKControllerPlugin::Prenote {
PrenoteDeletedPushEventHandler::PrenoteDeletedPushEventHandler(std::shared_ptr<PrenoteMessageCollection> prenotes)
: prenotes(std::move(prenotes))
PrenoteDeletedPushEventHandler::PrenoteDeletedPushEventHandler(
std::shared_ptr<PrenoteMessageCollection> prenotes, const PrenoteMessageEventHandlerCollection& eventHandlers)
: prenotes(std::move(prenotes)), eventHandlers(eventHandlers)
{
}

Expand All @@ -19,7 +21,8 @@ namespace UKControllerPlugin::Prenote {
}

int prenoteId = messageData.at("id").get<int>();
this->prenotes->Remove(prenoteId);
eventHandlers.MessageCancelled(*prenotes->GetById(prenoteId));
prenotes->Remove(prenoteId);
LogInfo("Deleted prenote id " + std::to_string(prenoteId));
}

Expand Down
8 changes: 7 additions & 1 deletion src/plugin/prenote/PrenoteDeletedPushEventHandler.h
Expand Up @@ -3,11 +3,14 @@

namespace UKControllerPlugin::Prenote {
class PrenoteMessageCollection;
class PrenoteMessageEventHandlerCollection;

class PrenoteDeletedPushEventHandler : public UKControllerPlugin::Push::PushEventProcessorInterface
{
public:
explicit PrenoteDeletedPushEventHandler(std::shared_ptr<PrenoteMessageCollection> prenotes);
explicit PrenoteDeletedPushEventHandler(
std::shared_ptr<PrenoteMessageCollection> prenotes,
const PrenoteMessageEventHandlerCollection& eventHandlers);
void ProcessPushEvent(const Push::PushEvent& message) override;
[[nodiscard]] auto GetPushEventSubscriptions() const
-> std::set<UKControllerPlugin::Push::PushEventSubscription> override;
Expand All @@ -17,5 +20,8 @@ namespace UKControllerPlugin::Prenote {

// All the prenotes
const std::shared_ptr<PrenoteMessageCollection> prenotes;

// Handles prenote events
const PrenoteMessageEventHandlerCollection& eventHandlers;
};
} // namespace UKControllerPlugin::Prenote
8 changes: 4 additions & 4 deletions src/plugin/prenote/PrenoteModule.cpp
Expand Up @@ -100,10 +100,10 @@ namespace UKControllerPlugin::Prenote {
// Push event processors
persistence.pushEventProcessors->AddProcessor(std::make_shared<NewPrenotePushEventHandler>(
persistence.prenotes, *persistence.controllerPositions, *persistence.prenoteMessageHandlers));
persistence.pushEventProcessors->AddProcessor(
std::make_shared<PrenoteAcknowledgedPushEventHandler>(persistence.prenotes));
persistence.pushEventProcessors->AddProcessor(
std::make_shared<PrenoteDeletedPushEventHandler>(persistence.prenotes));
persistence.pushEventProcessors->AddProcessor(std::make_shared<PrenoteAcknowledgedPushEventHandler>(
persistence.prenotes, *persistence.prenoteMessageHandlers));
persistence.pushEventProcessors->AddProcessor(std::make_shared<PrenoteDeletedPushEventHandler>(
persistence.prenotes, *persistence.prenoteMessageHandlers));
persistence.timedHandler->RegisterEvent(
std::make_shared<PrenoteMessageTimeout>(persistence.prenotes), MESSAGE_TIMEOUT_CHECK_INTERVAL);

Expand Down
12 changes: 11 additions & 1 deletion test/plugin/prenote/PrenoteAcknowledgedPushEventHandlerTest.cpp
Expand Up @@ -2,13 +2,15 @@
#include "prenote/PrenoteAcknowledgedPushEventHandler.h"
#include "prenote/PrenoteMessage.h"
#include "prenote/PrenoteMessageCollection.h"
#include "prenote/PrenoteMessageEventHandlerCollection.h"
#include "push/PushEvent.h"
#include "push/PushEventSubscription.h"

using UKControllerPlugin::Controller::ControllerPosition;
using UKControllerPlugin::Prenote::PrenoteAcknowledgedPushEventHandler;
using UKControllerPlugin::Prenote::PrenoteMessage;
using UKControllerPlugin::Prenote::PrenoteMessageCollection;
using UKControllerPlugin::Prenote::PrenoteMessageEventHandlerCollection;
using UKControllerPlugin::Push::PushEvent;
using UKControllerPlugin::Push::PushEventSubscription;

Expand All @@ -17,7 +19,7 @@ namespace UKControllerPluginTest::Prenote {
{
public:
PrenoteAcknowledgedPushEventHandlerTest()
: messages(std::make_shared<PrenoteMessageCollection>()), handler(messages)
: messages(std::make_shared<PrenoteMessageCollection>()), handler(messages, eventHandlers)
{
sendingPosition = std::make_shared<ControllerPosition>(
1, "EGKK_TWR", 124.225, std::vector<std::string>{"EGKK"}, true, false);
Expand All @@ -32,6 +34,8 @@ namespace UKControllerPluginTest::Prenote {
sendingPosition,
receivingPosition,
std::chrono::system_clock::now()));
mockHandler = std::make_shared<testing::NiceMock<MockPrenoteMessageEventHandlerInterface>>();
eventHandlers.AddHandler(mockHandler);
}

/*
Expand All @@ -51,9 +55,11 @@ namespace UKControllerPluginTest::Prenote {
return {"prenote-message.received", "test", eventData, eventData.dump()};
};

std::shared_ptr<testing::NiceMock<MockPrenoteMessageEventHandlerInterface>> mockHandler;
std::shared_ptr<ControllerPosition> sendingPosition;
std::shared_ptr<ControllerPosition> receivingPosition;
std::shared_ptr<PrenoteMessageCollection> messages;
PrenoteMessageEventHandlerCollection eventHandlers;
PrenoteAcknowledgedPushEventHandler handler;
};

Expand All @@ -67,24 +73,28 @@ namespace UKControllerPluginTest::Prenote {

TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItAcknowledgesPrenoteFromMessage)
{
EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(1);
this->handler.ProcessPushEvent(MakePushEvent());
EXPECT_TRUE(this->messages->GetById(1)->IsAcknowledged());
}

TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItHandlesMissingIdFromMessage)
{
EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(0);
this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object(), "id"));
EXPECT_FALSE(this->messages->GetById(1)->IsAcknowledged());
}

TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItHandlesIdNotIntegerFromMessage)
{
EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(0);
this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object({{"id", "abc"}})));
EXPECT_FALSE(this->messages->GetById(1)->IsAcknowledged());
}

TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItHandlesPrenoteNotFoundToAcknowledge)
{
EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(0);
this->messages->Remove(1);
EXPECT_NO_THROW(this->handler.ProcessPushEvent(MakePushEvent()));
}
Expand Down
12 changes: 11 additions & 1 deletion test/plugin/prenote/PrenoteDeletedPushEventHandlerTest.cpp
Expand Up @@ -2,11 +2,13 @@
#include "prenote/PrenoteDeletedPushEventHandler.h"
#include "prenote/PrenoteMessage.h"
#include "prenote/PrenoteMessageCollection.h"
#include "prenote/PrenoteMessageEventHandlerCollection.h"

using UKControllerPlugin::Controller::ControllerPosition;
using UKControllerPlugin::Prenote::PrenoteDeletedPushEventHandler;
using UKControllerPlugin::Prenote::PrenoteMessage;
using UKControllerPlugin::Prenote::PrenoteMessageCollection;
using UKControllerPlugin::Prenote::PrenoteMessageEventHandlerCollection;
using UKControllerPlugin::Push::PushEvent;
using UKControllerPlugin::Push::PushEventSubscription;

Expand All @@ -15,7 +17,7 @@ namespace UKControllerPluginTest::Prenote {
{
public:
PrenoteDeletedPushEventHandlerHandlerTest()
: messages(std::make_shared<PrenoteMessageCollection>()), handler(messages)
: messages(std::make_shared<PrenoteMessageCollection>()), handler(messages, eventHandlers)
{
sendingPosition = std::make_shared<ControllerPosition>(
1, "EGKK_TWR", 124.225, std::vector<std::string>{"EGKK"}, true, false);
Expand All @@ -30,6 +32,8 @@ namespace UKControllerPluginTest::Prenote {
sendingPosition,
receivingPosition,
std::chrono::system_clock::now()));
mockHandler = std::make_shared<testing::NiceMock<MockPrenoteMessageEventHandlerInterface>>();
eventHandlers.AddHandler(mockHandler);
}

/*
Expand All @@ -49,9 +53,11 @@ namespace UKControllerPluginTest::Prenote {
return {"prenote-message.received", "test", eventData, eventData.dump()};
};

std::shared_ptr<testing::NiceMock<MockPrenoteMessageEventHandlerInterface>> mockHandler;
std::shared_ptr<ControllerPosition> sendingPosition;
std::shared_ptr<ControllerPosition> receivingPosition;
std::shared_ptr<PrenoteMessageCollection> messages;
PrenoteMessageEventHandlerCollection eventHandlers;
PrenoteDeletedPushEventHandler handler;
};

Expand All @@ -64,24 +70,28 @@ namespace UKControllerPluginTest::Prenote {

TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItRemovesPrenoteFromMessage)
{
EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(1);
this->handler.ProcessPushEvent(MakePushEvent());
EXPECT_EQ(nullptr, this->messages->GetById(1));
}

TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItHandlesMissingIdFromMessage)
{
EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(0);
this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object(), "id"));
EXPECT_NE(nullptr, this->messages->GetById(1));
}

TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItHandlesIdNotIntegerFromMessage)
{
EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(0);
this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object({{"id", "abc"}})));
EXPECT_NE(nullptr, this->messages->GetById(1));
}

TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItHandlesPrenoteNotFoundToRemove)
{
EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(0);
this->messages->Remove(1);
EXPECT_NO_THROW(this->handler.ProcessPushEvent(MakePushEvent()));
}
Expand Down

0 comments on commit 25382df

Please sign in to comment.