/*
 * MIT License
 *
 * Copyright (c) 2022 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com>
 *
 * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "basicmessages.qpb.h"
#include "mapmessages.qpb.h"

#include <QObject>

#include <QtTest/QtTest>
#include <QProtobufJsonSerializer>

#include <limits>

class QtProtobufMapTypesJsonDeserializationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufMapTypesJsonDeserializationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void init()
    {
        serializer.reset(new QProtobufJsonSerializer);
    }

    void SimpleFixed32StringMapDeserializeTest();
    void SimpleSFixed32StringMapDeserializeTest();
    void SimpleInt32StringMapDeserializeTest();
    void SimpleSInt32StringMapDeserializeTest();
    void SimpleUInt32StringMapDeserializeTest();
    void SimpleFixed64StringMapDeserializeTest();
    void SimpleSFixed64StringMapDeserializeTest();
    void SimpleInt64StringMapDeserializeTest();
    void SimpleSInt64StringMapDeserializeTest();
    void SimpleUInt64StringMapDeserializeTest();
    void SimpleStringStringMapDeserializeTest();
private:
    std::unique_ptr<QProtobufJsonSerializer> serializer;
};

using namespace qtprotobufnamespace::tests;

void QtProtobufMapTypesJsonDeserializationTest::SimpleFixed32StringMapDeserializeTest()
{
    SimpleFixed32StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\",\"0\":\"\"}}");

    QCOMPARE(test.mapField().size(), 4);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[42].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");
    QCOMPARE(test.mapField()[0].toStdString().c_str(), "");
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleSFixed32StringMapDeserializeTest()
{
    SimpleSFixed32StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[-42].toStdString().c_str(), "minus fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleInt32StringMapDeserializeTest()
{
    SimpleInt32StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"-10\":\"minus ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[-10].toStdString().c_str(), "minus ten");
    QCOMPARE(test.mapField()[42].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleSInt32StringMapDeserializeTest()
{
    SimpleSInt32StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[-42].toStdString().c_str(), "minus fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleUInt32StringMapDeserializeTest()
{
    SimpleUInt32StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[42].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleFixed64StringMapDeserializeTest()
{
    SimpleFixed64StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[42].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleSFixed64StringMapDeserializeTest()
{
    SimpleSFixed64StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[-42].toStdString().c_str(), "minus fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleInt64StringMapDeserializeTest()
{
    SimpleInt64StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"-10\":\"minus ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[-10].toStdString().c_str(), "minus ten");
    QCOMPARE(test.mapField()[42].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleSInt64StringMapDeserializeTest()
{
    SimpleSInt64StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[-42].toStdString().c_str(), "minus fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleUInt64StringMapDeserializeTest()
{
    SimpleUInt64StringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"42\":\"fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()[10].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()[42].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()[15].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

void QtProtobufMapTypesJsonDeserializationTest::SimpleStringStringMapDeserializeTest()
{
    SimpleStringStringMapMessage test;
    test.deserialize(serializer.get(), "{\"mapField\":{\"ben\":\"ten\",\"sweet\":\"fifteen\",\"what is the answer?\":\"fourty two\"}}");

    QCOMPARE(test.mapField().size(), 3);
    QCOMPARE(test.mapField()["ben"].toStdString().c_str(), "ten");
    QCOMPARE(test.mapField()["what is the answer?"].toStdString().c_str(), "fourty two");
    QCOMPARE(test.mapField()["sweet"].toStdString().c_str(), "fifteen");

    test.deserialize(serializer.get(), "{\"mapField\":{}}");
    QCOMPARE(test.mapField().size(), 0);
}

QTEST_MAIN(QtProtobufMapTypesJsonDeserializationTest)
#include "deserializationmaptypes.moc"
