/*
 * 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 <QtTest/QtTest>
#include <QProtobufJsonSerializer>

class QtProtobufRepeatedTypesJsonSerializationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufRepeatedTypesJsonSerializationTest() {
        qRegisterProtobufTypes();
    }
private slots:
    void init() {
        m_serializer.reset(new QProtobufJsonSerializer);
    }
    void RepeatedIntMessageTest();
    void RepeatedSIntMessageTest();
    void RepeatedUIntMessageTest();
    void RepeatedInt64MessageTest();
    void RepeatedSInt64MessageTest();
    void RepeatedUInt64MessageTest();
    void RepeatedFixedIntMessageTest();
    void RepeatedSFixedIntMessageTest();
    void RepeatedFixedInt64MessageTest();
    void RepeatedSFixedInt64MessageTest();
    void RepeatedStringMessageTest();
    void RepeatedFloatMessageTest();
    void RepeatedDoubleMessageTest();
    void RepeatedBytesMessageTest();
    void RepeatedComplexMessageTest();
private:
    std::unique_ptr<QProtobufJsonSerializer> m_serializer;
};

using namespace qtprotobufnamespace::tests;

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedIntMessageTest()
{
    RepeatedIntMessage test;
    test.setTestRepeatedInt({0, 1, 321, -65999, 123245, -3, 3});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[0,1,321,-65999,123245,-3,3]}");

    test.setTestRepeatedInt(QtProtobuf::int32List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedSIntMessageTest()
{
    RepeatedSIntMessage test;
    test.setTestRepeatedInt({1, 321, -65999, 123245, -3, 3, 0});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,123245,-3,3,0]}");

    test.setTestRepeatedInt(QtProtobuf::sint32List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedUIntMessageTest()
{
    RepeatedUIntMessage test;
    test.setTestRepeatedInt({1, 0, 321, 65999, 123245, 3});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,0,321,65999,123245,3]}");

    test.setTestRepeatedInt(QtProtobuf::uint32List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedInt64MessageTest()
{
    RepeatedInt64Message test;
    test.setTestRepeatedInt({1, 321, -65999, 12324523123123, -3, 0, 3});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,12324523123123,-3,0,3]}");

    test.setTestRepeatedInt(QtProtobuf::int64List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedSInt64MessageTest()
{
    RepeatedSInt64Message test;
    test.setTestRepeatedInt({1, 321, -65999, 12324523123123, 0, -3, 3});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,12324523123123,0,-3,3]}");

    test.setTestRepeatedInt(QtProtobuf::sint64List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedUInt64MessageTest()
{
    RepeatedUInt64Message test;
    test.setTestRepeatedInt({1, 321, 0, 65999, 123245, 123245324235425234, 3});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,0,65999,123245,123245324235425234,3]}");

    test.setTestRepeatedInt(QtProtobuf::uint64List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedFixedIntMessageTest()
{
    RepeatedFixedIntMessage test;
    test.setTestRepeatedInt({1, 321, 65999, 12324523, 3, 3, 0});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,65999,12324523,3,3,0]}");

    test.setTestRepeatedInt(QtProtobuf::fixed32List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedSFixedIntMessageTest()
{
    RepeatedSFixedIntMessage test;
    test.setTestRepeatedInt({0, 1, 321, -65999, 12324523, -3, 3});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[0,1,321,-65999,12324523,-3,3]}");

    test.setTestRepeatedInt(QtProtobuf::sfixed32List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedFixedInt64MessageTest()
{
    RepeatedFixedInt64Message test;
    test.setTestRepeatedInt({1, 321, 65999, 123245324235425234, 3, 3, 0});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,65999,123245324235425234,3,3,0]}");

    test.setTestRepeatedInt(QtProtobuf::fixed64List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedSFixedInt64MessageTest()
{
    RepeatedSFixedInt64Message test;
    test.setTestRepeatedInt({1, 321, -65999, 123245324235425234, -3, 3, 0});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,123245324235425234,-3,3,0]}");

    test.setTestRepeatedInt(QtProtobuf::sfixed64List());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedStringMessageTest()
{
    RepeatedStringMessage test;
    test.setTestRepeatedString({"aaaa","bbbbb","ccc","dddddd","eeeee",""});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedString\":[\"aaaa\",\"bbbbb\",\"ccc\",\"dddddd\",\"eeeee\",\"\"]}");

    test.setTestRepeatedString(QStringList());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedString\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedDoubleMessageTest()
{
    RepeatedDoubleMessage test;
    test.setTestRepeatedDouble({0.1, 0.2, 0.3, 0.4, 0.5});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedDouble\":[0.1,0.2,0.3,0.4,0.5]}");

    test.setTestRepeatedDouble(QtProtobuf::DoubleList());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedDouble\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedBytesMessageTest()
{
    RepeatedBytesMessage test;
    test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
                               QByteArray::fromHex("ffffffff"),
                               QByteArray::fromHex("eaeaeaeaea"),
                               QByteArray::fromHex("010203040506")});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",\"/////w==\",\"6urq6uo=\",\"AQIDBAUG\"]}");

    test.setTestRepeatedBytes(QByteArrayList());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedBytes\":[]}");

    test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
                               QByteArray::fromHex(""),
                               QByteArray::fromHex("eaeaeaeaea"),
                               QByteArray::fromHex("010203040506")});
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",\"\",\"6urq6uo=\",\"AQIDBAUG\"]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedFloatMessageTest()
{
    RepeatedFloatMessage test;
    test.setTestRepeatedFloat({0.4f, 1.2f, 0.5f, 1.4f, 0.6f});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedFloat\":[0.4,1.2,0.5,1.4,0.6]}");

    test.setTestRepeatedFloat(QtProtobuf::FloatList());
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedFloat\":[]}");
}

void QtProtobufRepeatedTypesJsonSerializationTest::RepeatedComplexMessageTest()
{
    SimpleStringMessage stringMsg;
    stringMsg.setTestFieldString("qwerty");
    QSharedPointer<ComplexMessage> msg(new ComplexMessage);
    msg->setTestFieldInt(25);
    msg->setTestComplexField(stringMsg);
    RepeatedComplexMessage test;
    test.setTestRepeatedComplex({msg, msg, msg});
    QByteArray result = test.serialize(m_serializer.get());
    QVERIFY(QString::fromUtf8(result).toStdString() == "{\"testRepeatedComplex\":[{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25},{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25},{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25}]}"
                 || QString::fromUtf8(result).toStdString() == "{\"testRepeatedComplex\":[{\"testFieldInt\":25,\"testComplexField\":{\"testFieldString\":\"qwerty\"}},{\"testFieldInt\":25,\"testComplexField\":{\"testFieldString\":\"qwerty\"}},{\"testFieldInt\":25,\"testComplexField\":{\"testFieldString\":\"qwerty\"}}]}");

    test.setTestRepeatedComplex({});
    result = test.serialize(m_serializer.get());
    QCOMPARE(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedComplex\":[]}");
}

QTEST_MAIN(QtProtobufRepeatedTypesJsonSerializationTest)
#include "serializationrepeatedtypes.moc"
