webapi/src/test/kotlin/model/AttachmentsSerializationTest.kt
author Da Risk <da_risk@geekorum.com>
Fri, 31 Jul 2020 11:46:49 -0400
changeset 716 96b436cf2f1d
parent 710 8ce22c82b01d
child 750 3a182d06fb1b
permissions -rw-r--r--
webapi: Add serializers to replace nullable primitive value with default value These replace nullable ints with 0 and nullable longs with 0. This should fix decoding issues on Feed and Attachment

/*
 * Geekttrss is a RSS feed reader application on the Android Platform.
 *
 * Copyright (C) 2017-2020 by Frederic-Charles Barthelery.
 *
 * This file is part of Geekttrss.
 *
 * Geekttrss is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Geekttrss 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.geekorum.ttrss.webapi.model

import com.google.common.truth.Truth.assertThat
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.builtins.list
import kotlinx.serialization.json.Json
import kotlin.test.Test


@OptIn(UnstableDefault::class)
class AttachmentsSerializationTest {

    @Test
    fun testThatAttachmentsDoLoadCorrectlyWhenUsingFixerSerializer() {
        val jsonString = """
             [
                {
                  "0" : 313085,
                  "1" : "https://traffic.libsyn.com/rands/theimportantthing0024.mp3",
                  "2" : "audio/mpeg",
                  "3" : "",
                  "4" : "0",
                  "5" : 694227,
                  "6" : 0,
                  "7" : 0,
                  "content_type" : "audio/mpeg",
                  "content_url" : "https://traffic.libsyn.com/rands/theimportantthing0024.mp3",
                  "duration" : "0",
                  "height" : 0,
                  "id" : 313085,
                  "post_id" : 694227,
                  "title" : "",
                  "width" : 0
                },
                {
                   "0" : 189026,
                   "1" : "http://0.gravatar.com/avatar/0c686ee27e835834c44ad17115185e12?s=96&d=identicon&r=PG",
                   "2" : "",
                   "3" : "",
                   "4" : "",
                   "5" : 499753,
                   "6" : 0,
                   "7" : 0,
                   "content_type" : "",
                   "content_url" : "http://0.gravatar.com/avatar/0c686ee27e835834c44ad17115185e12?s=96&d=identicon&r=PG",
                   "duration" : "",
                   "height" : 0,
                   "id" : 189026,
                   "post_id" : 499753,
                   "title" : "",
                   "width" : 0
                }
             ]
        """.trimIndent()
        val serializer = getSerializer<Attachment>()
        val result = Json.parse(serializer.list, jsonString)
        val expected = listOf(
            Attachment(id = 313085,
                indexedId = 313085,
                postId = 694227,
                indexedPostId = 694227,
                contentUrl = "https://traffic.libsyn.com/rands/theimportantthing0024.mp3",
                indexedContentUrl = "https://traffic.libsyn.com/rands/theimportantthing0024.mp3",
                contentType = "audio/mpeg", indexedContentType = "audio/mpeg",
                title = "", indexedTitle = "",
                duration = 0, indexedDuration = 0,
                width = 0, indexedWidth = 0,
                height = 0, indexedHeight = 0
            ),
            Attachment(id = 189026,
                indexedId = 189026,
                postId = 499753,
                indexedPostId = 499753,
                contentUrl = "http://0.gravatar.com/avatar/0c686ee27e835834c44ad17115185e12?s=96&d=identicon&r=PG",
                indexedContentUrl = "http://0.gravatar.com/avatar/0c686ee27e835834c44ad17115185e12?s=96&d=identicon&r=PG",
                contentType = "", indexedContentType = "",
                title = "", indexedTitle = "",
                duration = 0, indexedDuration = 0,
                width = 0, indexedWidth = 0,
                height = 0, indexedHeight = 0
            )
        )
        assertThat(result).isEqualTo(expected)
    }
}