Factor out torrent matchers.
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / filters / torrents / TorrentMatcher.java
1 package net.pterodactylus.rhynodge.filters.torrents;
2
3 import java.util.Objects;
4
5 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
6
7 import org.hamcrest.Description;
8 import org.hamcrest.Matcher;
9 import org.hamcrest.Matchers;
10 import org.hamcrest.TypeSafeDiagnosingMatcher;
11
12 /**
13  * {@link Matcher}s for {@link TorrentFile}s.
14  *
15  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
16  */
17 public class TorrentMatcher {
18
19         public static Matcher<TorrentFile> isTorrent(String name, String size, int seedCount, int leechCount) {
20                 return new TypeSafeDiagnosingMatcher<TorrentFile>() {
21                         @Override
22                         protected boolean matchesSafely(TorrentFile torrentFile, Description mismatchDescription) {
23                                 if (!torrentFile.name().equals(name)) {
24                                         mismatchDescription.appendText("name is ").appendValue(torrentFile.name());
25                                         return false;
26                                 }
27                                 if (!torrentFile.size().equals(size)) {
28                                         mismatchDescription.appendText("size is ").appendValue(torrentFile.size());
29                                         return false;
30                                 }
31                                 if (torrentFile.seedCount() != seedCount) {
32                                         mismatchDescription.appendText("seed count is ").appendValue(torrentFile.seedCount());
33                                         return false;
34                                 }
35                                 if (torrentFile.leechCount() != leechCount) {
36                                         mismatchDescription.appendText("leech count is ").appendValue(torrentFile.leechCount());
37                                         return false;
38                                 }
39                                 return true;
40                         }
41
42                         @Override
43                         public void describeTo(Description description) {
44                                 description.appendText("torrent named ").appendValue(name);
45                                 description.appendText(", size ").appendValue(size);
46                                 description.appendText(", seeds ").appendValue(seedCount);
47                                 description.appendText(", leechs ").appendValue(leechCount);
48                         }
49                 };
50         }
51
52         public static Matcher<TorrentFile> isTorrent(String name, String size, String magnetLink, int seedCount, int leechCount) {
53                 return Matchers.allOf(isTorrent(name, size, seedCount, leechCount), torrentWithMagnetLink(magnetLink));
54         }
55
56         private static Matcher<? super TorrentFile> torrentWithMagnetLink(String magnetLink) {
57                 return new TypeSafeDiagnosingMatcher<TorrentFile>() {
58                         @Override
59                         protected boolean matchesSafely(TorrentFile item, Description mismatchDescription) {
60                                 if (!Objects.equals(item.magnetUri(), magnetLink)) {
61                                         mismatchDescription.appendText("magnet URI is ").appendValue(item.magnetUri());
62                                         return false;
63                                 }
64                                 return true;
65                         }
66
67                         @Override
68                         public void describeTo(Description description) {
69                                 description.appendText("magnet URI is ").appendValue(magnetLink);
70                         }
71                 };
72         }
73
74 }