Skip to content

Commit

Permalink
Merge pull request #162 from c-eg/companies-api
Browse files Browse the repository at this point in the history
Companies api
  • Loading branch information
c-eg authored Jan 27, 2024
2 parents 6470b3a + fac92e1 commit 76f7bf2
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/main/java/info/movito/themoviedbapi/TmdbApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public TmdbCollections getCollections() {
return new TmdbCollections(this);
}

public TmdbCompany getCompany() {
return new TmdbCompany(this);
public TmdbCompanies getCompanies() {
return new TmdbCompanies(this);
}

public TmdbConfiguration getConfiguration() {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/info/movito/themoviedbapi/TmdbCompanies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.AlternativeName;
import info.movito.themoviedbapi.model.Company;
import info.movito.themoviedbapi.model.core.ResultsPage;
import info.movito.themoviedbapi.model.core.image.LogoImageResults;
import info.movito.themoviedbapi.tools.ApiUrl;
import info.movito.themoviedbapi.tools.TmdbException;

/**
* The movie database api for companies. See the
* <a href="https://developer.themoviedb.org/reference/company-details">documentation</a> for more info.
*/
public class TmdbCompanies extends AbstractTmdbApi {
protected static final String TMDB_METHOD_COMPANY = "company";

/**
* Create a new TmdbCompany instance to call the company related TMDb API methods.
*/
TmdbCompanies(TmdbApi tmdbApi) {
super(tmdbApi);
}

/**
* <p>Get the company details by ID.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/company-details">documentation</a> for more info.</p>
*
* @param companyId The company ID
* @return The company details
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public Company getDetails(Integer companyId) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_COMPANY, companyId);
return mapJsonResult(apiUrl, Company.class);
}

/**
* <p>Gets the alternative company names by ID.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/company-alternative-names">documentation</a> for more info.</p>
*
* @param companyId The company ID
* @return The alternative company names
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public AlternativeNamesResultsPage getAlternativeNames(Integer companyId) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_COMPANY, companyId, "alternative_names");
return mapJsonResult(apiUrl, AlternativeNamesResultsPage.class);
}

/**
* <p>Get the company logos by ID.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/company-images">documentation</a> for more info.</p>
*
* @param companyId The company ID
* @return The company logos
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public LogoImageResults getImages(Integer companyId) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_COMPANY, companyId, "images");
return mapJsonResult(apiUrl, LogoImageResults.class);
}

@SuppressWarnings("checkstyle:MissingJavadocType")
public static class AlternativeNamesResultsPage extends ResultsPage<AlternativeName> { }
}
52 changes: 0 additions & 52 deletions src/main/java/info/movito/themoviedbapi/TmdbCompany.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/info/movito/themoviedbapi/model/AlternativeName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package info.movito.themoviedbapi.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import info.movito.themoviedbapi.model.core.AbstractJsonMapping;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class AlternativeName extends AbstractJsonMapping {
@JsonProperty("name")
private String name;

@JsonProperty("type")
private String type;
}
19 changes: 5 additions & 14 deletions src/main/java/info/movito/themoviedbapi/model/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@ public class Company extends NamedIdElement {
@JsonProperty("logo_path")
private String logoPath;

// TODO: is this field still supported? We need an example for info.movito.themoviedbapi.CompanyApiTest.testGetCompanyInfo
@JsonProperty("origin_country")
private String originCountry;

@JsonProperty("parent_company")
private Company parentCompany;

/**
* Sets the parent company.
*/
public void setParentCompany(int id, String name, String logoPath) {
Company parent = new Company();
parent.setId(id);
parent.setName(name);
parent.setLogoPath(logoPath);

this.parentCompany = parent;
}
private Integer parentCompanyId;
}

20 changes: 20 additions & 0 deletions src/main/java/info/movito/themoviedbapi/model/core/Results.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package info.movito.themoviedbapi.model.core;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Iterator;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class Results<T> extends IdElement implements Iterable<T> {
@JsonProperty("results")
private List<T> results;

@Override
public Iterator<T> iterator() {
return results.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,15 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Iterator;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
public class ResultsPage<T> extends AbstractJsonMapping implements Iterable<T> {
@JsonProperty("results")
private List<T> results;

@EqualsAndHashCode(callSuper = true)
public class ResultsPage<T> extends Results<T> {
@JsonProperty("page")
private int page;
private Integer page;

@JsonProperty("total_pages")
private int totalPages;
private Integer totalPages;

@JsonProperty("total_results")
private int totalResults;

@Override
public Iterator<T> iterator() {
return results.iterator();
}
private Integer totalResults;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package info.movito.themoviedbapi.model.core.image;

import com.fasterxml.jackson.annotation.JsonProperty;
import info.movito.themoviedbapi.model.core.StringIdElement;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class LogoImage extends StringIdElement {
@JsonProperty("aspect_ratio")
private Double aspectRatio;

@JsonProperty("file_path")
private String filePath;

@JsonProperty("height")
private Integer height;

@JsonProperty("file_type")
private String fileType;

@JsonProperty("vote_average")
private Double voteAverage;

@JsonProperty("vote_count")
private Integer voteCount;

@JsonProperty("width")
private Integer width;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package info.movito.themoviedbapi.model.core.image;

import com.fasterxml.jackson.annotation.JsonProperty;
import info.movito.themoviedbapi.model.core.IdElement;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class LogoImageResults extends IdElement {
@JsonProperty("logos")
private List<LogoImage> logos;
}
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
opens info.movito.themoviedbapi.model.config to com.fasterxml.jackson.databind; // todo: remove
opens info.movito.themoviedbapi.model.configuration to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.core to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.core.image to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.core.responses to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.keywords to com.fasterxml.jackson.databind;
opens info.movito.themoviedbapi.model.movies.changes to com.fasterxml.jackson.databind;
Expand All @@ -38,6 +39,7 @@
exports info.movito.themoviedbapi.model.config;
exports info.movito.themoviedbapi.model.configuration;
exports info.movito.themoviedbapi.model.core;
exports info.movito.themoviedbapi.model.core.image;
exports info.movito.themoviedbapi.model.core.responses;
exports info.movito.themoviedbapi.model.keywords;
exports info.movito.themoviedbapi.model.movies.changes;
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/info/movito/themoviedbapi/TmdbCompaniesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.AlternativeName;
import info.movito.themoviedbapi.model.Company;
import info.movito.themoviedbapi.model.core.image.LogoImage;
import info.movito.themoviedbapi.model.core.image.LogoImageResults;
import info.movito.themoviedbapi.tools.RequestType;
import info.movito.themoviedbapi.tools.TmdbException;
import info.movito.themoviedbapi.util.TestUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URL;

import static info.movito.themoviedbapi.TmdbCompanies.TMDB_METHOD_COMPANY;
import static info.movito.themoviedbapi.tools.ApiUrl.TMDB_API_BASE_URL;
import static info.movito.themoviedbapi.util.TestUtils.testForNullFieldsAndNewItems;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

/**
* Tests for {@link TmdbCompanies}.
*/
public class TmdbCompaniesTest extends AbstractTmdbApiTest {
/**
* Tests {@link TmdbCompanies#getDetails(Integer)}.
*/
@Test
public void testGetDetails() throws IOException, TmdbException {
int companyId = 1;

String body = TestUtils.readTestFile("api_responses/companies/details.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_COMPANY + "/" + companyId);
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbCompanies tmdbCompanies = getTmdbApi().getCompanies();
Company company = tmdbCompanies.getDetails(companyId);
assertNotNull(company);
testForNullFieldsAndNewItems(company);
}

/**
* Tests {@link TmdbCompanies#getAlternativeNames(Integer)}.
*/
@Test
public void testGetAlternativeNames() throws IOException, TmdbException {
int companyId = 1;

String body = TestUtils.readTestFile("api_responses/companies/alternative_names.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_COMPANY + "/" + companyId + "/alternative_names");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbCompanies tmdbCompanies = getTmdbApi().getCompanies();
TmdbCompanies.AlternativeNamesResultsPage alternativeNamesResultsPage = tmdbCompanies.getAlternativeNames(1);
assertNotNull(alternativeNamesResultsPage);
testForNullFieldsAndNewItems(alternativeNamesResultsPage);

AlternativeName alternativeName = alternativeNamesResultsPage.getResults().get(0);
assertNotNull(alternativeName);
testForNullFieldsAndNewItems(alternativeName);
}

/**
* Tests {@link TmdbCompanies#getImages(Integer)}.
*/
@Test
public void testGetImages() throws IOException, TmdbException {
int companyId = 1;

String body = TestUtils.readTestFile("api_responses/companies/images.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_COMPANY + "/" + companyId + "/images");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbCompanies tmdbCompanies = getTmdbApi().getCompanies();
LogoImageResults logoImageResults = tmdbCompanies.getImages(1);
assertNotNull(logoImageResults);
testForNullFieldsAndNewItems(logoImageResults);

LogoImage logoImage = logoImageResults.getLogos().get(0);
assertNotNull(logoImage);
testForNullFieldsAndNewItems(logoImage);
}
}
21 changes: 21 additions & 0 deletions src/test/resources/api_responses/companies/alternative_names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": 1,
"results": [
{
"name": "루카스필름",
"type": ""
},
{
"name": "Lucasfilm Limited, LLC",
"type": ""
},
{
"name": "Lucasfilm Ltd. LLC",
"type": ""
},
{
"name": "Lucasfilm",
"type": ""
}
]
}
Loading

0 comments on commit 76f7bf2

Please sign in to comment.