4
0
mirror of https://github.com/QuasarApp/Patronum.git synced 2025-05-10 22:19:35 +00:00

added unit tests

This commit is contained in:
Andrei Yankovich 2020-05-07 16:08:56 +03:00
parent aae07085c0
commit 13b30fdb3e
13 changed files with 111 additions and 152 deletions

@ -11,7 +11,7 @@ Controller::Controller(const QString &name):
d_ptr = new ControllerPrivate(name, this);
}
bool Controller::send(int argc, char **argv) {
bool Controller::send(int argc, const char **argv) {
if (!QuasarAppUtils::Params::parseParams(argc, argv)) {
return false;
}
@ -63,15 +63,8 @@ bool Controller::send(int argc, char **argv) {
}
bool Controller::waitForResponce(int msec) {
_responce = false;
qint64 waitFor = QDateTime::currentMSecsSinceEpoch() + msec;
while (!_responce && QDateTime::currentMSecsSinceEpoch() < waitFor) {
QCoreApplication::processEvents();
}
return _responce;
return d_ptr->waitForResponce(msec);
}
void Controller::handleFeatures(const QList<Feature> &features) {
@ -95,10 +88,7 @@ void Controller::handleFeatures(const QList<Feature> &features) {
options.insert(cmd, description);
}
_features = features;
QuasarAppUtils::Help::print(options);
_responce = true;
}
void Controller::handleResponce(const QVariantMap &responce) {
@ -116,6 +106,10 @@ QString Controller::defaultInstallLocation() {
return "";
}
QList<Feature> Controller::features() {
return d_ptr->features();
}
void Controller::printDefaultHelp() const {
QuasarAppUtils::Help::Charters help{{"General options of this controller",{
{"start", QObject::tr("Start a service")},

@ -30,7 +30,7 @@ public:
* @param argv - arguments list
* @return true if all sendet seccussful
*/
bool send(int argc, char **argv);
bool send(int argc, const char **argv);
/**
* @brief waitForResponce - waut for get a responce from servece
@ -52,9 +52,9 @@ protected:
/**
* @brief handleResponce - override this method if you want create a custom reaction of get responce from service
* Default inplementation print responce to console.
* @param feature - responce from service
* @param responce - responce from service
*/
void handleResponce(const QVariantMap &feature);
void handleResponce(const QVariantMap &responce);
/**
* @brief defaultInstallLocation - this method must be return a path for service executable or wrapper
@ -62,11 +62,16 @@ protected:
*/
virtual QString defaultInstallLocation();
/**
* @brief features - this metho return current features of connected service.
* @note If Responed from service not received then return empty list.
* @return features list
*/
QList<Feature> features();
private:
ControllerPrivate *d_ptr = nullptr;
QList<Feature> _features;
bool _responce = false;
void printDefaultHelp() const;

@ -24,7 +24,7 @@ public:
* @param argv - test of arguments
* @param name - name of your service
*/
Service(int argc, const char *argv[], const QString &name)
Service(int argc, char *argv[], const QString &name)
: QtService<Application>(argc, argv, name) {
d_ptr = new ServicePrivate(name, this);
@ -36,7 +36,7 @@ protected:
* @param data - is list of commands from controller
* Default inplementation send message abount error.
*/
void handleReceive(const QList<Feature> &data) {
void handleReceive(const QList<Feature> &data) override {
auto list = supportedFeatures();
@ -64,7 +64,7 @@ protected:
* @brief supportedFeatures
* @return list of supported features of this service. override this method for correctly work of your pair (service and controller)
*/
QList<Feature> supportedFeatures() {
QList<Feature> supportedFeatures() override {
QList<Feature> result;
return result;
}
@ -92,7 +92,7 @@ protected:
* @param argc argumnts count
* @param argv list of argumnts
*/
void createApplication(int argc, char **argv) {
void createApplication(int &argc, char **argv) override {
QuasarAppUtils::Params::parseParams(argc, argv);
QtService<Application>::createApplication(argc, argv);
}

@ -1,6 +1,8 @@
#include "controllerprivate.h"
#include "IPController.h"
#include "localsocket.h"
#include <QCoreApplication>
#include <QDateTime>
#include <quasarapp.h>
#include "package.h"
@ -43,12 +45,37 @@ bool ControllerPrivate::sendCmd(const QList<Feature> &result) {
return false;
}
QByteArray responce;
QDataStream stream(&responce, QIODevice::WriteOnly);
QByteArray request;
QDataStream stream(&request, QIODevice::WriteOnly);
stream << Command::Feature << result;
return _socket->send(responce);
if (_socket->send(request)){
_responce = false;
return true;
}
return false;
}
bool Patronum::ControllerPrivate::waitForResponce(int msec) {
_responce = false;
qint64 waitFor = QDateTime::currentMSecsSinceEpoch() + msec;
while (!_responce && QDateTime::currentMSecsSinceEpoch() < waitFor) {
QCoreApplication::processEvents();
}
return _responce;
}
QList<Feature> ControllerPrivate::features() const {
return _features;
}
bool ControllerPrivate::isConnected() const {
return _responce;
}
void ControllerPrivate::handleReceve(QByteArray data) {
@ -73,6 +100,9 @@ void ControllerPrivate::handleReceve(QByteArray data) {
QList<Feature> features;
stream >> features;
_features = features;
_responce = true;
_controller->handleFeatures(features);
@ -89,6 +119,8 @@ void ControllerPrivate::handleReceve(QByteArray data) {
QDataStream stream(package->data);
_responce = true;
QVariantMap feature;
stream >> feature;
_controller->handleResponce(feature);

@ -15,12 +15,21 @@ public:
bool sendFeaturesRequest();
bool sendCmd(const QList<Feature>& result);
bool waitForResponce(int msec);
QList<Feature> features() const;
bool isConnected() const;
signals:
void sigListFeatures(QList<Feature>);
private:
LocalSocket *_socket = nullptr;
IController *_controller = nullptr;
bool _responce = false;
QList<Feature> _features;
private slots:
void handleReceve(QByteArray data);

@ -22,7 +22,7 @@ bool LocalSocket::registerSokcet(QLocalSocket *socket) {
connect(m_socket, qOverload<QLocalSocket::LocalSocketError>(&QLocalSocket::error),
this, &LocalSocket::handleSocketError);
return m_socket->isValid();
return true;
}
bool LocalSocket::send(const QByteArray &data) {

BIN
Tests/build/Debug/PatronumTest Executable file

Binary file not shown.

@ -1,7 +1,14 @@
#include "defaultcontroller.h"
DefaultController::DefaultController():
Patronum::Controller("TestPatronum")
{
Patronum::Controller("TestPatronum") {
}
QVariantMap DefaultController::getResponce() {
return _receiveData;
}
void DefaultController::handleResponce(const QVariantMap &feature) {
_receiveData = feature;
}

@ -6,6 +6,15 @@ class DefaultController : public Patronum::Controller
{
public:
DefaultController();
QVariantMap getResponce();
protected:
void handleResponce(const QVariantMap &feature);
private:
QVariantMap _receiveData;
};
#endif // DEFAULTCONTROLLER_H

@ -1,12 +1,12 @@
#include "defaultservice.h"
const char* arg[] = {
"/",
"-e"
char* arg[] = {
const_cast<char*>("/"),
const_cast<char*>("-e")
};
DefaultService::DefaultService():
Patronum::Service<QCoreApplication>(0, arg, "TestPatronum") {
Patronum::Service<QCoreApplication>(2, arg, "TestPatronum") {
}
@ -19,7 +19,7 @@ void DefaultService::handleReceive(const QList<Patronum::Feature> &data) {
QList<Patronum::Feature> notSupportedList;
for (const auto& i : data) {
if (i.cmd() == "ping") {
sendResuylt("pomg");
sendResuylt("pong");
} else {
notSupportedList += i;
}

@ -3,68 +3,6 @@
#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()
{
@ -80,32 +18,3 @@ bool TestUtils::wait(const bool &forWait, int msec) {
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);
}

@ -3,29 +3,11 @@
#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);
};

@ -1,11 +1,10 @@
#include <QtTest>
#include <quasarapp.h>
#include <QCoreApplication>
#include "defaultcontroller.h"
#include "defaultservice.h"
#include "testutils.h"
#include <patronum.h>
#define TEST_LOCAL_HOST "127.0.0.1"
#define TEST_PORT 27777
@ -19,15 +18,13 @@ private:
public:
testProtockol();
void connectTest(Patronum::Service *service, Patronum::Controller *terminal);
~testProtockol();
private slots:
void initTestCase();
void testPakageData();
void testBaseNode();
void testUser();
void connectTest();
};
@ -37,9 +34,24 @@ testProtockol::testProtockol() {
}
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));
void testProtockol::connectTest() {
DefaultService serv;
QTimer::singleShot(0, [](){
const char* arg[] = {
"/",
"fd"
};
DefaultController cli;
QVERIFY(cli.send(2 , arg));
QVERIFY(cli.waitForResponce(1000));
QVERIFY(cli.getResponce().value("Result") == "pong");
});
QVERIFY(serv.exec() == 0);
}
testProtockol::~testProtockol() {
@ -51,4 +63,4 @@ void testProtockol::initTestCase() {
QTEST_APPLESS_MAIN(testProtockol)
#include "tst_testsnakeserver.moc"
#include "tst_unittests.moc"