2009-02-07 17:30:58 +01:00
|
|
|
/**
|
|
|
|
* This file is part of the KDE project
|
|
|
|
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public License
|
|
|
|
* along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2009-02-08 23:11:14 +01:00
|
|
|
#include <QtCore/QEventLoop>
|
2009-02-07 17:30:58 +01:00
|
|
|
#include <QtCore/QObject>
|
|
|
|
#include <QtCore/QProcess>
|
|
|
|
#include <QtNetwork/QTcpSocket>
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2009-02-08 23:11:14 +01:00
|
|
|
#include <sodepclient.h>
|
2009-02-07 17:30:58 +01:00
|
|
|
#include <sodepmessage.h>
|
|
|
|
#include "sodeptesthelpers.h"
|
|
|
|
|
|
|
|
#ifndef DATA_DIR
|
|
|
|
#error "DATA_DIR is not set. A directory containing test jolie scripts is required for this test"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void dump(const SodepValue &value, int level)
|
|
|
|
{
|
|
|
|
QString indent;
|
|
|
|
|
|
|
|
while (level>0) {
|
|
|
|
indent+=" ";
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << (indent+value.toString()) << value.toInt() << value.toDouble();
|
|
|
|
foreach (const QString &name, value.childrenNames()) {
|
|
|
|
QList<SodepValue> children = value.children(name);
|
|
|
|
qDebug() << (indent+"Children:") << name;
|
|
|
|
foreach (const SodepValue &child, children) {
|
|
|
|
dump(child, level+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump(const SodepMessage &message)
|
|
|
|
{
|
|
|
|
qDebug() << "Resource :" << message.resourcePath();
|
|
|
|
qDebug() << "Operation:" << message.operationName();
|
|
|
|
qDebug() << "Fault :" << message.fault().name();
|
|
|
|
dump(message.fault().data(), 1);
|
|
|
|
qDebug() << "Value :";
|
|
|
|
dump(message.data(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SodepMetaServiceTest : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2009-02-08 22:12:45 +01:00
|
|
|
QProcess m_metaserviceProcess;
|
|
|
|
QTcpSocket m_socket;
|
2009-02-08 23:11:14 +01:00
|
|
|
SodepClient m_client;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SodepMetaServiceTest()
|
|
|
|
: QObject(), m_client(&m_socket)
|
|
|
|
{
|
|
|
|
qRegisterMetaType<SodepMessage>();
|
|
|
|
}
|
2009-02-07 17:30:58 +01:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void initTestCase()
|
|
|
|
{
|
2009-02-08 22:12:45 +01:00
|
|
|
m_metaserviceProcess.start("metaservice");
|
|
|
|
QVERIFY2(m_metaserviceProcess.waitForStarted(), "Looks like you don't have Jolie's metaservice command");
|
2009-02-07 17:30:58 +01:00
|
|
|
QTest::qWait(1000);
|
2009-02-08 22:12:45 +01:00
|
|
|
|
|
|
|
m_socket.connectToHost("localhost", 9000);
|
|
|
|
QVERIFY(m_socket.waitForConnected(-1));
|
2009-02-07 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanupTestCase()
|
|
|
|
{
|
|
|
|
SodepMessage message("/", "shutdown");
|
2009-02-08 16:35:56 +01:00
|
|
|
sodepWrite(m_socket, message);
|
2009-02-07 17:30:58 +01:00
|
|
|
QTest::qWait(1000);
|
|
|
|
|
2009-02-08 22:12:45 +01:00
|
|
|
m_socket.close();
|
|
|
|
|
|
|
|
m_metaserviceProcess.waitForFinished();
|
2009-02-07 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void shouldLoadService_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("resourcePrefix");
|
|
|
|
QTest::addColumn<QString>("fileName");
|
|
|
|
|
|
|
|
QTest::newRow("printer service") << "Printer" << "printer.ol";
|
|
|
|
QTest::newRow("math service") << "Math" << "math.ol";
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldLoadService()
|
|
|
|
{
|
|
|
|
QFETCH(QString, resourcePrefix);
|
|
|
|
QFETCH(QString, fileName);
|
|
|
|
|
|
|
|
SodepMessage message("/", "loadEmbeddedJolieService");
|
2009-02-09 12:48:52 +01:00
|
|
|
SodepValue value;
|
2009-02-07 17:30:58 +01:00
|
|
|
value.children("resourcePrefix") << SodepValue(resourcePrefix);
|
|
|
|
value.children("filepath") << SodepValue(QString(DATA_DIR"/")+fileName);
|
|
|
|
message.setData(value);
|
2009-02-08 16:35:56 +01:00
|
|
|
sodepWrite(m_socket, message);
|
2009-02-07 17:30:58 +01:00
|
|
|
|
2009-02-08 16:35:56 +01:00
|
|
|
SodepMessage reply = sodepReadMessage(m_socket);
|
2009-02-07 17:30:58 +01:00
|
|
|
|
|
|
|
SodepMessage expected("/", "loadEmbeddedJolieService");
|
|
|
|
expected.setData(SodepValue(resourcePrefix));
|
|
|
|
|
|
|
|
sodepCompare(reply, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldListServices()
|
|
|
|
{
|
|
|
|
SodepMessage message("/", "getServices");
|
|
|
|
|
2009-02-08 23:11:14 +01:00
|
|
|
QSignalSpy spy(&m_client, SIGNAL(requestFinished(int, const SodepMessage&, bool)));
|
|
|
|
|
|
|
|
int id = m_client.postMessage(message);
|
|
|
|
QEventLoop eventLoop;
|
|
|
|
connect(&m_client, SIGNAL(requestFinished(int, const SodepMessage&, bool)),
|
|
|
|
&eventLoop, SLOT(quit()));
|
|
|
|
eventLoop.exec();
|
|
|
|
|
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
QVariantList signal = spy.takeFirst();
|
|
|
|
QCOMPARE(signal.at(0).toInt(), id);
|
|
|
|
sodepCompare(signal.at(1).value<SodepMessage>(), message);
|
|
|
|
QCOMPARE(signal.at(2).toBool(), false);
|
|
|
|
|
|
|
|
id = m_client.requestMessage();
|
|
|
|
eventLoop.exec();
|
2009-02-07 17:30:58 +01:00
|
|
|
|
|
|
|
SodepMessage expected("/", "getServices");
|
|
|
|
SodepValue value;
|
|
|
|
|
|
|
|
SodepValue s1;
|
2009-02-09 12:48:52 +01:00
|
|
|
s1.children("isEmbedded") << SodepValue(1);
|
2009-02-07 17:30:58 +01:00
|
|
|
s1.children("resourceName") << SodepValue("Math");
|
|
|
|
SodepValue s2;
|
2009-02-09 12:48:52 +01:00
|
|
|
s2.children("isEmbedded") << SodepValue(1);
|
2009-02-07 17:30:58 +01:00
|
|
|
s2.children("resourceName") << SodepValue("Printer");
|
|
|
|
|
|
|
|
value.children("service") << s1 << s2;
|
|
|
|
expected.setData(value);
|
|
|
|
|
2009-02-08 23:11:14 +01:00
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
signal = spy.takeFirst();
|
|
|
|
QCOMPARE(signal.at(0).toInt(), id);
|
|
|
|
sodepCompare(signal.at(1).value<SodepMessage>(), expected);
|
|
|
|
QCOMPARE(signal.at(2).toBool(), false);
|
2009-02-07 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void shouldPlaceServiceCalls_data()
|
|
|
|
{
|
2009-02-08 22:12:45 +01:00
|
|
|
QTest::addColumn<QString>("path");
|
2009-02-07 17:30:58 +01:00
|
|
|
QTest::addColumn<QString>("method");
|
|
|
|
QTest::addColumn<SodepValue>("data");
|
|
|
|
QTest::addColumn<SodepValue>("replyData");
|
|
|
|
|
2009-02-08 22:12:45 +01:00
|
|
|
QTest::newRow("printer service") << "/Printer" << "printInput" << SodepValue("Patapatapon!") << SodepValue("success");
|
|
|
|
QTest::newRow("math service") << "/Math" << "twice" << SodepValue(10.5) << SodepValue(21.0);
|
2009-02-07 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void shouldPlaceServiceCalls()
|
|
|
|
{
|
2009-02-08 22:12:45 +01:00
|
|
|
QFETCH(QString, path);
|
2009-02-07 17:30:58 +01:00
|
|
|
QFETCH(QString, method);
|
|
|
|
QFETCH(SodepValue, data);
|
|
|
|
QFETCH(SodepValue, replyData);
|
|
|
|
|
2009-02-08 22:12:45 +01:00
|
|
|
SodepMessage message(path, method);
|
2009-02-07 17:30:58 +01:00
|
|
|
message.setData(data);
|
2009-02-08 16:35:56 +01:00
|
|
|
sodepWrite(m_socket, message);
|
2009-02-07 17:30:58 +01:00
|
|
|
|
2009-02-08 16:35:56 +01:00
|
|
|
SodepMessage reply = sodepReadMessage(m_socket);
|
2009-02-07 17:30:58 +01:00
|
|
|
|
|
|
|
SodepMessage expected("/", method);
|
|
|
|
expected.setData(replyData);
|
|
|
|
|
|
|
|
sodepCompare(reply, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldUnloadService_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("serviceName");
|
|
|
|
|
|
|
|
QTest::newRow("printer service") << "PrinterService";
|
|
|
|
QTest::newRow("math service") << "MathService";
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldUnloadService()
|
|
|
|
{
|
|
|
|
QFETCH(QString, serviceName);
|
|
|
|
|
|
|
|
SodepMessage message("/", "unloadEmbeddedService");
|
|
|
|
SodepValue value(serviceName);
|
|
|
|
message.setData(value);
|
2009-02-08 16:35:56 +01:00
|
|
|
sodepWrite(m_socket, message);
|
2009-02-07 17:30:58 +01:00
|
|
|
|
2009-02-08 16:35:56 +01:00
|
|
|
SodepMessage reply = sodepReadMessage(m_socket);
|
2009-02-07 17:30:58 +01:00
|
|
|
|
|
|
|
SodepMessage expected("/", "unloadEmbeddedService");
|
|
|
|
|
|
|
|
sodepCompare(reply, expected);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_MAIN(SodepMetaServiceTest)
|
|
|
|
|
|
|
|
#include "sodepmetaservicetest.moc"
|