Skip to content

Commit

Permalink
primitives to objects for null safety. started validation
Browse files Browse the repository at this point in the history
  • Loading branch information
c-eg committed Dec 22, 2023
1 parent f447e4d commit a09fd6f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
18 changes: 17 additions & 1 deletion src/main/java/info/movito/themoviedbapi/TmdbChanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import info.movito.themoviedbapi.tools.ApiEndpoint;
import info.movito.themoviedbapi.tools.TmdbException;

import static info.movito.themoviedbapi.util.Utils.calculateDaysDifference;

/**
* The movie database api for changes. See the
* <a href="https://developer.themoviedb.org/reference/changes-movie-list">documentation</a> for more info.
Expand All @@ -25,14 +27,20 @@ public class TmdbChanges extends AbstractTmdbApi {
}

/**
* Get a list of all the movie ids that have been changed in the past 24 hours.
* <p>Get a list of all the movie ids that have been changed in the past 24 hours.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/changes-movie-list">documentation</a> for more info.</p>
*
* @param startDate the start date, in format: YYYY-MM-DD.
* @param endDate the end date, in format: YYYY-MM-DD.
* @param page the page.
* @return the changes results page.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public ChangesResultsPage getMovieChangesList(String startDate, String endDate, Integer page) throws TmdbException {
if (calculateDaysDifference(startDate, endDate) > 14) {
throw new IllegalArgumentException("The date range must be less than or equal to 14 days.");
}

ApiEndpoint apiEndpoint = new ApiEndpoint(TMDB_METHOD_MOVIE, TMDB_METHOD_CHANGES);
apiEndpoint.addQueryParam("start_date", startDate);
apiEndpoint.addQueryParam("end_date", endDate);
Expand All @@ -51,6 +59,10 @@ public ChangesResultsPage getMovieChangesList(String startDate, String endDate,
* @return the changes results page.
*/
public ChangesResultsPage getPeopleChangesList(String startDate, String endDate, Integer page) throws TmdbException {
if (calculateDaysDifference(startDate, endDate) > 14) {
throw new IllegalArgumentException("The date range must be less than or equal to 14 days.");
}

ApiEndpoint apiEndpoint = new ApiEndpoint(TMDB_METHOD_PERSON, TMDB_METHOD_CHANGES);
apiEndpoint.addQueryParam("start_date", startDate);
apiEndpoint.addQueryParam("end_date", endDate);
Expand All @@ -69,6 +81,10 @@ public ChangesResultsPage getPeopleChangesList(String startDate, String endDate,
* @return the changes results page.
*/
public ChangesResultsPage getTvChangesList(String startDate, String endDate, Integer page) throws TmdbException {
if (calculateDaysDifference(startDate, endDate) > 14) {
throw new IllegalArgumentException("The date range must be less than or equal to 14 days.");
}

ApiEndpoint apiEndpoint = new ApiEndpoint(TMDB_METHOD_TV, TMDB_METHOD_CHANGES);
apiEndpoint.addQueryParam("start_date", startDate);
apiEndpoint.addQueryParam("end_date", endDate);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/info/movito/themoviedbapi/TmdbGenre.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public List<Genre> getTvGenreList(String language) throws TmdbException {
*
* This prevents movies from 1 10/10 rating from being listed first and for the first 5 pages.
*/
public MovieDbResultsPage getGenreMovies(int genreId, String language, Integer page, boolean includeAllMovies) throws TmdbException {
public MovieDbResultsPage getGenreMovies(Integer genreId, String language, Integer page, Boolean includeAllMovies) throws TmdbException {
ApiEndpoint apiEndpoint = new ApiEndpoint(TMDB_METHOD_GENRE, genreId, "movies");

apiEndpoint.addLanguage(language);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/info/movito/themoviedbapi/TmdbSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public CollectionResultsPage searchCollection(String query, String language, boo
*
* The idea is to be a quick and light method so you can iterate through people quickly.
*/

public TmdbPeople.PersonResultsPage searchPerson(String query, boolean includeAdult, Integer page) throws TmdbException {
public TmdbPeople.PersonResultsPage searchPerson(String query, Boolean includeAdult, Integer page) throws TmdbException {
ApiEndpoint apiEndpoint = new ApiEndpoint(TMDB_METHOD_SEARCH, TmdbPeople.TMDB_METHOD_PERSON);

apiEndpoint.addPathParam(PARAM_QUERY, query);
Expand Down
28 changes: 7 additions & 21 deletions src/main/java/info/movito/themoviedbapi/tools/ApiEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public ApiEndpoint(Object... urlElements) {
StringBuilder baseUrlBuilder = new StringBuilder();

for (int i = 0; i < urlElements.length; i++) {
baseUrlBuilder.append(urlElements[i]);
Object object = urlElements[i];

if (object == null) {
throw new IllegalArgumentException("url element can not be null");
}

baseUrlBuilder.append(object);

if (i < urlElements.length - 1) {
baseUrlBuilder.append("/");
Expand Down Expand Up @@ -102,26 +108,6 @@ public void addPathParam(String name, String value) {
params.put(name, value);
}

/**
* Adds a parameter to the api endpoint (e.g. "movies/list").
*
* @param key the key
* @param value the value
*/
public void addPathParam(String key, int value) {
addPathParam(key, Integer.toString(value));
}

/**
* Adds a parameter to the api endpoint (e.g. "movies/list").
*
* @param key the key
* @param value the value
*/
public void addPathParam(String key, boolean value) {
addPathParam(key, Boolean.toString(value));
}

/**
* Adds an optional parameter to the api endpoint (e.g. "movies/list").
*
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/info/movito/themoviedbapi/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Map;

/**
Expand Down Expand Up @@ -137,4 +140,19 @@ public static String[] asStringArray(Object[] appendToResponse) {

return asArray;
}

/**
* Calculate the difference in days between two date strings.
*
* @param startDateString the start date string, in format: YYYY-MM-DD.
* @param endDateString the end date string, in format: YYYY-MM-DD.
* @return the difference in days.
*/
public static long calculateDaysDifference(String startDateString, String endDateString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(startDateString, formatter);
LocalDate endDate = LocalDate.parse(endDateString, formatter);

return ChronoUnit.DAYS.between(startDate, endDate);
}
}
14 changes: 14 additions & 0 deletions src/test/java/info/movito/themoviedbapi/AbstractTmdbApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,21 @@ protected void mockResponse(String body, int responseCode) {
* Tests the given object for null fields and unknown properties.
*/
public void testForNullFieldsAndUnknownProperties(AbstractJsonMapping objectToCheck) {
testForNullFields(objectToCheck);
testForUnknownProperties(objectToCheck);
}

/**
* Tests the given object for null fields.
*/
public void testForNullFields(AbstractJsonMapping objectToCheck) {
assertTrue(getNullFields(objectToCheck).isEmpty(), "Null fields found in object: " + objectToCheck);
}

/**
* Tests the given object for unknown properties.
*/
public void testForUnknownProperties(AbstractJsonMapping objectToCheck) {
assertTrue(objectToCheck.getUnknownProperties().isEmpty(), "Unknown properties found in object: " + objectToCheck);
}

Expand Down

0 comments on commit a09fd6f

Please sign in to comment.