4720d9b37e3c6e0e947b3c7f7619c668270c259b
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / filters / torrents / TorrentzEuFilterTest.java
1 package net.pterodactylus.rhynodge.filters.torrents;
2
3 import java.io.IOException;
4
5 import net.pterodactylus.rhynodge.State;
6 import net.pterodactylus.rhynodge.states.HtmlState;
7 import net.pterodactylus.rhynodge.states.TorrentState;
8 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
9
10 import org.hamcrest.Description;
11 import org.hamcrest.Matcher;
12 import org.hamcrest.MatcherAssert;
13 import org.hamcrest.Matchers;
14 import org.hamcrest.TypeSafeDiagnosingMatcher;
15 import org.jsoup.Jsoup;
16 import org.jsoup.nodes.Document;
17 import org.junit.Test;
18
19 /**
20  * Unit test for {@link TorrentzEuFilter}.
21  *
22  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23  */
24 public class TorrentzEuFilterTest {
25
26         private final TorrentzEuFilter torrentzEuFilter = new TorrentzEuFilter();
27
28         @Test
29         public void filterCanExtractAllTorrents() throws IOException {
30                 Document document = Jsoup.parse(getClass().getResourceAsStream("torrentz-eu-results.html"), "UTF-8", "http://torrentz.eu/searchA?f=Ubuntu+ISO");
31                 HtmlState htmlState = new HtmlState("http://torrentz.eu/searchA?f=Ubuntu+ISO", document);
32                 State newState = torrentzEuFilter.filter(htmlState);
33                 MatcherAssert.assertThat(newState, Matchers.notNullValue());
34                 MatcherAssert.assertThat(newState, Matchers.instanceOf(TorrentState.class));
35                 TorrentState torrentState = (TorrentState) newState;
36                 MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.hasSize(2));
37                 MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.contains(
38                                 isTorrent("Ubuntu 14 04 64Bit ISO File", "981 MB", 0, 1),
39                                 isTorrent("Ubuntu 14 04 LTS Desktop 64bit ISO", "964 MB", 75, 5)
40                 ));
41         }
42
43         private Matcher<TorrentFile> isTorrent(String name, String size, int seedCount, int leechCount) {
44                 return new TypeSafeDiagnosingMatcher<TorrentFile>() {
45                         @Override
46                         protected boolean matchesSafely(TorrentFile torrentFile, Description mismatchDescription) {
47                                 if (!torrentFile.name().equals(name)) {
48                                         mismatchDescription.appendText("name is ").appendValue(torrentFile.name());
49                                         return false;
50                                 }
51                                 if (!torrentFile.size().equals(size)) {
52                                         mismatchDescription.appendText("size is ").appendValue(torrentFile.size());
53                                         return false;
54                                 }
55                                 if (torrentFile.seedCount() != seedCount) {
56                                         mismatchDescription.appendText("seed count is ").appendValue(torrentFile.seedCount());
57                                         return false;
58                                 }
59                                 if (torrentFile.leechCount() != leechCount) {
60                                         mismatchDescription.appendText("leech count is ").appendValue(torrentFile.leechCount());
61                                         return false;
62                                 }
63                                 return true;
64                         }
65
66                         @Override
67                         public void describeTo(Description description) {
68                                 description.appendText("torrent named ").appendValue(name);
69                                 description.appendText(", size ").appendValue(size);
70                                 description.appendText(", seeds ").appendValue(seedCount);
71                                 description.appendText(", leechs ").appendValue(leechCount);
72                         }
73                 };
74         }
75
76 }