Add tests for Nextcloud News adapters

This commit is contained in:
Shinokuni 2021-07-19 18:52:54 +02:00
parent 7a51364f48
commit be90fa8d99
9 changed files with 172 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package com.readrops.api.services.nextcloudnews.adapters
import android.annotation.SuppressLint
import com.readrops.api.utils.exceptions.ParseException
import com.readrops.api.utils.extensions.nextNonEmptyString
import com.readrops.db.entities.Folder
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonReader
@ -30,7 +31,7 @@ class NextNewsFoldersAdapter {
with(folder) {
when (reader.selectName(NAMES)) {
0 -> remoteId = reader.nextInt().toString()
1 -> name = reader.nextString()
1 -> name = reader.nextNonEmptyString()
else -> reader.skipValue()
}
}

View File

@ -40,10 +40,10 @@ class NextNewsItemsAdapter : JsonAdapter<List<Item>>() {
when (reader.selectName(NAMES)) {
0 -> remoteId = reader.nextInt().toString()
1 -> link = reader.nextNullableString()
2 -> title = reader.nextString()
3 -> author = reader.nextString()
2 -> title = reader.nextNonEmptyString()
3 -> author = reader.nextNullableString()
4 -> pubDate = LocalDateTime(reader.nextLong() * 1000L, DateTimeZone.getDefault())
5 -> content = reader.nextString()
5 -> content = reader.nextNullableString()
6 -> enclosureMime = reader.nextNullableString()
7 -> enclosureLink = reader.nextNullableString()
8 -> feedRemoteId = reader.nextInt().toString()

View File

@ -18,7 +18,7 @@ class NextNewsFeedsAdapterTest {
@Test
fun validFeedsTest() {
val stream = TestUtils.loadResource("services/nextcloudnews/feeds.json")
val stream = TestUtils.loadResource("services/nextcloudnews/adapters/feeds.json")
val feeds = adapter.fromJson(Buffer().readFrom(stream))!!
val feed1 = feeds[0]
@ -31,6 +31,8 @@ class NextNewsFeedsAdapterTest {
assertEquals(feed1.iconUrl, "https://krebsonsecurity.com/favicon.ico")
val feed2 = feeds[1]
assertNull(feed2.iconUrl)
assertNull(feed2.siteUrl)
assertNull(feed2.remoteFolderId)
val feed3 = feeds[2]

View File

@ -0,0 +1,36 @@
package com.readrops.api.services.nextcloudnews.adapters
import com.readrops.api.TestUtils
import com.readrops.db.entities.Folder
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import junit.framework.TestCase.assertEquals
import okio.Buffer
import org.junit.Assert.assertThrows
import org.junit.Test
class NextNewsFoldersAdapterTest {
private val adapter = Moshi.Builder()
.add(NextNewsFoldersAdapter())
.build()
.adapter<List<Folder>>(Types.newParameterizedType(List::class.java, Folder::class.java))
@Test
fun validFoldersTest() {
val stream = TestUtils.loadResource("services/nextcloudnews/adapters/valid_folder.json")
val folder = adapter.fromJson(Buffer().readFrom(stream))!![0]
assertEquals(folder.remoteId, "4")
assertEquals(folder.name, "Media")
}
@Test
fun nonValidFoldersTest() {
val stream = TestUtils.loadResource("services/nextcloudnews/adapters/non_valid_folder.json")
assertThrows(JsonDataException::class.java) { adapter.fromJson(Buffer().readFrom(stream)) }
}
}

View File

@ -0,0 +1,46 @@
package com.readrops.api.services.nextcloudnews.adapters
import com.readrops.api.TestUtils
import com.readrops.db.entities.Item
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import junit.framework.TestCase.assertEquals
import okio.Buffer
import org.joda.time.LocalDateTime
import org.junit.Test
class NextNewsItemsAdapterTest {
private val adapter = Moshi.Builder()
.add(Types.newParameterizedType(List::class.java, Item::class.java), NextNewsItemsAdapter())
.build()
.adapter<List<Item>>(Types.newParameterizedType(List::class.java, Item::class.java))
@Test
fun validItemsTest() {
val stream = TestUtils.loadResource("services/nextcloudnews/adapters/items.json")
val items = adapter.fromJson(Buffer().readFrom(stream))!!
val item = items[0]
with(item) {
assertEquals(remoteId, "3443")
assertEquals(guid, "3059047a572cd9cd5d0bf645faffd077")
assertEquals(link, "http://grulja.wordpress.com/2013/04/29/plasma-nm-after-the-solid-sprint/")
assertEquals(title, "Plasma-nm after the solid sprint")
assertEquals(author, "Jan Grulich (grulja)")
assertEquals(content, "<p>At first I have to say...</p>")
assertEquals(feedRemoteId, "67")
assertEquals(isRead, false)
assertEquals(isStarred, false)
assertEquals(pubDate, LocalDateTime(1367270544000))
assertEquals(imageLink, null)
}
with(items[1]) {
assertEquals(imageLink, "https://test.org/image.jpg")
}
}
}

View File

@ -19,12 +19,12 @@
"id": 3,
"url": "https://krebsonsecurity.com/feed/",
"title": "Krebs on Security",
"faviconLink": "https://krebsonsecurity.com/favicon.ico",
"faviconLink": null,
"added": 1490999780,
"folderId": 0,
"unreadCount": 1,
"ordering": 0,
"link": "https://krebsonsecurity.com/",
"link": "",
"pinned": false,
"updateErrorCount": 0,
"lastUpdateError": null,

View File

@ -0,0 +1,64 @@
{
"items": [
{
"id": 3443,
"guid": "http://grulja.wordpress.com/?p=76",
"guidHash": "3059047a572cd9cd5d0bf645faffd077",
"url": "http://grulja.wordpress.com/2013/04/29/plasma-nm-after-the-solid-sprint/",
"title": "Plasma-nm after the solid sprint",
"author": "Jan Grulich (grulja)",
"pubDate": 1367270544,
"body": "<p>At first I have to say...</p>",
"enclosureMime": null,
"enclosureLink": null,
"mediaThumbnail": null,
"mediaDescription": null,
"feedId": 67,
"unread": true,
"starred": false,
"rtl": false,
"lastModified": 1367273003,
"fingerprint": "aeaae2123"
},
{
"id": 3443,
"guid": "http://grulja.wordpress.com/?p=76",
"guidHash": "3059047a572cd9cd5d0bf645faffd077",
"url": "http://grulja.wordpress.com/2013/04/29/plasma-nm-after-the-solid-sprint/",
"title": "Plasma-nm after the solid sprint",
"author": "Jan Grulich (grulja)",
"pubDate": 1367270544,
"body": "<p>At first I have to say...</p>",
"enclosureMime": "image",
"enclosureLink": "https://test.org/image.jpg",
"mediaThumbnail": null,
"mediaDescription": null,
"feedId": 67,
"unread": true,
"starred": false,
"rtl": false,
"lastModified": 1367273003,
"fingerprint": "aeaae2123"
},
{
"id": 3443,
"guid": "http://grulja.wordpress.com/?p=76",
"guidHash": "3059047a572cd9cd5d0bf645faffd077",
"url": "http://grulja.wordpress.com/2013/04/29/plasma-nm-after-the-solid-sprint/",
"title": "Plasma-nm after the solid sprint",
"author": "Jan Grulich (grulja)",
"pubDate": 1367270544,
"body": "<p>At first I have to say...</p>",
"enclosureMime": "video",
"enclosureLink": "https://test.org/image.jpg",
"mediaThumbnail": null,
"mediaDescription": null,
"feedId": 67,
"unread": true,
"starred": false,
"rtl": false,
"lastModified": 1367273003,
"fingerprint": "aeaae2123"
}
]
}

View File

@ -0,0 +1,8 @@
{
"folders": [
{
"id": 4,
"name": ""
}
]
}

View File

@ -0,0 +1,8 @@
{
"folders": [
{
"id":4,
"name": "Media"
}
]
}