🔒️ Update a bunch of URLs to https
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / filters / comics / ScandinaviaAndTheWorldComicFilterTest.java
1 package net.pterodactylus.rhynodge.filters.comics;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.contains;
5
6 import java.io.IOException;
7 import java.util.Objects;
8
9 import net.pterodactylus.rhynodge.Filter;
10 import net.pterodactylus.rhynodge.filters.ResourceLoader;
11 import net.pterodactylus.rhynodge.states.ComicState;
12 import net.pterodactylus.rhynodge.states.ComicState.Comic;
13 import net.pterodactylus.rhynodge.states.ComicState.Strip;
14 import net.pterodactylus.rhynodge.states.HtmlState;
15
16 import org.hamcrest.Description;
17 import org.hamcrest.Matcher;
18 import org.hamcrest.TypeSafeDiagnosingMatcher;
19 import org.jsoup.nodes.Document;
20 import org.junit.Test;
21
22 /**
23  * Unit test for {@link ScandinaviaAndTheWorldComicFilterTest}.
24  *
25  * @author <a href="mailto:david.roden@bietr.de">David Roden</a>
26  */
27 public class ScandinaviaAndTheWorldComicFilterTest {
28
29         private final Filter satwFilter = new ScandinaviaAndTheWorldComicFilter();
30         private final HtmlState htmlState;
31
32         public ScandinaviaAndTheWorldComicFilterTest() throws IOException {
33                 Document document = ResourceLoader.loadDocument(ScandinaviaAndTheWorldComicFilter.class, "scandinavia-and-the-world.html",
34                                 "https://satwcomic.com/latest");
35                 htmlState = new HtmlState("https://satwcomic.com/latest", document);
36         }
37
38         @Test
39         public void comicIsParsedCorrectly() {
40                 ComicState comicState = (ComicState) satwFilter.filter(htmlState);
41                 assertThat(comicState.comics(), contains(
42                                 isComic("The whale in the room", contains(
43                                                 isStrip("http://satwcomic.com/art/the-whale-in-the-room.png", "")
44                                 ))
45                 ));
46         }
47
48         private Matcher<? super Comic> isComic(String title, Matcher<Iterable<? extends Strip>> stripsMatcher) {
49                 return new TypeSafeDiagnosingMatcher<Comic>() {
50                         @Override
51                         protected boolean matchesSafely(Comic comic, Description mismatchDescription) {
52                                 if (!Objects.equals(comic.title(), title)) {
53                                         mismatchDescription.appendText("title is ").appendValue(comic.title());
54                                         return false;
55                                 }
56                                 if (!stripsMatcher.matches(comic.strips())) {
57                                         stripsMatcher.describeMismatch(comic.strips(), mismatchDescription);
58                                         return false;
59                                 }
60                                 return true;
61                         }
62
63                         @Override
64                         public void describeTo(Description description) {
65                                 description.appendText("is comic with title ").appendValue(title);
66                                 description.appendText(" and strips ").appendValueList("(", ", ", ")", stripsMatcher);
67                         }
68                 };
69         }
70
71         private Matcher<? super Strip> isStrip(String url, String comment) {
72                 return new TypeSafeDiagnosingMatcher<Strip>() {
73                         @Override
74                         protected boolean matchesSafely(Strip strip, Description mismatchDescription) {
75                                 if (!Objects.equals(strip.imageUrl(), url)) {
76                                         mismatchDescription.appendText("image URL is ").appendValue(strip.imageUrl());
77                                         return false;
78                                 }
79                                 if (!Objects.equals(strip.comment(), comment)) {
80                                         mismatchDescription.appendText("comment is ").appendValue(strip.comment());
81                                         return false;
82                                 }
83                                 return true;
84                         }
85
86                         @Override
87                         public void describeTo(Description description) {
88                                 description.appendText("is strip from ").appendValue(url);
89                                 description.appendText(" with comment ").appendValue(comment);
90                         }
91                 };
92         }
93
94 }