d1e9dc8d90133127680aba36ded8c9470e7a3ed3
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / filters / webpages / savoy / MovieExtractorTest.java
1 package net.pterodactylus.rhynodge.filters.webpages.savoy;
2
3 import static java.time.LocalDateTime.of;
4 import static java.util.Optional.empty;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.containsInAnyOrder;
7 import static org.hamcrest.Matchers.notNullValue;
8 import static org.jsoup.Jsoup.parse;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.time.LocalDateTime;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17
18 import org.hamcrest.Description;
19 import org.hamcrest.Matcher;
20 import org.hamcrest.TypeSafeDiagnosingMatcher;
21 import org.jsoup.nodes.Document;
22 import org.junit.Test;
23
24 /**
25  * Unit test for {@link MovieExtractor}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class MovieExtractorTest {
30
31         private final Document document;
32         private final MovieExtractor movieExtractor = new MovieExtractor();
33         private Collection<Movie> movies;
34
35         public MovieExtractorTest() throws IOException {
36                 document = loadDocument("savoy.html", "http://foo");
37                 movies = movieExtractor.getMovies(document);
38         }
39
40         private Document loadDocument(String resourceName, String baseUri) throws IOException {
41                 InputStream inputStream = getClass().getResourceAsStream(resourceName);
42                 return parse(inputStream, "UTF-8", baseUri);
43         }
44
45         @Test
46         public void moviesAreLocated() throws IOException {
47                 assertThat(movies, notNullValue());
48                 assertThat(movies, containsInAnyOrder(
49                                 movie("22 Jump Street (OV)", of(2014, 7, 30, 20, 15), of(2014, 7, 31, 20, 15), of(2014, 8, 1, 22, 30), of(2014, 8, 2, 20, 0), of(2014, 8, 2, 22, 45), of(2014, 8, 3, 17, 30), of(2014, 8, 4, 17, 30), of(2014, 8, 5, 20, 0)),
50                                 movie("How to Train Your Dragon 2 (3D/OV)", of(2014, 7, 30, 17, 45), of(2014, 8, 1, 14, 15), of(2014, 8, 2, 17, 30), of(2014, 8, 3, 15, 0), of(2014, 8, 4, 20, 0)),
51                                 movie("Jersey Boys (OV)", of(2014, 7, 31, 17, 15), of(2014, 8, 1, 16, 30), of(2014, 8, 3, 20, 0), of(2014, 8, 5, 17, 0)),
52                                 movie("FILM CLUB presents: Ghostbusters (OV)", of(2014, 8, 1, 19, 30)),
53                                 movie("Transformers: Age of Extinction (3D/OV)", of(2014, 8, 2, 14, 0)),
54                                 movie("Dawn of the Planet of the Apes (3D/OV)"),
55                                 movie("Traumkino: Yves Saint Laurent (Deutsche Fassung)"),
56                                 movie("Hector and the Search for Happiness (OV)"),
57                                 movie("The Expendables 3 (OV)"),
58                                 movie("Doctor Who: Deep Breath"),
59                                 movie("Traumkino: Boyhood (Deutsche Sprachfassung)")
60                 ));
61         }
62
63         private Matcher<Movie> movie(String name, LocalDateTime... presentationTimes) {
64                 return new TypeSafeDiagnosingMatcher<Movie>() {
65                         @Override
66                         protected boolean matchesSafely(Movie movie, Description mismatchDescription) {
67                                 if (!movie.getName().equals(name)) {
68                                         mismatchDescription.appendText("movie is named ").appendValue(movie.getName());
69                                         return false;
70                                 }
71                                 List<TicketLink> ticketLinks = new ArrayList<>(movie.getTicketLinks());
72                                 if (ticketLinks.size() != presentationTimes.length) {
73                                         mismatchDescription.appendText("has ").appendValue(ticketLinks.size()).appendText(" presentations");
74                                         return false;
75                                 }
76                                 for (LocalDateTime presentationTime : presentationTimes) {
77                                         Optional<TicketLink> foundTicketLink = empty();
78                                         for (TicketLink ticketLink : ticketLinks) {
79                                                 if (ticketLink.getPresentationTime().equals(presentationTime)) {
80                                                         foundTicketLink = Optional.of(ticketLink);
81                                                         break;
82                                                 }
83                                         }
84                                         if (!foundTicketLink.isPresent()) {
85                                                 mismatchDescription.appendValue("has no presentation at ").appendValue(presentationTime);
86                                                 return false;
87                                         }
88                                         ticketLinks.remove(foundTicketLink.get());
89                                 }
90                                 if (!ticketLinks.isEmpty()) {
91                                         mismatchDescription.appendText("has no presentations at ").appendValueList("", ", ", "", ticketLinks);
92                                         return false;
93                                 }
94                                 return true;
95                         }
96
97                         @Override
98                         public void describeTo(Description description) {
99                                 description.appendText("movie with name ").appendValue(name);
100                                 description.appendText(" and ").appendValue(presentationTimes.length).appendText(" presentations");
101                         }
102                 };
103         }
104
105 }