|
| 1 | +package com.apptasticsoftware.integrationtest; |
| 2 | + |
| 3 | +import com.apptasticsoftware.rssreader.Item; |
| 4 | +import com.apptasticsoftware.rssreader.RssReader; |
| 5 | +import com.apptasticsoftware.rssreader.util.RssServer; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.ZonedDateTime; |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +class ConnectionTest { |
| 18 | + private static final int PORT = 8008; |
| 19 | + private static final Duration NEGATIVE_DURATION = Duration.ofSeconds(-30); |
| 20 | + |
| 21 | + @Test |
| 22 | + void testConnectionTimeoutWithNullValue() { |
| 23 | + var rssReader = new RssReader(); |
| 24 | + var exception = assertThrows(NullPointerException.class, () -> rssReader.setConnectionTimeout(null)); |
| 25 | + assertEquals("Connection timeout must not be null", exception.getMessage()); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testRequestTimeoutWithNullValue() { |
| 30 | + var rssReader = new RssReader(); |
| 31 | + var exception = assertThrows(NullPointerException.class, () -> rssReader.setRequestTimeout(null)); |
| 32 | + assertEquals("Request timeout must not be null", exception.getMessage()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testReadTimeoutWithNullValue() { |
| 37 | + var rssReader = new RssReader(); |
| 38 | + var exception = assertThrows(NullPointerException.class, () -> rssReader.setReadTimeout(null)); |
| 39 | + assertEquals("Read timeout must not be null", exception.getMessage()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testConnectionTimeoutWithNegativeValue() { |
| 44 | + var rssReader = new RssReader(); |
| 45 | + var exception = assertThrows(IllegalArgumentException.class, () -> rssReader.setConnectionTimeout(NEGATIVE_DURATION)); |
| 46 | + assertEquals("Connection timeout must not be negative", exception.getMessage()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testRequestTimeoutWithNegativeValue() { |
| 51 | + var rssReader = new RssReader(); |
| 52 | + var exception = assertThrows(IllegalArgumentException.class, () -> rssReader.setRequestTimeout(NEGATIVE_DURATION)); |
| 53 | + assertEquals("Request timeout must not be negative", exception.getMessage()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testReadTimeoutWithNegativeValue() { |
| 58 | + var rssReader = new RssReader(); |
| 59 | + var exception = assertThrows(IllegalArgumentException.class, () -> rssReader.setReadTimeout(NEGATIVE_DURATION)); |
| 60 | + assertEquals("Read timeout must not be negative", exception.getMessage()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testReadFromLocalRssServerNoTimeout() throws IOException { |
| 65 | + var server = RssServer.with(getFile("atom-feed.xml")) |
| 66 | + .port(PORT) |
| 67 | + .endpointPath("/rss") |
| 68 | + .build(); |
| 69 | + server.start(); |
| 70 | + |
| 71 | + var items = new RssReader() |
| 72 | + .setConnectionTimeout(Duration.ZERO) |
| 73 | + .setRequestTimeout(Duration.ZERO) |
| 74 | + .setReadTimeout(Duration.ZERO) |
| 75 | + .read("http://localhost:8008/rss") |
| 76 | + .collect(Collectors.toList()); |
| 77 | + |
| 78 | + server.stop(); |
| 79 | + verify(3, items); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testReadFromLocalRssServer10SecondTimeout() throws IOException { |
| 84 | + var server = RssServer.with(getFile("atom-feed.xml")) |
| 85 | + .port(PORT) |
| 86 | + .endpointPath("/rss") |
| 87 | + .build(); |
| 88 | + server.start(); |
| 89 | + |
| 90 | + var items = new RssReader() |
| 91 | + .setConnectionTimeout(Duration.ofSeconds(10)) |
| 92 | + .setRequestTimeout(Duration.ofSeconds(10)) |
| 93 | + .setReadTimeout(Duration.ofSeconds(10)) |
| 94 | + .read("http://localhost:8008/rss") |
| 95 | + .collect(Collectors.toList()); |
| 96 | + |
| 97 | + server.stop(); |
| 98 | + verify(3, items); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + @Test |
| 103 | + void testReadFromLocalRssServer() throws IOException { |
| 104 | + var server = RssServer.with(getFile("atom-feed.xml")) |
| 105 | + .port(PORT) |
| 106 | + .endpointPath("/rss") |
| 107 | + .build(); |
| 108 | + server.start(); |
| 109 | + |
| 110 | + var items = new RssReader() |
| 111 | + .setReadTimeout(Duration.ofSeconds(2)) |
| 112 | + .read("http://localhost:8008/rss") |
| 113 | + .collect(Collectors.toList()); |
| 114 | + |
| 115 | + server.stop(); |
| 116 | + verify(3, items); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testNoReadTimeout() throws IOException { |
| 121 | + var server = RssServer.with(getFile("atom-feed.xml")) |
| 122 | + .port(PORT) |
| 123 | + .endpointPath("/rss") |
| 124 | + .build(); |
| 125 | + server.start(); |
| 126 | + |
| 127 | + var items = new RssReader() |
| 128 | + .setReadTimeout(Duration.ZERO) |
| 129 | + .read("http://localhost:8008/rss") |
| 130 | + .collect(Collectors.toList()); |
| 131 | + |
| 132 | + server.stop(); |
| 133 | + verify(3, items); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testReadTimeout() throws IOException { |
| 138 | + var server = RssServer.withWritePause(getFile("atom-feed.xml"), Duration.ofSeconds(4)) |
| 139 | + .port(PORT) |
| 140 | + .endpointPath("/slow-server") |
| 141 | + .build(); |
| 142 | + server.start(); |
| 143 | + |
| 144 | + var items = new RssReader() |
| 145 | + .setReadTimeout(Duration.ofSeconds(2)) |
| 146 | + .read("http://localhost:8008/slow-server") |
| 147 | + .collect(Collectors.toList()); |
| 148 | + |
| 149 | + server.stop(); |
| 150 | + verify(2, items); |
| 151 | + } |
| 152 | + |
| 153 | + private static void verify(int expectedSize, List<Item> items) { |
| 154 | + assertEquals(expectedSize, items.size()); |
| 155 | + |
| 156 | + if (!items.isEmpty()) { |
| 157 | + assertEquals("dive into mark", items.get(0).getChannel().getTitle()); |
| 158 | + assertEquals(65, items.get(0).getChannel().getDescription().length()); |
| 159 | + assertEquals("http://example.org/feed.atom", items.get(0).getChannel().getLink()); |
| 160 | + assertEquals("Copyright (c) 2003, Mark Pilgrim", items.get(0).getChannel().getCopyright().orElse(null)); |
| 161 | + assertEquals("Example Toolkit", items.get(0).getChannel().getGenerator().orElse(null)); |
| 162 | + assertEquals("2005-07-31T12:29:29Z", items.get(0).getChannel().getLastBuildDate().orElse(null)); |
| 163 | + |
| 164 | + assertEquals("Atom draft-07 snapshot", items.get(0).getTitle().orElse(null)); |
| 165 | + assertNull(items.get(1).getAuthor().orElse(null)); |
| 166 | + assertEquals("http://example.org/audio/ph34r_my_podcast.mp3", items.get(0).getLink().orElse(null)); |
| 167 | + assertEquals("tag:example.org,2003:3.2397", items.get(0).getGuid().orElse(null)); |
| 168 | + assertEquals("2003-12-13T08:29:29-04:00", items.get(0).getPubDate().orElse(null)); |
| 169 | + assertEquals("2005-07-31T12:29:29Z", items.get(0).getUpdated().orElse(null)); |
| 170 | + assertEquals(211, items.get(1).getDescription().orElse("").length()); |
| 171 | + } |
| 172 | + if (items.size() >= 2) { |
| 173 | + assertEquals("Atom-Powered Robots Run Amok", items.get(1).getTitle().orElse(null)); |
| 174 | + assertNull(items.get(1).getAuthor().orElse(null)); |
| 175 | + assertEquals("http://example.org/2003/12/13/atom03", items.get(1).getLink().orElse(null)); |
| 176 | + assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", items.get(1).getGuid().orElse(null)); |
| 177 | + assertEquals("2003-12-13T18:30:02Z", items.get(1).getPubDate().orElse(null)); |
| 178 | + assertEquals("2003-12-13T18:30:02Z", items.get(1).getUpdated().orElse(null)); |
| 179 | + assertEquals(211, items.get(1).getDescription().orElse("").length()); |
| 180 | + } |
| 181 | + if (items.size() >= 3) { |
| 182 | + assertEquals("Atom-Powered Robots Run Amok 2", items.get(2).getTitle().orElse(null)); |
| 183 | + assertNull(items.get(2).getAuthor().orElse(null)); |
| 184 | + assertEquals("http://example.org/2003/12/13/atom04", items.get(2).getLink().orElse(null)); |
| 185 | + assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6b", items.get(2).getGuid().orElse(null)); |
| 186 | + assertEquals("2003-12-13T09:28:28-04:00", items.get(2).getPubDate().orElse(null)); |
| 187 | + assertEquals(1071322108, items.get(2).getPubDateZonedDateTime().map(ZonedDateTime::toEpochSecond).orElse(null)); |
| 188 | + assertEquals("2003-12-13T18:30:01Z", items.get(2).getUpdated().orElse(null)); |
| 189 | + assertEquals(1071340201, items.get(2).getUpdatedZonedDateTime().map(ZonedDateTime::toEpochSecond).orElse(null)); |
| 190 | + assertEquals(47, items.get(2).getDescription().orElse("").length()); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private File getFile(String filename) { |
| 195 | + var url = getClass().getClassLoader().getResource(filename); |
| 196 | + return new File(url.getFile()); |
| 197 | + } |
| 198 | +} |
0 commit comments