Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Movie lists api #175

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/info/movito/themoviedbapi/TmdbApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public TmdbLists getLists() {
return new TmdbLists(this);
}

public TmdbMovieLists getMovieLists() {
return new TmdbMovieLists(this);
}

public TmdbMovies getMovies() {
return new TmdbMovies(this);
}
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/info/movito/themoviedbapi/TmdbMovieLists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.core.MovieResultsPage;
import info.movito.themoviedbapi.model.movielists.MovieResultsPageWithDates;
import info.movito.themoviedbapi.tools.ApiUrl;
import info.movito.themoviedbapi.tools.TmdbException;

/**
* The movie database api for movie lists. See the
* <a href="https://developer.themoviedb.org/reference/movie-now-playing-list">documentation</a> for more info.
*/
public class TmdbMovieLists extends AbstractTmdbApi {
protected static final String TMDB_METHOD_MOVIE_LISTS = "movie";

/**
* Create a new TmdbMovieLists instance to call the movie lists related TMDb API methods.
*/
TmdbMovieLists(TmdbApi tmdbApi) {
super(tmdbApi);
}

/**
* <p>Get a list of movies that are currently in theatres.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/movie-now-playing-list">documentation</a> for more info.</p>
*
* @param language optional - The language to display the results in. e.g. "en-US".
* @param page optional - The page of results to return.
* @param region optional - The region (ISO-3166-1 code) to display the results for. e.g. "US".
* @return The now playing movies.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public MovieResultsPageWithDates getNowPlaying(String language, Integer page, String region) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE_LISTS, "now_playing");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addQueryParam("region", region);
return mapJsonResult(apiUrl, MovieResultsPageWithDates.class);
}

/**
* <p>Get a list of movies ordered by popularity.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/movie-popular-list">documentation</a> for more info.</p>
*
* @param language optional - The language to display the results in. e.g. "en-US".
* @param page optional - The page of results to return.
* @param region optional - The region (ISO-3166-1 code) to display the results for. e.g. "US".
* @return The popular movies.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public MovieResultsPage getPopular(String language, Integer page, String region) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE_LISTS, "popular");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addQueryParam("region", region);
return mapJsonResult(apiUrl, MovieResultsPage.class);
}

/**
* <p>Get a list of movies ordered by rating.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/movie-top-rated-list">documentation</a> for more info.</p>
*
* @param language optional - The language to display the results in. e.g. "en-US".
* @param page optional - The page of results to return.
* @param region optional - The region (ISO-3166-1 code) to display the results for. e.g. "US".
* @return The top-rated movies.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public MovieResultsPage getTopRated(String language, Integer page, String region) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE_LISTS, "top_rated");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addQueryParam("region", region);
return mapJsonResult(apiUrl, MovieResultsPage.class);
}

/**
* <p>Get a list of movies that are being released soon.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/movie-upcoming-list">documentation</a> for more info.</p>
*
* @param language optional - The language to display the results in. e.g. "en-US".
* @param page optional - The page of results to return.
* @param region optional - The region (ISO-3166-1 code) to display the results for. e.g. "US".
* @return The upcoming movies.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public MovieResultsPageWithDates getUpcoming(String language, Integer page, String region) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE_LISTS, "upcoming");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addQueryParam("region", region);
return mapJsonResult(apiUrl, MovieResultsPageWithDates.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class TmdbPeopleLists extends AbstractTmdbApi {
* @param language optional - The language to display the results in. e.g. "en-US".
* @param page optional - The page of results to return.
* @return The popular people.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public PopularPersonResults getPopular(String language, Integer page) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_PEOPLE_LISTS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package info.movito.themoviedbapi.model.movielists;

import com.fasterxml.jackson.annotation.JsonProperty;
import info.movito.themoviedbapi.model.core.AbstractJsonMapping;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class Dates extends AbstractJsonMapping {
@JsonProperty("maximum")
private String maximum;

@JsonProperty("minimum")
private String minimum;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package info.movito.themoviedbapi.model.movielists;

import com.fasterxml.jackson.annotation.JsonProperty;
import info.movito.themoviedbapi.model.core.Movie;
import info.movito.themoviedbapi.model.core.ResultsPage;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class MovieResultsPageWithDates extends ResultsPage<Movie> {
@JsonProperty("dates")
private Dates dates;
}
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
opens info.movito.themoviedbapi.model.core.image to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.core.responses to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.keywords to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.movielists to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.movies to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.movies.changes to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.networks to com.fasterxml.jackson.databind;
Expand All @@ -48,6 +49,7 @@
exports info.movito.themoviedbapi.model.core.image;
exports info.movito.themoviedbapi.model.core.responses;
exports info.movito.themoviedbapi.model.keywords;
exports info.movito.themoviedbapi.model.movielists;
exports info.movito.themoviedbapi.model.movies;
exports info.movito.themoviedbapi.model.movies.changes;
exports info.movito.themoviedbapi.model.networks;
Expand Down
146 changes: 146 additions & 0 deletions src/test/java/info/movito/themoviedbapi/TmdbMovieListsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.core.Movie;
import info.movito.themoviedbapi.model.core.MovieResultsPage;
import info.movito.themoviedbapi.model.movielists.Dates;
import info.movito.themoviedbapi.model.movielists.MovieResultsPageWithDates;
import info.movito.themoviedbapi.tools.RequestType;
import info.movito.themoviedbapi.tools.TmdbException;
import info.movito.themoviedbapi.util.TestUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import static info.movito.themoviedbapi.TmdbMovieLists.TMDB_METHOD_MOVIE_LISTS;
import static info.movito.themoviedbapi.tools.ApiUrl.TMDB_API_BASE_URL;
import static info.movito.themoviedbapi.util.TestUtils.testForNullFieldsAndNewItems;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

/**
* Tests for {@link TmdbMovieLists}.
*/
public class TmdbMovieListsTest extends AbstractTmdbApiTest {
/**
* Test {@link TmdbMovieLists#getNowPlaying(String, Integer, String)} with an expected result.
*/
@Test
public void testGetNowPlaying() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/movie_lists/now_playing.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_MOVIE_LISTS + "/now_playing?language=en-US&page=1");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbMovieLists tmdbMovieLists = getTmdbApi().getMovieLists();
MovieResultsPageWithDates movieResultsPageWithDates = tmdbMovieLists.getNowPlaying("en-US", 1, null);
assertNotNull(movieResultsPageWithDates);
testForNullFieldsAndNewItems(movieResultsPageWithDates);

Dates dates = movieResultsPageWithDates.getDates();
assertNotNull(dates);
testForNullFieldsAndNewItems(dates);

List<Movie> movies = movieResultsPageWithDates.getResults();
assertNotNull(movies);
assertFalse(movies.isEmpty());

Movie movie = movies.get(0);
assertNotNull(movie);
testForNullFieldsAndNewItems(movie);

List<Integer> genreIds = movie.getGenreIds();
assertNotNull(genreIds);
assertFalse(genreIds.isEmpty());
assertNotNull(genreIds.get(0));
}

/**
* Test {@link TmdbMovieLists#getPopular(String, Integer, String)} with an expected result.
*/
@Test
public void testGetPopular() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/movie_lists/popular.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_MOVIE_LISTS + "/popular?language=en-US&page=1");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbMovieLists tmdbMovieLists = getTmdbApi().getMovieLists();
MovieResultsPage movieResultsPage = tmdbMovieLists.getPopular("en-US", 1, null);
assertNotNull(movieResultsPage);
testForNullFieldsAndNewItems(movieResultsPage);

List<Movie> movies = movieResultsPage.getResults();
assertNotNull(movies);
assertFalse(movies.isEmpty());

Movie movie = movies.get(0);
assertNotNull(movie);
testForNullFieldsAndNewItems(movie);

List<Integer> genreIds = movie.getGenreIds();
assertNotNull(genreIds);
assertFalse(genreIds.isEmpty());
assertNotNull(genreIds.get(0));
}

/**
* Test {@link TmdbMovieLists#getTopRated(String, Integer, String)} with an expected result.
*/
@Test
public void testGetTopRated() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/movie_lists/top_rated.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_MOVIE_LISTS + "/top_rated?language=en-US&page=1");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbMovieLists tmdbMovieLists = getTmdbApi().getMovieLists();
MovieResultsPage movieResultsPage = tmdbMovieLists.getTopRated("en-US", 1, null);
assertNotNull(movieResultsPage);
testForNullFieldsAndNewItems(movieResultsPage);

List<Movie> movies = movieResultsPage.getResults();
assertNotNull(movies);
assertFalse(movies.isEmpty());

Movie movie = movies.get(0);
assertNotNull(movie);
testForNullFieldsAndNewItems(movie);

List<Integer> genreIds = movie.getGenreIds();
assertNotNull(genreIds);
assertFalse(genreIds.isEmpty());
assertNotNull(genreIds.get(0));
}

/**
* Test {@link TmdbMovieLists#getUpcoming(String, Integer, String)} with an expected result.
*/
@Test
public void testGetUpcoming() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/movie_lists/upcoming.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_MOVIE_LISTS + "/upcoming?language=en-US&page=1");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbMovieLists tmdbMovieLists = getTmdbApi().getMovieLists();
MovieResultsPageWithDates movieResultsPageWithDates = tmdbMovieLists.getUpcoming("en-US", 1, null);
assertNotNull(movieResultsPageWithDates);
testForNullFieldsAndNewItems(movieResultsPageWithDates);

Dates dates = movieResultsPageWithDates.getDates();
assertNotNull(dates);
testForNullFieldsAndNewItems(dates);

List<Movie> movies = movieResultsPageWithDates.getResults();
assertNotNull(movies);
assertFalse(movies.isEmpty());

Movie movie = movies.get(0);
assertNotNull(movie);
testForNullFieldsAndNewItems(movie);

List<Integer> genreIds = movie.getGenreIds();
assertNotNull(genreIds);
assertFalse(genreIds.isEmpty());
assertNotNull(genreIds.get(0));
}
}
Loading
Loading