/*
 * 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 "deserializationmaptypes.h"

using namespace qtprotobufnamespace::tests;

void QtProtobufMapTypesDeserializationTest::SimpleFixed32ComplexMapDeserializeTest()
{
    SimpleFixed32ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("3a180d0a00000012110810120d320b74656e207369787465656e3a230d2a000000121c080a12183216666f757274792074776f2074656e207369787465656e3a110d13000100120a080a120632045755543f"));
    QVERIFY(*test.mapField()[10].data() == ComplexMessage({16, {"ten sixteen"}}));
    QVERIFY(*test.mapField()[42].data() == ComplexMessage({10, {"fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555].data() == ComplexMessage({10, {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleSFixed32ComplexMapDeserializeTest()
{
    SimpleSFixed32ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("4a290dd6ffffff1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a4a180d0a0000001211120d320b74656e207369787465656e08104a110d13000100120a120632045755543f080a"));

    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[-42] == ComplexMessage({10 , {"minus fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleInt32ComplexMapDeserializeTest()
{
    SimpleInt32ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("1a2f08d6ffffffffffffffff011222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a1a15080a1211120d320b74656e207369787465656e08101a1008938004120a120632045755543f080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[-42] == ComplexMessage({10 , {"minus fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleSInt32ComplexMapDeserializeTest()
{
    SimpleSInt32ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a1608a580081210120c320a6d696e7573205755543f080a0a1508141211120d320b74656e207369787465656e08100a200854121c12183216666f757274792074776f2074656e207369787465656e080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[42] == ComplexMessage({10 , {"fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[-65555] == ComplexMessage({10 , {"minus WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleUInt32ComplexMapDeserializeTest()
{
    SimpleUInt32ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("2a15080a1211120d320b74656e207369787465656e08102a20082a121c12183216666f757274792074776f2074656e207369787465656e080a2a1008938004120a120632045755543f080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[42] == ComplexMessage({10 , {"fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleFixed64ComplexMapDeserializeTest()
{
    SimpleFixed64ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("421c090a000000000000001211120d320b74656e207369787465656e08104215091300010000000000120a120632045755543f080a422b09ffffffffffffffff1220121c321a6d696e757320666f757274792074776f2074656e204d41414158082a"));

    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[UINT64_MAX] == ComplexMessage({42 , {"minus fourty two ten MAAAX"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleSFixed64ComplexMapDeserializeTest()
{
    SimpleSFixed64ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("522d09d6ffffffffffffff1222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a521c090a000000000000001211120d320b74656e207369787465656e08105215091300010000000000120a120632045755543f080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[-42] == ComplexMessage({10 , {"minus fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleInt64ComplexMapDeserializeTest()
{
    SimpleInt64ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("222f08d6ffffffffffffffff011222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a2215080a1211120d320b74656e207369787465656e0810221008938004120a120632045755543f080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[-42] == ComplexMessage({10 , {"minus fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleSInt64ComplexMapDeserializeTest()
{
    SimpleSInt64ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("122608531222121e321c6d696e757320666f757274792074776f2074656e207369787465656e080a121508141211120d320b74656e207369787465656e0810121008a68008120a120632045755543f080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({16 , {"ten sixteen"}}));
    QVERIFY(*test.mapField()[-42] == ComplexMessage({10 , {"minus fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleUInt64ComplexMapDeserializeTest()
{
    SimpleUInt64ComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("3214080a1210120c320a74656e20656c6576656e080b3220082a121c12183216666f757274792074776f2074656e207369787465656e080a321008938004120a120632045755543f080a"));
    QVERIFY(*test.mapField()[10] == ComplexMessage({11 , {"ten eleven"}}));
    QVERIFY(*test.mapField()[42] == ComplexMessage({10 , {"fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()[65555] == ComplexMessage({10 , {"WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleStringComplexMapDeserializeTest()
{
    SimpleStringComplexMessageMapMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("6a140a055755543f3f120b120732053f5755543f080a6a170a0362656e1210120c320a74656e20656c6576656e080b6a350a157768657265206973206d792063617220647564653f121c12183216666f757274792074776f2074656e207369787465656e080a"));
    QVERIFY(*test.mapField()["ben"] == ComplexMessage({11 , {"ten eleven"}}));
    QVERIFY(*test.mapField()["where is my car dude?"] == ComplexMessage({10 , {"fourty two ten sixteen"}}));
    QVERIFY(*test.mapField()["WUT??"] == ComplexMessage({10 , {"?WUT?"}}));
}

void QtProtobufMapTypesDeserializationTest::SimpleUInt64ComplexMapInvalidLengthDeserializeTest()
{
    SimpleUInt64ComplexMessageMapMessage test;
    QVERIFY_THROWS_EXCEPTION(std::invalid_argument,
                             test.deserialize(serializer.get(), QByteArray::fromHex("3214080a1210120c320a74656e20656c6576656e080b3220082a121c12183216666f757274792074776f2074656e207369787465656e080a321008938004120a120")));
    QVERIFY(test.mapField().isEmpty());
}

void QtProtobufMapTypesDeserializationTest::SimpleStringComplexMapInvalidLengthDeserializeTest()
{
    SimpleStringComplexMessageMapMessage test;
    QVERIFY_THROWS_EXCEPTION(std::out_of_range,
                             test.deserialize(serializer.get(), QByteArray::fromHex("6a140a055755543f3f120b120732053f5755543f080a6a170a0362656e1210120c320a74656e20656c6576656e080b6a350a157768657265206973206d792063617220647564653f121c12183216666f757274792074776f2074656e20736978746565")));
    QVERIFY(*test.mapField()["ben"] == ComplexMessage({11 , {"ten eleven"}}));
    QVERIFY(*test.mapField()["WUT??"] == ComplexMessage({10 , {"?WUT?"}}));
    QVERIFY(!test.mapField().contains("where is my car dude?"));
}

void QtProtobufMapTypesDeserializationTest::SimpleUInt64ComplexMapCorruptedDeserializeTest()
{
    SimpleUInt64ComplexMessageMapMessage test;
    QVERIFY_THROWS_EXCEPTION(std::invalid_argument,
                             test.deserialize(serializer.get(), QByteArray::fromHex("3214080a1210120c320a74656e20656c6576656e080b3221233522345b2183216666f757274792074776f2074656e207369787465656e080a321008938004120a120632045755543f080a")));
    QVERIFY(test.mapField().isEmpty());
}
