added tests module

This commit is contained in:
Andrei Yankovich 2020-05-06 23:23:46 +03:00
parent 4795abc73d
commit aae07085c0
10 changed files with 323 additions and 4 deletions

View File

@ -6,7 +6,7 @@
#
cmake_minimum_required(VERSION 3.10)
project(Patronum)
project(Patronum LANGUAGES CXX)
if(TARGET ${PROJECT_NAME})
message("The ${PROJECT_NAME} arledy included in main Project")
return()
@ -15,6 +15,7 @@ endif()
include(Patronum/QuasarAppLib/CMake/ccache.cmake)
# Add sub directories
add_subdirectory(Patronum)
add_subdirectory(Tests)
include(Patronum/QuasarAppLib/CMake/QuasarAppCITargets.cmake)

View File

@ -24,7 +24,7 @@ public:
* @param argv - test of arguments
* @param name - name of your service
*/
Service(int argc, char **argv, const QString &name)
Service(int argc, const char *argv[], const QString &name)
: QtService<Application>(argc, argv, name) {
d_ptr = new ServicePrivate(name, this);
@ -37,7 +37,6 @@ protected:
* Default inplementation send message abount error.
*/
void handleReceive(const QList<Feature> &data) {
Q_UNUSED(data)
auto list = supportedFeatures();
@ -49,7 +48,12 @@ protected:
QVariantMap result;
result["Error"] = "Wrong command!";
QString commandList;
for (const auto&i : data ) {
commandList += i.toString() + " ";
}
result["Error"] = "Wrong command! The commands : " + commandList + " is notsupported";
result["Available commands"] = stringList;
sendResuylt(result);
@ -74,6 +78,15 @@ protected:
return d_ptr->sendCmdResult(result);
}
/**
* @brief sendResuylt this method send text responce to controller
* @param result - message
* @return true if data sendet is seccusseful
*/
bool sendResuylt(const QString &result) {
return d_ptr->sendCmdResult({{"Result:", result}});
}
/**
* @brief createApplication default implementation create a Application object and parse argumnts.
* @param argc argumnts count

33
Tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,33 @@
#
# Copyright (C) 2018-2020 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
cmake_minimum_required(VERSION 3.10)
set(CURRENT_PROJECT ${PROJECT_NAME}Test)
include(../Patronum/QuasarAppLib/CMake/ProjectOut.cmake)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Test REQUIRED)
file(GLOB SOURCE_CPP
"*.cpp"
)
add_executable(${CURRENT_PROJECT} ${SOURCE_CPP})
target_link_libraries(${CURRENT_PROJECT} PRIVATE Qt5::Test Patronum)
target_include_directories(${CURRENT_PROJECT} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
include(../Patronum/QuasarAppLib/CMake/QuasarAppCITargets.cmake)
addTests("NetworkProtokol" ${CURRENT_PROJECT})
initTests()

View File

@ -0,0 +1,7 @@
#include "defaultcontroller.h"
DefaultController::DefaultController():
Patronum::Controller("TestPatronum")
{
}

11
Tests/defaultcontroller.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef DEFAULTCONTROLLER_H
#define DEFAULTCONTROLLER_H
#include <patronum.h>
class DefaultController : public Patronum::Controller
{
public:
DefaultController();
};
#endif // DEFAULTCONTROLLER_H

36
Tests/defaultservice.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "defaultservice.h"
const char* arg[] = {
"/",
"-e"
};
DefaultService::DefaultService():
Patronum::Service<QCoreApplication>(0, arg, "TestPatronum") {
}
void DefaultService::start() {
QuasarAppUtils::Params::log("Server started!", QuasarAppUtils::Info);
}
void DefaultService::handleReceive(const QList<Patronum::Feature> &data) {
QList<Patronum::Feature> notSupportedList;
for (const auto& i : data) {
if (i.cmd() == "ping") {
sendResuylt("pomg");
} else {
notSupportedList += i;
}
}
Patronum::Service<QCoreApplication>::handleReceive(notSupportedList);
}
QList<Patronum::Feature> DefaultService::supportedFeatures() {
QList<Patronum::Feature> res;
res += Patronum::Feature("ping", {}, "test ping", "ping");
return res;
}

21
Tests/defaultservice.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef DEFAULTSERVICE_H
#define DEFAULTSERVICE_H
#include <patronum.h>
class DefaultService : public Patronum::Service<QCoreApplication>
{
public:
DefaultService();
// QtServiceBase interface
protected:
void start();
// IService interface
public:
void handleReceive(const QList<Patronum::Feature> &data);
QList<Patronum::Feature> supportedFeatures();
};
#endif // DEFAULTSERVICE_H

111
Tests/testutils.cpp Normal file
View File

@ -0,0 +1,111 @@
#include "testutils.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QVariantMap>
#include <basenode.h>
#include <client.h>
bool funcPrivate(std::function<bool()> requestFunc,
NP::BaseNode* node,
SP<NP::AbstractData>* responce = nullptr,
QHostAddress *responceSender = nullptr) {
bool received = false;
QMetaObject::Connection m_connection;
m_connection = QObject::connect(node, &NP::BaseNode::incomingData,
[ &received, responce, responceSender]
(SP<NP::AbstractData> pkg,
const QHostAddress& sender) {
received = true;
if (responce) {
*responce = pkg;
}
if (responceSender) {
*responceSender = sender;
}
});
if (!requestFunc()) {
return false;
}
if (!TestUtils::wait(received, 10000))
return false;
QObject::disconnect(m_connection);
return true;
}
bool funcPrivateConnect(std::function<bool()> requestFunc,
NP::Client* node) {
bool connected = false;
QMetaObject::Connection m_connection;
m_connection = QObject::connect(node, &NP::Client::statusChanged,
[ &connected](int new_status) {
connected = NP::Client::Status::Online == static_cast<NP::Client::Status>(new_status);
});
if (!requestFunc()) {
return false;
}
TestUtils::wait(connected, 10900);
QObject::disconnect(m_connection);
return connected;
}
TestUtils::TestUtils()
{
}
bool TestUtils::wait(const bool &forWait, int msec) {
auto curmsec = QDateTime::currentMSecsSinceEpoch() + msec;
while (curmsec > QDateTime::currentMSecsSinceEpoch() && !forWait) {
QCoreApplication::processEvents();
}
QCoreApplication::processEvents();
return forWait;
}
bool TestUtils::loginFunc(
NP::Client *cli,
const QString& login,
const QByteArray& pass,
bool sendResult,
bool loginResult) {
auto wraper = [cli, login, pass](){return cli->login(login, pass);};
bool result = funcPrivate(wraper, cli);
if (!result) {
return !sendResult;
}
return loginResult == (cli->status() == NP::Client::Logined);
}
bool TestUtils::connectFunc(
NP::Client *cli,
const QString& address,
unsigned short port) {
auto wraper = [&cli, address, port](){
cli->setHost(QHostAddress(address), port);
return cli->connectClient();
};
return funcPrivateConnect(wraper, cli);
}

32
Tests/testutils.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef TESTUTILS_H
#define TESTUTILS_H
#include <patronum.h>
namespace NP {
class Client;
}
class TestUtils
{
public:
TestUtils();
static bool wait(const bool &forWait, int msec);
static bool loginFunc(NP::Client *cli,
const QString &login,
const QByteArray &pass,
bool sendResult,
bool loginResult);
static bool connectFunc(NP::Client *cli,
const QString &address,
unsigned short port);
// static bool getState(ServerProtocol::Client &cli, QVariantMap &state);
// static bool unBanFunc(ServerProtocol::Client &cli, const QHostAddress &address);
// static bool banFunc(ServerProtocol::Client &cli, const QHostAddress &address);
// static bool reconnectFunc(ClientProtocol::Client &cli);
// static bool registerFunc(ClientProtocol::Client &cli, const QString &login,
// const QByteArray &pass, bool sendResult, bool loginResult);
};
#endif // TESTUTILS_H

View File

@ -0,0 +1,54 @@
#include <QtTest>
#include <quasarapp.h>
#include <QCoreApplication>
#include "testutils.h"
#include <patronum.h>
#define TEST_LOCAL_HOST "127.0.0.1"
#define TEST_PORT 27777
class testProtockol : public QObject
{
Q_OBJECT
private:
public:
testProtockol();
void connectTest(Patronum::Service *service, Patronum::Controller *terminal);
~testProtockol();
private slots:
void initTestCase();
void testPakageData();
void testBaseNode();
void testUser();
};
testProtockol::testProtockol() {
QuasarAppUtils::Params::setArg("verbose", 3);
}
void testProtockol::connectTest(NP::Client *cli, NP::BaseNode *serv) {
QVERIFY(serv->run(TEST_LOCAL_HOST, TEST_PORT));
QVERIFY(TestUtils::connectFunc(cli, TEST_LOCAL_HOST, TEST_PORT));
}
testProtockol::~testProtockol() {
}
void testProtockol::initTestCase() {
}
QTEST_APPLESS_MAIN(testProtockol)
#include "tst_testsnakeserver.moc"