#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include "Swift/Controllers/NickResolver.h"
#include "Swiften/Roster/XMPPRoster.h"

using namespace Swift;

class NickResolverTest : public CppUnit::TestFixture
{
		CPPUNIT_TEST_SUITE(NickResolverTest);
		CPPUNIT_TEST(testNoMatch);
		CPPUNIT_TEST(testZeroLengthMatch);
		CPPUNIT_TEST(testMatch);
		CPPUNIT_TEST(testOverwrittenMatch);
		CPPUNIT_TEST(testRemovedMatch);
		CPPUNIT_TEST_SUITE_END();

		std::vector<String> groups_;

	public:
		NickResolverTest() {}

		void testNoMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");

			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}
		
		void testZeroLengthMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "", groups_);
			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}

		void testMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "Test", groups_);

			CPPUNIT_ASSERT_EQUAL(String("Test"), resolver.jidToNick(testJID));
		}

		void testOverwrittenMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "FailTest", groups_);
			xmppRoster->addContact(testJID, "Test", groups_);

			CPPUNIT_ASSERT_EQUAL(String("Test"), resolver.jidToNick(testJID));
		}

		void testRemovedMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "FailTest", groups_);
			xmppRoster->removeContact(testJID);
			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}

};

CPPUNIT_TEST_SUITE_REGISTRATION(NickResolverTest);