From 581d2866049c05401b6c336153993ab44c5f6688 Mon Sep 17 00:00:00 2001 From: Sergey Yakubov <sergey.yakubov@desy.de> Date: Sun, 17 Jun 2018 16:20:42 +0200 Subject: [PATCH] more work on authorization handler --- receiver/CMakeLists.txt | 12 ++++-- receiver/src/receiver_config.cpp | 1 - receiver/src/receiver_config.h | 1 - receiver/src/receiver_error.h | 1 - receiver/src/request.cpp | 23 ++++++++++-- receiver/src/request.h | 16 ++++++-- receiver/src/request_handler_authorize.cpp | 19 ++++++++++ receiver/src/request_handler_authorize.h | 22 +++++++++++ receiver/src/request_handler_db_write.cpp | 8 +++- receiver/src/request_handler_db_write.h | 1 + receiver/src/request_handler_file_write.cpp | 2 +- receiver/src/requests_dispatcher.cpp | 2 +- receiver/unittests/mock_receiver_config.cpp | 1 - receiver/unittests/test_config.cpp | 2 - receiver/unittests/test_connection.cpp | 2 +- receiver/unittests/test_request.cpp | 10 ++++- receiver/unittests/test_request_factory.cpp | 34 ++++++++++++----- .../test_request_handler_db_writer.cpp | 37 ++++++++++++------- .../test_request_handler_file_write.cpp | 22 +++++++---- .../unittests/test_requests_dispatcher.cpp | 14 +++---- 20 files changed, 170 insertions(+), 60 deletions(-) create mode 100644 receiver/src/request_handler_authorize.cpp create mode 100644 receiver/src/request_handler_authorize.h diff --git a/receiver/CMakeLists.txt b/receiver/CMakeLists.txt index 20bf08ef3..5e3745068 100644 --- a/receiver/CMakeLists.txt +++ b/receiver/CMakeLists.txt @@ -1,15 +1,19 @@ set(TARGET_NAME receiver) set(SOURCE_FILES - src/receiver.h src/receiver.cpp - src/connection.h src/connection.cpp - src/receiver_error.h + src/receiver.cpp + src/connection.cpp src/request.cpp src/request_handler_file_write.cpp src/statistics.cpp src/statistics_sender_influx_db.cpp src/receiver_config.cpp src/producer_logger.cpp - src/request_handler_db_write.cpp src/statistics_sender_fluentd.cpp src/statistics_sender_fluentd.h src/connection_authorizer.cpp src/connection_authorizer.h src/requests_dispatcher.cpp src/requests_dispatcher.h) + src/request_handler_db_write.cpp + src/request_handler_authorize.cpp + src/statistics_sender_fluentd.cpp + src/connection_authorizer.cpp + src/requests_dispatcher.cpp +) ################################ diff --git a/receiver/src/receiver_config.cpp b/receiver/src/receiver_config.cpp index 7b737df1e..28977ad95 100644 --- a/receiver/src/receiver_config.cpp +++ b/receiver/src/receiver_config.cpp @@ -20,7 +20,6 @@ Error ReceiverConfigFactory::SetConfigFromFile(std::string file_name) { (err = parser.GetBool("WriteToDisk", &config.write_to_disk)) || (err = parser.GetBool("WriteToDb", &config.write_to_db)) || (err = parser.GetString("BrokerDbAddress", &config.broker_db_uri)) || - (err = parser.GetString("BrokerDbName", &config.broker_db_name)) || (err = parser.GetString("Tag", &config.tag)) || (err = parser.GetUInt64("AuthorizationInterval", &config.authorization_interval)) || (err = parser.GetString("RootFolder", &config.root_folder)) || diff --git a/receiver/src/receiver_config.h b/receiver/src/receiver_config.h index b3a5ff130..7fb5ad4bc 100644 --- a/receiver/src/receiver_config.h +++ b/receiver/src/receiver_config.h @@ -11,7 +11,6 @@ struct ReceiverConfig { std::string monitor_db_uri; std::string monitor_db_name; std::string broker_db_uri; - std::string broker_db_name; std::string root_folder; uint64_t listen_port = 0; uint64_t authorization_interval = 0; diff --git a/receiver/src/receiver_error.h b/receiver/src/receiver_error.h index cbe4434b3..c90a0d555 100644 --- a/receiver/src/receiver_error.h +++ b/receiver/src/receiver_error.h @@ -66,7 +66,6 @@ auto const kAuthorizationFailure = ReceiverErrorTemplate { "authorization failure", ReceiverErrorType::kAuthorizationFailure }; - }; } diff --git a/receiver/src/request.cpp b/receiver/src/request.cpp index 85b230a07..a3d8e1b5e 100644 --- a/receiver/src/request.cpp +++ b/receiver/src/request.cpp @@ -5,7 +5,8 @@ namespace asapo { Request::Request(const GenericRequestHeader& header, - SocketDescriptor socket_fd) : io__{GenerateDefaultIO()}, request_header_(header), socket_fd_{socket_fd} { + SocketDescriptor socket_fd,std::string origin_uri) : io__{GenerateDefaultIO()}, request_header_(header), + socket_fd_{socket_fd},origin_uri_{std::move(origin_uri)} { } Error Request::AllocateDataBuffer() { @@ -77,14 +78,24 @@ std::string Request::GetFileName() const { return std::to_string(request_header_.data_id) + ".bin"; } +const std::string &Request::GetOriginUri() const { + return origin_uri_; +} +const std::string &Request::GetBeamtimeId() const { + return beamtime_id_; +} +void Request::SetBeamtimeID(std::string beamtime_id) { + beamtime_id_ = std::move(beamtime_id); +} + std::unique_ptr<Request> RequestFactory::GenerateRequest(const GenericRequestHeader& - request_header, SocketDescriptor socket_fd, + request_header, SocketDescriptor socket_fd,std::string origin_uri, Error* err) const noexcept { *err = nullptr; + auto request = std::unique_ptr<Request> {new Request{request_header, socket_fd,std::move(origin_uri)}}; switch (request_header.op_code) { case Opcode::kOpcodeTransferData: { - auto request = std::unique_ptr<Request> {new Request{request_header, socket_fd}}; - + request->AddHandler(&request_handler_authorize_); if (GetReceiverConfig()->write_to_disk) { request->AddHandler(&request_handler_filewrite_); } @@ -95,6 +106,10 @@ std::unique_ptr<Request> RequestFactory::GenerateRequest(const GenericRequestHea return request; } + case Opcode::kOpcodeAuthorize: { + request->AddHandler(&request_handler_authorize_); + return request; + } default: *err = ReceiverErrorTemplates::kInvalidOpCode.Generate(); return nullptr; diff --git a/receiver/src/request.h b/receiver/src/request.h index 3effd5fb2..7c518ed48 100644 --- a/receiver/src/request.h +++ b/receiver/src/request.h @@ -1,12 +1,16 @@ #ifndef ASAPO_REQUEST_H #define ASAPO_REQUEST_H +#include <string> + #include "receiver_error.h" #include "common/networking.h" #include "io/io.h" #include "request_handler.h" #include "request_handler_file_write.h" #include "request_handler_db_write.h" +#include "request_handler_authorize.h" + #include "statistics.h" #include "preprocessor/definitions.h" @@ -18,14 +22,17 @@ class Request { public: VIRTUAL Error Handle(Statistics*); ~Request() = default; - Request(const GenericRequestHeader& request_header, SocketDescriptor socket_fd); + Request(const GenericRequestHeader& request_header, SocketDescriptor socket_fd,std::string origin_uri); VIRTUAL void AddHandler(const RequestHandler*); VIRTUAL const RequestHandlerList& GetListHandlers() const; VIRTUAL uint64_t GetDataSize() const; VIRTUAL uint64_t GetDataID() const; VIRTUAL std::string GetFileName() const; VIRTUAL const FileData& GetData() const; - std::unique_ptr<IO> io__; + const std::string& GetOriginUri() const; + VIRTUAL const std::string& GetBeamtimeId() const; + void SetBeamtimeID(std::string beamtime_id); + std::unique_ptr<IO> io__; private: Error AllocateDataBuffer(); Error ReceiveData(); @@ -33,15 +40,18 @@ class Request { const SocketDescriptor socket_fd_; FileData data_buffer_; RequestHandlerList handlers_; + std::string origin_uri_; + std::string beamtime_id_; }; class RequestFactory { public: virtual std::unique_ptr<Request> GenerateRequest(const GenericRequestHeader& request_header, - SocketDescriptor socket_fd, Error* err) const noexcept; + SocketDescriptor socket_fd,std::string origin_uri, Error* err) const noexcept; private: RequestHandlerFileWrite request_handler_filewrite_; RequestHandlerDbWrite request_handler_dbwrite_; + RequestHandlerAuthorize request_handler_authorize_; }; } diff --git a/receiver/src/request_handler_authorize.cpp b/receiver/src/request_handler_authorize.cpp new file mode 100644 index 000000000..361f7fc6e --- /dev/null +++ b/receiver/src/request_handler_authorize.cpp @@ -0,0 +1,19 @@ +#include "request_handler_authorize.h" +#include "receiver_config.h" +#include "receiver_logger.h" + +namespace asapo { + +Error RequestHandlerAuthorize::ProcessRequest(const Request& request) const { + return nullptr; +} + +RequestHandlerAuthorize::RequestHandlerAuthorize(): log__{GetDefaultReceiverLogger()} { +} + +StatisticEntity RequestHandlerAuthorize::GetStatisticEntity() const { + return StatisticEntity::kDatabase; +} + + +} \ No newline at end of file diff --git a/receiver/src/request_handler_authorize.h b/receiver/src/request_handler_authorize.h new file mode 100644 index 000000000..7dc730cfa --- /dev/null +++ b/receiver/src/request_handler_authorize.h @@ -0,0 +1,22 @@ +#ifndef ASAPO_REQUEST_HANDLER_AUTHORIZE_H +#define ASAPO_REQUEST_HANDLER_AUTHORIZE_H + +#include "request_handler.h" +#include "logger/logger.h" + +#include "io/io.h" + +namespace asapo { + +class RequestHandlerAuthorize final: public RequestHandler { + public: + RequestHandlerAuthorize(); + StatisticEntity GetStatisticEntity() const override; + Error ProcessRequest(const Request& request) const override; + const AbstractLogger* log__; + private: +}; + +} + +#endif //ASAPO_REQUEST_HANDLER_AUTHORIZE_H diff --git a/receiver/src/request_handler_db_write.cpp b/receiver/src/request_handler_db_write.cpp index 9461205a9..17580b9b7 100644 --- a/receiver/src/request_handler_db_write.cpp +++ b/receiver/src/request_handler_db_write.cpp @@ -6,6 +6,10 @@ namespace asapo { Error RequestHandlerDbWrite::ProcessRequest(const Request& request) const { + if (db_name_.empty()) { + db_name_=request.GetBeamtimeId(); + } + if (Error err = ConnectToDbIfNeeded() ) { return err; } @@ -16,7 +20,7 @@ Error RequestHandlerDbWrite::ProcessRequest(const Request& request) const { file_info.id = request.GetDataID(); auto err = db_client__->Insert(file_info, false); if (!err) { - log__->Debug(std::string{"insert record to "} + kDBCollectionName + " in " + GetReceiverConfig()->broker_db_name + + log__->Debug(std::string{"insert record to "} + kDBCollectionName + " in " + db_name_ + " at " + GetReceiverConfig()->broker_db_uri); } return err; @@ -34,7 +38,7 @@ StatisticEntity RequestHandlerDbWrite::GetStatisticEntity() const { Error RequestHandlerDbWrite::ConnectToDbIfNeeded() const { if (!connected_to_db) { - Error err = db_client__->Connect(GetReceiverConfig()->broker_db_uri, GetReceiverConfig()->broker_db_name, + Error err = db_client__->Connect(GetReceiverConfig()->broker_db_uri, db_name_, kDBCollectionName); if (err) { return err; diff --git a/receiver/src/request_handler_db_write.h b/receiver/src/request_handler_db_write.h index 8fbd671dc..f93ed822c 100644 --- a/receiver/src/request_handler_db_write.h +++ b/receiver/src/request_handler_db_write.h @@ -19,6 +19,7 @@ class RequestHandlerDbWrite final: public RequestHandler { private: Error ConnectToDbIfNeeded() const; mutable bool connected_to_db = false; + mutable std::string db_name_; }; } diff --git a/receiver/src/request_handler_file_write.cpp b/receiver/src/request_handler_file_write.cpp index 62f0c6395..f171484fd 100644 --- a/receiver/src/request_handler_file_write.cpp +++ b/receiver/src/request_handler_file_write.cpp @@ -16,7 +16,7 @@ Error RequestHandlerFileWrite::ProcessRequest(const Request& request) const { const FileData& data = request.GetData(); auto fname = request.GetFileName(); - auto root_folder = GetReceiverConfig()->root_folder + kPathSeparator; + auto root_folder = GetReceiverConfig()->root_folder + kPathSeparator + request.GetBeamtimeId()+kPathSeparator; auto err = io__->WriteDataToFile(root_folder + fname, data, fsize); if (!err) { log__->Debug("saved file of size " + std::to_string(fsize) + " to " + root_folder + fname); diff --git a/receiver/src/requests_dispatcher.cpp b/receiver/src/requests_dispatcher.cpp index e483cc121..33bdd3f7e 100644 --- a/receiver/src/requests_dispatcher.cpp +++ b/receiver/src/requests_dispatcher.cpp @@ -64,7 +64,7 @@ return nullptr; } statistics__-> StopTimer(); -auto request = request_factory__->GenerateRequest(generic_request_header, socket_fd_, err); +auto request = request_factory__->GenerateRequest(generic_request_header, socket_fd_,producer_uri_, err); if (*err) { log__->Error("error processing request from " + producer_uri_+" - "+(*err)-> Explain() diff --git a/receiver/unittests/mock_receiver_config.cpp b/receiver/unittests/mock_receiver_config.cpp index 1408de29b..1783f3fac 100644 --- a/receiver/unittests/mock_receiver_config.cpp +++ b/receiver/unittests/mock_receiver_config.cpp @@ -36,7 +36,6 @@ Error SetReceiverConfig (const ReceiverConfig& config) { auto config_string = std::string("{\"MonitorDbAddress\":") + "\"" + config.monitor_db_uri + "\""; config_string += "," + std::string("\"MonitorDbName\":") + "\"" + config.monitor_db_name + "\""; - config_string += "," + std::string("\"BrokerDbName\":") + "\"" + config.broker_db_name + "\""; config_string += "," + std::string("\"BrokerDbAddress\":") + "\"" + config.broker_db_uri + "\""; config_string += "," + std::string("\"ListenPort\":") + std::to_string(config.listen_port); config_string += "," + std::string("\"AuthorizationInterval\":") + std::to_string(config.authorization_interval); diff --git a/receiver/unittests/test_config.cpp b/receiver/unittests/test_config.cpp index 856fad31f..4dba7c993 100644 --- a/receiver/unittests/test_config.cpp +++ b/receiver/unittests/test_config.cpp @@ -56,7 +56,6 @@ TEST_F(ConfigTests, ReadSettings) { test_config.write_to_disk = true; test_config.write_to_db = true; test_config.broker_db_uri = "localhost:27017"; - test_config.broker_db_name = "test"; test_config.log_level = asapo::LogLevel::Error; test_config.root_folder = "test_fodler"; test_config.authorization_interval = 10000; @@ -70,7 +69,6 @@ TEST_F(ConfigTests, ReadSettings) { ASSERT_THAT(config->monitor_db_uri, Eq("localhost:8086")); ASSERT_THAT(config->monitor_db_name, Eq("db_test")); ASSERT_THAT(config->broker_db_uri, Eq("localhost:27017")); - ASSERT_THAT(config->broker_db_name, Eq("test")); ASSERT_THAT(config->listen_port, Eq(4200)); ASSERT_THAT(config->authorization_interval, Eq(10000)); ASSERT_THAT(config->write_to_disk, Eq(true)); diff --git a/receiver/unittests/test_connection.cpp b/receiver/unittests/test_connection.cpp index a4c336d16..ca2be07d1 100644 --- a/receiver/unittests/test_connection.cpp +++ b/receiver/unittests/test_connection.cpp @@ -121,7 +121,7 @@ class ConnectionTests : public Test { )); return nullptr; } else { - auto request = new Request(GenericRequestHeader{asapo::kOpcodeUnknownOp,0,1,""},0); + auto request = new Request(GenericRequestHeader{asapo::kOpcodeUnknownOp,0,1,""},0,connected_uri); EXPECT_CALL(mock_dispatcher, GetNextRequest_t(_)) .WillOnce(DoAll( SetArgPointee<0>(nullptr), diff --git a/receiver/unittests/test_request.cpp b/receiver/unittests/test_request.cpp index 6f75bdc28..f3fb5b5b8 100644 --- a/receiver/unittests/test_request.cpp +++ b/receiver/unittests/test_request.cpp @@ -67,6 +67,7 @@ class RequestTests : public Test { asapo::SocketDescriptor socket_fd_{1}; uint64_t data_size_ {100}; uint64_t data_id_{15}; + std::string expected_origin_uri="origin_uri"; std::unique_ptr<Request> request; NiceMock<MockIO> mock_io; NiceMock<MockStatistics> mock_statistics; @@ -75,7 +76,7 @@ class RequestTests : public Test { stat = &mock_statistics; generic_request_header.data_size = data_size_; generic_request_header.data_id = data_id_; - request.reset(new Request{generic_request_header, socket_fd_}); + request.reset(new Request{generic_request_header, socket_fd_,expected_origin_uri}); request->io__ = std::unique_ptr<asapo::IO> {&mock_io}; ON_CALL(mock_io, Receive_t(socket_fd_, _, data_size_, _)).WillByDefault( DoAll(SetArgPointee<3>(nullptr), @@ -91,7 +92,7 @@ class RequestTests : public Test { TEST_F(RequestTests, HandleDoesNotReceiveEmptyData) { generic_request_header.data_size = 0; request->io__.release(); - request.reset(new Request{generic_request_header, socket_fd_}); + request.reset(new Request{generic_request_header, socket_fd_,""}); request->io__ = std::unique_ptr<asapo::IO> {&mock_io};; EXPECT_CALL(mock_io, Receive_t(_, _, _, _)).Times(0); @@ -178,6 +179,11 @@ TEST_F(RequestTests, GetDataID) { ASSERT_THAT(id, Eq(data_id_)); } +TEST_F(RequestTests, OriginUriEmptyByDefault) { + auto uri = request->GetOriginUri(); + + ASSERT_THAT(uri, Eq(expected_origin_uri)); +} TEST_F(RequestTests, GetDataSize) { diff --git a/receiver/unittests/test_request_factory.cpp b/receiver/unittests/test_request_factory.cpp index f3a34e840..46b3f8c7d 100644 --- a/receiver/unittests/test_request_factory.cpp +++ b/receiver/unittests/test_request_factory.cpp @@ -9,6 +9,8 @@ #include "../src/request_handler.h" #include "../src/request_handler_file_write.h" #include "../src/request_handler_db_write.h" +#include "../src/request_handler_authorize.h" + #include "database/database.h" #include "mock_statistics.h" @@ -52,7 +54,7 @@ class FactoryTests : public Test { Error err{nullptr}; GenericRequestHeader generic_request_header; ReceiverConfig config; - + std::string origin_uri{"origin_uri"}; void SetUp() override { generic_request_header.op_code = asapo::Opcode::kOpcodeTransferData; config.write_to_disk = true; @@ -65,29 +67,42 @@ class FactoryTests : public Test { TEST_F(FactoryTests, ErrorOnWrongCode) { generic_request_header.op_code = asapo::Opcode::kOpcodeUnknownOp; - auto request = factory.GenerateRequest(generic_request_header, 1, &err); + auto request = factory.GenerateRequest(generic_request_header, 1,origin_uri, &err); ASSERT_THAT(err, Ne(nullptr)); } TEST_F(FactoryTests, ReturnsDataRequestOnkNetOpcodeSendDataCode) { generic_request_header.op_code = asapo::Opcode::kOpcodeTransferData; - auto request = factory.GenerateRequest(generic_request_header, 1, &err); + auto request = factory.GenerateRequest(generic_request_header, 1,origin_uri, &err); ASSERT_THAT(err, Eq(nullptr)); ASSERT_THAT(dynamic_cast<asapo::Request*>(request.get()), Ne(nullptr)); - ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerFileWrite*>(request->GetListHandlers()[0]), Ne(nullptr)); + ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerAuthorize*>(request->GetListHandlers()[0]), Ne(nullptr)); + ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerFileWrite*>(request->GetListHandlers()[1]), Ne(nullptr)); ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerDbWrite*>(request->GetListHandlers().back()), Ne(nullptr)); } + +TEST_F(FactoryTests, ReturnsDataRequestForAuthorizationCode) { + generic_request_header.op_code = asapo::Opcode::kOpcodeAuthorize; + auto request = factory.GenerateRequest(generic_request_header, 1,origin_uri, &err); + + ASSERT_THAT(err, Eq(nullptr)); + ASSERT_THAT(dynamic_cast<asapo::Request*>(request.get()), Ne(nullptr)); + ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerAuthorize*>(request->GetListHandlers()[0]), Ne(nullptr)); +} + + TEST_F(FactoryTests, DoNotAddDiskWriterIfNotWanted) { config.write_to_disk = false; SetReceiverConfig(config); - auto request = factory.GenerateRequest(generic_request_header, 1, &err); + auto request = factory.GenerateRequest(generic_request_header, 1,origin_uri, &err); ASSERT_THAT(err, Eq(nullptr)); - ASSERT_THAT(request->GetListHandlers().size(), Eq(1)); + ASSERT_THAT(request->GetListHandlers().size(), Eq(2)); + ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerAuthorize*>(request->GetListHandlers()[0]), Ne(nullptr)); ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerDbWrite*>(request->GetListHandlers().back()), Ne(nullptr)); } @@ -96,10 +111,11 @@ TEST_F(FactoryTests, DoNotAddDbWriterIfNotWanted) { SetReceiverConfig(config); - auto request = factory.GenerateRequest(generic_request_header, 1, &err); + auto request = factory.GenerateRequest(generic_request_header, 1,origin_uri, &err); ASSERT_THAT(err, Eq(nullptr)); - ASSERT_THAT(request->GetListHandlers().size(), Eq(1)); - ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerFileWrite*>(request->GetListHandlers()[0]), Ne(nullptr)); + ASSERT_THAT(request->GetListHandlers().size(), Eq(2)); + ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerAuthorize*>(request->GetListHandlers()[0]), Ne(nullptr)); + ASSERT_THAT(dynamic_cast<const asapo::RequestHandlerFileWrite*>(request->GetListHandlers()[1]), Ne(nullptr)); } diff --git a/receiver/unittests/test_request_handler_db_writer.cpp b/receiver/unittests/test_request_handler_db_writer.cpp index 465b1efbb..ff24f9daa 100644 --- a/receiver/unittests/test_request_handler_db_writer.cpp +++ b/receiver/unittests/test_request_handler_db_writer.cpp @@ -50,35 +50,40 @@ using asapo::ReceiverConfig; namespace { -class MockRequestHandler: public Request { +class MockRequest: public Request { public: - MockRequestHandler(const GenericRequestHeader& request_header, SocketDescriptor socket_fd): - Request(request_header, socket_fd) {}; + MockRequest(const GenericRequestHeader& request_header, SocketDescriptor socket_fd): + Request(request_header, socket_fd,"") {}; MOCK_CONST_METHOD0(GetFileName, std::string()); MOCK_CONST_METHOD0(GetDataSize, uint64_t()); MOCK_CONST_METHOD0(GetDataID, uint64_t()); MOCK_CONST_METHOD0(GetData, const asapo::FileData & ()); + MOCK_CONST_METHOD0(GetBeamtimeId, const std::string & ()); }; class DbWriterHandlerTests : public Test { public: RequestHandlerDbWrite handler; NiceMock<MockIO> mock_io; - std::unique_ptr<NiceMock<MockRequestHandler>> mock_request; + std::unique_ptr<NiceMock<MockRequest>> mock_request; NiceMock<MockDatabase> mock_db; NiceMock<asapo::MockLogger> mock_logger; ReceiverConfig config; - void SetUp() override { + std::string expected_beamtime_id = "beamtime_id"; + void SetUp() override { GenericRequestHeader request_header; request_header.data_id = 2; handler.db_client__ = std::unique_ptr<asapo::Database> {&mock_db}; handler.log__ = &mock_logger; - mock_request.reset(new NiceMock<MockRequestHandler> {request_header, 1}); - } + mock_request.reset(new NiceMock<MockRequest> {request_header, 1}); + ON_CALL(*mock_request, GetBeamtimeId()).WillByDefault(ReturnRef(expected_beamtime_id)); + } void TearDown() override { handler.db_client__.release(); } + + }; TEST(DBWritewr, Constructor) { @@ -96,11 +101,15 @@ TEST_F(DbWriterHandlerTests, CheckStatisticEntity) { TEST_F(DbWriterHandlerTests, ProcessRequestCallsConnectDbWhenNotConnected) { - config.broker_db_name = "test"; config.broker_db_uri = "127.0.0.1:27017"; SetReceiverConfig(config); - EXPECT_CALL(mock_db, Connect_t("127.0.0.1:27017", "test", asapo::kDBCollectionName)). + + EXPECT_CALL(*mock_request, GetBeamtimeId()) + .WillOnce(ReturnRef(expected_beamtime_id)) + ; + + EXPECT_CALL(mock_db, Connect_t("127.0.0.1:27017", expected_beamtime_id, asapo::kDBCollectionName)). WillOnce(testing::Return(nullptr)); auto err = handler.ProcessRequest(*mock_request); @@ -137,13 +146,15 @@ MATCHER_P(CompareFileInfo, file, "") { } - TEST_F(DbWriterHandlerTests, CallsInsert) { - config.broker_db_name = "test"; config.broker_db_uri = "127.0.0.1:27017"; SetReceiverConfig(config); - EXPECT_CALL(mock_db, Connect_t(config.broker_db_uri, config.broker_db_name, asapo::kDBCollectionName)). + EXPECT_CALL(*mock_request, GetBeamtimeId()) + .WillOnce(ReturnRef(expected_beamtime_id)) + ; + + EXPECT_CALL(mock_db, Connect_t(config.broker_db_uri, expected_beamtime_id, asapo::kDBCollectionName)). WillOnce(testing::Return(nullptr)); std::string expected_file_name = "2.bin"; @@ -172,7 +183,7 @@ TEST_F(DbWriterHandlerTests, CallsInsert) { EXPECT_CALL(mock_logger, Debug(AllOf(HasSubstr("insert record"), HasSubstr(config.broker_db_uri), - HasSubstr(config.broker_db_name), + HasSubstr(expected_beamtime_id), HasSubstr(asapo::kDBCollectionName) ) ) diff --git a/receiver/unittests/test_request_handler_file_write.cpp b/receiver/unittests/test_request_handler_file_write.cpp index 07a5edac2..16558f684 100644 --- a/receiver/unittests/test_request_handler_file_write.cpp +++ b/receiver/unittests/test_request_handler_file_write.cpp @@ -47,29 +47,31 @@ TEST(FileWrite, Constructor) { } -class MockRequestHandler: public Request { +class MockRequest: public Request { public: - MockRequestHandler(const GenericRequestHeader& request_header, SocketDescriptor socket_fd): - Request(request_header, socket_fd) {}; + MockRequest(const GenericRequestHeader& request_header, SocketDescriptor socket_fd): + Request(request_header, socket_fd,"") {}; MOCK_CONST_METHOD0(GetFileName, std::string()); MOCK_CONST_METHOD0(GetDataSize, uint64_t()); MOCK_CONST_METHOD0(GetData, const asapo::FileData & ()); + MOCK_CONST_METHOD0(GetBeamtimeId, const std::string & ()); }; class FileWriteHandlerTests : public Test { public: RequestHandlerFileWrite handler; NiceMock<MockIO> mock_io; - std::unique_ptr<MockRequestHandler> mock_request; + std::unique_ptr<MockRequest> mock_request; NiceMock<asapo::MockLogger> mock_logger; std::string expected_file_name = "2.bin"; - uint64_t expected_file_size = 10; + std::string expected_beamtime_id = "beamtime_id"; + uint64_t expected_file_size = 10; void MockRequestData(); void SetUp() override { GenericRequestHeader request_header; request_header.data_id = 2; - mock_request.reset(new MockRequestHandler{request_header, 1}); + mock_request.reset(new MockRequest{request_header, 1}); handler.io__ = std::unique_ptr<asapo::IO> {&mock_io}; handler.log__ = &mock_logger; } @@ -115,6 +117,10 @@ void FileWriteHandlerTests::MockRequestData() { .WillOnce(ReturnRef(data)) ; + EXPECT_CALL(*mock_request, GetBeamtimeId()) + .WillOnce(ReturnRef(expected_beamtime_id)) + ; + EXPECT_CALL(*mock_request, GetFileName()) .WillOnce(Return(expected_file_name)) ; @@ -128,7 +134,8 @@ TEST_F(FileWriteHandlerTests, CallsWriteFile) { MockRequestData(); - std::string expected_path = std::string("test_folder") + asapo::kPathSeparator + expected_file_name; + std::string expected_path = std::string("test_folder") + asapo::kPathSeparator + expected_beamtime_id + + asapo::kPathSeparator + expected_file_name; EXPECT_CALL(mock_io, WriteDataToFile_t(expected_path.c_str(), _, expected_file_size)) .WillOnce( @@ -150,6 +157,7 @@ TEST_F(FileWriteHandlerTests, WritesToLog) { EXPECT_CALL(mock_logger, Debug(AllOf(HasSubstr("saved file"), HasSubstr(expected_file_name), + HasSubstr(expected_beamtime_id), HasSubstr(std::to_string(expected_file_size)) ) ) diff --git a/receiver/unittests/test_requests_dispatcher.cpp b/receiver/unittests/test_requests_dispatcher.cpp index f013f9275..6e41c2af0 100644 --- a/receiver/unittests/test_requests_dispatcher.cpp +++ b/receiver/unittests/test_requests_dispatcher.cpp @@ -65,7 +65,7 @@ TEST(RequestDispatcher, Constructor) { class MockRequest: public Request { public: MockRequest(const GenericRequestHeader& request_header, SocketDescriptor socket_fd): - Request(request_header, socket_fd) {}; + Request(request_header, socket_fd,"") {}; Error Handle(Statistics* statistics) override { return Error{Handle_t()}; }; @@ -76,16 +76,16 @@ class MockRequest: public Request { class MockRequestFactory: public asapo::RequestFactory { public: std::unique_ptr<Request> GenerateRequest(const GenericRequestHeader& request_header, - SocketDescriptor socket_fd, + SocketDescriptor socket_fd,std::string origin_uri, Error* err) const noexcept override { ErrorInterface* error = nullptr; - auto res = GenerateRequest_t(request_header, socket_fd, &error); + auto res = GenerateRequest_t(request_header, socket_fd,origin_uri, &error); err->reset(error); return std::unique_ptr<Request> {res}; } - MOCK_CONST_METHOD3(GenerateRequest_t, Request * (const GenericRequestHeader&, - SocketDescriptor socket_fd, + MOCK_CONST_METHOD4(GenerateRequest_t, Request * (const GenericRequestHeader&, + SocketDescriptor ,std::string , ErrorInterface**)); }; @@ -150,9 +150,9 @@ class RequestsDispatcherTests : public Test { } void MockCreateRequest(bool error ){ - EXPECT_CALL(mock_factory, GenerateRequest_t(_, _, _)) + EXPECT_CALL(mock_factory, GenerateRequest_t(_, _, _,_)) .WillOnce( - DoAll(SetArgPointee<2>(error?asapo::ReceiverErrorTemplates::kInvalidOpCode.Generate().release():nullptr), + DoAll(SetArgPointee<3>(error?asapo::ReceiverErrorTemplates::kInvalidOpCode.Generate().release():nullptr), Return(nullptr)) ); if (error) { -- GitLab