Made code more explicit and additional tests.
This commit is contained in:
parent
10d2a6b912
commit
456d4830fe
2 changed files with 27 additions and 24 deletions
|
@ -71,27 +71,17 @@ void NetworkPacket::createIdentityPacket(NetworkPacket* np)
|
||||||
//qCDebug(KDECONNECT_CORE) << "createIdentityPacket" << np->serialize();
|
//qCDebug(KDECONNECT_CORE) << "createIdentityPacket" << np->serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
|
||||||
QVariantMap qobject2qvariant(const T* object)
|
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
auto metaObject = T::staticMetaObject;
|
|
||||||
for(int i = metaObject.propertyOffset(); i < metaObject.propertyCount(); ++i) {
|
|
||||||
QMetaProperty prop = metaObject.property(i);
|
|
||||||
map.insert(QString::fromLatin1(prop.name()), prop.readOnGadget(object));
|
|
||||||
}
|
|
||||||
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray NetworkPacket::serialize() const
|
QByteArray NetworkPacket::serialize() const
|
||||||
{
|
{
|
||||||
//Object -> QVariant
|
//Object -> QVariant
|
||||||
QVariantMap variant = qobject2qvariant(this);
|
QVariantMap variant;
|
||||||
|
variant.insert(QStringLiteral("id"), m_id);
|
||||||
|
variant.insert(QStringLiteral("type"), m_type);
|
||||||
|
variant.insert(QStringLiteral("body"), m_body);
|
||||||
|
|
||||||
if (!hasPayload()) {
|
if (hasPayload()) {
|
||||||
variant.remove(QStringLiteral("payloadSize"));
|
variant.insert(QStringLiteral("payloadSize"), m_payloadSize);
|
||||||
variant.remove(QStringLiteral("payloadTransferInfo"));
|
variant.insert(QStringLiteral("payloadTransferInfo"), m_payloadTransferInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
//QVariant -> json
|
//QVariant -> json
|
||||||
|
|
|
@ -82,9 +82,10 @@ void NetworkPacketTests::networkPacketIdentityTest()
|
||||||
void NetworkPacketTests::networkPacketPayloadTest()
|
void NetworkPacketTests::networkPacketPayloadTest()
|
||||||
{
|
{
|
||||||
QByteArray json;
|
QByteArray json;
|
||||||
|
NetworkPacket np;
|
||||||
|
|
||||||
// empty package
|
// empty package
|
||||||
NetworkPacket np(QStringLiteral("com.test"));
|
np = NetworkPacket(QStringLiteral("com.test"));
|
||||||
json = np.serialize();
|
json = np.serialize();
|
||||||
qDebug() << json;
|
qDebug() << json;
|
||||||
QVERIFY(!json.contains("\"payloadSize\""));
|
QVERIFY(!json.contains("\"payloadSize\""));
|
||||||
|
@ -93,10 +94,10 @@ void NetworkPacketTests::networkPacketPayloadTest()
|
||||||
// package with payload
|
// package with payload
|
||||||
QByteArray buffer("test data");
|
QByteArray buffer("test data");
|
||||||
auto payload = QSharedPointer<QIODevice>(new QBuffer(&buffer, this));
|
auto payload = QSharedPointer<QIODevice>(new QBuffer(&buffer, this));
|
||||||
NetworkPacket payloadNp(QStringLiteral("com.test"));
|
np = NetworkPacket(QStringLiteral("com.test"));
|
||||||
payloadNp.setPayload(payload, buffer.size());
|
np.setPayload(payload, buffer.size());
|
||||||
|
|
||||||
json = payloadNp.serialize();
|
json = np.serialize();
|
||||||
qDebug() << json;
|
qDebug() << json;
|
||||||
QVERIFY(json.contains("\"payloadSize\":9"));
|
QVERIFY(json.contains("\"payloadSize\":9"));
|
||||||
QVERIFY(json.contains("\"payloadTransferInfo\""));
|
QVERIFY(json.contains("\"payloadTransferInfo\""));
|
||||||
|
@ -104,13 +105,25 @@ void NetworkPacketTests::networkPacketPayloadTest()
|
||||||
// package with empty payload
|
// package with empty payload
|
||||||
QByteArray emptyBuffer("test data");
|
QByteArray emptyBuffer("test data");
|
||||||
auto emptyPayload = QSharedPointer<QIODevice>(new QBuffer(&emptyBuffer, this));
|
auto emptyPayload = QSharedPointer<QIODevice>(new QBuffer(&emptyBuffer, this));
|
||||||
NetworkPacket emptyPayloadNp(QStringLiteral("com.test"));
|
np = NetworkPacket(QStringLiteral("com.test"));
|
||||||
emptyPayloadNp.setPayload(emptyPayload, 0);
|
np.setPayload(emptyPayload, 0);
|
||||||
|
|
||||||
json = emptyPayloadNp.serialize();
|
json = np.serialize();
|
||||||
qDebug() << json;
|
qDebug() << json;
|
||||||
QVERIFY(!json.contains("\"payloadSize\""));
|
QVERIFY(!json.contains("\"payloadSize\""));
|
||||||
QVERIFY(!json.contains("\"payloadTransferInfo\""));
|
QVERIFY(!json.contains("\"payloadTransferInfo\""));
|
||||||
|
|
||||||
|
// incoming package without payload
|
||||||
|
np = NetworkPacket();
|
||||||
|
QVERIFY(NetworkPacket::unserialize(
|
||||||
|
"{\"body\":{},\"id\":\"1578136807254\",\"type\":\"com.test\"}\n", &np));
|
||||||
|
QVERIFY(!np.hasPayload());
|
||||||
|
|
||||||
|
// incoming package without payload (but with payload keys)
|
||||||
|
np = NetworkPacket();
|
||||||
|
QVERIFY(NetworkPacket::unserialize(
|
||||||
|
"{\"body\":{},\"id\":\"1578136807254\",\"payloadSize\":0,\"payloadTransferInfo\":{},\"type\":\"com.test\"}\n", &np));
|
||||||
|
QVERIFY(!np.hasPayload());
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkPacketTests::cleanupTestCase()
|
void NetworkPacketTests::cleanupTestCase()
|
||||||
|
|
Loading…
Reference in a new issue