Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling committed Aug 14, 2023
1 parent ee77de8 commit 3f54528
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ void testTimestampSortTest() {
"https://www.politico.com/rss/politicopicks.xml",
"https://www.e1.ru/talk/forum/rss.php?f=86",
"https://failed-to-read-from-this-url.com",
"https://www.nrdc.org/rss.xml");
"https://www.nrdc.org/rss.xml",
"https://www.theverge.com/rss/reviews/index.xml",
"https://feeds.macrumors.com/MacRumors-All");


List<String> extendedUrlList = new ArrayList<>(urlList);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.apptasticsoftware.rssreader.util;

import com.apptasticsoftware.rssreader.DateTime;
import com.apptasticsoftware.rssreader.RssReader;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class ItemComparatorTest {

@Test
void testSortNewestItem() throws IOException {
var items = new RssReader().read("https://www.theverge.com/rss/reviews/index.xml")
.sorted(ItemComparator.newestItemFirst())
.map(i -> i.getPubDateZonedDateTime().orElse(null))
.filter(Objects::nonNull)
.map(ZonedDateTime::toEpochSecond)
.collect(Collectors.toList());

assertTrue(isDescendingSortOrder(items));
}

@Test
void testSortNewestItemWithDateTimeParser() throws IOException {
var items = new RssReader().read("https://www.theverge.com/rss/reviews/index.xml")
.sorted(ItemComparator.newestItemFirst(new DateTime()))
.map(i -> i.getPubDateZonedDateTime().orElse(null))
.filter(Objects::nonNull)
.map(ZonedDateTime::toEpochSecond)
.collect(Collectors.toList());

assertTrue(isDescendingSortOrder(items));
}

@Test
void testSortOldestItemFirst() throws IOException {
var items = new RssReader().read("https://www.theverge.com/rss/reviews/index.xml")
.sorted(ItemComparator.oldestItemFirst())
.map(i -> i.getPubDateZonedDateTime().orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList());

assertTrue(isAscendingSortOrder(items));
}

@Test
void testSortOldestItemFirstWithDateTimeParser() throws IOException {
var items = new RssReader().read("https://www.theverge.com/rss/reviews/index.xml")
.sorted(ItemComparator.oldestItemFirst(new DateTime()))
.map(i -> i.getPubDateZonedDateTime().orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList());

assertTrue(isAscendingSortOrder(items));
}

@Test
void testSortChannelTitle() {
var urlList = List.of("https://www.theverge.com/rss/reviews/index.xml", "https://feeds.macrumors.com/MacRumors-All");
var items = new RssReader().read(urlList)
.sorted(ItemComparator.channelTitle())
.map(i -> i.getChannel().getTitle())
.filter(Objects::nonNull)
.collect(Collectors.toList());

assertTrue(isAscendingSortOrder(items));
}


private static <T extends Comparable<? super T>> boolean isAscendingSortOrder(List<T> array){
for (int i = 0; i < array.size()-1; i++) {
if (array.get(i).compareTo(array.get(i+1)) > 0){
return false;
}
}
return true;
}

private static <T extends Comparable<? super T>> boolean isDescendingSortOrder(List<T> array){
for (int i = 0; i < array.size()-1; i++) {
if (array.get(i).compareTo(array.get(i+1)) < 0){
return false;
}
}
return true;
}

}

0 comments on commit 3f54528

Please sign in to comment.