/*
    Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301  USA.
*/

#include "siwebserviceclient.h"
#include "moc_siwebserviceclienttest.cpp"

#include <siwebserviceclient.h>
#include <siweatherstation.h>

#include <webserverprocess.h>

#include <QTest>
#include <QSignalSpy>

using namespace SiWeather::Library;

void SiWebserviceClientTest::initTestCase()
{
	_webserverProcess = new WebserverProcess(this);
	QCOMPARE(_webserverProcess->start(), true);
	QCOMPARE(_webserverProcess->isRunning(), true);
	QCOMPARE(_webserverProcess->setup(), true);
}

void SiWebserviceClientTest::cleanupTestCase()
{
	QVERIFY(_webserverProcess);
	QCOMPARE(_webserverProcess->stop(), true);
}

void SiWebserviceClientTest::apiVersion()
{
	QVERIFY(_webserverProcess);
	QCOMPARE(_webserverProcess->isRunning(), true);

	SiWebserviceClient *client = new SiWebserviceClient(_webserverProcess->webserviceBaseUrl(), this);
	QCOMPARE(client->apiVersion(), _webserverProcess->webserviceApiVersion());

	delete client;
}

void SiWebserviceClientTest::stationsRegister()
{
	QVERIFY(_webserverProcess);
	QCOMPARE(_webserverProcess->isRunning(), true);

	SiWebserviceClient *client = new SiWebserviceClient(_webserverProcess->webserviceBaseUrl(), this);
	QSignalSpy spyError(client, SIGNAL(error(SiWeather::Library::SiWebserviceClient::ReplyType, const QString&)));
	QSignalSpy spyFinished(client, SIGNAL(finished(SiWeather::Library::SiWebserviceClient::ReplyType, bool, const QByteArray&)));
	QSignalSpy spyFinishedStationsRegister(client, SIGNAL(finishedStationsRegister(bool, const QUuid&, const QString&)));
	QString errorMessage;

	SiWeatherStation station;
	QCOMPARE(client->stationsRegister(station, errorMessage), SiWebserviceClient::Error);
	QCOMPARE(errorMessage.isEmpty(), false);
	QCOMPARE(spyError.count(), 0);
	QCOMPARE(spyFinished.count(), 0);
	QCOMPARE(spyFinishedStationsRegister.count(), 0);

	spyError.clear();
	spyFinished.clear();
	spyFinishedStationsRegister.clear();
	errorMessage.clear();
	station.setName("Test Station Name");
	station.setCity("New York");
	station.setEMail("test@example.org");
	station.setLatitude(40.712778);
	station.setLongitude(-74.005833);
	station.setType("Test Type");

	QCOMPARE(station.isValidForRegister(), true);
	QCOMPARE(client->stationsRegister(station, errorMessage), SiWebserviceClient::StationsRegister);
	QTest::qWarn(errorMessage.toUtf8());
	QCOMPARE(errorMessage.isEmpty(), true);

	// use long timeout to allow webservice query for geolocation!
	QTime dieTime = QTime::currentTime().addSecs(10);
	while(QTime::currentTime() < dieTime){
		QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100);
		if(spyError.count() != 0 || spyFinished.count() != 0)
			break;
	}

	if(spyError.count() > 0){
		QVariantList data = spyError.takeFirst();
		QCOMPARE(data.count(), 2);
		QWARN(data[1].toString().toUtf8());
	}
	QCOMPARE(spyError.count(), 0);
	QCOMPARE(spyFinished.count(), 1);
	QCOMPARE(spyFinishedStationsRegister.count(), 1);
	QVariantList data = spyFinishedStationsRegister.takeFirst();
	QCOMPARE(data.count(), 3);
	if(!data[0].toBool()){
		QTest::qWarn(data[2].toString().toUtf8());
	}
	QCOMPARE(data[0].toBool(), true);
	QCOMPARE(data[1].toUuid().isNull(), false);
	QCOMPARE(data[2].toString().isEmpty(), false);

	delete client;
}

QTEST_MAIN(SiWebserviceClientTest)
