Accept null value for Nextcloud News feed attribute folderId

This commit is contained in:
Shinokuni 2020-11-11 22:22:07 +01:00
parent a8e3a689d5
commit 1cba1b6e17
4 changed files with 103 additions and 9 deletions

View File

@ -2,8 +2,10 @@ package com.readrops.api.services.nextcloudnews.adapters
import android.annotation.SuppressLint
import com.readrops.api.utils.exceptions.ParseException
import com.readrops.db.entities.Feed
import com.readrops.api.utils.extensions.nextNonEmptyString
import com.readrops.api.utils.extensions.nextNullableInt
import com.readrops.api.utils.extensions.nextNullableString
import com.readrops.db.entities.Feed
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonReader
import com.squareup.moshi.ToJson
@ -43,13 +45,13 @@ class NextNewsFeedsAdapter {
while (reader.hasNext()) {
with(feed) {
when (reader.selectName(NAMES)) {
0 -> remoteId = reader.nextString()
1 -> url = reader.nextString()
2 -> name = reader.nextString()
3 -> iconUrl = reader.nextString()
0 -> remoteId = reader.nextNonEmptyString()
1 -> url = reader.nextNonEmptyString()
2 -> name = reader.nextNonEmptyString()
3 -> iconUrl = reader.nextNullableString()
4 -> {
val nextInt = reader.nextInt()
remoteFolderId = if (nextInt > 0) nextInt.toString() else null
val nextInt = reader.nextNullableInt()
remoteFolderId = if (nextInt != null && nextInt > 0) nextInt.toString() else null
}
5 -> siteUrl = reader.nextNullableString()
else -> reader.skipValue()
@ -65,6 +67,7 @@ class NextNewsFeedsAdapter {
}
companion object {
val NAMES: JsonReader.Options = JsonReader.Options.of("id", "url", "title", "faviconLink", "folderId", "link")
val NAMES: JsonReader.Options = JsonReader.Options.of("id", "url", "title",
"faviconLink", "folderId", "link")
}
}

View File

@ -9,4 +9,7 @@ fun JsonReader.nextNullableString(): String? =
fun JsonReader.nextNonEmptyString(): String {
val text = nextString()
return if (text.isNotEmpty()) text.trim() else throw ParseException("Json value can't be null")
}
}
fun JsonReader.nextNullableInt(): Int? =
if (peek() != JsonReader.Token.NULL) nextInt() else nextNull()

View File

@ -0,0 +1,39 @@
package com.readrops.api.services.nextcloudnews.adapters
import com.readrops.api.TestUtils
import com.readrops.db.entities.Feed
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import okio.Buffer
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
class NextNewsFeedsAdapterTest {
private val adapter = Moshi.Builder()
.add(NextNewsFeedsAdapter())
.build()
.adapter<List<Feed>>(Types.newParameterizedType(List::class.java, Feed::class.java))
@Test
fun validFeedsTest() {
val stream = TestUtils.loadResource("services/nextcloudnews/feeds.json")
val feeds = adapter.fromJson(Buffer().readFrom(stream))!!
val feed1 = feeds[0]
assertEquals(feed1.name, "Krebs on Security")
assertEquals(feed1.url, "https://krebsonsecurity.com/feed/")
assertEquals(feed1.siteUrl, "https://krebsonsecurity.com/")
assertEquals(feed1.remoteId, "3")
assertNull(feed1.remoteFolderId)
assertEquals(feed1.iconUrl, "https://krebsonsecurity.com/favicon.ico")
val feed2 = feeds[1]
assertNull(feed2.remoteFolderId)
val feed3 = feeds[2]
assertEquals(feed3.remoteFolderId, "5")
}
}

View File

@ -0,0 +1,49 @@
{
"feeds": [
{
"id": 3,
"url": "https://krebsonsecurity.com/feed/",
"title": "Krebs on Security",
"faviconLink": "https://krebsonsecurity.com/favicon.ico",
"added": 1490999780,
"folderId": null,
"unreadCount": 1,
"ordering": 0,
"link": "https://krebsonsecurity.com/",
"pinned": false,
"updateErrorCount": 0,
"lastUpdateError": null,
"items": []
},
{
"id": 3,
"url": "https://krebsonsecurity.com/feed/",
"title": "Krebs on Security",
"faviconLink": "https://krebsonsecurity.com/favicon.ico",
"added": 1490999780,
"folderId": 0,
"unreadCount": 1,
"ordering": 0,
"link": "https://krebsonsecurity.com/",
"pinned": false,
"updateErrorCount": 0,
"lastUpdateError": null,
"items": []
},
{
"id": 3,
"url": "https://krebsonsecurity.com/feed/",
"title": "Krebs on Security",
"faviconLink": "https://krebsonsecurity.com/favicon.ico",
"added": 1490999780,
"folderId": 5,
"unreadCount": 1,
"ordering": 0,
"link": "https://krebsonsecurity.com/",
"pinned": false,
"updateErrorCount": 0,
"lastUpdateError": null,
"items": []
}
]
}