diff --git a/pom.xml b/pom.xml index 5ad3bf0db..83f7ed9e0 100644 --- a/pom.xml +++ b/pom.xml @@ -117,6 +117,13 @@ api-common 1.4 + + + commons-io + commons-io + 2.4 + test + diff --git a/src/test/java/com/omertron/themoviedbapi/AbstractTests.java b/src/test/java/com/omertron/themoviedbapi/AbstractTests.java index a7cb01b55..f2bb77a2e 100644 --- a/src/test/java/com/omertron/themoviedbapi/AbstractTests.java +++ b/src/test/java/com/omertron/themoviedbapi/AbstractTests.java @@ -26,7 +26,12 @@ import com.omertron.themoviedbapi.model.TokenAuthorisation; import com.omertron.themoviedbapi.model.TokenSession; import com.omertron.themoviedbapi.tools.HttpTools; import java.io.File; +import java.io.IOException; +import java.io.Serializable; import java.util.Properties; +import java.util.concurrent.TimeUnit; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.SerializationUtils; import org.apache.http.client.HttpClient; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -38,6 +43,8 @@ public class AbstractTests { protected static final Logger LOG = LoggerFactory.getLogger(AbstractTests.class); private static final String PROP_FIlENAME = "testing.properties"; + private static final String ACCT_FILENAME = "account.bin"; + private static final String TOKEN_FILENAME = "token.bin"; private static final Properties props = new Properties(); private static HttpClient httpClient; private static HttpTools httpTools; @@ -49,6 +56,11 @@ public class AbstractTests { protected static final String LANGUAGE_ENGLISH = "en"; protected static final String LANGUAGE_RUSSIAN = "ru"; + /** + * Do the initial configuration for the test cases + * + * @throws MovieDbException + */ public static final void doConfiguration() throws MovieDbException { TestLogger.Configure(); httpClient = new SimpleHttpClientBuilder().build(); @@ -72,7 +84,80 @@ public class AbstractTests { } } + /** + * Write out the object to a file + * + * @param object + * @param filename + * @return + */ + private static boolean writeObject(Serializable object, String filename) { + File serFile = new File(filename); + + if (serFile.exists()) { + long ft = serFile.lastModified(); + long diff = System.currentTimeMillis() - ft; + LOG.info("File time: {}, diff: {} - {}", ft, diff, TimeUnit.HOURS.toMillis(1)); + if (diff < TimeUnit.HOURS.toMillis(1)) { + LOG.info("File is current, no need to reacquire"); + return true; + } else { + LOG.info("File is too old, re-acquiring"); + } + } + + try { + byte[] serObject = SerializationUtils.serialize(object); + FileUtils.writeByteArrayToFile(serFile, serObject); + return true; + } catch (IOException ex) { + LOG.info("Failed to write {}: {}", filename, ex.getMessage(), ex); + return false; + } + } + + /** + * Read the object back from a file + * + * @param + * @param filename + * @return + */ + private static T readObject(String filename) { + File serFile = new File(filename); + + if (serFile.exists()) { + long diff = System.currentTimeMillis() - serFile.lastModified(); + if (diff < TimeUnit.HOURS.toMillis(1)) { + LOG.info("File is current, no need to reacquire"); + return null; + } else { + LOG.info("File is too old, re-acquiring"); + } + } else { + LOG.info("File doesn't exist: {}", filename); + return null; + } + + try { + byte[] serObject = FileUtils.readFileToByteArray(serFile); + return SerializationUtils.deserialize(serObject); + } catch (IOException ex) { + LOG.info("Failed to read {}: {}", filename, ex.getMessage(), ex); + return null; + } + } + + /** + * get the Session ID + * + * @return + * @throws MovieDbException + */ public static final String getSessionId() throws MovieDbException { + // Read the object from a file + tokenSession = readObject(TOKEN_FILENAME); + if (tokenSession == null) { TmdbAuthentication auth = new TmdbAuthentication(getApiKey(), getHttpTools()); LOG.info("Test and create a session token for the rest of the tests"); @@ -85,39 +170,85 @@ public class AbstractTests { tokenSession = auth.getSessionToken(token); assertTrue("Session token is not valid", tokenSession.getSuccess()); + // Write the object to a file + writeObject(token, TOKEN_FILENAME); + } return tokenSession.getSessionId(); } + /** + * Get the Account information + * + * @return + * @throws MovieDbException + */ public static final int getAccountId() throws MovieDbException { + // Read the object from a file + account = readObject(ACCT_FILENAME); + if (account == null) { TmdbAccount instance = new TmdbAccount(getApiKey(), getHttpTools()); // Get the account for later tests account = instance.getAccount(tokenSession.getSessionId()); + + // Write the object to a file + writeObject(account, ACCT_FILENAME); } return account.getId(); } + /** + * get the Http Client + * + * @return + */ public static HttpClient getHttpClient() { return httpClient; } + /** + * Get the Http Tools + * + * @return + */ public static HttpTools getHttpTools() { return httpTools; } + /** + * Get the API Key + * + * @return + */ public static String getApiKey() { return props.getProperty("API_Key"); } + /** + * Get the Account username + * + * @return + */ public static String getUsername() { return props.getProperty("Username"); } + /** + * Get the Account password + * + * @return + */ public static String getPassword() { return props.getProperty("Password"); } + /** + * Get the named property + * + * @param property + * @return + */ public static String getProperty(String property) { return props.getProperty(property); } diff --git a/src/test/java/com/omertron/themoviedbapi/methods/TmdbAccountTest.java b/src/test/java/com/omertron/themoviedbapi/methods/TmdbAccountTest.java index dafddb550..21efffb37 100644 --- a/src/test/java/com/omertron/themoviedbapi/methods/TmdbAccountTest.java +++ b/src/test/java/com/omertron/themoviedbapi/methods/TmdbAccountTest.java @@ -21,22 +21,15 @@ package com.omertron.themoviedbapi.methods; import com.omertron.themoviedbapi.AbstractTests; import com.omertron.themoviedbapi.MovieDbException; -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 java.util.List; import org.junit.After; import org.junit.AfterClass; 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.Ignore; import org.junit.Test; /** @@ -63,11 +56,11 @@ public class TmdbAccountTest extends AbstractTests { } @Before - public void setUp() throws Exception { + public void setUp() throws MovieDbException { } @After - public void tearDown() throws Exception { + public void tearDown() throws MovieDbException { } /** @@ -86,172 +79,11 @@ public class TmdbAccountTest extends AbstractTests { /** * Test of getUserLists method, of class TmdbAccount. - * * @throws com.omertron.themoviedbapi.MovieDbException */ @Test public void testGetUserLists() throws MovieDbException { LOG.info("getUserLists"); List result = instance.getUserLists(getSessionId(), getAccountId()); - assertNotNull("Null list returned", result); - } - - /** - * Test of getFavoriteMovies method, of class TmdbAccount. - * - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetFavoriteMovies() throws MovieDbException { - LOG.info("getFavoriteMovies"); - - LOG.info("Check favorite list is empty"); - // make sure it's empty (because it's just a test account - List favList = instance.getFavoriteMovies(getSessionId(), getAccountId()); - assertTrue("Favorite list was not empty!", favList.isEmpty()); - - LOG.info("Add movie to list"); - // add a movie - StatusCode status = instance.changeFavoriteStatus(getSessionId(), getAccountId(), ID_MOVIE_FIGHT_CLUB, MediaType.MOVIE, true); - LOG.info("Add Status: {}", status); - - LOG.info("Get favorite list"); - favList = instance.getFavoriteMovies(getSessionId(), getAccountId()); - assertNotNull("Empty favorite list returned", favList); - assertFalse("Favorite list empty", favList.isEmpty()); - - // clean up again - LOG.info("Remove movie(s) from list"); - for (MovieDb movie : favList) { - LOG.info("Removing movie {}-'{}'", movie.getId(), movie.getTitle()); - status = instance.changeFavoriteStatus(getSessionId(), getAccountId(), movie.getId(), MediaType.MOVIE, false); - LOG.info("Remove status: {}", status); - } - assertTrue("Favorite list was not empty", instance.getFavoriteMovies(getSessionId(), getAccountId()).isEmpty()); - } - - /** - * Test of changeFavoriteStatus method, of class TmdbAccount. - * - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Ignore("Tested as part of getFavoriteMovies") - public void testChangeFavoriteStatus() throws MovieDbException { - } - - /** - * Test of getRatedMovies method, of class TmdbAccount. - * - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetRatedMovies() throws MovieDbException { - LOG.info("getRatedMovies"); - List result = instance.getRatedMovies(getSessionId(), getAccountId()); - assertFalse("No rated movies", result.isEmpty()); - } - - /** - * Test of getWatchList method, of class TmdbAccount. - * - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetWatchList() throws MovieDbException { - LOG.info("getWatchList"); - - LOG.info("Check list is empty"); - // make sure it's empty (because it's just a test account - List watchList = instance.getWatchListMovie(getSessionId(), getAccountId()); - assertTrue("Watch list was not empty!", watchList.isEmpty()); - - LOG.info("Add movie to list"); - // add a movie - StatusCode status = instance.modifyWatchList(getSessionId(), getAccountId(), ID_MOVIE_FIGHT_CLUB, MediaType.MOVIE, true); - LOG.info("Add Status: {}", status); - - LOG.info("Get watch list"); - watchList = instance.getWatchListMovie(getSessionId(), getAccountId()); - assertNotNull("Empty watch list returned", watchList); - assertFalse("Watchlist list empty", watchList.isEmpty()); - - LOG.info("Removing movie(s) from list"); - // clean up again - for (MovieDb movie : watchList) { - LOG.info("Removing movie {}-'{}'", movie.getId(), movie.getTitle()); - status = instance.modifyWatchList(getSessionId(), getAccountId(), movie.getId(), MediaType.MOVIE, false); - LOG.info("Remove status: {}", status); - } - - assertTrue(instance.getWatchListMovie(getSessionId(), getAccountId()).isEmpty()); - } - - /** - * Test of modifyWatchList method, of class TmdbAccount. - * - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Ignore("Tested as part of testGetWatchList") - public void testModifyWatchList() throws MovieDbException { - } - - /** - * Test of getFavoriteTv method, of class TmdbAccount. - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetFavoriteTv() throws MovieDbException { - System.out.println("getFavoriteTv"); - List result = instance.getFavoriteTv(getSessionId(), getAccountId()); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getRatedTV method, of class TmdbAccount. - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetRatedTV() throws MovieDbException { - System.out.println("getRatedTV"); - String sessionId = ""; - int accountId = 0; - List expResult = null; - List result = instance.getRatedTV(sessionId, accountId); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getWatchListMovie method, of class TmdbAccount. - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetWatchListMovie() throws MovieDbException { - System.out.println("getWatchListMovie"); - String sessionId = ""; - int accountId = 0; - List expResult = null; - List result = instance.getWatchListMovie(sessionId, accountId); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getWatchListTV method, of class TmdbAccount. - * @throws com.omertron.themoviedbapi.MovieDbException - */ - @Test - public void testGetWatchListTV() throws MovieDbException { - System.out.println("getWatchListTV"); - String sessionId = ""; - int accountId = 0; - List expResult = null; - List result = instance.getWatchListTV(sessionId, accountId); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); } }