/*
 * MIT License
 *
 * Copyright (c) 2022 Alexey Edelev <semlanik@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 "repeatedmessages.qpb.h"

#include <QObject>

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

#include <limits>

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

    void RepeatedStringMessageTest();
    void RepeatedBytesMessageTest();
    void RepeatedFloatMessageTest();
    void RepeatedDoubleMessageTest();
    void RepeatedIntMessageTest();
    void RepeatedSIntMessageTest();
    void RepeatedUIntMessageTest();
    void RepeatedInt64MessageTest();
    void RepeatedSInt64MessageTest();
    void RepeatedUInt64MessageTest();
    void RepeatedFixedIntMessageTest();
    void RepeatedSFixedIntMessageTest();
    void RepeatedFixedInt64MessageTest();
    void RepeatedSFixedInt64MessageTest();
    void RepeatedComplexMessageTest();
private:
    std::unique_ptr<QProtobufJsonSerializer> serializer;
};

using namespace qtprotobufnamespace::tests;

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedIntMessageTest()
{
    RepeatedIntMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[0,1,321,-65999,123245,-3,3]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int32List({0, 1, 321, -65999, 123245, -3, 3}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[0,1,321,-65999,123245,-3,3,null]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int32List({0, 1, 321, -65999, 123245, -3, 3,0}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":null}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedSIntMessageTest()
{
    RepeatedSIntMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,-65999,123245,-3,3,0]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint32List({1, 321, -65999, 123245, -3, 3, 0}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedUIntMessageTest()
{
    RepeatedUIntMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,0,321,65999,123245,3]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint32List({1, 0, 321, 65999, 123245, 3}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedInt64MessageTest()
{
    RepeatedInt64Message test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,-65999,12324523123123,-3,0,3]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int64List({1, 321, -65999, 12324523123123, -3, 0, 3}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedSInt64MessageTest()
{
    RepeatedSInt64Message test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,-65999,12324523123123,0,-3,3]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint64List({1, 321, -65999, 12324523123123, 0, -3, 3}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedUInt64MessageTest()
{
    RepeatedUInt64Message test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,0,65999,123245,123245324235425234,3]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint64List({1, 321, 0, 65999, 123245, 123245324235425234, 3}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedFixedIntMessageTest()
{
    RepeatedFixedIntMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,65999,12324523,3,3,0]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 321, 65999, 12324523, 3, 3, 0}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedSFixedIntMessageTest()
{
    RepeatedSFixedIntMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[0,1,321,-65999,12324523,-3,3]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed32List({0, 1, 321, -65999, 12324523, -3, 3}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedFixedInt64MessageTest()
{
    RepeatedFixedInt64Message test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,65999,123245324235425234,3,3,0]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 321, 65999, 123245324235425234, 3, 3, 0}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedSFixedInt64MessageTest()
{
    RepeatedSFixedInt64Message test;
    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,-65999,123245324235425234,-3,3,0]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 321, -65999, 123245324235425234, -3, 3, 0}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[1,321,-65999,11,123245324235425234,-3,3,0]}");
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 321, -65999, 11, 123245324235425234, -3, 3, 0}));

    test.deserialize(serializer.get(), "{\"testRepeatedInt\":[]}");
    QVERIFY(test.testRepeatedInt().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedStringMessageTest()
{
    RepeatedStringMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedString\":[\"aaaa\",\"bbbbb\",\"ccc\",\"dddddd\",\"eeeee\",\"\"]}");
    QVERIFY(test.testRepeatedString() == QStringList({"aaaa","bbbbb","ccc","dddddd","eeeee",""}));

    test.deserialize(serializer.get(), "{\"testRepeatedString\":[]}");
    QVERIFY(test.testRepeatedString().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedString\":[\"aaaa\",\"bbbbb\",\"ccc\",\"dddddd\",\"eeeee\",null]}");
    QVERIFY(test.testRepeatedString() == QStringList({"aaaa","bbbbb","ccc","dddddd","eeeee",""}));

    test.deserialize(serializer.get(), "{\"testRepeatedString\":null}");
    QVERIFY(test.testRepeatedString().isEmpty());
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedDoubleMessageTest()
{
    RepeatedDoubleMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedDouble\":[0.1,0.2,0.3,0.4,0.5]}");
    QVERIFY(test.testRepeatedDouble() == QList<double>({0.1, 0.2, 0.3, 0.4, 0.5}));

    test.deserialize(serializer.get(), "{\"testRepeatedDouble\":[]}");
    QVERIFY(test.testRepeatedDouble().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedDouble\":[0.4,1.2,0.5,1.4,0.6,null]}");
    QVERIFY(test.testRepeatedDouble() == QList<double>({0.4, 1.2, 0.5, 1.4, 0.6, 0.0}));

    test.deserialize(serializer.get(), "{\"testRepeatedDouble\":null}");
    QVERIFY(test.testRepeatedDouble().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedDouble\":[0.4,1.2,0.5,1.4,0.6,\"NaN\"]}");
    QVERIFY(test.testRepeatedDouble() == QList<double>({0.4, 1.2, 0.5, 1.4, 0.6, 0.0}));

    test.deserialize(serializer.get(), "{\"testRepeatedDouble\":[\"Infinity\",1.4,\"-Infinity\",\"NaN\"]}");
    QVERIFY(test.testRepeatedDouble() == QList<double>({0.0, 1.4, 0.0, 0.0}));
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedBytesMessageTest()
{
    RepeatedBytesMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",\"/////w==\",\"6urq6uo=\",\"AQIDBAUG\"]}");
    QVERIFY(test.testRepeatedBytes() == QList<QByteArray>({QByteArray::fromHex("010203040506"),
                                                               QByteArray::fromHex("ffffffff"),
                                                               QByteArray::fromHex("eaeaeaeaea"),
                                                               QByteArray::fromHex("010203040506")}));

    test.deserialize(serializer.get(), "{\"testRepeatedBytes\":[]}");
    QVERIFY(test.testRepeatedBytes().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",\"\",\"6urq6uo=\",\"AQIDBAUG\"]}");
    QVERIFY(test.testRepeatedBytes() == QList<QByteArray>({QByteArray::fromHex("010203040506"),
                                                               QByteArray::fromHex(""),
                                                               QByteArray::fromHex("eaeaeaeaea"),
                                                               QByteArray::fromHex("010203040506")}));

    test.deserialize(serializer.get(), "{\"testRepeatedBytes\":null}");
    QVERIFY(test.testRepeatedBytes().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",null,\"6urq6uo=\",\"AQIDBAUG\"]}");
    QVERIFY(test.testRepeatedBytes() == QList<QByteArray>({QByteArray::fromHex("010203040506"),
                                                               QByteArray::fromHex(""),
                                                               QByteArray::fromHex("eaeaeaeaea"),
                                                               QByteArray::fromHex("010203040506")}));
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedFloatMessageTest()
{
    RepeatedFloatMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedFloat\":[0.4,1.2,0.5,1.4,0.6]}");
    QVERIFY(test.testRepeatedFloat() == QList<float>({0.4f, 1.2f, 0.5f, 1.4f, 0.6f}));

    test.deserialize(serializer.get(), "{\"testRepeatedFloat\":[]}");
    QVERIFY(test.testRepeatedFloat().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedFloat\":[0.4,1.2,0.5,1.4,0.6,null]}");
    QVERIFY(test.testRepeatedFloat() == QList<float>({0.4f, 1.2f, 0.5f, 1.4f, 0.6f, 0.0f}));

    test.deserialize(serializer.get(), "{\"testRepeatedFloat\":null}");
    QVERIFY(test.testRepeatedFloat().isEmpty());

    test.deserialize(serializer.get(), "{\"testRepeatedFloat\":[0.4,1.2,0.5,1.4,0.6,\"NaN\"]}");
    QVERIFY(test.testRepeatedFloat() == QList<float>({0.4f, 1.2f, 0.5f, 1.4f, 0.6f, 0.0f}));

    test.deserialize(serializer.get(), "{\"testRepeatedFloat\":[\"Infinity\",1.4,\"-Infinity\",\"NaN\"]}");
    QVERIFY(test.testRepeatedFloat() == QList<float>({0.0f, 1.4f, 0.0f, 0.0f}));
}

void QtProtobufRepeatedTypesJsonDeserializationTest::RepeatedComplexMessageTest()
{
    RepeatedComplexMessage test;
    test.deserialize(serializer.get(), "{\"testRepeatedComplex\":[{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25},{\"testComplexField\":{\"testFieldString\":\"\"},\"testFieldInt\":25},{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25}]}");
    QCOMPARE(test.testRepeatedComplex().size(), 3);
    QCOMPARE(test.testRepeatedComplex()[0]->testFieldInt(), 25);
    QCOMPARE(test.testRepeatedComplex()[1]->testFieldInt(), 25);
    QCOMPARE(test.testRepeatedComplex()[2]->testFieldInt(), 25);
    QCOMPARE(test.testRepeatedComplex()[0]->testComplexField().testFieldString().toStdString().c_str(), "qwerty");
    QCOMPARE(test.testRepeatedComplex()[1]->testComplexField().testFieldString().toStdString().c_str(), "");
    QCOMPARE(test.testRepeatedComplex()[2]->testComplexField().testFieldString().toStdString().c_str(), "qwerty");

    test.deserialize(serializer.get(), "{\"testRepeatedComplex\":[]}");
    QVERIFY(test.testRepeatedComplex().isEmpty());
}

QTEST_MAIN(QtProtobufRepeatedTypesJsonDeserializationTest)
#include "deserializationrepeatedtypes.moc"
