verion which did not include the handling in RTService wth creating the
Json file
This commit is contained in:
85
vrpmdvaotserver/test/MonitoringControllerTest.cpp
Normal file
85
vrpmdvaotserver/test/MonitoringControllerTest.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "MonitoringControllerTest.hpp"
|
||||
|
||||
#include "oatpp/web/client/HttpRequestExecutor.hpp"
|
||||
#include "oatpp-test/web/ClientServerTestRunner.hpp"
|
||||
|
||||
#include "controller/MonitoringController.hpp"
|
||||
|
||||
#include "app/TestClient.hpp"
|
||||
#include "app/TestComponent.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
void MonitoringControllerTest::onRun() {
|
||||
|
||||
/* Remove test database file before running the test */
|
||||
OATPP_LOGI(TAG, "DB-File='%s'", TESTDATABASE_FILE);
|
||||
std::remove(TESTDATABASE_FILE);
|
||||
|
||||
/* Register test components */
|
||||
TestComponent component;
|
||||
|
||||
/* Create client-server test runner */
|
||||
oatpp::test::web::ClientServerTestRunner runner;
|
||||
|
||||
/* Add MonitoringController endpoints to the router of the test server */
|
||||
runner.addController(std::make_shared<MonitoringController>());
|
||||
|
||||
/* Run test */
|
||||
runner.run([this, &runner] {
|
||||
|
||||
/* Get client connection provider for Api Client */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
|
||||
|
||||
/* Get object mapper component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
|
||||
|
||||
/* Create http request executor for Api Client */
|
||||
auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider);
|
||||
|
||||
/* Create Test API client */
|
||||
auto client = TestClient::createShared(requestExecutor, objectMapper);
|
||||
|
||||
auto dto = MonitoringDto::createShared();
|
||||
|
||||
dto->name = "Monitoring1";
|
||||
dto->samplerate = "5000";
|
||||
dto->sampleperiod = "3";
|
||||
dto->downtime = "10";
|
||||
// dto->status =
|
||||
|
||||
/* Call server API */
|
||||
auto addedMonResponse = client->addMonitoring(dto);
|
||||
|
||||
/* Assert that server responds with 200 */
|
||||
OATPP_ASSERT(addedMonResponse->getStatusCode() == 200);
|
||||
|
||||
/* Read response body as MessageDto */
|
||||
auto addedMonDto = addedMonResponse->readBodyToDto<oatpp::Object<MonitoringDto>>(objectMapper.get());
|
||||
|
||||
int addedMonId = addedMonDto->id;
|
||||
|
||||
/* Assert that monitoring has been added */
|
||||
auto newMonResponse = client->getMonitoring(addedMonId);
|
||||
|
||||
OATPP_ASSERT(newMonResponse->getStatusCode() == 200);
|
||||
|
||||
auto newMonDto = newMonResponse->readBodyToDto<oatpp::Object<MonitoringDto>>(objectMapper.get());
|
||||
|
||||
OATPP_ASSERT(newMonDto->id == addedMonId);
|
||||
|
||||
/* Delete newly added Monitorings */
|
||||
auto deletedMonResponse = client->deleteMonitoring(addedMonId);
|
||||
|
||||
OATPP_ASSERT(deletedMonResponse->getStatusCode() == 200);
|
||||
|
||||
}, std::chrono::minutes(10) /* test timeout */);
|
||||
|
||||
/* wait all server threads finished */
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
/* stop db connection pool */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
|
||||
dbConnectionProvider->stop();
|
||||
|
||||
}
|
||||
14
vrpmdvaotserver/test/MonitoringControllerTest.hpp
Normal file
14
vrpmdvaotserver/test/MonitoringControllerTest.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef MonitoringControllerTest_hpp
|
||||
#define MonitoringControllerTest_hpp
|
||||
|
||||
#include "oatpp-test/UnitTest.hpp"
|
||||
|
||||
class MonitoringControllerTest : public oatpp::test::UnitTest {
|
||||
public:
|
||||
MonitoringControllerTest() : oatpp::test::UnitTest("TEST[MonitoringControllerTest]")
|
||||
{}
|
||||
|
||||
void onRun() override;
|
||||
};
|
||||
|
||||
#endif // MonitoringControllerTest_hpp
|
||||
83
vrpmdvaotserver/test/UserControllerTest.cpp
Normal file
83
vrpmdvaotserver/test/UserControllerTest.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "UserControllerTest.hpp"
|
||||
|
||||
#include "oatpp/web/client/HttpRequestExecutor.hpp"
|
||||
#include "oatpp-test/web/ClientServerTestRunner.hpp"
|
||||
|
||||
#include "controller/UserController.hpp"
|
||||
|
||||
#include "app/TestClient.hpp"
|
||||
#include "app/TestComponent.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
void UserControllerTest::onRun() {
|
||||
|
||||
/* Remove test database file before running the test */
|
||||
OATPP_LOGI(TAG, "DB-File='%s'", TESTDATABASE_FILE);
|
||||
std::remove(TESTDATABASE_FILE);
|
||||
|
||||
/* Register test components */
|
||||
TestComponent component;
|
||||
|
||||
/* Create client-server test runner */
|
||||
oatpp::test::web::ClientServerTestRunner runner;
|
||||
|
||||
/* Add UserController endpoints to the router of the test server */
|
||||
runner.addController(std::make_shared<UserController>());
|
||||
|
||||
/* Run test */
|
||||
runner.run([this, &runner] {
|
||||
|
||||
/* Get client connection provider for Api Client */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
|
||||
|
||||
/* Get object mapper component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
|
||||
|
||||
/* Create http request executor for Api Client */
|
||||
auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider);
|
||||
|
||||
/* Create Test API client */
|
||||
auto client = TestClient::createShared(requestExecutor, objectMapper);
|
||||
|
||||
auto dto = UserDto::createShared();
|
||||
|
||||
dto->userName = "jondoe";
|
||||
dto->email = "jon.doe@abc.com";
|
||||
dto->password = "1234";
|
||||
|
||||
/* Call server API */
|
||||
auto addedUserResponse = client->addUser(dto);
|
||||
|
||||
/* Assert that server responds with 200 */
|
||||
OATPP_ASSERT(addedUserResponse->getStatusCode() == 200);
|
||||
|
||||
/* Read response body as MessageDto */
|
||||
auto addedUserDto = addedUserResponse->readBodyToDto<oatpp::Object<UserDto>>(objectMapper.get());
|
||||
|
||||
int addedUserId = addedUserDto->id;
|
||||
|
||||
/* Assert that user has been added */
|
||||
auto newUserResponse = client->getUser(addedUserId);
|
||||
|
||||
OATPP_ASSERT(newUserResponse->getStatusCode() == 200);
|
||||
|
||||
auto newUserDto = newUserResponse->readBodyToDto<oatpp::Object<UserDto>>(objectMapper.get());
|
||||
|
||||
OATPP_ASSERT(newUserDto->id == addedUserId);
|
||||
|
||||
/* Delete newly added users */
|
||||
auto deletedUserResponse = client->deleteUser(addedUserId);
|
||||
|
||||
OATPP_ASSERT(deletedUserResponse->getStatusCode() == 200);
|
||||
|
||||
}, std::chrono::minutes(10) /* test timeout */);
|
||||
|
||||
/* wait all server threads finished */
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
/* stop db connection pool */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
|
||||
dbConnectionProvider->stop();
|
||||
|
||||
}
|
||||
14
vrpmdvaotserver/test/UserControllerTest.hpp
Normal file
14
vrpmdvaotserver/test/UserControllerTest.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef UserControllerTest_hpp
|
||||
#define UserControllerTest_hpp
|
||||
|
||||
#include "oatpp-test/UnitTest.hpp"
|
||||
|
||||
class UserControllerTest : public oatpp::test::UnitTest {
|
||||
public:
|
||||
UserControllerTest() : oatpp::test::UnitTest("TEST[UserControllerTest]")
|
||||
{}
|
||||
|
||||
void onRun() override;
|
||||
};
|
||||
|
||||
#endif // UserControllerTest_hpp
|
||||
48
vrpmdvaotserver/test/app/TestClient.hpp
Normal file
48
vrpmdvaotserver/test/app/TestClient.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef DatabaseTestClient_hpp
|
||||
#define DatabaseTestClient_hpp
|
||||
|
||||
#include "oatpp/web/client/ApiClient.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
|
||||
#include "dto/UserDto.hpp"
|
||||
#include "dto/MonitoringDto.hpp"
|
||||
|
||||
/* Begin Api Client code generation */
|
||||
#include OATPP_CODEGEN_BEGIN(ApiClient)
|
||||
|
||||
/**
|
||||
* Test API client.
|
||||
* Use this client to call application APIs.
|
||||
*/
|
||||
class TestClient : public oatpp::web::client::ApiClient {
|
||||
|
||||
API_CLIENT_INIT(TestClient)
|
||||
|
||||
/*****************************************************************
|
||||
* UserController
|
||||
*****************************************************************/
|
||||
|
||||
API_CALL("POST", "/users", addUser, BODY_DTO(Object<UserDto>, userDto))
|
||||
API_CALL("GET", "/users/{userId}", getUser, PATH(Int32, userId))
|
||||
API_CALL("DELETE", "/users/{userId}", deleteUser, PATH(Int32, userId))
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
// TODO - add more client API calls here
|
||||
/*****************************************************************
|
||||
* MonitoringController
|
||||
*****************************************************************/
|
||||
|
||||
API_CALL("POST", "/monitorings", addMonitoring, BODY_DTO(Object<MonitoringDto>, monitoringDto))
|
||||
API_CALL("GET", "/monitorings/{id}", getMonitoring, PATH(Int32, id))
|
||||
API_CALL("DELETE", "/monitorings/{id}", deleteMonitoring, PATH(Int32, id))
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
};
|
||||
|
||||
/* End Api Client code generation */
|
||||
#include OATPP_CODEGEN_END(ApiClient)
|
||||
|
||||
#endif // DatabaseTestClient_hpp
|
||||
72
vrpmdvaotserver/test/app/TestComponent.hpp
Normal file
72
vrpmdvaotserver/test/app/TestComponent.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef TestComponent_hpp
|
||||
#define TestComponent_hpp
|
||||
|
||||
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
||||
|
||||
#include "oatpp/network/virtual_/client/ConnectionProvider.hpp"
|
||||
#include "oatpp/network/virtual_/server/ConnectionProvider.hpp"
|
||||
#include "oatpp/network/virtual_/Interface.hpp"
|
||||
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
#include "TestDatabaseComponent.hpp"
|
||||
|
||||
/**
|
||||
* Test Components config
|
||||
*/
|
||||
class TestComponent {
|
||||
public:
|
||||
|
||||
TestDatabaseComponent databaseComponent;
|
||||
|
||||
/**
|
||||
* Create oatpp virtual network interface for test networking
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
|
||||
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create server ConnectionProvider of oatpp virtual connections for test
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
|
||||
return oatpp::network::virtual_::server::ConnectionProvider::createShared(interface);
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create client ConnectionProvider of oatpp virtual connections for test
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider)([] {
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
|
||||
return oatpp::network::virtual_::client::ConnectionProvider::createShared(interface);
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create Router component
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
|
||||
return oatpp::web::server::HttpRouter::createShared();
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create ConnectionHandler component which uses Router component to route requests
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
|
||||
return oatpp::web::server::HttpConnectionHandler::createShared(router);
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
|
||||
return oatpp::parser::json::mapping::ObjectMapper::createShared();
|
||||
}());
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // TestComponent_hpp
|
||||
61
vrpmdvaotserver/test/app/TestDatabaseComponent.hpp
Normal file
61
vrpmdvaotserver/test/app/TestDatabaseComponent.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
|
||||
#ifndef TEST_DATABASECOMPONENT_HPP
|
||||
#define TEST_DATABASECOMPONENT_HPP
|
||||
|
||||
#include "db/UserDb.hpp"
|
||||
#include "db/MonitoringDb.hpp"
|
||||
|
||||
class TestDatabaseComponent {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create database connection provider component
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider)([] {
|
||||
|
||||
/* Create database-specific ConnectionProvider */
|
||||
auto connectionProvider = std::make_shared<oatpp::sqlite::ConnectionProvider>(TESTDATABASE_FILE);
|
||||
|
||||
/* Create database-specific ConnectionPool */
|
||||
return oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
|
||||
10 /* max-connections */,
|
||||
std::chrono::seconds(5) /* connection TTL */);
|
||||
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create database client
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
|
||||
|
||||
/* Get database ConnectionProvider component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
|
||||
|
||||
/* Create database-specific Executor */
|
||||
auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
|
||||
|
||||
/* Create MyClient database client */
|
||||
return std::make_shared<UserDb>(executor);
|
||||
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create database client
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<MonitoringDb>, monitoringDb)([] {
|
||||
|
||||
/* Get database ConnectionProvider component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
|
||||
|
||||
/* Create database-specific Executor */
|
||||
auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
|
||||
|
||||
/* Create MyClient database client */
|
||||
return std::make_shared<MonitoringDb>(executor);
|
||||
|
||||
}());
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif //TEST_DATABASECOMPONENT_HPP
|
||||
36
vrpmdvaotserver/test/tests.cpp
Normal file
36
vrpmdvaotserver/test/tests.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
#include "oatpp-test/UnitTest.hpp"
|
||||
#include "oatpp/core/base/Environment.hpp"
|
||||
#include "UserControllerTest.hpp"
|
||||
#include "MonitoringControllerTest.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
void runTests() {
|
||||
|
||||
OATPP_RUN_TEST(UserControllerTest);
|
||||
OATPP_RUN_TEST(MonitoringControllerTest);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
oatpp::base::Environment::init();
|
||||
|
||||
runTests();
|
||||
|
||||
/* Print how much objects were created during app running, and what have left-probably leaked */
|
||||
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
|
||||
std::cout << "\nEnvironment:\n";
|
||||
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
|
||||
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
|
||||
|
||||
OATPP_ASSERT(oatpp::base::Environment::getObjectsCount() == 0);
|
||||
|
||||
oatpp::base::Environment::destroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user