Skip to content

Commit e1a730d

Browse files
authored
Merge pull request #10 from oatpp/fix_tests
Fix tests
2 parents 42ce042 + 6093bbb commit e1a730d

11 files changed

+211
-202
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ target_link_libraries(crud-exe crud-lib)
7272

7373
add_executable(crud-test
7474
test/tests.cpp
75-
test/app/DatabaseTestClient.hpp
75+
test/app/TestClient.hpp
7676
test/app/TestDatabaseComponent.hpp
77-
test/app/TestComponent.hpp
78-
test/DatabaseTest.hpp
79-
test/DatabaseTest.cpp)
77+
test/app/TestComponent.hpp
78+
test/UserControllerTest.hpp
79+
test/UserControllerTest.cpp)
8080
target_link_libraries(crud-test crud-lib)
8181

8282
enable_testing()

src/service/UserService.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ oatpp::Object<UserDto> UserService::createUser(const oatpp::Object<UserDto>& dto
88

99
auto userId = oatpp::sqlite::Utils::getLastInsertRowId(dbResult->getConnection());
1010

11-
OATPP_LOGD("AAA", "new userId=%d", userId);
12-
1311
return getUserById((v_int32) userId);
1412

1513
}

test/DatabaseTest.cpp

Lines changed: 0 additions & 71 deletions
This file was deleted.

test/DatabaseTest.hpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/UserControllerTest.cpp

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

test/UserControllerTest.hpp

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

test/app/DatabaseTestClient.hpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/app/TestClient.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 "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 TestClient : public oatpp::web::client::ApiClient {
17+
18+
API_CLIENT_INIT(TestClient)
19+
20+
/*****************************************************************
21+
* UserController
22+
*****************************************************************/
23+
24+
API_CALL("POST", "/users", addUser, BODY_DTO(Object<UserDto>, userDto))
25+
API_CALL("GET", "/users/{userId}", getUser, PATH(Int32, userId))
26+
API_CALL("DELETE", "/users/{userId}", deleteUser, PATH(Int32, userId))
27+
28+
/*****************************************************************/
29+
30+
// TODO - add more client API calls here
31+
32+
};
33+
34+
/* End Api Client code generation */
35+
#include OATPP_CODEGEN_END(ApiClient)
36+
37+
#endif // DatabaseTestClient_hpp

test/app/TestComponent.hpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,52 @@
1919
class TestComponent {
2020
public:
2121

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-
}());
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+
}());
6868

6969
};
7070

0 commit comments

Comments
 (0)