85 lines
2.7 KiB
C++
85 lines
2.7 KiB
C++
#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();
|
|
|
|
} |