/*
 * Copyright (C) 2009 - 2010 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * 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 Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#define BOOST_TEST_MAIN

#include <boost/test/unit_test.hpp>
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "TestCommon.h"

#include "DeviceAdapter/DummyDeviceAdapter.h"
#include "DeviceAdapter/DeviceUpdater.h"

#include "../../treemanager/UTestMOTree/MockDataStorage.h"
#include "treemanager/MOTreeManager.h"
#include "MockServerExchangeManager.h"
#include "MockNotificationCenter.h"

NS_DM_Client::NS_DataStorage::IDataStorage* S_dataStorage = 0;
NS_DM_Client::IDeviceAdapter* S_dummyDeviceAdapter = 0;
NS_DM_Client::IMOTreeManager* S_treeManager;
NS_DM_Client::ProfileComponentsHolder* S_ProfileHolder = 0;
NS_DM_Client::IServerExchangeManager* S_serverExchangeManager = 0;
NS_DM_Client::INotificationCenter* S_NotificationCenter = 0;

namespace fs = boost::filesystem;
using namespace NS_DM_Client;

class DataStorageConfig
{
public:
	DataStorageConfig()
	{
		S_dataStorage = new NS_DM_Client::NS_DataStorage::MockDataStorage();
		S_treeManager = new MOTreeManager();
		S_dummyDeviceAdapter =  (NS_DM_Client::DummyDeviceAdapter*)CreateDeviceAdapter(0);
		S_serverExchangeManager = new NS_DM_Client::MockServerExchangeManager();
		S_ProfileHolder = new NS_DM_Client::ProfileComponentsHolder();
		S_NotificationCenter = new NS_DM_Client::MockNotificationCenter();

		S_ProfileHolder->SetDataStorage(S_dataStorage);
		S_ProfileHolder->SetDeviceAdapter(S_dummyDeviceAdapter);
		S_ProfileHolder->SetMOTreeManager(S_treeManager);
		S_ProfileHolder->SetServerExchangeManager(S_serverExchangeManager);
		S_ProfileHolder->SetNotificationCenter(S_NotificationCenter);

		NS_DM_Client::StringMap settings;
		S_treeManager->Init(settings, "DeviceAdapterUnitTests", *S_ProfileHolder);

		// remove data after synchronization
		fs::remove_all(NS_DM_Client::NS_DataStorage::S_workingDirectory);

		std::cout << "Setup" << std::endl;
	}

	~DataStorageConfig()
	{
		delete S_ProfileHolder;
		std::cout << "Tear down" << std::endl;
	}
};

// NS_DM_Client::IDeviceAdapter* CreateDeviceAdapter(void* param)
// {
// 	return new NS_DM_Client::DummyDeviceAdapter();
// }

BOOST_GLOBAL_FIXTURE(DataStorageConfig);

BOOST_AUTO_TEST_CASE(GetDeviceID)
{
	std::string deviceID = "Some DeviceID";
	BOOST_CHECK(S_dummyDeviceAdapter->SetDeviceID(deviceID.c_str(), deviceID.size()));
	char* buffer = new char[100];
	size_t size = 100;
	BOOST_CHECK(S_dummyDeviceAdapter->GetDeviceID(buffer, size));
	BOOST_CHECK(deviceID == std::string(buffer));
	delete buffer;
}
