Fix People class with MixIn
This commit is contained in:
@@ -20,13 +20,18 @@
|
||||
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.enumeration.ArtworkType;
|
||||
import com.omertron.themoviedbapi.model.artwork.Artwork;
|
||||
import com.omertron.themoviedbapi.model.artwork.ArtworkMedia;
|
||||
import com.omertron.themoviedbapi.model.person.CreditBasic;
|
||||
import com.omertron.themoviedbapi.model.person.CreditMovieBasic;
|
||||
import com.omertron.themoviedbapi.model.person.CreditTVBasic;
|
||||
import com.omertron.themoviedbapi.model.person.ExternalID;
|
||||
import com.omertron.themoviedbapi.model.person.Person;
|
||||
import com.omertron.themoviedbapi.model.person.PersonCredits;
|
||||
import com.omertron.themoviedbapi.model.person.PersonCreditsMixIn;
|
||||
import com.omertron.themoviedbapi.model.person.PersonFind;
|
||||
import com.omertron.themoviedbapi.results.TmdbResultsList;
|
||||
import com.omertron.themoviedbapi.tools.ApiUrl;
|
||||
@@ -91,7 +96,7 @@ public class TmdbPeople extends AbstractMethod {
|
||||
* @return
|
||||
* @throws MovieDbException
|
||||
*/
|
||||
public PersonCredits getPersonMovieCredits(int personId, String language, String... appendToResponse) throws MovieDbException {
|
||||
public PersonCredits<CreditMovieBasic> getPersonMovieCredits(int personId, String language, String... appendToResponse) throws MovieDbException {
|
||||
TmdbParameters parameters = new TmdbParameters();
|
||||
parameters.add(Param.ID, personId);
|
||||
parameters.add(Param.LANGUAGE, language);
|
||||
@@ -101,7 +106,8 @@ public class TmdbPeople extends AbstractMethod {
|
||||
String webpage = httpTools.getRequest(url);
|
||||
|
||||
try {
|
||||
return MAPPER.readValue(webpage, PersonCredits.class);
|
||||
TypeReference tr = new TypeReference<PersonCredits<CreditMovieBasic>>(){};
|
||||
return MAPPER.readValue(webpage, tr);
|
||||
} catch (IOException ex) {
|
||||
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get person movie credits", url, ex);
|
||||
}
|
||||
@@ -122,7 +128,7 @@ public class TmdbPeople extends AbstractMethod {
|
||||
* @return
|
||||
* @throws MovieDbException
|
||||
*/
|
||||
public PersonCredits getPersonTVCredits(int personId, String language, String... appendToResponse) throws MovieDbException {
|
||||
public PersonCredits<CreditTVBasic> getPersonTVCredits(int personId, String language, String... appendToResponse) throws MovieDbException {
|
||||
TmdbParameters parameters = new TmdbParameters();
|
||||
parameters.add(Param.ID, personId);
|
||||
parameters.add(Param.LANGUAGE, language);
|
||||
@@ -132,7 +138,8 @@ public class TmdbPeople extends AbstractMethod {
|
||||
String webpage = httpTools.getRequest(url);
|
||||
|
||||
try {
|
||||
return MAPPER.readValue(webpage, PersonCredits.class);
|
||||
TypeReference tr = new TypeReference<PersonCredits<CreditTVBasic>>(){};
|
||||
return MAPPER.readValue(webpage, tr);
|
||||
} catch (IOException ex) {
|
||||
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get person TV credits", url, ex);
|
||||
}
|
||||
@@ -153,7 +160,7 @@ public class TmdbPeople extends AbstractMethod {
|
||||
* @return
|
||||
* @throws MovieDbException
|
||||
*/
|
||||
public PersonCredits getPersonCombinedCredits(int personId, String language, String... appendToResponse) throws MovieDbException {
|
||||
public PersonCredits<CreditBasic> getPersonCombinedCredits(int personId, String language, String... appendToResponse) throws MovieDbException {
|
||||
TmdbParameters parameters = new TmdbParameters();
|
||||
parameters.add(Param.ID, personId);
|
||||
parameters.add(Param.LANGUAGE, language);
|
||||
@@ -163,7 +170,11 @@ public class TmdbPeople extends AbstractMethod {
|
||||
String webpage = httpTools.getRequest(url);
|
||||
|
||||
try {
|
||||
return MAPPER.readValue(webpage, PersonCredits.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.addMixIn(PersonCredits.class, PersonCreditsMixIn.class);
|
||||
TypeReference tr = new TypeReference<PersonCredits<CreditBasic>>(){};
|
||||
// return mapper.readValue(webpage, PersonCredits.class);
|
||||
return mapper.readValue(webpage, tr);
|
||||
} catch (IOException ex) {
|
||||
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get person combined credits", url, ex);
|
||||
}
|
||||
|
||||
@@ -21,22 +21,21 @@ package com.omertron.themoviedbapi.model.person;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.omertron.themoviedbapi.model.AbstractJsonMapping;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author stuart.boston
|
||||
* @param <T>
|
||||
*/
|
||||
public class PersonCredits extends AbstractJsonMapping {
|
||||
public class PersonCredits<T extends CreditBasic> extends AbstractJsonMapping {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@JsonProperty("id")
|
||||
private int id;
|
||||
private List<? extends CreditBasic> cast;
|
||||
private List<? extends CreditBasic> crew;
|
||||
private List<T> cast;
|
||||
private List<T> crew;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
@@ -46,41 +45,21 @@ public class PersonCredits extends AbstractJsonMapping {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<? extends CreditBasic> getCast() {
|
||||
public List<T> getCast() {
|
||||
return cast;
|
||||
}
|
||||
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
property = "media_type",
|
||||
defaultImpl = CreditBasic.class
|
||||
)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = CreditMovieBasic.class, name = "movie"),
|
||||
@JsonSubTypes.Type(value = CreditTVBasic.class, name = "tv")
|
||||
})
|
||||
@JsonSetter("cast")
|
||||
public void setCast(List<? extends CreditBasic> cast) {
|
||||
public void setCast(List<T> cast) {
|
||||
this.cast = cast;
|
||||
}
|
||||
|
||||
public List<? extends CreditBasic> getCrew() {
|
||||
public List<T> getCrew() {
|
||||
return crew;
|
||||
}
|
||||
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
property = "media_type",
|
||||
defaultImpl = CreditBasic.class
|
||||
)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = CreditMovieBasic.class, name = "movie"),
|
||||
@JsonSubTypes.Type(value = CreditTVBasic.class, name = "tv")
|
||||
})
|
||||
@JsonSetter("crew")
|
||||
public void setCrew(List<? extends CreditBasic> crew) {
|
||||
public void setCrew(List<T> crew) {
|
||||
this.crew = crew;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.model.person;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Jackson mixin class to deserialize the combined credits
|
||||
*
|
||||
* @author Stuart.Boston
|
||||
*/
|
||||
public class PersonCreditsMixIn {
|
||||
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
property = "media_type",
|
||||
defaultImpl = CreditBasic.class
|
||||
)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = CreditMovieBasic.class, name = "movie"),
|
||||
@JsonSubTypes.Type(value = CreditTVBasic.class, name = "tv")
|
||||
})
|
||||
@JsonSetter("cast")
|
||||
public void setCast(List<CreditBasic> cast){}
|
||||
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
property = "media_type",
|
||||
defaultImpl = CreditBasic.class
|
||||
)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = CreditMovieBasic.class, name = "movie"),
|
||||
@JsonSubTypes.Type(value = CreditTVBasic.class, name = "tv")
|
||||
})
|
||||
@JsonSetter("crew")
|
||||
public void setCrew(List<CreditBasic> crew){}
|
||||
|
||||
}
|
||||
@@ -23,10 +23,14 @@ import com.omertron.themoviedbapi.AbstractTests;
|
||||
import com.omertron.themoviedbapi.MovieDbException;
|
||||
import com.omertron.themoviedbapi.TestID;
|
||||
import com.omertron.themoviedbapi.enumeration.ArtworkType;
|
||||
import com.omertron.themoviedbapi.enumeration.MediaType;
|
||||
import com.omertron.themoviedbapi.model.artwork.Artwork;
|
||||
import com.omertron.themoviedbapi.model.artwork.ArtworkMedia;
|
||||
import com.omertron.themoviedbapi.model.change.ChangeKeyItem;
|
||||
import com.omertron.themoviedbapi.model.change.ChangeListItem;
|
||||
import com.omertron.themoviedbapi.model.person.CreditBasic;
|
||||
import com.omertron.themoviedbapi.model.person.CreditMovieBasic;
|
||||
import com.omertron.themoviedbapi.model.person.CreditTVBasic;
|
||||
import com.omertron.themoviedbapi.model.person.ExternalID;
|
||||
import com.omertron.themoviedbapi.model.person.Person;
|
||||
import com.omertron.themoviedbapi.model.person.PersonCredits;
|
||||
@@ -67,7 +71,7 @@ public class TmdbPeopleTest extends AbstractTests {
|
||||
doConfiguration();
|
||||
instance = new TmdbPeople(getApiKey(), getHttpTools());
|
||||
testIDs.add(new TestID("Bruce Willis", "nm0000246", 62));
|
||||
testIDs.add(new TestID("Will Smith", "nm0000226", 2888));
|
||||
// testIDs.add(new TestID("Will Smith", "nm0000226", 2888));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
@@ -118,11 +122,15 @@ public class TmdbPeopleTest extends AbstractTests {
|
||||
String[] appendToResponse = null;
|
||||
|
||||
for (TestID test : testIDs) {
|
||||
PersonCredits result = instance.getPersonMovieCredits(test.getTmdb(), language, appendToResponse);
|
||||
PersonCredits<CreditMovieBasic> result = instance.getPersonMovieCredits(test.getTmdb(), language, appendToResponse);
|
||||
LOG.info("ID: {}, # Cast: {}, # Crew: {}", result.getId(), result.getCast().size(), result.getCrew().size());
|
||||
assertEquals("Incorrect ID", test.getTmdb(), result.getId());
|
||||
assertFalse("No cast", result.getCast().isEmpty());
|
||||
assertFalse("No crew", result.getCrew().isEmpty());
|
||||
|
||||
// Check that we have the movie specific fields
|
||||
assertTrue("No title", StringUtils.isNotBlank(result.getCast().get(0).getTitle()));
|
||||
assertTrue("No title", StringUtils.isNotBlank(result.getCrew().get(0).getTitle()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,11 +146,15 @@ public class TmdbPeopleTest extends AbstractTests {
|
||||
String[] appendToResponse = null;
|
||||
|
||||
for (TestID test : testIDs) {
|
||||
PersonCredits result = instance.getPersonTVCredits(test.getTmdb(), language, appendToResponse);
|
||||
PersonCredits<CreditTVBasic> result = instance.getPersonTVCredits(test.getTmdb(), language, appendToResponse);
|
||||
LOG.info("ID: {}, # Cast: {}, # Crew: {}", result.getId(), result.getCast().size(), result.getCrew().size());
|
||||
assertEquals("Incorrect ID", test.getTmdb(), result.getId());
|
||||
assertFalse("No cast", result.getCast().isEmpty());
|
||||
assertFalse("No crew", result.getCrew().isEmpty());
|
||||
|
||||
// Check that we have the TV specific fields
|
||||
assertTrue("No title", StringUtils.isNotBlank(result.getCast().get(0).getName()));
|
||||
assertTrue("No title", StringUtils.isNotBlank(result.getCrew().get(0).getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,11 +170,33 @@ public class TmdbPeopleTest extends AbstractTests {
|
||||
String[] appendToResponse = null;
|
||||
|
||||
for (TestID test : testIDs) {
|
||||
PersonCredits result = instance.getPersonCombinedCredits(test.getTmdb(), language, appendToResponse);
|
||||
PersonCredits<CreditBasic> result = instance.getPersonCombinedCredits(test.getTmdb(), language, appendToResponse);
|
||||
LOG.info("ID: {}, # Cast: {}, # Crew: {}", result.getId(), result.getCast().size(), result.getCrew().size());
|
||||
assertEquals("Incorrect ID", test.getTmdb(), result.getId());
|
||||
assertFalse("No cast", result.getCast().isEmpty());
|
||||
assertFalse("No crew", result.getCrew().isEmpty());
|
||||
|
||||
boolean checkedMovie = false;
|
||||
boolean checkedTV = false;
|
||||
|
||||
for (CreditBasic p : result.getCast()) {
|
||||
if (!checkedMovie && p.getMediaType() == MediaType.MOVIE) {
|
||||
CreditMovieBasic c = (CreditMovieBasic) p;
|
||||
assertTrue("No title", StringUtils.isNotBlank(c.getTitle()));
|
||||
checkedMovie = true;
|
||||
}
|
||||
|
||||
if (!checkedTV && p.getMediaType() == MediaType.TV) {
|
||||
CreditTVBasic c = (CreditTVBasic) p;
|
||||
assertTrue("No name", StringUtils.isNotBlank(c.getName()));
|
||||
checkedTV = true;
|
||||
}
|
||||
|
||||
if (checkedMovie && checkedTV) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user