1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

refactor: simlify usage of wfp_json_doc in unit tests

This commit is contained in:
Falk Werner
2020-07-12 15:13:34 +02:00
parent 2506873805
commit 2979904514
9 changed files with 118 additions and 231 deletions

View File

@@ -1,12 +1,13 @@
#include "webfuse_provider/impl/operation/close.h"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/mocks/mock_provider.hpp"
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
#include "webfuse_provider/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using ::webfuse_test::MockProvider;
using ::webfuse_test::create_context;
using ::webfuse_test::JsonDoc;
using ::testing::_;
TEST(wfp_close, close)
@@ -16,11 +17,9 @@ TEST(wfp_close, close)
wfp_impl_invokation_context context = create_context(provider);
char params[] = "[\"test.filesystem\", 42, 101, 23]";
wfp_json_doc * doc = wfp_impl_json_parse(params);
JsonDoc doc("[\"test.filesystem\", 42, 101, 23]");
wfp_impl_close(&context, wfp_impl_json_root(doc), 42);
wfp_impl_json_dispose(doc);
wfp_impl_close(&context, doc.root(), 42);
}
TEST(wfp_close, close_fail_invalid_param_count)
@@ -30,11 +29,9 @@ TEST(wfp_close, close_fail_invalid_param_count)
wfp_impl_invokation_context context = create_context(provider);
char params[] = "[]";
wfp_json_doc * doc = wfp_impl_json_parse(params);
JsonDoc doc("[]");
wfp_impl_close(&context, wfp_impl_json_root(doc), 42);
wfp_impl_json_dispose(doc);
wfp_impl_close(&context, doc.root(), 42);
}
TEST(wfp_close, close_fail_inode_invalid_type)
@@ -44,11 +41,9 @@ TEST(wfp_close, close_fail_inode_invalid_type)
wfp_impl_invokation_context context = create_context(provider);
char params[] = "[\"test.filesystem\", \"42\", 0, 0]";
wfp_json_doc * doc = wfp_impl_json_parse(params);
JsonDoc doc("[\"test.filesystem\", \"42\", 0, 0]");
wfp_impl_close(&context, wfp_impl_json_root(doc), 42);
wfp_impl_json_dispose(doc);
wfp_impl_close(&context, doc.root(), 42);
}
TEST(wfp_close, close_fail_handle_invalid_type)
@@ -58,11 +53,9 @@ TEST(wfp_close, close_fail_handle_invalid_type)
wfp_impl_invokation_context context = create_context(provider);
char params[] = "[\"test.filesystem\", 0, \"42\", 0]";
wfp_json_doc * doc = wfp_impl_json_parse(params);
JsonDoc doc("[\"test.filesystem\", 0, \"42\", 0]");
wfp_impl_close(&context, wfp_impl_json_root(doc), 42);
wfp_impl_json_dispose(doc);
wfp_impl_close(&context, doc.root(), 42);
}
TEST(wfp_close, close_fail_flags_invalid_type)
@@ -72,11 +65,9 @@ TEST(wfp_close, close_fail_flags_invalid_type)
wfp_impl_invokation_context context = create_context(provider);
char params[] = "[\"test.filesystem\", 0, 0, \"42\"]";
wfp_json_doc * doc = wfp_impl_json_parse(params);
JsonDoc doc("[\"test.filesystem\", 0, 0, \"42\"]");
wfp_impl_close(&context, wfp_impl_json_root(doc), 42);
wfp_impl_json_dispose(doc);
wfp_impl_close(&context, doc.root(), 42);
}

View File

@@ -2,7 +2,7 @@
#include "webfuse_provider/test_util/webfuse_server.hpp"
#include "webfuse_provider/mocks/mock_provider_client.hpp"
#include "webfuse_provider/test_util/client.hpp"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/test_util/json_doc.hpp"
#include "webfuse_provider/impl/json/node.h"
#include <gtest/gtest.h>
@@ -12,6 +12,7 @@
using webfuse_test::WebfuseServer;
using webfuse_test::MockProviderClient;
using webfuse_test::Client;
using webfuse_test::JsonDoc;
using testing::Invoke;
using testing::_;
using testing::StrEq;
@@ -148,9 +149,8 @@ TEST(Client, Lookup)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
std::string response_text = server.Lookup(1, "foo");
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
wfp_json const * response = wfp_impl_json_root(doc);
JsonDoc doc(server.Lookup(1, "foo"));
wfp_json const * response = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(response));
wfp_json const * result = wfp_impl_json_object_get(response, "result");
@@ -163,8 +163,6 @@ TEST(Client, Lookup)
wfp_json const * type = wfp_impl_json_object_get(result, "type");
ASSERT_STREQ("file", wfp_impl_json_get_string(type));
wfp_impl_json_dispose(doc);
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
@@ -198,9 +196,8 @@ TEST(Client, LookupFail)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
std::string response_text = server.Lookup(1, "foo");
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
wfp_json const * response = wfp_impl_json_root(doc);
JsonDoc doc(server.Lookup(1, "foo"));
wfp_json const * response = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(response));
wfp_json const * error = wfp_impl_json_object_get(response, "error");
@@ -208,8 +205,6 @@ TEST(Client, LookupFail)
wfp_json const * code = wfp_impl_json_object_get(error, "code");
ASSERT_NE(0, wfp_impl_json_get_int(code));
wfp_impl_json_dispose(doc);
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
@@ -243,9 +238,8 @@ TEST(Client, Open)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
std::string response_text = server.Open(1, 0);
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
wfp_json const * response = wfp_impl_json_root(doc);
JsonDoc doc(server.Open(1, 0));
wfp_json const * response = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(response));
wfp_json const * result = wfp_impl_json_object_get(response, "result");
@@ -253,8 +247,6 @@ TEST(Client, Open)
wfp_json const * handle = wfp_impl_json_object_get(result, "handle");
ASSERT_EQ(4711, wfp_impl_json_get_int(handle));
wfp_impl_json_dispose(doc);
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
@@ -289,9 +281,8 @@ TEST(Client, Read)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
std::string response_text = server.Read(42, 5, 0, 1);
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
wfp_json const * response = wfp_impl_json_root(doc);
JsonDoc doc(server.Read(42, 5, 0, 1));
wfp_json const * response = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(response));
wfp_json const * result = wfp_impl_json_object_get(response, "result");
@@ -305,8 +296,6 @@ TEST(Client, Read)
wfp_json const * data = wfp_impl_json_object_get(result, "data");
ASSERT_STREQ("Kg==", wfp_impl_json_get_string(data));
wfp_impl_json_dispose(doc);
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
@@ -342,9 +331,8 @@ TEST(Client, ReadDir)
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
std::string response_text = server.ReadDir(42);
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char*>(response_text.data()));
wfp_json const * response = wfp_impl_json_root(doc);
JsonDoc doc(server.ReadDir(42));
wfp_json const * response = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(response));
wfp_json const * result = wfp_impl_json_object_get(response, "result");
@@ -352,8 +340,6 @@ TEST(Client, ReadDir)
ASSERT_TRUE(wfp_impl_json_is_array(result));
ASSERT_EQ(3, wfp_impl_json_array_size(result));
wfp_impl_json_dispose(doc);
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}

View File

@@ -7,7 +7,7 @@
#include "webfuse_provider/mocks/mock_provider_client.hpp"
#include "webfuse_provider/protocol_names.h"
#include "webfuse_provider/test_util/timeout_watcher.hpp"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/test_util/json_doc.hpp"
#include "webfuse_provider/impl/json/node.h"
#include <libwebsockets.h>
@@ -20,6 +20,7 @@ using webfuse_test::WsServer;
using webfuse_test::MockProviderClient;
using webfuse_test::IProviderClient;
using webfuse_test::TimeoutWatcher;
using webfuse_test::JsonDoc;
using testing::_;
using testing::AtMost;
using testing::Invoke;
@@ -102,9 +103,8 @@ public:
std::string const & expected_username,
std::string const & expected_password)
{
std::string request_text = ReceiveMessageFromClient();
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char *>(request_text.data()));
wfp_json const * request = wfp_impl_json_root(doc);
JsonDoc doc(ReceiveMessageFromClient());
wfp_json const * request = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(request));
wfp_json const * method = wfp_impl_json_object_get(request, "method");
@@ -136,15 +136,12 @@ public:
std::ostringstream response;
response << "{\"result\": {}, \"id\": " << wfp_impl_json_get_int(id) << "}";
SendToClient(response.str());
wfp_impl_json_dispose(doc);
}
void AwaitAddFilesystem(std::string& filesystemName)
{
std::string request_text = ReceiveMessageFromClient();
wfp_json_doc * doc = wfp_impl_json_parse(const_cast<char *>(request_text.data()));
wfp_json const * request = wfp_impl_json_root(doc);
JsonDoc doc(ReceiveMessageFromClient());
wfp_json const * request = doc.root();
ASSERT_TRUE(wfp_impl_json_is_object(request));
wfp_json const * method = wfp_impl_json_object_get(request, "method");
@@ -165,8 +162,6 @@ public:
response << "{\"result\": {\"id\": \"" << wfp_impl_json_get_string(filesystem) << "\"}, \"id\": " << wfp_impl_json_get_int(id) << "}";
SendToClient(response.str());
wfp_impl_json_dispose(doc);
}
private: