Skip to content

Commit 42ce042

Browse files
authored
Merge pull request #9 from ktnr/master
Added simple test to add and delete users.
2 parents 32d3da9 + 9887b1d commit 42ce042

File tree

9 files changed

+235
-4
lines changed

9 files changed

+235
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ cmake-build-debug/
4141

4242
# sqlite
4343
**/*.sqlite
44+
/.vs

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ add_definitions(
5151
## define path to swagger-ui static resources folder
5252
-DOATPP_SWAGGER_RES_PATH="${oatpp-swagger_INCLUDE_DIRS}/../bin/oatpp-swagger/res"
5353

54-
## SQLite databse file
54+
## SQLite database file
5555
-DDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/db.sqlite"
56+
## SQLite database test file
57+
-DTESTDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/test-db.sqlite"
5658

5759
## Path to database migration scripts
5860
-DDATABASE_MIGRATIONS="${CMAKE_CURRENT_SOURCE_DIR}/sql"
@@ -70,7 +72,11 @@ target_link_libraries(crud-exe crud-lib)
7072

7173
add_executable(crud-test
7274
test/tests.cpp
73-
)
75+
test/app/DatabaseTestClient.hpp
76+
test/app/TestDatabaseComponent.hpp
77+
test/app/TestComponent.hpp
78+
test/DatabaseTest.hpp
79+
test/DatabaseTest.cpp)
7480
target_link_libraries(crud-test crud-lib)
7581

7682
enable_testing()

src/controller/UserController.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
class UserController : public oatpp::web::server::api::ApiController {
1717
public:
18-
UserController(const std::shared_ptr<ObjectMapper>& objectMapper)
18+
UserController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
1919
: oatpp::web::server::api::ApiController(objectMapper)
2020
{}
2121
private:

test/DatabaseTest.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "DatabaseTest.hpp"
2+
3+
#include "oatpp/web/client/HttpRequestExecutor.hpp"
4+
#include "oatpp-test/web/ClientServerTestRunner.hpp"
5+
6+
#include "controller/UserController.hpp"
7+
8+
#include "app/DatabaseTestClient.hpp"
9+
#include "app/TestComponent.hpp"
10+
11+
void DatabaseTest::onRun() {
12+
/* Register test components */
13+
TestComponent component;
14+
15+
/* Create client-server test runner */
16+
oatpp::test::web::ClientServerTestRunner runner;
17+
18+
/* Add UserController endpoints to the router of the test server */
19+
runner.addController(std::make_shared<UserController>());
20+
21+
/* Run test */
22+
runner.run([this, &runner] {
23+
24+
/* Get client connection provider for Api Client */
25+
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
26+
27+
/* Get object mapper component */
28+
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
29+
30+
/* Create http request executor for Api Client */
31+
auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider);
32+
33+
/* Create Test API client */
34+
auto client = DatabaseTestClient::createShared(requestExecutor, objectMapper);
35+
36+
auto dto = UserDto::createShared();
37+
38+
dto->userName = "jondoe";
39+
dto->email = "[email protected]";
40+
dto->password = "1234";
41+
42+
/* Call server API */
43+
auto addedUserResponse = client->addUser(dto);
44+
45+
/* Assert that server responds with 200 */
46+
OATPP_ASSERT(addedUserResponse->getStatusCode() == 200);
47+
48+
/* Read response body as MessageDto */
49+
auto addedUserDto = addedUserResponse->readBodyToDto<oatpp::Object<UserDto>>(objectMapper.get());
50+
51+
int addedUserId = addedUserDto->id;
52+
53+
/* Assert that user has been added */
54+
auto newUserResponse = client->getUser(addedUserId);
55+
56+
OATPP_ASSERT(newUserResponse->getStatusCode() == 200);
57+
58+
auto newUserDto = newUserResponse->readBodyToDto<oatpp::Object<UserDto>>(objectMapper.get());
59+
60+
OATPP_ASSERT(newUserDto->id == addedUserId);
61+
62+
/* Delete newly added users */
63+
auto deletedUserResponse = client->deleteUser(addedUserId);
64+
65+
OATPP_ASSERT(deletedUserResponse->getStatusCode() == 200);
66+
67+
}, std::chrono::minutes(10) /* test timeout */);
68+
69+
/* wait all server threads finished */
70+
std::this_thread::sleep_for(std::chrono::seconds(1));
71+
}

test/DatabaseTest.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef DatabaseTest_hpp
2+
#define DatabaseTest_hpp
3+
4+
#include "oatpp-test/UnitTest.hpp"
5+
6+
class DatabaseTest : public oatpp::test::UnitTest {
7+
public:
8+
DatabaseTest() : oatpp::test::UnitTest("TEST[DatabaseTest]")
9+
{}
10+
11+
void onRun() override;
12+
};
13+
14+
#endif // DatabaseTest_hpp

test/app/DatabaseTestClient.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef DatabaseTestClient_hpp
2+
#define DatabaseTestClient_hpp
3+
4+
#include "oatpp/web/client/ApiClient.hpp"
5+
#include "oatpp/core/macro/codegen.hpp"
6+
7+
#include "../../src/dto/UserDto.hpp"
8+
9+
/* Begin Api Client code generation */
10+
#include OATPP_CODEGEN_BEGIN(ApiClient)
11+
12+
/**
13+
* Test API client.
14+
* Use this client to call application APIs.
15+
*/
16+
class DatabaseTestClient : public oatpp::web::client::ApiClient {
17+
18+
API_CLIENT_INIT(DatabaseTestClient)
19+
20+
API_CALL("POST", "/users", addUser, BODY_DTO(Object<UserDto>, userDto))
21+
API_CALL("GET", "/users/{userId}", getUser, PATH(Int32, userId))
22+
API_CALL("DELETE", "/users/{userId}", deleteUser, PATH(Int32, userId))
23+
24+
// TODO - add more client API calls here
25+
26+
};
27+
28+
/* End Api Client code generation */
29+
#include OATPP_CODEGEN_END(ApiClient)
30+
31+
#endif // DatabaseTestClient_hpp

test/app/TestComponent.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef TestComponent_hpp
2+
#define TestComponent_hpp
3+
4+
#include "oatpp/web/server/HttpConnectionHandler.hpp"
5+
6+
#include "oatpp/network/virtual_/client/ConnectionProvider.hpp"
7+
#include "oatpp/network/virtual_/server/ConnectionProvider.hpp"
8+
#include "oatpp/network/virtual_/Interface.hpp"
9+
10+
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
11+
12+
#include "oatpp/core/macro/component.hpp"
13+
14+
#include "TestDatabaseComponent.hpp"
15+
16+
/**
17+
* Test Components config
18+
*/
19+
class TestComponent {
20+
public:
21+
22+
TestDatabaseComponent databaseComponent;
23+
24+
/**
25+
* Create oatpp virtual network interface for test networking
26+
*/
27+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
28+
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
29+
}());
30+
31+
/**
32+
* Create server ConnectionProvider of oatpp virtual connections for test
33+
*/
34+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
35+
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
36+
return oatpp::network::virtual_::server::ConnectionProvider::createShared(interface);
37+
}());
38+
39+
/**
40+
* Create client ConnectionProvider of oatpp virtual connections for test
41+
*/
42+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider)([] {
43+
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
44+
return oatpp::network::virtual_::client::ConnectionProvider::createShared(interface);
45+
}());
46+
47+
/**
48+
* Create Router component
49+
*/
50+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
51+
return oatpp::web::server::HttpRouter::createShared();
52+
}());
53+
54+
/**
55+
* Create ConnectionHandler component which uses Router component to route requests
56+
*/
57+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
58+
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
59+
return oatpp::web::server::HttpConnectionHandler::createShared(router);
60+
}());
61+
62+
/**
63+
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
64+
*/
65+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
66+
return oatpp::parser::json::mapping::ObjectMapper::createShared();
67+
}());
68+
69+
};
70+
71+
72+
#endif // TestComponent_hpp

test/app/TestDatabaseComponent.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#ifndef TEST_DATABASECOMPONENT_HPP
3+
#define TEST_DATABASECOMPONENT_HPP
4+
5+
#include "db/UserDb.hpp"
6+
7+
class TestDatabaseComponent {
8+
public:
9+
10+
/**
11+
* Create database client
12+
*/
13+
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
14+
15+
/* Create database-specific ConnectionProvider */
16+
auto connectionProvider = std::make_shared<oatpp::sqlite::ConnectionProvider>(TESTDATABASE_FILE);
17+
18+
/* Create database-specific ConnectionPool */
19+
auto connectionPool = oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
20+
10 /* max-connections */,
21+
std::chrono::seconds(5) /* connection TTL */);
22+
23+
/* Create database-specific Executor */
24+
auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionPool);
25+
26+
/* Create MyClient database client */
27+
return std::make_shared<UserDb>(executor);
28+
29+
}());
30+
31+
32+
};
33+
34+
#endif //TEST_DATABASECOMPONENT_HPP

test/tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "oatpp-swagger/oas3/Model.hpp"
88

9+
#include "DatabaseTest.hpp"
10+
911
#include <iostream>
1012

1113
namespace {
@@ -25,7 +27,7 @@ void runTests() {
2527
OATPP_LOGD("test", "insert oatpp-swagger tests here");
2628

2729
OATPP_RUN_TEST(Test);
28-
30+
OATPP_RUN_TEST(DatabaseTest);
2931
}
3032

3133
}

0 commit comments

Comments
 (0)