1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

refactored test fixture

This commit is contained in:
Falk Werner 2020-02-24 18:16:30 +01:00
parent 7ed99088db
commit 5a80e0233c

View File

@ -12,43 +12,21 @@
using webfuse_test::WebsocketServer; using webfuse_test::WebsocketServer;
using webfuse_test::MockProviderClient; using webfuse_test::MockProviderClient;
using webfuse_test::IProviderClient;
using testing::_; using testing::_;
using testing::AtMost;
namespace namespace
{ {
// ToDo: Refactor Me class ClientProtocolFixture
class ClientProtocolTest: public ::testing::Test
{ {
public: public:
ClientProtocolTest() ClientProtocolFixture(IProviderClient& client)
: server(nullptr)
, config(nullptr)
, protocol(nullptr)
{
// empty
}
protected:
void SetUp()
{ {
config = wfp_client_config_create(); config = wfp_client_config_create();
client.AttachTo(config);
server = nullptr;
protocol = nullptr;
}
void TearDown()
{
if (nullptr != server)
{
StopServer();
}
}
void StartServer()
{
protocol = wfp_client_protocol_create(config); protocol = wfp_client_protocol_create(config);
struct lws_protocols client_protocol; struct lws_protocols client_protocol;
@ -58,20 +36,11 @@ protected:
server = new WebsocketServer(54321, &client_protocol, 1); server = new WebsocketServer(54321, &client_protocol, 1);
} }
void StopServer() ~ClientProtocolFixture()
{ {
delete server; delete server;
wfp_client_protocol_dispose(protocol); wfp_client_protocol_dispose(protocol);
wfp_client_config_dispose(config); wfp_client_config_dispose(config);
server = nullptr;
protocol = nullptr;
config = nullptr;
}
wfp_client_config * GetClientConfig()
{
return config;
} }
void Connect() void Connect()
@ -80,6 +49,16 @@ protected:
server->waitForConnection(); server->waitForConnection();
} }
void SendToClient(json_t * request)
{
server->sendMessage(request);
}
json_t * ReceiveMessageFromClient()
{
return server->receiveMessage();
}
void AwaitAddFilesystem(std::string& filesystemName) void AwaitAddFilesystem(std::string& filesystemName)
{ {
json_t * addFilesystemRequest = server->receiveMessage(); json_t * addFilesystemRequest = server->receiveMessage();
@ -113,40 +92,46 @@ protected:
} }
WebsocketServer * server;
private: private:
WebsocketServer * server;
wfp_client_config * config; wfp_client_config * config;
wfp_client_protocol * protocol; wfp_client_protocol * protocol;
}; };
} }
TEST_F(ClientProtocolTest, connect) TEST(client_protocol, connect)
{ {
StartServer(); MockProviderClient provider;
Connect(); ClientProtocolFixture fixture(provider);
if (HasFatalFailure()) { return; }
StopServer(); EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
} EXPECT_CALL(provider, OnDisconnected()).Times(1);
TEST_F(ClientProtocolTest, getattr) fixture.Connect();
{
MockProviderClient client;
client.AttachTo(GetClientConfig());
EXPECT_CALL(client, OnConnected()).Times(1);
EXPECT_CALL(client, OnDisconnected()).Times(1);
EXPECT_CALL(client, GetAttr(1, _)).Times(1);
StartServer();
Connect();
if (HasFatalFailure()) { return; } if (HasFatalFailure()) { return; }
std::string filesystem; std::string filesystem;
AwaitAddFilesystem(filesystem); fixture.AwaitAddFilesystem(filesystem);
if (HasFatalFailure()) { return; }
}
TEST(client_protocol, getattr)
{
MockProviderClient provider;
ClientProtocolFixture fixture(provider);
EXPECT_CALL(provider, OnConnected()).Times(1);
EXPECT_CALL(provider, OnDisconnected()).Times(1);
EXPECT_CALL(provider, GetAttr(1, _)).Times(1);
fixture.Connect();
if (HasFatalFailure()) { return; }
std::string filesystem;
fixture.AwaitAddFilesystem(filesystem);
if (HasFatalFailure()) { return; } if (HasFatalFailure()) { return; }
json_t * params = json_array(); json_t * params = json_array();
@ -157,11 +142,9 @@ TEST_F(ClientProtocolTest, getattr)
json_object_set_new(request, "params", params); json_object_set_new(request, "params", params);
json_object_set_new(request, "id", json_integer(42)); json_object_set_new(request, "id", json_integer(42));
server->sendMessage(request); fixture.SendToClient(request);
json_t * response = server->receiveMessage(); json_t * response = fixture.ReceiveMessageFromClient();
ASSERT_TRUE(json_is_object(response)); ASSERT_TRUE(json_is_object(response));
json_decref(response); json_decref(response);
StopServer();
} }