Final account tests

master
Stuart Boston 11 years ago
parent 9ea18f546d
commit e9ef62d031

@ -384,8 +384,8 @@ public class TheMovieDbApi {
* @return
* @throws MovieDbException
*/
public StatusCode addToWatchList(String sessionId, int accountId, Integer mediaId, MediaType mediaType) throws MovieDbException {
return tmdbAccount.modifyWatchList(sessionId, accountId, mediaId, mediaType, true);
public StatusCode addToWatchList(String sessionId, int accountId, MediaType mediaType, Integer mediaId) throws MovieDbException {
return tmdbAccount.modifyWatchList(sessionId, accountId, mediaType, mediaId, true);
}
/**
@ -398,8 +398,8 @@ public class TheMovieDbApi {
* @return
* @throws MovieDbException
*/
public StatusCode removeFromWatchList(String sessionId, int accountId, Integer mediaId, MediaType mediaType) throws MovieDbException {
return tmdbAccount.modifyWatchList(sessionId, accountId, mediaId, mediaType, false);
public StatusCode removeFromWatchList(String sessionId, int accountId, MediaType mediaType, Integer mediaId) throws MovieDbException {
return tmdbAccount.modifyWatchList(sessionId, accountId, mediaType, mediaId, false);
}
/**

@ -22,6 +22,9 @@ package com.omertron.themoviedbapi.methods;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.list.MovieFavorite;
import com.omertron.themoviedbapi.model.list.TVFavorite;
import com.omertron.themoviedbapi.model.list.UserList;
import com.omertron.themoviedbapi.tools.HttpTools;
import com.omertron.themoviedbapi.wrapper.WrapperGenericList;
import java.io.IOException;
@ -46,6 +49,12 @@ public class AbstractMethod {
protected static final ObjectMapper MAPPER = new ObjectMapper();
// Logger
protected static final Logger LOG = LoggerFactory.getLogger(TmdbAccount.class);
protected static final TypeReference<WrapperGenericList<MovieFavorite>> TR_MOVIE_FAV = new TypeReference<WrapperGenericList<MovieFavorite>>() {
};
protected static final TypeReference<WrapperGenericList<TVFavorite>> TR_TV_FAV = new TypeReference<WrapperGenericList<TVFavorite>>() {
};
protected static final TypeReference<WrapperGenericList<UserList>> TR_USER_LIST = new TypeReference<WrapperGenericList<UserList>>() {
};
/**
* Default constructor for the methods
@ -62,22 +71,23 @@ public class AbstractMethod {
* Process the wrapper list and return the results
*
* @param <T> Type of list to process
* @param typeRef
* @param url URL of the page (Error output only)
* @param errorMessageSuffix Error message to output (Error output only)
* @return
* @throws MovieDbException
*/
public <T> List<T> processWrapperList(URL url, String errorMessageSuffix) throws MovieDbException {
public <T> List<T> processWrapperList(TypeReference typeRef, URL url, String errorMessageSuffix) throws MovieDbException {
String webpage = httpTools.getRequest(url);
try {
TypeReference<WrapperGenericList<T>> ref = new TypeReference<WrapperGenericList<T>>() {
};
WrapperGenericList<T> results = MAPPER.readValue(webpage, ref);
// Due to type erasure, this doesn't work
// TypeReference<WrapperGenericList<T>> typeRef = new TypeReference<WrapperGenericList<T>>() {};
WrapperGenericList<T> results = MAPPER.readValue(webpage, typeRef);
return results.getResults();
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get " + errorMessageSuffix, url, ex);
}
}
}

@ -19,7 +19,6 @@
*/
package com.omertron.themoviedbapi.methods;
import com.fasterxml.jackson.core.type.TypeReference;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.enumeration.MediaType;
import com.omertron.themoviedbapi.model.Account;
@ -97,7 +96,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.ID, accountId);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.LISTS).buildUrl(parameters);
return processWrapperList(url, "user list");
return processWrapperList(TR_USER_LIST, url, "user list");
}
/**
@ -114,7 +113,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.ID, accountId);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE_MOVIES).buildUrl(parameters);
return processWrapperList(url, "favorite movies");
return processWrapperList(TR_MOVIE_FAV, url, "favorite movies");
}
/**
@ -131,7 +130,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.ID, accountId);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE_TV).buildUrl(parameters);
return processWrapperList(url, "favorite TV shows");
return processWrapperList(TR_TV_FAV, url, "favorite TV shows");
}
/**
@ -186,9 +185,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.RATED_MOVIES).buildUrl(parameters);
TypeReference<MovieFavorite> ref = new TypeReference<MovieFavorite>() {
};
return processWrapperList(url, "rated movies");
return processWrapperList(TR_MOVIE_FAV, url, "rated movies");
}
/**
@ -211,7 +208,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.RATED_TV).buildUrl(parameters);
return processWrapperList(url, "rated TV shows");
return processWrapperList(TR_TV_FAV, url, "rated TV shows");
}
/**
@ -234,7 +231,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST_MOVIES).buildUrl(parameters);
return processWrapperList(url, "movie watch list");
return processWrapperList(TR_MOVIE_FAV, url, "movie watch list");
}
/**
@ -257,7 +254,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST_TV).buildUrl(parameters);
return processWrapperList(url, "TV watch list");
return processWrapperList(TR_TV_FAV, url, "TV watch list");
}
/**
@ -271,7 +268,7 @@ public class TmdbAccount extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public StatusCode modifyWatchList(String sessionId, int accountId, Integer movieId, MediaType mediaType, boolean addToWatchlist) throws MovieDbException {
public StatusCode modifyWatchList(String sessionId, int accountId, MediaType mediaType, Integer movieId, boolean addToWatchlist) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.SESSION, sessionId);
parameters.add(Param.ID, accountId);

@ -20,7 +20,9 @@
package com.omertron.themoviedbapi.wrapper;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.Serializable;
import java.util.List;
@ -32,6 +34,9 @@ import java.util.List;
*/
public class WrapperGenericList<T> extends AbstractWrapperAll implements Serializable {
@JsonIgnore
private final TypeReference typeRef = new TypeReference<WrapperGenericList<T>>() {
};
@JsonProperty("results")
private List<T> results;
@ -44,4 +49,7 @@ public class WrapperGenericList<T> extends AbstractWrapperAll implements Seriali
this.results = results;
}
public TypeReference getTypeRef() {
return typeRef;
}
}

@ -1,230 +0,0 @@
/*
* Copyright (c) 2004-2015 Stuart Boston
*
* This file is part of TheMovieDB API.
*
* TheMovieDB API is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* TheMovieDB API is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TheMovieDB API. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi;
import com.omertron.themoviedbapi.enumeration.MediaType;
import com.omertron.themoviedbapi.model.Account;
import com.omertron.themoviedbapi.model.MovieDb;
import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model.StatusCode;
import com.omertron.themoviedbapi.model.TokenAuthorisation;
import com.omertron.themoviedbapi.model.TokenSession;
import com.omertron.themoviedbapi.model.list.MovieFavorite;
import java.util.List;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class TestAccounts extends AbstractTests {
private static TheMovieDbApi tmdb;
private static TokenSession tokenSession = null;
private static Account account = null;
public TestAccounts() throws MovieDbException {
}
@BeforeClass
public static void setUpClass() throws MovieDbException {
doConfiguration();
tmdb = new TheMovieDbApi(getApiKey());
}
@Before
public void setUp() throws MovieDbException {
if (tokenSession == null) {
testSessionCreation();
}
if (account == null) {
testAccount();
}
}
/**
* Test and create a session token for the rest of the tests
*
* @throws MovieDbException
*/
@Test
public void testSessionCreation() throws MovieDbException {
LOG.info("Test and create a session token for the rest of the tests");
// 1: Create a request token
TokenAuthorisation token = tmdb.getAuthorisationToken();
assertFalse("Token (auth) is null", token == null);
assertTrue("Token (auth) is not valid", token.getSuccess());
LOG.info("Token (auth): {}", token.toString());
// 2b; Get user permission
token = tmdb.getSessionTokenLogin(token, getUsername(), getPassword());
assertFalse("Token (login) is null", token == null);
assertTrue("Token (login) is not valid", token.getSuccess());
LOG.info("Token (login): {}", token.toString());
// 3: Create the sessions ID
tokenSession = tmdb.getSessionToken(token);
assertFalse("Session token is null", tokenSession == null);
assertTrue("Session token is not valid", tokenSession.getSuccess());
LOG.info("Session token: {}", tokenSession.toString());
}
/**
* Test the account information
*
* @throws MovieDbException
*/
@Test
public void testAccount() throws MovieDbException {
LOG.info("Using Session ID '{}' for test", tokenSession.getSessionId());
account = tmdb.getAccount(tokenSession.getSessionId());
LOG.info("Account: {}", account);
// Make sure properties are extracted correctly
assertEquals("Wrong username!", getUsername(), account.getUserName());
}
@Test
public void testWatchList() throws MovieDbException {
// If the static account is null, get it
if (account == null) {
account = tmdb.getAccount(tokenSession.getSessionId());
}
// make sure it's empty (because it's just a test account
assertTrue(tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()).isEmpty());
// add a movie
tmdb.addToWatchList(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE);
List<MovieDb> watchList = tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId());
assertNotNull("Empty watch list returned", watchList);
assertEquals("Watchlist wrong size", 1, watchList.size());
// clean up again
tmdb.removeFromWatchList(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE);
assertTrue(tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()).isEmpty());
}
@Test
public void testFavorites() throws MovieDbException {
// If the static account is null, get it
if (account == null) {
account = tmdb.getAccount(tokenSession.getSessionId());
}
// make sure it's empty (because it's just a test account
assertTrue(tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()).isEmpty());
// add a movie
tmdb.modifyFavoriteStatus(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE, true);
List<MovieFavorite> watchList = tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId());
assertNotNull("Empty watch list returned", watchList);
assertEquals("Watchlist wrong size", 1, watchList.size());
// clean up again
tmdb.modifyFavoriteStatus(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE, false);
assertTrue(tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()).isEmpty());
}
/**
* Test of getSessionToken method, of class TheMovieDbApi.
*
* TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication
*
* @throws MovieDbException
*/
@Ignore("Tested in setup")
public void testGetSessionToken() throws MovieDbException {
}
/**
* Test of getGuestSessionToken method, of class TheMovieDbApi.
*
* @throws MovieDbException
*/
@Test
public void testGetGuestSessionToken() throws MovieDbException {
LOG.info("getGuestSessionToken");
TokenSession result = tmdb.getGuestSessionToken();
assertTrue("Failed to get guest session", result.getSuccess());
}
/**
* Test of postMovieRating method, of class TheMovieDbApi.
*
* TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication
*
* @throws MovieDbException
*/
@Test
public void testMovieRating() throws MovieDbException {
LOG.info("postMovieRating");
Integer movieID = 68724;
Integer rating = new Random().nextInt(10) + 1;
boolean wasPosted = tmdb.postMovieRating(tokenSession.getSessionId(), movieID, rating);
assertTrue("Rating was not added", wasPosted);
// get all rated movies
List<MovieDb> ratedMovies = tmdb.getRatedMovies(tokenSession.getSessionId(), account.getId(), null, null, null);
assertTrue("No rated movies", ratedMovies.size() > 0);
// We should check that the movie was correctly rated, but the CDN does not update fast enough.
}
@Test
public void testMovieLists() throws MovieDbException {
Integer movieID = 68724;
// use a random name to avoid that we clash we leftovers of incomplete test runs
String name = "test list " + new Random().nextInt(100);
// create the list
String listId = tmdb.createList(tokenSession.getSessionId(), name, "api testing only");
// add a movie, and test that it is on the list now
tmdb.addMovieToList(tokenSession.getSessionId(), listId, movieID);
MovieDbList list = tmdb.getList(listId);
assertNotNull("Movie list returned was null", list);
assertEquals("Unexpected number of items returned", 1, list.getItemCount());
assertEquals((int) movieID, list.getItems().get(0).getId());
// now remove the movie
tmdb.removeMovieFromList(tokenSession.getSessionId(), listId, movieID);
assertEquals(tmdb.getList(listId).getItemCount(), 0);
// delete the test list
StatusCode statusCode = tmdb.deleteMovieList(tokenSession.getSessionId(), listId);
assertEquals(statusCode.getStatusCode(), 13);
}
}

@ -34,7 +34,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -76,7 +75,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetAccount() throws MovieDbException {
LOG.info("getAccount");
Account result = instance.getAccount(getSessionId());
@ -90,7 +89,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetUserLists() throws MovieDbException {
LOG.info("getUserLists");
List<UserList> results = instance.getUserLists(getSessionId(), getAccountId());
@ -106,7 +105,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetFavoriteMovies() throws MovieDbException {
LOG.info("getFavoriteMovies");
List<MovieFavorite> results = instance.getFavoriteMovies(getSessionId(), getAccountId());
@ -122,7 +121,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetFavoriteTv() throws MovieDbException {
LOG.info("getFavoriteTv");
List<TVFavorite> results = instance.getFavoriteTv(getSessionId(), getAccountId());
@ -138,7 +137,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testModifyFavoriteStatus() throws MovieDbException {
LOG.info("modifyFavoriteStatus");
@ -161,7 +160,6 @@ public class TmdbAccountTest extends AbstractTests {
result = instance.modifyFavoriteStatus(getSessionId(), getAccountId(), MediaType.TV, ID_TV_WALKING_DEAD, false);
LOG.info("Result: {}", result);
assertTrue("Incorrect status code", result.getStatusCode() == 13);
}
/**
@ -175,11 +173,6 @@ public class TmdbAccountTest extends AbstractTests {
List<MovieFavorite> results = instance.getRatedMovies(getSessionId(), getAccountId(), null, null, null);
assertNotNull("No rated list found", results);
assertFalse("No entries found", results.isEmpty());
for(MovieFavorite r: results){
LOG.info(" {}-{}", r.getClass(),r.toString());
}
}
/**
@ -187,7 +180,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetRatedTV() throws MovieDbException {
LOG.info("getRatedTV");
List<TVFavorite> results = instance.getRatedTV(getSessionId(), getAccountId(), null, null, null);
@ -203,7 +196,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetWatchListMovie() throws MovieDbException {
LOG.info("getWatchListMovie");
List<MovieFavorite> results = instance.getWatchListMovie(getSessionId(), getAccountId(), null, null, null);
@ -219,7 +212,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testGetWatchListTV() throws MovieDbException {
LOG.info("getWatchListTV");
List<TVFavorite> results = instance.getWatchListTV(getSessionId(), getAccountId(), null, null, null);
@ -235,16 +228,28 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
// @Test
@Test
public void testModifyWatchList() throws MovieDbException {
LOG.info("modifyWatchList");
Integer movieId = null;
MediaType mediaType = null;
boolean addToWatchlist = false;
StatusCode expResult = null;
StatusCode result = instance.modifyWatchList(getSessionId(), getAccountId(), movieId, mediaType, addToWatchlist);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
// Add a movie to the watch list
StatusCode result = instance.modifyWatchList(getSessionId(), getAccountId(), MediaType.MOVIE, ID_MOVIE_FIGHT_CLUB, true);
LOG.info("Result: {}", result);
assertTrue("Incorrect status code", result.getStatusCode() == 1 || result.getStatusCode() == 12);
// Remove a movie from the watch list
result = instance.modifyWatchList(getSessionId(), getAccountId(), MediaType.MOVIE, ID_MOVIE_FIGHT_CLUB, false);
LOG.info("Result: {}", result);
assertTrue("Incorrect status code", result.getStatusCode() == 13);
// Add a TV Show to the watch list
result = instance.modifyWatchList(getSessionId(), getAccountId(), MediaType.TV, ID_TV_WALKING_DEAD, true);
LOG.info("Result: {}", result);
assertTrue("Incorrect status code", result.getStatusCode() == 1 || result.getStatusCode() == 12);
// Remove a TV Show from the watch list
result = instance.modifyWatchList(getSessionId(), getAccountId(), MediaType.TV, ID_TV_WALKING_DEAD, false);
LOG.info("Result: {}", result);
assertTrue("Incorrect status code", result.getStatusCode() == 13);
}
}

@ -29,6 +29,7 @@ import com.omertron.themoviedbapi.model.MovieList;
import com.omertron.themoviedbapi.model.ReleaseInfo;
import com.omertron.themoviedbapi.model.Translation;
import com.omertron.themoviedbapi.model.Video;
import com.omertron.themoviedbapi.model.list.MovieFavorite;
import com.omertron.themoviedbapi.results.TmdbResultsList;
import java.util.List;
import java.util.Random;
@ -290,8 +291,7 @@ public class TmdbMoviesTest extends AbstractTests {
/**
* Test of postMovieRating method, of class TheMovieDbApi.
*
* TODO: Cannot be tested without a HTTP authorisation:
* http://help.themoviedb.org/kb/api/user-authentication /**
* TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication /**
*
* @throws MovieDbException
*/
@ -313,14 +313,14 @@ public class TmdbMoviesTest extends AbstractTests {
// get all rated movies
TmdbAccount account = new TmdbAccount(getApiKey(), getHttpTools());
List<MovieDb> ratedMovies = account.getRatedMovies(getSessionId(), getAccountId(),null,null,null);
List<MovieFavorite> ratedMovies = account.getRatedMovies(getSessionId(), getAccountId(), null, null, null);
assertTrue(ratedMovies.size() > 0);
// make sure that we find the movie and it is rated correctly
boolean foundMovie = false;
for (MovieDb movie : ratedMovies) {
for (MovieFavorite movie : ratedMovies) {
if (movie.getId() == movieID) {
assertEquals("Incorrect rating", movie.getUserRating(), (float) rating, 0);
assertEquals("Incorrect rating", movie.getRating(), (float) rating, 0);
foundMovie = true;
break;
}

Loading…
Cancel
Save