mirror of https://github.com/readrops/Readrops.git
Call ATOMItemAdapter in ATOMFeedAdapter
This commit is contained in:
parent
58122cb456
commit
7a417e83de
|
@ -2,7 +2,7 @@ package com.readrops.api.localfeed
|
|||
|
||||
import com.gitlab.mvysny.konsumexml.Konsumer
|
||||
import com.readrops.api.localfeed.atom.ATOMFeedAdapter
|
||||
import com.readrops.api.localfeed.atom.ATOMItemsAdapter
|
||||
import com.readrops.api.localfeed.atom.ATOMItemAdapter
|
||||
import com.readrops.api.localfeed.rss1.RSS1FeedAdapter
|
||||
import com.readrops.api.localfeed.rss1.RSS1ItemsAdapter
|
||||
import com.readrops.api.localfeed.rss2.RSS2FeedAdapter
|
||||
|
@ -18,7 +18,7 @@ interface XmlAdapter<T> {
|
|||
fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<Feed> = when (type) {
|
||||
LocalRSSHelper.RSSType.RSS_1 -> RSS1FeedAdapter()
|
||||
LocalRSSHelper.RSSType.RSS_2 -> RSS2FeedAdapter()
|
||||
LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter()
|
||||
//LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter()
|
||||
else -> throw IllegalArgumentException("Unknown RSS type : $type")
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ interface XmlAdapter<T> {
|
|||
when (type) {
|
||||
LocalRSSHelper.RSSType.RSS_1 -> RSS1ItemsAdapter()
|
||||
LocalRSSHelper.RSSType.RSS_2 -> RSS2ItemsAdapter()
|
||||
LocalRSSHelper.RSSType.ATOM -> ATOMItemsAdapter()
|
||||
//LocalRSSHelper.RSSType.ATOM -> ATOMItemAdapter()
|
||||
else -> throw IllegalArgumentException("Unknown RSS type : $type")
|
||||
}
|
||||
|
||||
|
|
|
@ -10,12 +10,16 @@ import com.readrops.api.utils.extensions.checkElement
|
|||
import com.readrops.api.utils.extensions.nonNullText
|
||||
import com.readrops.api.utils.extensions.nullableText
|
||||
import com.readrops.db.entities.Feed
|
||||
import com.readrops.db.entities.Item
|
||||
|
||||
class ATOMFeedAdapter : XmlAdapter<Feed> {
|
||||
class ATOMFeedAdapter : XmlAdapter<Pair<Feed, List<Item>>> {
|
||||
|
||||
override fun fromXml(konsumer: Konsumer): Feed {
|
||||
override fun fromXml(konsumer: Konsumer): Pair<Feed, List<Item>> {
|
||||
val feed = Feed()
|
||||
|
||||
val items = arrayListOf<Item>()
|
||||
val itemAdapter = ATOMItemAdapter()
|
||||
|
||||
return try {
|
||||
konsumer.checkElement(LocalRSSHelper.ATOM_ROOT_NAME) {
|
||||
it.allChildrenAutoIgnore(names) {
|
||||
|
@ -24,19 +28,20 @@ class ATOMFeedAdapter : XmlAdapter<Feed> {
|
|||
"title" -> name = nonNullText()
|
||||
"link" -> parseLink(this@allChildrenAutoIgnore, feed)
|
||||
"subtitle" -> description = nullableText()
|
||||
"entry" -> items += itemAdapter.fromXml(this@allChildrenAutoIgnore)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
konsumer.close()
|
||||
feed
|
||||
Pair(feed, items)
|
||||
} catch (e: Exception) {
|
||||
throw ParseException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseLink(konsume: Konsumer, feed: Feed) = with(konsume) {
|
||||
private fun parseLink(konsumer: Konsumer, feed: Feed) = with(konsumer) {
|
||||
val rel = attributes.getValueOrNull("rel")
|
||||
|
||||
if (rel == "self")
|
||||
|
@ -46,6 +51,6 @@ class ATOMFeedAdapter : XmlAdapter<Feed> {
|
|||
}
|
||||
|
||||
companion object {
|
||||
val names = Names.of("title", "link", "subtitle")
|
||||
val names = Names.of("title", "link", "subtitle", "entry")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.readrops.api.localfeed.atom
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.Konsumer
|
||||
import com.gitlab.mvysny.konsumexml.Names
|
||||
import com.gitlab.mvysny.konsumexml.allChildrenAutoIgnore
|
||||
import com.readrops.api.localfeed.XmlAdapter
|
||||
import com.readrops.api.utils.*
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import com.readrops.api.utils.extensions.nonNullText
|
||||
import com.readrops.api.utils.extensions.nullableText
|
||||
import com.readrops.api.utils.extensions.nullableTextRecursively
|
||||
import com.readrops.db.entities.Item
|
||||
import org.joda.time.LocalDateTime
|
||||
|
||||
class ATOMItemAdapter : XmlAdapter<Item> {
|
||||
|
||||
override fun fromXml(konsumer: Konsumer): Item {
|
||||
val item = Item()
|
||||
|
||||
return try {
|
||||
item.apply {
|
||||
konsumer.allChildrenAutoIgnore(names) {
|
||||
when (tagName) {
|
||||
"title" -> title = nonNullText()
|
||||
"id" -> guid = nullableText()
|
||||
"updated" -> pubDate = DateUtils.parse(nullableText())
|
||||
"link" -> parseLink(this, this@apply)
|
||||
"author" -> allChildrenAutoIgnore("name") { author = nullableText() }
|
||||
"summary" -> description = nullableTextRecursively()
|
||||
"content" -> content = nullableTextRecursively()
|
||||
else -> skipContents()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
validateItem(item)
|
||||
if (item.pubDate == null) item.pubDate = LocalDateTime.now()
|
||||
if (item.guid == null) item.guid = item.link
|
||||
|
||||
konsumer.close()
|
||||
item
|
||||
} catch (e: Exception) {
|
||||
throw ParseException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseLink(konsumer: Konsumer, item: Item) = with(konsumer) {
|
||||
if (attributes.getValueOrNull("rel") == null ||
|
||||
attributes["rel"] == "alternate")
|
||||
item.link = attributes.getValueOrNull("href")
|
||||
}
|
||||
|
||||
private fun validateItem(item: Item) {
|
||||
when {
|
||||
item.title == null -> throw ParseException("Item title is required")
|
||||
item.link == null -> throw ParseException("Item link is required")
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val names = Names.of("title", "id", "updated", "link", "author", "summary", "content")
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
package com.readrops.api.localfeed.atom
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.Konsumer
|
||||
import com.gitlab.mvysny.konsumexml.Names
|
||||
import com.gitlab.mvysny.konsumexml.allChildrenAutoIgnore
|
||||
import com.readrops.api.localfeed.XmlAdapter
|
||||
import com.readrops.api.utils.*
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import com.readrops.api.utils.extensions.nonNullText
|
||||
import com.readrops.api.utils.extensions.nullableText
|
||||
import com.readrops.api.utils.extensions.nullableTextRecursively
|
||||
import com.readrops.db.entities.Item
|
||||
import org.joda.time.LocalDateTime
|
||||
|
||||
class ATOMItemsAdapter : XmlAdapter<List<Item>> {
|
||||
|
||||
override fun fromXml(konsumer: Konsumer): List<Item> {
|
||||
val items = arrayListOf<Item>()
|
||||
|
||||
return try {
|
||||
konsumer.child("feed") {
|
||||
allChildrenAutoIgnore("entry") {
|
||||
val item = Item().apply {
|
||||
allChildrenAutoIgnore(names) {
|
||||
when (tagName) {
|
||||
"title" -> title = nonNullText()
|
||||
"id" -> guid = nullableText()
|
||||
"updated" -> pubDate = DateUtils.parse(nullableText())
|
||||
"link" -> parseLink(this, this@apply)
|
||||
"author" -> allChildrenAutoIgnore("name") { author = nullableText() }
|
||||
"summary" -> description = nullableTextRecursively()
|
||||
"content" -> content = nullableTextRecursively()
|
||||
else -> skipContents()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
validateItem(item)
|
||||
if (item.pubDate == null) item.pubDate = LocalDateTime.now()
|
||||
if (item.guid == null) item.guid = item.link
|
||||
|
||||
items += item
|
||||
}
|
||||
}
|
||||
|
||||
konsumer.close()
|
||||
items
|
||||
} catch (e: Exception) {
|
||||
throw ParseException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseLink(konsumer: Konsumer, item: Item) {
|
||||
konsumer.apply {
|
||||
if (attributes.getValueOpt("rel") == null ||
|
||||
attributes["rel"] == "alternate")
|
||||
item.link = attributes.getValueOpt("href")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun validateItem(item: Item) {
|
||||
when {
|
||||
item.title == null -> throw ParseException("Item title is required")
|
||||
item.link == null -> throw ParseException("Item link is required")
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val names = Names.of("title", "id", "updated", "link", "author", "summary", "content")
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package com.readrops.api.localfeed
|
||||
|
||||
import com.readrops.api.localfeed.atom.ATOMFeedAdapter
|
||||
import com.readrops.api.localfeed.atom.ATOMItemsAdapter
|
||||
import com.readrops.api.localfeed.atom.ATOMItemAdapter
|
||||
import com.readrops.api.localfeed.rss1.RSS1FeedAdapter
|
||||
import com.readrops.api.localfeed.rss1.RSS1ItemsAdapter
|
||||
import com.readrops.api.localfeed.rss2.RSS2FeedAdapter
|
||||
|
@ -20,7 +20,7 @@ class XmlAdapterTest {
|
|||
fun xmlFeedAdapterFactoryTest() {
|
||||
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_1) is RSS1FeedAdapter)
|
||||
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSS2FeedAdapter)
|
||||
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMFeedAdapter)
|
||||
//assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMFeedAdapter)
|
||||
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN)
|
||||
|
@ -30,7 +30,7 @@ class XmlAdapterTest {
|
|||
fun xmlItemsAdapterFactoryTest() {
|
||||
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.RSS_1) is RSS1ItemsAdapter)
|
||||
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSS2ItemsAdapter)
|
||||
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMItemsAdapter)
|
||||
//assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMItemAdapter)
|
||||
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN)
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
package com.readrops.api.localfeed.atom
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import com.readrops.api.TestUtils
|
||||
import com.readrops.api.utils.DateUtils
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import junit.framework.TestCase
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.lang.Exception
|
||||
|
||||
class ATOMAdapterTest {
|
||||
|
||||
private val adapter = ATOMFeedAdapter()
|
||||
|
||||
@Test
|
||||
fun normalCasesTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items.xml")
|
||||
|
||||
val pair = adapter.fromXml(stream.konsumeXml())
|
||||
val feed = pair.first
|
||||
val items = pair.second
|
||||
|
||||
with(feed) {
|
||||
assertEquals(name, "Recent Commits to Readrops:develop")
|
||||
assertEquals(url, "https://github.com/readrops/Readrops/commits/develop.atom")
|
||||
assertEquals(siteUrl, "https://github.com/readrops/Readrops/commits/develop")
|
||||
assertEquals(description, "Here is a subtitle")
|
||||
}
|
||||
|
||||
with(items[0]) {
|
||||
assertEquals(items.size, 4)
|
||||
assertEquals(title, "Add an option to open item url in custom tab")
|
||||
assertEquals(link, "https://github.com/readrops/Readrops/commit/c15f093a1bc4211e85f8d1817c9073e307afe5ac")
|
||||
assertEquals(pubDate, DateUtils.parse("2020-09-06T21:09:59Z"))
|
||||
assertEquals(author, "Shinokuni")
|
||||
assertEquals(description, "Summary")
|
||||
assertEquals(guid, "tag:github.com,2008:Grit::Commit/c15f093a1bc4211e85f8d1817c9073e307afe5ac")
|
||||
TestCase.assertNotNull(content)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noDateTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_date.xml")
|
||||
|
||||
val item = adapter.fromXml(stream.konsumeXml()).second[0]
|
||||
TestCase.assertNotNull(item.pubDate)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noTitleTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_title.xml")
|
||||
|
||||
val exception = assertThrows(ParseException::class.java) {
|
||||
adapter.fromXml(stream.konsumeXml())
|
||||
}
|
||||
|
||||
assertTrue(exception.message!!.contains("Item title is required"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noLinkTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_link.xml")
|
||||
|
||||
val exception = assertThrows(ParseException::class.java) {
|
||||
adapter.fromXml(stream.konsumeXml())
|
||||
}
|
||||
|
||||
assertTrue(exception.message!!.contains("Item link is required"))
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package com.readrops.api.localfeed.atom
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import com.readrops.api.TestUtils
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class ATOMFeedAdapterTest {
|
||||
|
||||
private val adapter = ATOMFeedAdapter()
|
||||
|
||||
@Test
|
||||
fun normalCasesTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_feed.xml")
|
||||
|
||||
val feed = adapter.fromXml(stream.konsumeXml())
|
||||
|
||||
assertEquals(feed.name, "Recent Commits to Readrops:develop")
|
||||
assertEquals(feed.url, "https://github.com/readrops/Readrops/commits/develop.atom")
|
||||
assertEquals(feed.siteUrl, "https://github.com/readrops/Readrops/commits/develop")
|
||||
assertEquals(feed.description, "Here is a subtitle")
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
package com.readrops.api.localfeed.atom
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import com.readrops.api.TestUtils
|
||||
import com.readrops.api.utils.DateUtils
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
|
||||
class ATOMItemsAdapterTest {
|
||||
|
||||
private val adapter = ATOMItemsAdapter()
|
||||
|
||||
@get:Rule
|
||||
val expectedException: ExpectedException = ExpectedException.none()
|
||||
|
||||
@Test
|
||||
fun normalCasesTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items.xml")
|
||||
|
||||
val items = adapter.fromXml(stream.konsumeXml())
|
||||
val item = items[0]
|
||||
|
||||
assertEquals(items.size, 4)
|
||||
assertEquals(item.title, "Add an option to open item url in custom tab")
|
||||
assertEquals(item.link, "https://github.com/readrops/Readrops/commit/c15f093a1bc4211e85f8d1817c9073e307afe5ac")
|
||||
assertEquals(item.pubDate, DateUtils.parse("2020-09-06T21:09:59Z"))
|
||||
assertEquals(item.author, "Shinokuni")
|
||||
assertEquals(item.description, "Summary")
|
||||
assertEquals(item.guid, "tag:github.com,2008:Grit::Commit/c15f093a1bc4211e85f8d1817c9073e307afe5ac")
|
||||
assertNotNull(item.content)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noDateTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_date.xml")
|
||||
|
||||
val item = adapter.fromXml(stream.konsumeXml()).first()
|
||||
assertNotNull(item.pubDate)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noTitleTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_title.xml")
|
||||
|
||||
expectedException.expect(ParseException::class.java)
|
||||
expectedException.expectMessage("Item title is required")
|
||||
|
||||
adapter.fromXml(stream.konsumeXml())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noLinkTest() {
|
||||
val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_link.xml")
|
||||
|
||||
expectedException.expect(ParseException::class.java)
|
||||
expectedException.expectMessage("Item link is required")
|
||||
|
||||
adapter.fromXml(stream.konsumeXml())
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
<link type="application/atom+xml" rel="self" href="https://github.com/readrops/Readrops/commits/develop.atom"/>
|
||||
<title>Recent Commits to Readrops:develop</title>
|
||||
<updated>2020-09-06T21:09:59Z</updated>
|
||||
<subtitle>Here is a subtitle</subtitle>
|
||||
<entry>
|
||||
<id>tag:github.com,2008:Grit::Commit/c15f093a1bc4211e85f8d1817c9073e307afe5ac</id>
|
||||
<link type="text/html" rel="alternate" href="https://github.com/readrops/Readrops/commit/c15f093a1bc4211e85f8d1817c9073e307afe5ac"/>
|
||||
|
|
Loading…
Reference in New Issue