From 1597cbc82c7d0b0bb3a3d4ee34c21a3ce613b8d2 Mon Sep 17 00:00:00 2001 From: w3stling Date: Thu, 8 Aug 2024 19:12:55 +0200 Subject: [PATCH 1/2] Add atom updated field --- .../rssreader/AbstractRssReader.java | 5 ++- .../com/apptasticsoftware/rssreader/Item.java | 35 +++++++++++++++++-- .../RssReaderIntegrationTest.java | 14 +++++++- src/test/resources/atom-feed.xml | 1 + 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/apptasticsoftware/rssreader/AbstractRssReader.java b/src/main/java/com/apptasticsoftware/rssreader/AbstractRssReader.java index ef567de..a601bf2 100644 --- a/src/main/java/com/apptasticsoftware/rssreader/AbstractRssReader.java +++ b/src/main/java/com/apptasticsoftware/rssreader/AbstractRssReader.java @@ -211,7 +211,10 @@ protected void registerItemTags() { itemTags.putIfAbsent("category", Item::addCategory); itemTags.putIfAbsent("pubDate", Item::setPubDate); itemTags.putIfAbsent("published", Item::setPubDate); - itemTags.putIfAbsent("updated", (item, value) -> Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate)); + itemTags.putIfAbsent("updated", (item, value) -> { + item.setUpdated(value); + Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate); + }); itemTags.putIfAbsent("comments", Item::setComments); itemTags.putIfAbsent("dc:creator", (item, value) -> Mapper.mapIfEmpty(value, item::getAuthor, item::setAuthor)); itemTags.putIfAbsent("dc:date", (item, value) -> Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate)); diff --git a/src/main/java/com/apptasticsoftware/rssreader/Item.java b/src/main/java/com/apptasticsoftware/rssreader/Item.java index 9f26b34..e9b9e40 100644 --- a/src/main/java/com/apptasticsoftware/rssreader/Item.java +++ b/src/main/java/com/apptasticsoftware/rssreader/Item.java @@ -45,6 +45,7 @@ public class Item implements Comparable { private String guid; private Boolean isPermaLink; private String pubDate; + private String updated; private String comments; private Enclosure enclosure; private final List enclosures = new ArrayList<>(); @@ -211,7 +212,7 @@ public void setGuid(String guid) { /** * If the guid element has an attribute named "isPermaLink" with a value of true, the reader may assume that - * it is a permalink to the item, that is, a url that can be opened in a Web browser, that points to the full + * it is a permalink to the item, that is, an url that can be opened in a Web browser, that points to the full * item described by the item element. * * @return permanent link @@ -222,7 +223,7 @@ public Optional getIsPermaLink() { /** * If the guid element has an attribute named "isPermaLink" with a value of true, the reader may assume that - * it is a permalink to the item, that is, a url that can be opened in a Web browser, that points to the full + * it is a permalink to the item, that is, an url that can be opened in a Web browser, that points to the full * item described by the item element. * * @param isPermaLink is perma link @@ -258,6 +259,33 @@ public Optional getPubDateZonedDateTime() { return getPubDate().map(dateTimeParser::parse); } + /** + * Get a string that indicates when the item was updated. + * + * @return updated date + */ + public Optional getUpdated() { + return Optional.ofNullable(updated); + } + + /** + * Set a string that indicates when the item was updated. + * + * @param updated updated date + */ + public void setUpdated(String updated) { + this.updated = updated; + } + + /** + * Get a ZonedDateTime that indicates when the item was updated. + * + * @return publication date + */ + public Optional getUpdatedZonedDateTime() { + return getUpdated().map(dateTimeParser::parse); + } + /** * Get comments relating to the item. * @return comments @@ -341,6 +369,7 @@ public boolean equals(Object o) { Objects.equals(getGuid(), item.getGuid()) && Objects.equals(getIsPermaLink(), item.getIsPermaLink()) && Objects.equals(getPubDate(), item.getPubDate()) && + Objects.equals(getUpdated(), item.getUpdated()) && Objects.equals(getComments(), item.getComments()) && getEnclosures().equals(item.getEnclosures()) && Objects.equals(getChannel(), item.getChannel()); @@ -349,7 +378,7 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash(getTitle(), getDescription(), getLink(), getAuthor(), getCategories(), - getGuid(), getIsPermaLink(), getPubDate(), getComments(), getEnclosures(), getChannel()); + getGuid(), getIsPermaLink(), getPubDate(), getUpdated(), getComments(), getEnclosures(), getChannel()); } /** diff --git a/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java b/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java index 16c2fb5..92e0bdb 100644 --- a/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java +++ b/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java @@ -618,18 +618,30 @@ void testAtomFeed() { assertEquals("Example Toolkit", items.get(0).getChannel().getGenerator().orElse(null)); assertEquals("2005-07-31T12:29:29Z", items.get(0).getChannel().getLastBuildDate().orElse(null)); + assertEquals("Atom draft-07 snapshot", items.get(0).getTitle().orElse(null)); + assertNull(items.get(1).getAuthor().orElse(null)); + assertEquals("http://example.org/audio/ph34r_my_podcast.mp3", items.get(0).getLink().orElse(null)); + assertEquals("tag:example.org,2003:3.2397", items.get(0).getGuid().orElse(null)); + assertEquals("2003-12-13T08:29:29-04:00", items.get(0).getPubDate().orElse(null)); + assertEquals("2005-07-31T12:29:29Z", items.get(0).getUpdated().orElse(null)); + assertEquals(211, items.get(1).getDescription().orElse("").length()); + assertEquals("Atom-Powered Robots Run Amok", items.get(1).getTitle().orElse(null)); assertNull(items.get(1).getAuthor().orElse(null)); assertEquals("http://example.org/2003/12/13/atom03", items.get(1).getLink().orElse(null)); assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", items.get(1).getGuid().orElse(null)); assertEquals("2003-12-13T18:30:02Z", items.get(1).getPubDate().orElse(null)); + assertEquals("2003-12-13T18:30:02Z", items.get(1).getUpdated().orElse(null)); assertEquals(211, items.get(1).getDescription().orElse("").length()); assertEquals("Atom-Powered Robots Run Amok 2", items.get(2).getTitle().orElse(null)); assertNull(items.get(2).getAuthor().orElse(null)); assertEquals("http://example.org/2003/12/13/atom04", items.get(2).getLink().orElse(null)); assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6b", items.get(2).getGuid().orElse(null)); - assertEquals("2003-12-13T18:30:01Z", items.get(2).getPubDate().orElse(null)); + assertEquals("2003-12-13T09:28:28-04:00", items.get(2).getPubDate().orElse(null)); + assertEquals(1071322108, items.get(2).getPubDateZonedDateTime().map(ZonedDateTime::toEpochSecond).orElse(null)); + assertEquals("2003-12-13T18:30:01Z", items.get(2).getUpdated().orElse(null)); + assertEquals(1071340201, items.get(2).getUpdatedZonedDateTime().map(ZonedDateTime::toEpochSecond).orElse(null)); assertEquals(47, items.get(2).getDescription().orElse("").length()); } diff --git a/src/test/resources/atom-feed.xml b/src/test/resources/atom-feed.xml index 5c8f9de..f57a750 100644 --- a/src/test/resources/atom-feed.xml +++ b/src/test/resources/atom-feed.xml @@ -63,6 +63,7 @@ Atom-Powered Robots Run Amok 2 urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6b + 2003-12-13T09:28:28-04:00 2003-12-13T18:30:01Z \ No newline at end of file From 43c2796fde19d163a449fa47801eaac91731b662 Mon Sep 17 00:00:00 2001 From: w3stling Date: Thu, 8 Aug 2024 19:29:56 +0200 Subject: [PATCH 2/2] Fix code smell --- .../integrationtest/RssReaderIntegrationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java b/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java index 92e0bdb..b64df6f 100644 --- a/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java +++ b/src/test/java/com/apptasticsoftware/integrationtest/RssReaderIntegrationTest.java @@ -604,6 +604,7 @@ void testCloseTwice() throws IOException { } } + @SuppressWarnings("java:S5961") @Test void testAtomFeed() { var items = new RssReader().read(fromFile("atom-feed.xml"))