Add classes to interpret data from colnod api

This commit is contained in:
Ondřej Fischer 2019-10-01 16:11:17 +02:00
parent 16625aff2d
commit 41fdabd5f6
9 changed files with 391 additions and 3 deletions

View File

@ -3,9 +3,15 @@ INCLUDEPATH += $$PWD
message($$PWD) message($$PWD)
SOURCES += \ SOURCES += \
$$PWD/databasemanager.cpp \ $$PWD/databasemanager.cpp \
$$PWD/requestmanager.cpp $$PWD/entity.cpp \
$$PWD/requestmanager.cpp \
$$PWD/subject.cpp \
$$PWD/token.cpp
HEADERS += \ HEADERS += \
$$PWD/databasemanager.h \ $$PWD/databasemanager.h \
$$PWD/requestmanager.h $$PWD/entity.h \
$$PWD/requestmanager.h \
$$PWD/subject.h \
$$PWD/token.h
message($$SOURCES) message($$SOURCES)

52
src/colnod/entity.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <QJsonObject>
#include "entity.h"
const QUuid &Entity::getId() const {
return id;
}
void Entity::setId(const QUuid &id) {
Entity::id = id;
}
const QString &Entity::getName() const {
return name;
}
void Entity::setName(const QString &name) {
Entity::name = name;
}
const QString &Entity::getDescription() const {
return description;
}
void Entity::setDescription(const QString &description) {
Entity::description = description;
}
uint64_t Entity::getLastUpdate() const {
return lastUpdate;
}
void Entity::setLastUpdate(uint64_t lastUpdate) {
this->lastUpdate = lastUpdate;
}
void Entity::fromJSON(const QJsonObject &json) {
id = json.value("id").toString();
name = json.value("name").toString();
description = json.value("description").toString();
lastUpdate = json.value("lastUpdate").toDouble();
}
QJsonObject Entity::toJSON() const {
QJsonObject json;
json.value("id") = id.toString(QUuid::WithoutBraces);
json.value("name") = name;
json.value("description") = description;
json.value("lastUpdate") = lastUpdate;
return json;
}

37
src/colnod/entity.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef COLNOD_CONNECTOR_ENTITY_H
#define COLNOD_CONNECTOR_ENTITY_H
#include <QtCore/QObject>
#include <QtCore/QUuid>
#include <QtCore/QJsonObject>
class Entity : public QObject {
Q_OBJECT
public:
using QObject::QObject;
const QUuid &getId() const;
void setId(const QUuid &id);
const QString &getName() const;
void setName(const QString &name);
const QString &getDescription() const;
void setDescription(const QString &description);
uint64_t getLastUpdate() const;
void setLastUpdate(uint64_t lastUpdate);
virtual void fromJSON(const QJsonObject &json);
virtual QJsonObject toJSON() const;
protected:
QUuid id;
QString name;
QString description;
qint64 lastUpdate;
};
#endif //COLNOD_CONNECTOR_ENTITY_H

74
src/colnod/subject.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <utility>
#include "Subject.h"
#include <QJsonArray>
QString Subject::getEmail() const {
return attributes["email"].toString();
}
void Subject::fromJSON(const QJsonObject &json) {
Entity::fromJSON(json);
attributes = json["attributes"]["items"].toObject();
attributes = json.value("attributes").toObject().value("items").toObject();
const auto &tokenFlagsArray = json.value("tokenFlags").toArray();
for(const auto &flag : tokenFlagsArray){
tokenFlags.insert(flag.toString());
}
pin = json.value("pin").toString();
const auto &pinFlagsArray = json.value("pinFlags").toArray();
for(const auto &flag : pinFlagsArray){
pinFlags.insert(flag.toString());
}
}
QJsonObject Subject::toJSON() const {
QJsonObject json = Entity::toJSON();
QJsonObject items({{"items", attributes}});
json.value("atributes") = items;
json.value("tokenFlags") = QJsonArray::fromStringList(tokenFlags.toList());
json.value("pin") = pin;
json.value("pinFlags") = QJsonArray::fromStringList(pinFlags.toList());
return json;
}
const QJsonObject &Subject::getAttributes() const {
return attributes;
}
void Subject::setAttributes(const QJsonObject &attributes) {
Subject::attributes = attributes;
}
const QSet<QString> &Subject::getTokenFlags() const {
return tokenFlags;
}
void Subject::setTokenFlags(const QSet<QString> &tokenFlags) {
Subject::tokenFlags = tokenFlags;
}
const QString &Subject::getPin() const {
return pin;
}
void Subject::setPin(const QString &pin) {
Subject::pin = pin;
}
const QSet<QString> &Subject::getPinFlags() const {
return pinFlags;
}
void Subject::setPinFlags(const QSet<QString> &pinFlags) {
Subject::pinFlags = pinFlags;
}

35
src/colnod/subject.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef COLNOD_CONNECTOR_SUBJECT_H
#define COLNOD_CONNECTOR_SUBJECT_H
#include <QSet>
#include "entity.h"
class Subject : public Entity {
public:
using Entity::Entity;
QString getEmail() const;
const QJsonObject &getAttributes() const;
void setAttributes(const QJsonObject &attributes);
const QSet<QString> &getTokenFlags() const;
void setTokenFlags(const QSet<QString> &tokenFlags);
const QString &getPin() const;
void setPin(const QString &pin);
const QSet<QString> &getPinFlags() const;
void setPinFlags(const QSet<QString> &pinFlags);
void fromJSON(const QJsonObject &json) override;
QJsonObject toJSON() const override;
protected:
QJsonObject attributes;
QSet<QString> tokenFlags;
QString pin;
QSet<QString> pinFlags;
};
#endif //COLNOD_CONNECTOR_SUBJECT_H

108
src/colnod/token.cpp Normal file
View File

@ -0,0 +1,108 @@
#include "token.h"
#include <QJsonArray>
void Token::fromJSON(const QJsonObject &json) {
Entity::fromJSON(json);
tokenType = json.value("tokenType").toString();
tokenBits = json.value("tokenBits").toInt();
tokenData = json.value("tokenData").toString();
validFrom = json.value("validFrom").toDouble();
validTo = json.value("validTo").toDouble();
timeToLive = json.value("timeToLive").toInt();
authErrors = json["authErrors"].toInt();
const auto &flagsArray = json["flags"].toArray();
for(const auto &flag : flagsArray){
flags.insert(flag.toString());
}
subjectId = json.value("subjectId").toString();
}
QJsonObject Token::toJSON() const {
QJsonObject json = Entity::toJSON();
json.value("tokenType") = tokenType;
json.value("tokenBits") = tokenBits;
json.value("tokenData") = tokenData;
json.value("validFrom") = validFrom;
json.value("validTo") = validTo;
json.value("timeToLive") = timeToLive;
json.value("authErrors") = authErrors;
json.value("flags") = QJsonArray::fromStringList(flags.toList());
json.value("subjectId") = subjectId.toString(QUuid::WithoutBraces);
return json;
}
const QString &Token::getTokenType() const {
return tokenType;
}
void Token::setTokenType(const QString &tokenType) {
Token::tokenType = tokenType;
}
int Token::getTokenBits() const {
return tokenBits;
}
void Token::setTokenBits(int tokenBits) {
Token::tokenBits = tokenBits;
}
const QString &Token::getTokenData() const {
return tokenData;
}
void Token::setTokenData(const QString &tokenData) {
Token::tokenData = tokenData;
}
qint64 Token::getValidFrom() const {
return validFrom;
}
void Token::setValidFrom(qint64 validFrom) {
Token::validFrom = validFrom;
}
qint64 Token::getValidTo() const {
return validTo;
}
void Token::setValidTo(qint64 validTo) {
Token::validTo = validTo;
}
int Token::getTimeToLive() const {
return timeToLive;
}
void Token::setTimeToLive(int timeToLive) {
Token::timeToLive = timeToLive;
}
int Token::getAuthErrors() const {
return authErrors;
}
void Token::setAuthErrors(int authErrors) {
Token::authErrors = authErrors;
}
const QSet<QString> &Token::getFlags() const {
return flags;
}
void Token::setFlags(const QSet<QString> &flags) {
Token::flags = flags;
}
const QUuid &Token::getSubjectId() const {
return subjectId;
}
void Token::setSubjectId(const QUuid &subjectId) {
Token::subjectId = subjectId;
}

48
src/colnod/token.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef COLNOD_CONNECTOR_TOKEN_H
#define COLNOD_CONNECTOR_TOKEN_H
#include "entity.h"
#include <QSet>
class Token : public Entity {
public:
// using Entity::Entity;
const QString &getTokenType() const;
void setTokenType(const QString &tokenType);
int getTokenBits() const;
void setTokenBits(int tokenBits);
const QString &getTokenData() const;
void setTokenData(const QString &tokenData);
qint64 getValidFrom() const;
void setValidFrom(qint64 validFrom);
qint64 getValidTo() const;
void setValidTo(qint64 validTo);
int getTimeToLive() const;
void setTimeToLive(int timeToLive);
int getAuthErrors() const;
void setAuthErrors(int authErrors);
const QSet<QString> &getFlags() const;
void setFlags(const QSet<QString> &flags);
const QUuid &getSubjectId() const;
void setSubjectId(const QUuid &subjectId);
void fromJSON(const QJsonObject &json) override;
QJsonObject toJSON() const override;
protected:
QString tokenType;
int tokenBits;
QString tokenData;
qint64 validFrom;
qint64 validTo;
int timeToLive;
int authErrors;
QSet<QString> flags;
QUuid subjectId;
};
#endif //COLNOD_CONNECTOR_TOKEN_H

View File

@ -62,5 +62,32 @@ void UnitTest::getTokensOpenManager()
QVERIFY(!response.isEmpty()); QVERIFY(!response.isEmpty());
} }
void UnitTest::subjectJSONParser()
{
QFile jsonData(QDir::current().filePath("subjects.json"));
if (!jsonData.open(QIODevice::ReadOnly)) {
qCritical() << "Couldn't open config file config.json" << QDir::current().filePath("subjects.json");
QCoreApplication::exit(-1);
return;
}
QJsonDocument doc = QJsonDocument::fromJson(jsonData.readAll());
QJsonObject obj = doc.object();
QJsonArray arr = obj["items"].toArray();
/*
for(auto item : arr)
{
auto subject = new Subject();
subject->fromJSON(item.toObject());
}*/
auto testSubject = new Subject();
testSubject->fromJSON(arr[1].toObject());
qDebug() << testSubject->getEmail();
qDebug() << testSubject->getName();
QVERIFY(true);
}
QTEST_MAIN(UnitTest) QTEST_MAIN(UnitTest)

View File

@ -3,6 +3,7 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <QSignalSpy> #include <QSignalSpy>
#include <requestmanager.h> #include <requestmanager.h>
#include "subject.h"
class UnitTest : public QObject class UnitTest : public QObject
@ -15,7 +16,7 @@ private slots:
void getSubjects(); // /SubjectManager/getSubjects void getSubjects(); // /SubjectManager/getSubjects
void getTokens(); // /TokenManager/getTokens void getTokens(); // /TokenManager/getTokens
void getTokensOpenManager(); // /OpenManager/getTokenSubject void getTokensOpenManager(); // /OpenManager/getTokenSubject
void subjectJSONParser();
private: private:
// QString host = "https://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master) // QString host = "https://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master)
QString host = "https://192.168.1.3:8443"; //local testing server QString host = "https://192.168.1.3:8443"; //local testing server