2009-04-30 08:11:53 +01:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2009 Alistair Riddoch
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
// $Id$
|
|
|
|
|
|
2011-02-15 09:30:46 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
#include "TestBase.h"
|
|
|
|
|
|
2009-04-30 08:11:53 +01:00
|
|
|
#include "common/Router.h"
|
|
|
|
|
|
2010-03-24 01:59:06 +00:00
|
|
|
#include "common/log.h"
|
|
|
|
|
|
2009-04-30 08:11:53 +01:00
|
|
|
#include <Atlas/Objects/Anonymous.h>
|
|
|
|
|
#include <Atlas/Objects/Operation.h>
|
|
|
|
|
#include <Atlas/Objects/SmartPtr.h>
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
class TestRouter : public Router {
|
|
|
|
|
public:
|
|
|
|
|
TestRouter(const std::string & id, long intId) : Router(id, intId) { }
|
|
|
|
|
|
2012-12-04 20:39:35 +00:00
|
|
|
virtual void externalOperation(const Operation &, Link &) { }
|
2009-04-30 08:11:53 +01:00
|
|
|
virtual void operation(const Operation &, OpVector &) { }
|
|
|
|
|
};
|
|
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
class Routertest : public Cyphesis::TestBase
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
Router * m_router;
|
|
|
|
|
public:
|
|
|
|
|
Routertest();
|
|
|
|
|
|
|
|
|
|
void setup();
|
|
|
|
|
void teardown();
|
|
|
|
|
|
|
|
|
|
void test_error();
|
|
|
|
|
void test_error_to();
|
|
|
|
|
void test_error_serialno();
|
|
|
|
|
void test_error_serialno_to();
|
|
|
|
|
void test_clientError();
|
|
|
|
|
void test_addToMessage();
|
|
|
|
|
void test_addToEntity();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Routertest::Routertest() : m_router(0)
|
|
|
|
|
{
|
|
|
|
|
ADD_TEST(Routertest::test_error);
|
|
|
|
|
ADD_TEST(Routertest::test_error_to);
|
|
|
|
|
ADD_TEST(Routertest::test_error_serialno);
|
|
|
|
|
ADD_TEST(Routertest::test_error_serialno_to);
|
|
|
|
|
ADD_TEST(Routertest::test_clientError);
|
|
|
|
|
ADD_TEST(Routertest::test_addToMessage);
|
|
|
|
|
ADD_TEST(Routertest::test_addToEntity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Routertest::setup()
|
|
|
|
|
{
|
|
|
|
|
m_router = new TestRouter("1", 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Routertest::teardown()
|
|
|
|
|
{
|
|
|
|
|
delete m_router;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Routertest::test_error()
|
|
|
|
|
{
|
|
|
|
|
OpVector res;
|
|
|
|
|
Atlas::Objects::Operation::Get op;
|
|
|
|
|
|
|
|
|
|
m_router->error(op, "test failure", res);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(res.size(), 1u);
|
|
|
|
|
|
|
|
|
|
const Atlas::Objects::Operation::RootOperation & reply = res.front();
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(reply->getClassNo(),
|
|
|
|
|
Atlas::Objects::Operation::ERROR_NO);
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultTo());
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultSerialno());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().size(), 2u);
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs()[1]->getClassNo(),
|
|
|
|
|
op->getClassNo());
|
|
|
|
|
ASSERT_TRUE(reply->getArgs().front()->getAttr("message").isString());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().front()->getAttr("message").String(),
|
|
|
|
|
"test failure");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Routertest::test_error_to()
|
2009-04-30 08:11:53 +01:00
|
|
|
{
|
2012-11-21 11:03:53 +00:00
|
|
|
OpVector res;
|
|
|
|
|
Atlas::Objects::Operation::Get op;
|
|
|
|
|
|
|
|
|
|
m_router->error(op, "test failure", res, "2");
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(res.size(), 1u);
|
|
|
|
|
|
|
|
|
|
const Atlas::Objects::Operation::RootOperation & reply = res.front();
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(reply->getClassNo(),
|
|
|
|
|
Atlas::Objects::Operation::ERROR_NO);
|
|
|
|
|
ASSERT_TRUE(!reply->isDefaultTo());
|
|
|
|
|
ASSERT_EQUAL(reply->getTo(), "2");
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultSerialno());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().size(), 2u);
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs()[1]->getClassNo(),
|
|
|
|
|
op->getClassNo());
|
|
|
|
|
ASSERT_TRUE(reply->getArgs().front()->getAttr("message").isString());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().front()->getAttr("message").String(),
|
|
|
|
|
"test failure");
|
|
|
|
|
}
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
void Routertest::test_error_serialno()
|
|
|
|
|
{
|
|
|
|
|
OpVector res;
|
|
|
|
|
Atlas::Objects::Operation::Get op;
|
|
|
|
|
op->setSerialno(23);
|
|
|
|
|
|
|
|
|
|
m_router->error(op, "test failure", res);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(res.size(), 1u);
|
|
|
|
|
|
|
|
|
|
const Atlas::Objects::Operation::RootOperation & reply = res.front();
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(reply->getClassNo(),
|
|
|
|
|
Atlas::Objects::Operation::ERROR_NO);
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultTo());
|
|
|
|
|
// FIXME We probably should set this, regardless of whether TO
|
|
|
|
|
// is set
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultRefno());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().size(), 2u);
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs()[1]->getClassNo(),
|
|
|
|
|
op->getClassNo());
|
|
|
|
|
ASSERT_TRUE(reply->getArgs().front()->getAttr("message").isString());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().front()->getAttr("message").String(),
|
|
|
|
|
"test failure");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Routertest::test_error_serialno_to()
|
|
|
|
|
{
|
|
|
|
|
OpVector res;
|
|
|
|
|
Atlas::Objects::Operation::Get op;
|
|
|
|
|
op->setSerialno(23);
|
|
|
|
|
|
|
|
|
|
m_router->error(op, "test failure", res, "2");
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(res.size(), 1u);
|
|
|
|
|
|
|
|
|
|
const Atlas::Objects::Operation::RootOperation & reply = res.front();
|
|
|
|
|
|
|
|
|
|
ASSERT_EQUAL(reply->getClassNo(),
|
|
|
|
|
Atlas::Objects::Operation::ERROR_NO);
|
|
|
|
|
ASSERT_TRUE(!reply->isDefaultTo());
|
|
|
|
|
ASSERT_EQUAL(reply->getTo(), "2");
|
|
|
|
|
ASSERT_TRUE(!reply->isDefaultRefno());
|
|
|
|
|
ASSERT_EQUAL(reply->getRefno(), op->getSerialno());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().size(), 2u);
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs()[1]->getClassNo(),
|
|
|
|
|
op->getClassNo());
|
|
|
|
|
ASSERT_TRUE(reply->getArgs().front()->getAttr("message").isString());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().front()->getAttr("message").String(),
|
|
|
|
|
"test failure");
|
|
|
|
|
}
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
void Routertest::test_clientError()
|
|
|
|
|
{
|
|
|
|
|
OpVector res;
|
|
|
|
|
Atlas::Objects::Operation::Get op;
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
m_router->clientError(op, "test failure", res);
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
ASSERT_EQUAL(res.size(), 1u);
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
const Atlas::Objects::Operation::RootOperation & reply = res.front();
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
ASSERT_EQUAL(reply->getClassNo(),
|
|
|
|
|
Atlas::Objects::Operation::ERROR_NO);
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultTo());
|
|
|
|
|
ASSERT_TRUE(reply->isDefaultSerialno());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().size(), 2u);
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs()[1]->getClassNo(),
|
|
|
|
|
op->getClassNo());
|
|
|
|
|
ASSERT_TRUE(reply->getArgs().front()->getAttr("message").isString());
|
|
|
|
|
ASSERT_EQUAL(reply->getArgs().front()->getAttr("message").String(),
|
|
|
|
|
"test failure");
|
|
|
|
|
}
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
void Routertest::test_addToMessage()
|
|
|
|
|
{
|
|
|
|
|
Atlas::Message::MapType msg;
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
m_router->addToMessage(msg);
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
ASSERT_TRUE(msg.find("objtype") != msg.end());
|
|
|
|
|
ASSERT_TRUE(msg["objtype"].isString());
|
|
|
|
|
ASSERT_EQUAL(msg["objtype"].String(), "obj");
|
|
|
|
|
ASSERT_TRUE(msg.find("id") != msg.end());
|
|
|
|
|
ASSERT_TRUE(msg["id"].isString());
|
|
|
|
|
ASSERT_EQUAL(msg["id"].String(), "1");
|
|
|
|
|
}
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
void Routertest::test_addToEntity()
|
|
|
|
|
{
|
|
|
|
|
Atlas::Objects::Entity::Anonymous ent;
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
m_router->addToEntity(ent);
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
ASSERT_TRUE(!ent->isDefaultObjtype());
|
|
|
|
|
ASSERT_EQUAL(ent->getObjtype(), "obj");
|
|
|
|
|
ASSERT_TRUE(!ent->isDefaultId());
|
|
|
|
|
ASSERT_EQUAL(ent->getId(), "1");
|
|
|
|
|
}
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
Routertest t;
|
2009-04-30 08:11:53 +01:00
|
|
|
|
2012-11-21 11:03:53 +00:00
|
|
|
return t.run();
|
2009-04-30 08:11:53 +01:00
|
|
|
}
|
2010-03-24 01:59:06 +00:00
|
|
|
|
|
|
|
|
// stubs
|
|
|
|
|
|
|
|
|
|
void log(LogLevel lvl, const std::string & msg)
|
|
|
|
|
{
|
|
|
|
|
}
|