adhoc tests
This commit is contained in:
parent
672f91a630
commit
974bf2de0a
3 changed files with 124 additions and 5 deletions
|
@ -113,8 +113,15 @@ bool AdHocManager::handleGetRequest(const Swift::JID& from, const Swift::JID& to
|
|||
bool AdHocManager::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload) {
|
||||
AdHocCommand *command = NULL;
|
||||
// Try to find AdHocCommand according to 'from' and session_id
|
||||
if (m_sessions.find(from) != m_sessions.end() && m_sessions[from].find(payload->getSessionID()) != m_sessions[from].end()) {
|
||||
command = m_sessions[from][payload->getSessionID()];
|
||||
if (m_sessions.find(from) != m_sessions.end()) {
|
||||
if (m_sessions[from].find(payload->getSessionID()) != m_sessions[from].end()) {
|
||||
command = m_sessions[from][payload->getSessionID()];
|
||||
}
|
||||
else {
|
||||
LOG4CXX_ERROR(logger, from.toString() << ": Unknown session id " << payload->getSessionID() << " - ignoring");
|
||||
sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Check if we can create command with this node
|
||||
else if (m_factories.find(payload->getNode()) != m_factories.end()) {
|
||||
|
@ -148,7 +155,7 @@ bool AdHocManager::handleSetRequest(const Swift::JID& from, const Swift::JID& to
|
|||
command->refreshLastActivity();
|
||||
|
||||
// Command completed, so we can remove it now
|
||||
if (response->getStatus() == Swift::Command::Completed) {
|
||||
if (response->getStatus() == Swift::Command::Completed || response->getStatus() == Swift::Command::Canceled) {
|
||||
m_sessions[from].erase(command->getId());
|
||||
if (m_sessions[from].empty()) {
|
||||
m_sessions.erase(from);
|
||||
|
|
|
@ -53,13 +53,16 @@ boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleResponse(boost::sh
|
|||
|
||||
|
||||
|
||||
boost::shared_ptr<Swift::Command> response;
|
||||
response->setStatus(Swift::Command::Completed);
|
||||
boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Completed));
|
||||
return response;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleRequest(boost::shared_ptr<Swift::Command> payload) {
|
||||
boost::shared_ptr<Swift::Command> response;
|
||||
if (payload->getAction() == Swift::Command::Cancel) {
|
||||
response = boost::shared_ptr<Swift::Command>(new Swift::Command("settings", m_id, Swift::Command::Canceled));
|
||||
return response;
|
||||
}
|
||||
|
||||
switch (m_state) {
|
||||
case Init:
|
||||
|
|
|
@ -27,6 +27,8 @@ class SettingsAdHocCommandTest : public CPPUNIT_NS :: TestFixture, public BasicT
|
|||
CPPUNIT_TEST_SUITE(SettingsAdHocCommandTest);
|
||||
CPPUNIT_TEST(getItems);
|
||||
CPPUNIT_TEST(execute);
|
||||
CPPUNIT_TEST(executeBadSessionID);
|
||||
CPPUNIT_TEST(cancel);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
|
@ -73,6 +75,113 @@ class SettingsAdHocCommandTest : public CPPUNIT_NS :: TestFixture, public BasicT
|
|||
iq->setFrom("user@localhost");
|
||||
injectIQ(iq);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("settings"), getStanza(received[0])->getPayload<Swift::Command>()->getNode());
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::Command::Executing, getStanza(received[0])->getPayload<Swift::Command>()->getStatus());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>()->getForm());
|
||||
|
||||
std::string sessionId = getStanza(received[0])->getPayload<Swift::Command>()->getSessionID();
|
||||
|
||||
// finish the command
|
||||
payload = boost::shared_ptr<Swift::Command>(new Swift::Command("settings"));
|
||||
payload->setSessionID(sessionId);
|
||||
payload->setForm(getStanza(received[0])->getPayload<Swift::Command>()->getForm());
|
||||
iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", payload);
|
||||
iq->setFrom("user@localhost");
|
||||
received.clear();
|
||||
injectIQ(iq);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("settings"), getStanza(received[0])->getPayload<Swift::Command>()->getNode());
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::Command::Completed, getStanza(received[0])->getPayload<Swift::Command>()->getStatus());
|
||||
}
|
||||
|
||||
void executeBadSessionID() {
|
||||
boost::shared_ptr<Swift::Command> payload(new Swift::Command("settings"));
|
||||
boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", payload);
|
||||
iq->setFrom("user@localhost");
|
||||
injectIQ(iq);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("settings"), getStanza(received[0])->getPayload<Swift::Command>()->getNode());
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::Command::Executing, getStanza(received[0])->getPayload<Swift::Command>()->getStatus());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>()->getForm());
|
||||
|
||||
std::string sessionId = "somethingwrong";
|
||||
|
||||
// finish the command
|
||||
payload = boost::shared_ptr<Swift::Command>(new Swift::Command("settings"));
|
||||
payload->setSessionID(sessionId);
|
||||
payload->setForm(getStanza(received[0])->getPayload<Swift::Command>()->getForm());
|
||||
iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", payload);
|
||||
iq->setFrom("user@localhost");
|
||||
received.clear();
|
||||
injectIQ(iq);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Error, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
boost::shared_ptr<Swift::Command> payload(new Swift::Command("settings"));
|
||||
boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", payload);
|
||||
iq->setFrom("user@localhost");
|
||||
injectIQ(iq);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("settings"), getStanza(received[0])->getPayload<Swift::Command>()->getNode());
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::Command::Executing, getStanza(received[0])->getPayload<Swift::Command>()->getStatus());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>()->getForm());
|
||||
|
||||
std::string sessionId = getStanza(received[0])->getPayload<Swift::Command>()->getSessionID();
|
||||
|
||||
// cancel the command
|
||||
payload = boost::shared_ptr<Swift::Command>(new Swift::Command("settings"));
|
||||
payload->setSessionID(sessionId);
|
||||
payload->setAction(Swift::Command::Cancel);
|
||||
iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", payload);
|
||||
iq->setFrom("user@localhost");
|
||||
received.clear();
|
||||
injectIQ(iq);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
|
||||
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::Command>());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("settings"), getStanza(received[0])->getPayload<Swift::Command>()->getNode());
|
||||
CPPUNIT_ASSERT_EQUAL(Swift::Command::Canceled, getStanza(received[0])->getPayload<Swift::Command>()->getStatus());
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue