improve code coverage

This commit is contained in:
Simon Stürz 2016-04-13 14:11:14 +02:00 committed by Michael Zanetti
parent bd23b42e36
commit 100e7145c9
6 changed files with 53 additions and 99 deletions

View File

@ -82,13 +82,6 @@ CoapOption::CoapOption()
} }
CoapOption::CoapOption(const CoapOption::Option &option, const QByteArray &data) :
m_option(option),
m_data(data)
{
}
void CoapOption::setOption(const CoapOption::Option &option) void CoapOption::setOption(const CoapOption::Option &option)
{ {
m_option = option; m_option = option;

View File

@ -56,7 +56,6 @@ public:
}; };
CoapOption(); CoapOption();
CoapOption(const Option &option, const QByteArray &data);
void setOption(const Option &option); void setOption(const Option &option);
Option option() const; Option option() const;

View File

@ -356,7 +356,11 @@ void CoapPdu::addOption(const CoapOption::Option &option, const QByteArray &data
break; break;
} }
} }
m_options.insert(index + 1, CoapOption(option, data));
CoapOption o;
o.setOption(option);
o.setData(data);
m_options.insert(index + 1, o);
} }
/*! Returns the block of this \l{CoapPdu}. */ /*! Returns the block of this \l{CoapPdu}. */

View File

@ -75,16 +75,3 @@ CoapPdu::MessageType CoapRequest::messageType() const
{ {
return m_messageType; return m_messageType;
} }
void CoapRequest::setStatusCode(const CoapPdu::StatusCode &statusCode)
{
m_statusCode = statusCode;
}
CoapPdu::StatusCode CoapRequest::statusCode()
{
return m_statusCode;
}

View File

@ -49,9 +49,6 @@ private:
CoapPdu::MessageType m_messageType; CoapPdu::MessageType m_messageType;
CoapPdu::StatusCode m_statusCode; CoapPdu::StatusCode m_statusCode;
void setStatusCode(const CoapPdu::StatusCode &statusCode);
CoapPdu::StatusCode statusCode();
}; };
#endif // COAPREQUEST_H #endif // COAPREQUEST_H

View File

@ -74,9 +74,6 @@ void CoapTests::invalidUrl()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(reply->isFinished(), "Reply not finished."); QVERIFY2(reply->isFinished(), "Reply not finished.");
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->error(), CoapReply::HostNotFoundError); QCOMPARE(reply->error(), CoapReply::HostNotFoundError);
@ -85,14 +82,50 @@ void CoapTests::invalidUrl()
void CoapTests::invalidScheme() void CoapTests::invalidScheme()
{ {
CoapRequest request(QUrl("http://coap.me")); CoapRequest request(QUrl("http://coap.me"));
qDebug() << request.url().toString();
QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*)));
// Get
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(1000); spy.wait(1000);
qDebug() << "===================================="; QVERIFY2(reply->isFinished(), "Reply not finished.");
qDebug() << reply; QVERIFY2(spy.count() == 0, "Got a response.");
QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError);
reply->deleteLater();
// Post
spy.clear();
reply = m_coap->post(request);
spy.wait(1000);
QVERIFY2(reply->isFinished(), "Reply not finished.");
QVERIFY2(spy.count() == 0, "Got a response.");
QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError);
// Put
spy.clear();
reply = m_coap->put(request);
spy.wait(1000);
QVERIFY2(reply->isFinished(), "Reply not finished.");
QVERIFY2(spy.count() == 0, "Got a response.");
QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError);
// Delete
spy.clear();
reply = m_coap->deleteResource(request);
spy.wait(1000);
QVERIFY2(reply->isFinished(), "Reply not finished.");
QVERIFY2(spy.count() == 0, "Got a response.");
QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError);
// Ping
spy.clear();
reply = m_coap->ping(request);
spy.wait(1000);
QVERIFY2(reply->isFinished(), "Reply not finished."); QVERIFY2(reply->isFinished(), "Reply not finished.");
QVERIFY2(spy.count() == 0, "Got a response."); QVERIFY2(spy.count() == 0, "Got a response.");
@ -101,16 +134,14 @@ void CoapTests::invalidScheme()
void CoapTests::ping() void CoapTests::ping()
{ {
CoapRequest request(QUrl("coap://coap.me/")); CoapRequest request;
request.setUrl(QUrl("coap://coap.me/"));
qDebug() << request.url().toString(); qDebug() << request.url().toString();
QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*)));
CoapReply *reply = m_coap->ping(request); CoapReply *reply = m_coap->ping(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(reply->isFinished(), "Reply not finished."); QVERIFY2(reply->isFinished(), "Reply not finished.");
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->statusCode(), CoapPdu::Empty); QCOMPARE(reply->statusCode(), CoapPdu::Empty);
@ -122,15 +153,14 @@ void CoapTests::ping()
void CoapTests::hello() void CoapTests::hello()
{ {
CoapRequest request(QUrl("coap://coap.me/hello")); CoapRequest request(QUrl("coap://coap.me/hello"));
request.setMessageType(CoapPdu::Confirmable);
qDebug() << request.url().toString(); qDebug() << request.url().toString();
QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*)));
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -149,9 +179,6 @@ void CoapTests::broken()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::InternalServerError); QCOMPARE(reply->statusCode(), CoapPdu::InternalServerError);
@ -170,9 +197,6 @@ void CoapTests::query()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -191,9 +215,6 @@ void CoapTests::subPath()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -212,9 +233,6 @@ void CoapTests::extendedOptionLength()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -233,9 +251,6 @@ void CoapTests::specialCharacters()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -268,9 +283,6 @@ void CoapTests::extendedDelta()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -289,9 +301,6 @@ void CoapTests::secret()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Unauthorized); QCOMPARE(reply->statusCode(), CoapPdu::Unauthorized);
@ -310,9 +319,6 @@ void CoapTests::separated()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(10000); spy.wait(10000);
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -331,9 +337,6 @@ void CoapTests::deleteResource()
CoapReply *reply = m_coap->deleteResource(request); CoapReply *reply = m_coap->deleteResource(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Deleted); QCOMPARE(reply->statusCode(), CoapPdu::Deleted);
@ -346,15 +349,13 @@ void CoapTests::deleteResource()
void CoapTests::post() void CoapTests::post()
{ {
CoapRequest request(QUrl("coap://coap.me:5683/validate")); CoapRequest request(QUrl("coap://coap.me:5683/validate"));
request.setContentType();
qDebug() << request.url().toString(); qDebug() << request.url().toString();
QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*)));
CoapReply *reply = m_coap->post(request, "guh is awesome"); CoapReply *reply = m_coap->post(request, "guh is awesome");
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Created); QCOMPARE(reply->statusCode(), CoapPdu::Created);
@ -373,9 +374,6 @@ void CoapTests::put()
CoapReply *reply = m_coap->put(request, "guh is awesome"); CoapReply *reply = m_coap->put(request, "guh is awesome");
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Changed); QCOMPARE(reply->statusCode(), CoapPdu::Changed);
@ -419,9 +417,6 @@ void CoapTests::largeDownload()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(20000); spy.wait(20000);
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -442,9 +437,6 @@ void CoapTests::largeCreate()
CoapReply *reply = m_coap->post(request, m_uploadData); CoapReply *reply = m_coap->post(request, m_uploadData);
spy.wait(20000); spy.wait(20000);
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Created); QCOMPARE(reply->statusCode(), CoapPdu::Created);
@ -459,9 +451,6 @@ void CoapTests::largeCreate()
reply = m_coap->get(request); reply = m_coap->get(request);
spy.wait(20000); spy.wait(20000);
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -482,9 +471,6 @@ void CoapTests::largeUpdate()
CoapReply *reply = m_coap->put(request, m_uploadData); CoapReply *reply = m_coap->put(request, m_uploadData);
spy.wait(20000); spy.wait(20000);
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Changed); QCOMPARE(reply->statusCode(), CoapPdu::Changed);
@ -499,9 +485,6 @@ void CoapTests::largeUpdate()
reply = m_coap->get(request); reply = m_coap->get(request);
spy.wait(20000); spy.wait(20000);
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -531,8 +514,6 @@ void CoapTests::multipleCalls()
spy.clear(); spy.clear();
foreach (CoapReply *reply, replies) { foreach (CoapReply *reply, replies) {
qDebug() << "====================================";
qDebug() << reply;
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->error(), CoapReply::NoError); QCOMPARE(reply->error(), CoapReply::NoError);
reply->deleteLater(); reply->deleteLater();
@ -550,9 +531,6 @@ void CoapTests::coreLinkParser()
CoapReply *reply = m_coap->get(request); CoapReply *reply = m_coap->get(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -562,6 +540,10 @@ void CoapTests::coreLinkParser()
CoreLinkParser parser(reply->payload()); CoreLinkParser parser(reply->payload());
QCOMPARE(parser.links().count(), 28); QCOMPARE(parser.links().count(), 28);
foreach (const CoreLink &link, parser.links()) {
qDebug() << link;
}
reply->deleteLater(); reply->deleteLater();
} }
@ -576,9 +558,6 @@ void CoapTests::observeResource()
CoapReply *reply = m_coap->enableResourceNotifications(request); CoapReply *reply = m_coap->enableResourceNotifications(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -603,9 +582,6 @@ void CoapTests::observeLargeResource()
CoapReply *reply = m_coap->enableResourceNotifications(request); CoapReply *reply = m_coap->enableResourceNotifications(request);
spy.wait(); spy.wait();
qDebug() << "====================================";
qDebug() << reply;
QVERIFY2(spy.count() > 0, "Did not get a response."); QVERIFY2(spy.count() > 0, "Did not get a response.");
QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement);
QCOMPARE(reply->statusCode(), CoapPdu::Content); QCOMPARE(reply->statusCode(), CoapPdu::Content);
@ -619,6 +595,4 @@ void CoapTests::observeLargeResource()
QTest::qWait(5000); QTest::qWait(5000);
} }
QTEST_MAIN(CoapTests) QTEST_MAIN(CoapTests)