ref #46 Added methods is fromJson and toJson, tests.

This commit is contained in:
IgorekLoschinin 2021-04-30 00:46:42 +03:00
parent c0e4477605
commit e7dae7c6da
5 changed files with 106 additions and 23 deletions

View File

@ -28,4 +28,30 @@ const int &Owner::getTimePoint() const {
return _timePoint;
}
void Owner::fromjson(const QJsonObject &objJs) {
if (objJs.contains("name") && objJs.value("name").isString()) {
_name = objJs.value("name").toString();
}
if (objJs.contains("timePoint")) {
_timePoint = objJs.value("timePoint").toInt();
}
}
const QJsonObject &Owner::toJson(QJsonObject &objJs) const {
objJs["name"] = _name;
objJs["timePoint"] = _timePoint;
return objJs;
}
bool Owner::isValid() const {
if (_name.size() != 0 && _timePoint > 0) {
return true;
}
return false;
}
};

View File

@ -10,6 +10,7 @@
#include "CopyrighFixer_global.h"
#include <QString>
#include <QJsonObject>
namespace CopyrighFixer{
@ -36,17 +37,36 @@ public:
* @brief setTimeRange A method that allows you to set the time interval for using a file.
* @param interval This is a string value indicating the date of ownership of the file.
*/
void setTimePoint(const QString &interval);
void setTimePoint(const int &interval);
/**
* @brief getTimeRange The method changes the timestamp of the usage.
* @return the time interval when the file was modified
*/
const QString& getTimePoint() const;
const int& getTimePoint() const;
/**
* @brief fromjson Reads data from json file.
* @param objJs It's object json that contains information about the owner.
*/
void fromjson(const QJsonObject &objJs);
/**
* @brief toJson This method that converts object Owner to json object.
* @param objJs It's object json - container.
* @return Returns a json object with owner a information.
*/
const QJsonObject &toJson(QJsonObject &objJs) const;
/**
* @brief isValid Checks if an object is initialized.
* @return Returns true if object is initialized.
*/
bool isValid() const;
private:
QString _name;
int _timePoint;
QString _name = "";
int _timePoint = 1;
};

View File

@ -12,7 +12,6 @@
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonObject>
#include <QByteArray>
#include <QJsonDocument>
@ -57,8 +56,7 @@ bool Signature::fromJson(const QString &pathToFile) {
return false;
}
QByteArray byteSignFile;
byteSignFile = file.readAll();
QByteArray byteSignFile(file.readAll());
file.close();
QJsonDocument jsDoc(QJsonDocument::fromJson(byteSignFile));
@ -77,18 +75,18 @@ bool Signature::fromJson(const QString &pathToFile) {
return false;
}
_licenseTitle = jsObj["license"].toString();
_customMessage = jsObj["licenseText"].toString();
Owner OwnObj;
QMap<int, Owner> ownMap;
// _ownersMap = jsObj["ownersList"]
Owner ownObj;
QJsonArray ownLst(jsObj.value("ownersList").toArray());
for (auto itemLst = ownLst.cbegin(); itemLst != ownLst.end(); ++itemLst) {
ownObj.fromjson(itemLst->toObject());
_ownersMap.insert(itemLst->toObject().value("timePoint").toInt(), ownObj);
}
_licenseTitle = jsObj.value("license").toString();
_customMessage = jsObj.value("licenseText").toString();
return true;
} else {
return false;
}
@ -97,9 +95,14 @@ bool Signature::fromJson(const QString &pathToFile) {
bool Signature::toJson(QString &pathToFile) const {
QFileInfo checkFile(pathToFile);
if (checkFile.exists() && checkFile.isFile()) {
QFile::remove(pathToFile);
}
QJsonArray lstObjown;
QJsonObject objOwner;
for (auto obj = _ownersMap.cbegin(); obj != _ownersMap.end(); ++obj) {
objOwner["timePoint"] = obj.value().getTimePoint();
objOwner["name"] = obj.value().getOwnerName();
@ -124,7 +127,19 @@ bool Signature::toJson(QString &pathToFile) const {
saveFile.write(jsonDoc);
saveFile.close();
return 1;
return true;
}
bool Signature::isValid() const {
if (_licenseTitle.size() != 0 || _customMessage != 0) {
return true;
}
if (_ownersMap.cbegin().key() > 0 && _ownersMap.cbegin().value().isValid()) {
return true;
}
return false;
}
}

View File

@ -70,10 +70,17 @@ public:
*/
bool toJson(QString &pathToFile) const;
/**
* @brief isValid Checks if an object is initialized.
* @return Returns true if object is initialized.
*/
bool isValid() const;
private:
QMap<int, Owner> _ownersMap;
QString _licenseTitle;
QString _customMessage;
QString _licenseTitle = "";
QString _customMessage = "";
};
}

View File

@ -21,8 +21,23 @@ void SignTest::test() {
}
void SignTest::testJsonObj() {
CopyrighFixer::Signature sign_fromJson;
CopyrighFixer::Signature sign_toJson;
QString filename = "signature.json";
QVERIFY(sign_fromJson.fromJson() == sign_toJson.toJson());
CopyrighFixer::Owner ownerObj;
ownerObj.setName("QuasarApp");
ownerObj.setTimePoint(QDateTime::currentMSecsSinceEpoch());
QMap<int, CopyrighFixer::Owner> OwnerMap;
OwnerMap.insert(ownerObj.getTimePoint(), ownerObj);
CopyrighFixer::Signature sign_toJson;
sign_toJson.setLicenseTitle("Copyright (C) 2020-2021 QuasarApp.");
sign_toJson.setMessage("Distributed under the lgplv3 software license, see the accompany.");
sign_toJson.setMapOwners(OwnerMap);
sign_toJson.toJson(filename);
CopyrighFixer::Signature sign_fromJson;
sign_fromJson.fromJson(filename);
QVERIFY(sign_fromJson.isValid() == sign_toJson.isValid());
}