add REST api and tests
This commit is contained in:
parent
7b3348168b
commit
36533b61aa
@ -202,7 +202,7 @@ HttpReply *DevicesResource::proccessPostRequest(const HttpRequest &request, cons
|
|||||||
|
|
||||||
// POST /api/v1/devices/{deviceId}
|
// POST /api/v1/devices/{deviceId}
|
||||||
if (urlTokens.count() == 4)
|
if (urlTokens.count() == 4)
|
||||||
return createErrorReply(HttpReply::BadRequest);
|
return editDevice(request.payload());
|
||||||
|
|
||||||
// POST /api/v1/devices/{deviceId}/execute/{actionTypeId}
|
// POST /api/v1/devices/{deviceId}/execute/{actionTypeId}
|
||||||
if (urlTokens.count() >= 6 && urlTokens.at(4) == "execute") {
|
if (urlTokens.count() >= 6 && urlTokens.at(4) == "execute") {
|
||||||
@ -393,6 +393,22 @@ HttpReply *DevicesResource::addConfiguredDevice(const QByteArray &payload) const
|
|||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HttpReply *DevicesResource::editDevice(const QByteArray &payload) const
|
||||||
|
{
|
||||||
|
QPair<bool, QVariant> verification = RestResource::verifyPayload(payload);
|
||||||
|
if (!verification.first)
|
||||||
|
return createErrorReply(HttpReply::BadRequest);
|
||||||
|
|
||||||
|
QVariantMap params = verification.second.toMap();
|
||||||
|
QString name = params.value("name").toString();
|
||||||
|
DeviceManager::DeviceError status = GuhCore::instance()->deviceManager()->editDevice(m_device->id(), name);
|
||||||
|
|
||||||
|
if (status != DeviceManager::DeviceErrorNoError)
|
||||||
|
return createDeviceErrorReply(HttpReply::BadRequest, status);
|
||||||
|
|
||||||
|
return createDeviceErrorReply(HttpReply::Ok, status);
|
||||||
|
}
|
||||||
|
|
||||||
HttpReply *DevicesResource::pairDevice(const QByteArray &payload) const
|
HttpReply *DevicesResource::pairDevice(const QByteArray &payload) const
|
||||||
{
|
{
|
||||||
QPair<bool, QVariant> verification = RestResource::verifyPayload(payload);
|
QPair<bool, QVariant> verification = RestResource::verifyPayload(payload);
|
||||||
|
|||||||
@ -69,6 +69,7 @@ private:
|
|||||||
// Post methods
|
// Post methods
|
||||||
HttpReply *executeAction(Device *device, const ActionTypeId &actionTypeId, const QByteArray &payload) const;
|
HttpReply *executeAction(Device *device, const ActionTypeId &actionTypeId, const QByteArray &payload) const;
|
||||||
HttpReply *addConfiguredDevice(const QByteArray &payload) const;
|
HttpReply *addConfiguredDevice(const QByteArray &payload) const;
|
||||||
|
HttpReply *editDevice(const QByteArray &payload) const;
|
||||||
HttpReply *pairDevice(const QByteArray &payload) const;
|
HttpReply *pairDevice(const QByteArray &payload) const;
|
||||||
HttpReply *confirmPairDevice(const QByteArray &payload) const;
|
HttpReply *confirmPairDevice(const QByteArray &payload) const;
|
||||||
|
|
||||||
|
|||||||
@ -87,7 +87,6 @@ private slots:
|
|||||||
void editDevices_data();
|
void editDevices_data();
|
||||||
void editDevices();
|
void editDevices();
|
||||||
|
|
||||||
|
|
||||||
void reconfigureDevices_data();
|
void reconfigureDevices_data();
|
||||||
void reconfigureDevices();
|
void reconfigureDevices();
|
||||||
|
|
||||||
@ -757,9 +756,16 @@ void TestDevices::editDevices()
|
|||||||
QString originalName = "Test device";
|
QString originalName = "Test device";
|
||||||
|
|
||||||
// add device
|
// add device
|
||||||
|
QVariantList deviceParams;
|
||||||
|
QVariantMap httpportParam;
|
||||||
|
httpportParam.insert("name", "httpport");
|
||||||
|
httpportParam.insert("value", 8888);
|
||||||
|
deviceParams.append(httpportParam);
|
||||||
|
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("deviceClassId", mockParentDeviceClassId);
|
params.insert("deviceClassId", mockDeviceClassId);
|
||||||
params.insert("name", originalName);
|
params.insert("name", originalName);
|
||||||
|
params.insert("deviceParams", deviceParams);
|
||||||
QVariant response = injectAndWait("Devices.AddConfiguredDevice", params);
|
QVariant response = injectAndWait("Devices.AddConfiguredDevice", params);
|
||||||
verifyDeviceError(response);
|
verifyDeviceError(response);
|
||||||
DeviceId deviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString());
|
DeviceId deviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString());
|
||||||
|
|||||||
@ -61,11 +61,14 @@ private slots:
|
|||||||
void getStateValue_data();
|
void getStateValue_data();
|
||||||
void getStateValue();
|
void getStateValue();
|
||||||
|
|
||||||
|
void editDevices_data();
|
||||||
|
void editDevices();
|
||||||
|
|
||||||
void reconfigureDevices_data();
|
void reconfigureDevices_data();
|
||||||
void reconfigureDevices();
|
void reconfigureDevices();
|
||||||
|
|
||||||
void editByDiscovery_data();
|
void reconfigureByDiscovery_data();
|
||||||
void editByDiscovery();
|
void reconfigureByDiscovery();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -519,6 +522,57 @@ void TestRestDevices::getStateValue()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestRestDevices::editDevices_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("name");
|
||||||
|
|
||||||
|
QTest::newRow("change name") << "New device name";
|
||||||
|
QTest::newRow("change name") << "Foo device";
|
||||||
|
QTest::newRow("change name") << "Bar device";
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestRestDevices::editDevices()
|
||||||
|
{
|
||||||
|
QFETCH(QString, name);
|
||||||
|
QString originalName = "Test device";
|
||||||
|
|
||||||
|
QVariantList deviceParams;
|
||||||
|
QVariantMap httpportParam;
|
||||||
|
httpportParam.insert("name", "httpport");
|
||||||
|
httpportParam.insert("value", m_mockDevice1Port - 2);
|
||||||
|
deviceParams.append(httpportParam);
|
||||||
|
|
||||||
|
QVariantMap params;
|
||||||
|
params.insert("deviceClassId", mockDeviceClassId.toString());
|
||||||
|
params.insert("name", originalName);
|
||||||
|
params.insert("deviceParams", deviceParams);
|
||||||
|
|
||||||
|
QNetworkRequest addRequest(QUrl("http://localhost:3333/api/v1/devices"));
|
||||||
|
addRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
|
|
||||||
|
QVariant response = postAndWait(addRequest, params);
|
||||||
|
DeviceId deviceId = DeviceId(response.toMap().value("id").toString());
|
||||||
|
QVERIFY2(!deviceId.isNull(), "invalid device id");
|
||||||
|
|
||||||
|
// edit device
|
||||||
|
params.clear();
|
||||||
|
params.insert("name", name);
|
||||||
|
|
||||||
|
QNetworkRequest deviceRequest(QUrl(QString("http://localhost:3333/api/v1/devices/%1").arg(deviceId.toString())));
|
||||||
|
deviceRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
|
|
||||||
|
response = postAndWait(deviceRequest, params);
|
||||||
|
QVERIFY2(response.toMap().value("error").toString() == JsonTypes::deviceErrorToString(DeviceManager::DeviceErrorNoError), "Could not edit device name");
|
||||||
|
|
||||||
|
// check device name
|
||||||
|
response = getAndWait(deviceRequest);
|
||||||
|
QCOMPARE(response.toMap().value("name").toString(), name);
|
||||||
|
|
||||||
|
// Remove the device
|
||||||
|
response = deleteAndWait(deviceRequest);
|
||||||
|
QVERIFY2(response.toMap().value("error").toString() == JsonTypes::deviceErrorToString(DeviceManager::DeviceErrorNoError), "Could not remove device");
|
||||||
|
}
|
||||||
|
|
||||||
void TestRestDevices::reconfigureDevices_data()
|
void TestRestDevices::reconfigureDevices_data()
|
||||||
{
|
{
|
||||||
QVariantList asyncChangeDeviceParams;
|
QVariantList asyncChangeDeviceParams;
|
||||||
@ -594,7 +648,7 @@ void TestRestDevices::reconfigureDevices()
|
|||||||
DeviceId deviceId = DeviceId(response.toMap().value("id").toString());
|
DeviceId deviceId = DeviceId(response.toMap().value("id").toString());
|
||||||
QVERIFY2(deviceId != DeviceId(), "DeviceId not returned");
|
QVERIFY2(deviceId != DeviceId(), "DeviceId not returned");
|
||||||
|
|
||||||
// now EDIT the added device
|
// now RECONFIGURE the added device
|
||||||
QVariantMap editParams;
|
QVariantMap editParams;
|
||||||
editParams.insert("deviceId", deviceId);
|
editParams.insert("deviceId", deviceId);
|
||||||
editParams.insert("deviceParams", newDeviceParams);
|
editParams.insert("deviceParams", newDeviceParams);
|
||||||
@ -605,7 +659,7 @@ void TestRestDevices::reconfigureDevices()
|
|||||||
response = putAndWait(request, editParams, expectedStatusCode);
|
response = putAndWait(request, editParams, expectedStatusCode);
|
||||||
QVERIFY2(!response.isNull(), "Could not read edit device response");
|
QVERIFY2(!response.isNull(), "Could not read edit device response");
|
||||||
|
|
||||||
// if the edit should have been successfull
|
// if the reconfigure should have been successfull
|
||||||
if (expectedStatusCode == 200) {
|
if (expectedStatusCode == 200) {
|
||||||
request.setUrl(QUrl(QString("http://localhost:3333/api/v1/devices/%1").arg(deviceId.toString())));
|
request.setUrl(QUrl(QString("http://localhost:3333/api/v1/devices/%1").arg(deviceId.toString())));
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/json");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/json");
|
||||||
@ -622,7 +676,7 @@ void TestRestDevices::reconfigureDevices()
|
|||||||
QVERIFY2(!response.isNull(), "Could not delete device");
|
QVERIFY2(!response.isNull(), "Could not delete device");
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestRestDevices::editByDiscovery_data()
|
void TestRestDevices::reconfigureByDiscovery_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<DeviceClassId>("deviceClassId");
|
QTest::addColumn<DeviceClassId>("deviceClassId");
|
||||||
QTest::addColumn<int>("resultCount");
|
QTest::addColumn<int>("resultCount");
|
||||||
@ -638,7 +692,7 @@ void TestRestDevices::editByDiscovery_data()
|
|||||||
QTest::newRow("discover 2 devices with params") << mockDeviceClassId << 2 << discoveryParams << 200;
|
QTest::newRow("discover 2 devices with params") << mockDeviceClassId << 2 << discoveryParams << 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestRestDevices::editByDiscovery()
|
void TestRestDevices::reconfigureByDiscovery()
|
||||||
{
|
{
|
||||||
QFETCH(DeviceClassId, deviceClassId);
|
QFETCH(DeviceClassId, deviceClassId);
|
||||||
QFETCH(int, resultCount);
|
QFETCH(int, resultCount);
|
||||||
@ -711,7 +765,7 @@ void TestRestDevices::editByDiscovery()
|
|||||||
QVERIFY(!descriptorId2.isNull());
|
QVERIFY(!descriptorId2.isNull());
|
||||||
qDebug() << "edit device 1 (55555) with descriptor 2 (55556) " << descriptorId2;
|
qDebug() << "edit device 1 (55555) with descriptor 2 (55556) " << descriptorId2;
|
||||||
|
|
||||||
// EDIT
|
// RECONFIGURE
|
||||||
response.clear();
|
response.clear();
|
||||||
params.clear();
|
params.clear();
|
||||||
params.insert("deviceId", deviceId.toString());
|
params.insert("deviceId", deviceId.toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user