Replace CloseableHttpClient with HttpClient

master
Stuart Boston 11 years ago
parent e5e7cd1ac3
commit debfb1209e

@ -78,7 +78,7 @@ import com.omertron.themoviedbapi.tools.MethodSub;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.client.HttpClient;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.yamj.api.common.http.SimpleHttpClientBuilder; import org.yamj.api.common.http.SimpleHttpClientBuilder;
@ -137,7 +137,7 @@ public class TheMovieDbApi {
* @param httpClient The httpClient to use for web requests. * @param httpClient The httpClient to use for web requests.
* @throws MovieDbException * @throws MovieDbException
*/ */
public TheMovieDbApi(String apiKey, CloseableHttpClient httpClient) throws MovieDbException { public TheMovieDbApi(String apiKey, HttpClient httpClient) throws MovieDbException {
this.apiKey = apiKey; this.apiKey = apiKey;
this.httpTools = new HttpTools(httpClient); this.httpTools = new HttpTools(httpClient);
initialise(apiKey, httpTools); initialise(apiKey, httpTools);

@ -7,12 +7,12 @@ import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.yamj.api.common.exception.ApiExceptionType; import org.yamj.api.common.exception.ApiExceptionType;
import org.yamj.api.common.http.DigestedResponse; import org.yamj.api.common.http.DigestedResponse;
@ -25,11 +25,11 @@ import org.yamj.api.common.http.DigestedResponseReader;
*/ */
public class HttpTools { public class HttpTools {
private final CloseableHttpClient httpClient; private final HttpClient httpClient;
private static final Charset CHARSET = Charset.forName("UTF-8"); private static final Charset CHARSET = Charset.forName("UTF-8");
private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_JSON = "application/json";
public HttpTools(CloseableHttpClient httpClient) { public HttpTools(HttpClient httpClient) {
this.httpClient = httpClient; this.httpClient = httpClient;
} }

@ -19,39 +19,26 @@
*/ */
package com.omertron.themoviedbapi; package com.omertron.themoviedbapi;
import com.omertron.themoviedbapi.enumeration.MediaType;
import com.omertron.themoviedbapi.model.Account; import com.omertron.themoviedbapi.model.Account;
import com.omertron.themoviedbapi.model.MovieDb; import com.omertron.themoviedbapi.model.MovieDb;
import com.omertron.themoviedbapi.model.MovieDbList; import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model.StatusCode; import com.omertron.themoviedbapi.model.StatusCode;
import com.omertron.themoviedbapi.model.TokenAuthorisation; import com.omertron.themoviedbapi.model.TokenAuthorisation;
import com.omertron.themoviedbapi.model.TokenSession; import com.omertron.themoviedbapi.model.TokenSession;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.Random; import java.util.Random;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestAccounts { public class TestAccounts extends AbstractTests {
private static final Logger LOG = LoggerFactory.getLogger(TestAccounts.class);
// API Key
private static final String PROP_FIlENAME = "testing.properties";
private static String API_KEY;
private static String ACCOUNT_USERNAME;
private static String ACCOUNT_PASSWORD;
private static TheMovieDbApi tmdb; private static TheMovieDbApi tmdb;
private static TokenSession tokenSession = null; private static TokenSession tokenSession = null;
private static Account account = null; private static Account account = null;
@ -61,30 +48,11 @@ public class TestAccounts {
@BeforeClass @BeforeClass
public static void setUpClass() throws MovieDbException { public static void setUpClass() throws MovieDbException {
TestLogger.Configure(); tmdb = new TheMovieDbApi(getApiKey());
Properties props = new Properties();
File f = new File(PROP_FIlENAME);
if (f.exists()) {
LOG.info("Loading properties from '{}'", PROP_FIlENAME);
TestLogger.loadProperties(props, f);
API_KEY = props.getProperty("API_Key");
ACCOUNT_USERNAME = props.getProperty("Username");
ACCOUNT_PASSWORD = props.getProperty("Password");
} else {
// Properties file is created in the main test class
fail("Failed to get key information from properties file '" + PROP_FIlENAME + "'");
}
tmdb = new TheMovieDbApi(API_KEY);
}
@AfterClass
public static void tearDownClass() {
} }
@Before @Before
@Override
public void setUp() throws MovieDbException { public void setUp() throws MovieDbException {
if (tokenSession == null) { if (tokenSession == null) {
testSessionCreation(); testSessionCreation();
@ -95,10 +63,6 @@ public class TestAccounts {
} }
} }
@After
public void tearDown() {
}
/** /**
* Test and create a session token for the rest of the tests * Test and create a session token for the rest of the tests
* *
@ -115,7 +79,7 @@ public class TestAccounts {
LOG.info("Token (auth): {}", token.toString()); LOG.info("Token (auth): {}", token.toString());
// 2b; Get user permission // 2b; Get user permission
token = tmdb.getSessionTokenLogin(token, ACCOUNT_USERNAME, ACCOUNT_PASSWORD); token = tmdb.getSessionTokenLogin(token, getUsername(), getPassword());
assertFalse("Token (login) is null", token == null); assertFalse("Token (login) is null", token == null);
assertTrue("Token (login) is not valid", token.getSuccess()); assertTrue("Token (login) is not valid", token.getSuccess());
LOG.info("Token (login): {}", token.toString()); LOG.info("Token (login): {}", token.toString());
@ -140,7 +104,7 @@ public class TestAccounts {
LOG.info("Account: {}", account); LOG.info("Account: {}", account);
// Make sure properties are extracted correctly // Make sure properties are extracted correctly
assertEquals("Wrong username!", ACCOUNT_USERNAME, account.getUserName()); assertEquals("Wrong username!", getUsername(), account.getUserName());
} }
@Test @Test
@ -151,19 +115,19 @@ public class TestAccounts {
} }
// make sure it's empty (because it's just a test account // make sure it's empty (because it's just a test account
Assert.assertTrue(tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()).isEmpty()); assertTrue(tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()).isEmpty());
// add a movie // add a movie
tmdb.addToWatchList(tokenSession.getSessionId(), account.getId(), 550); tmdb.addToWatchList(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE);
List<MovieDb> watchList = tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()); List<MovieDb> watchList = tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId());
assertNotNull("Empty watch list returned", watchList); assertNotNull("Empty watch list returned", watchList);
assertEquals("Watchlist wrong size", 1, watchList.size()); assertEquals("Watchlist wrong size", 1, watchList.size());
// clean up again // clean up again
tmdb.removeFromWatchList(tokenSession.getSessionId(), account.getId(), 550); tmdb.removeFromWatchList(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE);
Assert.assertTrue(tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()).isEmpty()); assertTrue(tmdb.getWatchListMovie(tokenSession.getSessionId(), account.getId()).isEmpty());
} }
@Test @Test
@ -174,26 +138,25 @@ public class TestAccounts {
} }
// make sure it's empty (because it's just a test account // make sure it's empty (because it's just a test account
Assert.assertTrue(tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()).isEmpty()); assertTrue(tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()).isEmpty());
// add a movie // add a movie
tmdb.changeFavoriteStatus(tokenSession.getSessionId(), account.getId(), 550, true); tmdb.changeFavoriteStatus(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE, true);
List<MovieDb> watchList = tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()); List<MovieDb> watchList = tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId());
assertNotNull("Empty watch list returned", watchList); assertNotNull("Empty watch list returned", watchList);
assertEquals("Watchlist wrong size", 1, watchList.size()); assertEquals("Watchlist wrong size", 1, watchList.size());
// clean up again // clean up again
tmdb.changeFavoriteStatus(tokenSession.getSessionId(), account.getId(), 550, false); tmdb.changeFavoriteStatus(tokenSession.getSessionId(), account.getId(), 550, MediaType.MOVIE, false);
Assert.assertTrue(tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()).isEmpty()); assertTrue(tmdb.getFavoriteMovies(tokenSession.getSessionId(), account.getId()).isEmpty());
} }
/** /**
* Test of getSessionToken method, of class TheMovieDbApi. * Test of getSessionToken method, of class TheMovieDbApi.
* *
* TODO: Cannot be tested without a HTTP authorisation: * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication
* http://help.themoviedb.org/kb/api/user-authentication
* *
* @throws MovieDbException * @throws MovieDbException
*/ */
@ -217,8 +180,7 @@ public class TestAccounts {
/** /**
* Test of postMovieRating method, of class TheMovieDbApi. * Test of postMovieRating method, of class TheMovieDbApi.
* *
* TODO: Cannot be tested without a HTTP authorisation: * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication
* http://help.themoviedb.org/kb/api/user-authentication
* *
* @throws MovieDbException * @throws MovieDbException
*/ */

@ -21,7 +21,6 @@ package com.omertron.themoviedbapi;
import com.omertron.themoviedbapi.model.AlternativeTitle; import com.omertron.themoviedbapi.model.AlternativeTitle;
import com.omertron.themoviedbapi.model.Artwork; import com.omertron.themoviedbapi.model.Artwork;
import com.omertron.themoviedbapi.model.ChangedItem;
import com.omertron.themoviedbapi.model.ChangedMedia; import com.omertron.themoviedbapi.model.ChangedMedia;
import com.omertron.themoviedbapi.model.Collection; import com.omertron.themoviedbapi.model.Collection;
import com.omertron.themoviedbapi.model.CollectionInfo; import com.omertron.themoviedbapi.model.CollectionInfo;
@ -44,37 +43,23 @@ import com.omertron.themoviedbapi.model.TokenSession;
import com.omertron.themoviedbapi.model.Translation; import com.omertron.themoviedbapi.model.Translation;
import com.omertron.themoviedbapi.model.Video; import com.omertron.themoviedbapi.model.Video;
import com.omertron.themoviedbapi.results.TmdbResultsList; import com.omertron.themoviedbapi.results.TmdbResultsList;
import com.omertron.themoviedbapi.results.TmdbResultsMap;
import java.io.File;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Test cases for TheMovieDbApi API * Test cases for TheMovieDbApi API
* *
* @author stuart.boston * @author stuart.boston
*/ */
public class TheMovieDbApiTest { public class TheMovieDbApiTest extends AbstractTests{
// Logger
private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApiTest.class);
// API Key
private static final String PROP_FIlENAME = "testing.properties";
private static String API_KEY;
private static TheMovieDbApi tmdb; private static TheMovieDbApi tmdb;
// Test data // Test data
private static final int ID_MOVIE_BLADE_RUNNER = 78; private static final int ID_MOVIE_BLADE_RUNNER = 78;
@ -96,46 +81,15 @@ public class TheMovieDbApiTest {
@BeforeClass @BeforeClass
public static void setUpClass() throws MovieDbException { public static void setUpClass() throws MovieDbException {
TestLogger.Configure(); tmdb = new TheMovieDbApi(getApiKey());
Properties props = new Properties();
File f = new File(PROP_FIlENAME);
if (f.exists()) {
LOG.info("Loading properties from '{}'", PROP_FIlENAME);
TestLogger.loadProperties(props, f);
API_KEY = props.getProperty("API_Key");
} else {
LOG.info("Property file '{}' not found, creating dummy file.", PROP_FIlENAME);
props.setProperty("API_Key", "INSERT_YOUR_KEY_HERE");
props.setProperty("Username", "INSERT_YOUR_USERNAME_HERE");
props.setProperty("Password", "INSERT_YOUR_PASSWORD_HERE");
TestLogger.saveProperties(props, f, "Properties file for tests");
fail("Failed to get key information from properties file '" + PROP_FIlENAME + "'");
}
tmdb = new TheMovieDbApi(API_KEY);
}
@AfterClass
public static void tearDownClass() throws MovieDbException {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
} }
/** /**
* Test of getConfiguration method, of class TheMovieDbApi. * Test of getConfiguration method, of class TheMovieDbApi.
* @throws com.omertron.themoviedbapi.MovieDbException
*/ */
@Test @Test
public void testConfiguration() { public void testConfiguration() throws MovieDbException {
LOG.info("Test Configuration"); LOG.info("Test Configuration");
Configuration tmdbConfig = tmdb.getConfiguration(); Configuration tmdbConfig = tmdb.getConfiguration();
@ -599,7 +553,7 @@ public class TheMovieDbApiTest {
// Get some popular movies // Get some popular movies
TmdbResultsList<MovieDb> movieList = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); TmdbResultsList<MovieDb> movieList = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0);
for (MovieDb movie : movieList.getResults()) { for (MovieDb movie : movieList.getResults()) {
TmdbResultsMap<String, List<ChangedItem>> result = tmdb.getMovieChanges(movie.getId(), startDate, endDate); TmdbResultsList result = tmdb.getMovieChanges(movie.getId(), startDate, endDate);
LOG.info("{} has {} changes.", movie.getTitle(), result.getResults().size()); LOG.info("{} has {} changes.", movie.getTitle(), result.getResults().size());
assertTrue("No changes found", result.getResults().size() > 0); assertTrue("No changes found", result.getResults().size() > 0);
break; break;
@ -750,7 +704,7 @@ public class TheMovieDbApiTest {
int page = 0; int page = 0;
String startDate = ""; String startDate = "";
String endDate = ""; String endDate = "";
TmdbResultsList<ChangedMedia> result = tmdb.getMovieChangesList(page, startDate, endDate); TmdbResultsList<ChangedMedia> result = tmdb.getMovieChangeList(page, startDate, endDate);
assertFalse("No movie changes.", result.getResults().isEmpty()); assertFalse("No movie changes.", result.getResults().isEmpty());
} }
@ -765,7 +719,7 @@ public class TheMovieDbApiTest {
int page = 0; int page = 0;
String startDate = ""; String startDate = "";
String endDate = ""; String endDate = "";
tmdb.getPersonChangesList(page, startDate, endDate); tmdb.getPersonChangeList(page, startDate, endDate);
// TODO review the generated test code and remove the default call to fail. // TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype."); fail("The test case is a prototype.");
} }
@ -793,7 +747,7 @@ public class TheMovieDbApiTest {
Discover discover = new Discover(); Discover discover = new Discover();
discover.year(2013).language(LANGUAGE_ENGLISH); discover.year(2013).language(LANGUAGE_ENGLISH);
TmdbResultsList<MovieDb> result = tmdb.getDiscover(discover); TmdbResultsList<MovieDb> result = tmdb.getDiscoverMovies(discover);
assertFalse("No movies discovered", result.getResults().isEmpty()); assertFalse("No movies discovered", result.getResults().isEmpty());
} }
} }

Loading…
Cancel
Save