Skip to content

Commit

Permalink
Add content field to item (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling authored Nov 24, 2024
1 parent c3a3240 commit e5f56e9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ protected void registerItemTags() {
itemTags.putIfAbsent("/rss/channel/item/title", Item::setTitle);
itemTags.putIfAbsent("description", Item::setDescription);
itemTags.putIfAbsent("summary", Item::setDescription);
itemTags.putIfAbsent("content", Item::setDescription);
itemTags.putIfAbsent("content", Item::setContent);
itemTags.putIfAbsent("content:encoded", (item, value) -> Mapper.mapIfEmpty(value, item::getContent, item::setContent));
itemTags.putIfAbsent("link", Item::setLink);
itemTags.putIfAbsent("author", Item::setAuthor);
itemTags.putIfAbsent("/feed/entry/author/name", Item::setAuthor);
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/apptasticsoftware/rssreader/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Item implements Comparable<Item> {
private final Comparator<Item> defaultComparator;
private String title;
private String description;
private String content;
private String link;
private String author;
private String category;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void setTitle(String title) {
* @return description
*/
public Optional<String> getDescription() {
return Optional.ofNullable(description);
return Optional.ofNullable(description).or(this::getContent);
}

/**
Expand All @@ -110,6 +111,24 @@ public void setDescription(String description) {
this.description = description;
}

/**
* Get the item content.
*
* @return content
*/
public Optional<String> getContent() {
return Optional.ofNullable(content);
}

/**
* Set the item content.
*
* @param content content
*/
public void setContent(String content) {
this.content = content;
}

/**
* Get the URL of the item.
*
Expand Down Expand Up @@ -364,6 +383,7 @@ public boolean equals(Object o) {
Item item = (Item) o;
return Objects.equals(getTitle(), item.getTitle()) &&
Objects.equals(getDescription(), item.getDescription()) &&
Objects.equals(getContent(), item.getContent()) &&
Objects.equals(getLink(), item.getLink()) &&
Objects.equals(getAuthor(), item.getAuthor()) &&
getCategories().equals(item.getCategories()) &&
Expand All @@ -378,7 +398,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(getTitle(), getDescription(), getLink(), getAuthor(), getCategories(),
return Objects.hash(getTitle(), getDescription(), getContent(), getLink(), getAuthor(), getCategories(),
getGuid(), getIsPermaLink(), getPubDate(), getUpdated(), getComments(), getEnclosures(), getChannel());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void nullHttpClient() {
}


@Disabled("Investigating")
@Test
void rssRiksbanken() throws IOException {
RssReader reader = new RssReader();
Expand Down Expand Up @@ -135,7 +134,6 @@ void rssScb() throws IOException {
}


@Disabled("Investigate")
@Test
void rssPlacera() throws IOException {
RssReader reader = new RssReader();
Expand Down Expand Up @@ -239,7 +237,7 @@ void rssBreakit() throws IOException {
@Test
void rssRealtid() throws IOException {
RssReader reader = new RssReader();
List<Item> items = reader.read("https://www.realtid.se/rss/senaste").collect(Collectors.toList());
List<Item> items = reader.read("https://www.realtid.se/rss/").collect(Collectors.toList());

assertFalse(items.isEmpty());

Expand All @@ -250,7 +248,7 @@ void rssRealtid() throws IOException {
assertThat(channel.getTitle(), is("Realtid"));
assertThat(channel.getDescription(), is(""));
assertThat(channel.getLanguage(), isPresentAndIs("sv"));
assertThat(channel.getLink(), is("https://www.realtid.se/rss/senaste"));
assertThat(channel.getLink(), is("https://www.realtid.se/rss/"));
assertThat(channel.getCopyright(), isEmpty());
assertThat(channel.getGenerator(), isEmpty());
assertThat(channel.getLastBuildDate(), isEmpty());
Expand Down Expand Up @@ -414,6 +412,29 @@ void rssWorldOfTank() throws IOException {
}


@Test
void contentEncoding() throws IOException {
RssReader reader = new RssReader();
List<Item> items = reader.read("https://akka.io/blog/rss.xml").collect(Collectors.toList());
assertFalse(items.isEmpty());

for (Item item : items) {
// Validate channel
Channel channel = item.getChannel();
assertNotNull(channel);
assertThat(channel.getTitle(), containsString("AKKA Blogs"));
assertThat(channel.getLanguage(), isPresentAndIs("en"));

// Validate item
assertNotNull(item);
assertThat(item.getTitle(), isPresentAnd(not(emptyString())));
assertThat(item.getDescription(), isPresentAnd(not(emptyString())));
assertThat(item.getContent(), isPresentAnd(not(emptyString())));
assertNotEquals(item.getDescription(), item.getContent());
}
}


@Test
void zonedDateTime() throws IOException {
RssReader reader = new RssReader();
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/com/apptasticsoftware/rssreader/RssReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,21 @@ void enclosureEqualsTest() {

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(Channel.class).withIgnoredFields("dateTimeParser").withIgnoredFields("category").withIgnoredFields("syUpdatePeriod").withIgnoredFields("syUpdateFrequency").withNonnullFields("categories").verify();
EqualsVerifier.simple().forClass(Item.class).withIgnoredFields("defaultComparator").withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").withIgnoredFields("enclosure").withNonnullFields("enclosures").verify();
EqualsVerifier.simple().forClass(Channel.class)
.withIgnoredFields("dateTimeParser")
.withIgnoredFields("category")
.withIgnoredFields("syUpdatePeriod")
.withIgnoredFields("syUpdateFrequency")
.withNonnullFields("categories")
.verify();
EqualsVerifier.simple().forClass(Item.class)
.withIgnoredFields("defaultComparator")
.withIgnoredFields("dateTimeParser")
.withIgnoredFields("category")
.withNonnullFields("categories")
.withIgnoredFields("enclosure")
.withNonnullFields("enclosures")
.verify();
EqualsVerifier.simple().forClass(Enclosure.class).verify();
EqualsVerifier.simple().forClass(Image.class).verify();
}
Expand Down

0 comments on commit e5f56e9

Please sign in to comment.