diff --git a/.travis.yml b/.travis.yml index ec08dd0a..120a3b4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,11 @@ install: true sudo: false jdk: - - oraclejdk8 - - oraclejdk9 - openjdk8 +# JDK 9 disable because of TravisCI problems +# https://travis-ci.community/t/install-jdk-sh-failing-for-openjdk9-and-10/3998/17 +# - openjdk9 + before_install: - chmod +x gradlew diff --git a/README.md b/README.md index f389858e..df34c0e9 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,19 @@ FlightOffer[] flightOffers = amadeus.shopping.flightOffers.get(Params .and("destination", "MAD") .and("departureDate", "2019-08-01")); +// Flight Choice Prediction +// Note that the example calls 2 APIs: Flight Low-fare Search & Flight Choice Prediction +FlightOffer[] flightOffers = amadeus.shopping.flightOffers + .get(Params.with("origin", "MAD").and("destination", "NYC").and("departureDate", "2020-01-01").and("max", "2")); + +// Using a JSonObject +JsonObject result = flightOffers[0].getResponse().getResult(); +FlightOffer[] flightOffersPrediction = amadeus.shopping.flightOffers.prediction.post(result); + +// Using a String +String body = flightOffers[0].getResponse().getBody(); +FlightOffer[] flightOffersPrediction = amadeus.shopping.flightOffers.prediction.post(body); + // Flight Check-in Links CheckinLink[] checkinLinks = amadeus.referenceData.urls.checkinLinks.get(Params .with("airlineCode", "BA")); diff --git a/src/main/java/com/amadeus/Amadeus.java b/src/main/java/com/amadeus/Amadeus.java index 169e9f26..915b6354 100644 --- a/src/main/java/com/amadeus/Amadeus.java +++ b/src/main/java/com/amadeus/Amadeus.java @@ -83,7 +83,7 @@ public static Configuration builder(@NonNull String clientId, @NonNull String cl */ public static Configuration builder(Map environment) { String clientId = environment.get("AMADEUS_CLIENT_ID"); - String clientSecret = environment.get("AMADEUS_CLIENT_ID"); + String clientSecret = environment.get("AMADEUS_CLIENT_SECRET"); Configuration configuration = Amadeus.builder(clientId, clientSecret); configuration.parseEnvironment(environment); diff --git a/src/main/java/com/amadeus/HTTPClient.java b/src/main/java/com/amadeus/HTTPClient.java index 51785bfb..1faf2645 100644 --- a/src/main/java/com/amadeus/HTTPClient.java +++ b/src/main/java/com/amadeus/HTTPClient.java @@ -5,6 +5,7 @@ import com.amadeus.exceptions.NetworkException; import com.amadeus.exceptions.ResponseException; import com.amadeus.resources.Resource; +import com.google.gson.JsonObject; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; @@ -36,7 +37,7 @@ protected HTTPClient(Configuration configuration) { * @see Amadeus#get(String, Params) */ public Response get(String path) throws ResponseException { - return request(Constants.GET, path, null); + return request(Constants.GET, path, null,null); } /** @@ -63,7 +64,7 @@ public Response get(String path) throws ResponseException { * @return a Response object containing the status code, body, and parsed data. */ public Response get(String path, Params params) throws ResponseException { - return request(Constants.GET, path, params); + return request(Constants.GET, path, params, null); } /** @@ -73,7 +74,7 @@ public Response get(String path, Params params) throws ResponseException { * @see Amadeus#post(String, Params) */ public Response post(String path) throws ResponseException { - return request(Constants.POST, path, null); + return request(Constants.POST, path, null, null); } /** @@ -100,7 +101,57 @@ public Response post(String path) throws ResponseException { * @return a Response object containing the status code, body, and parsed data. */ public Response post(String path, Params params) throws ResponseException { - return request(Constants.POST, path, params); + return request(Constants.POST, path, params, null); + } + + /** + *

+ * A helper module for making generic POST requests calls. It is used by + * every namespaced API POST method. + *

+ * + *
+   *   amadeus.foo.bar.post(Params.with("airline", "1X"));
+   * 
+ * + *

+ * It can be used to make any generic API call that is automatically + * authenticated using your API credentials: + *

+ * + *
+   *    amadeus.post("/v1/foo/bar", Params.with("airline", "1X"));
+   * 
+ * + * @param path The full path for the API call + * @param body The optional POST params to pass to the API + * @return a Response object containing the status code, body, and parsed data. + */ + public Response post(String path, String body) throws ResponseException { + return request(Constants.POST, path, null, body); + } + + /** + *

+ * A helper module for making generic POST requests calls. It is used by + * every namespaced API POST method. + *

+ * + *

+ * It can be used to make any generic API call that is automatically + * authenticated using your API credentials: + *

+ * + *
+   *    amadeus.post("/v1/foo/bar", { "foo" : "bar" })
+   * 
+ * + * @param path The full path for the API call + * @param body The POST JsonObject body to pass to the API + * @return a Response object containing the status code, body, and parsed data. + */ + public Response post(String path, JsonObject body) throws ResponseException { + return request(Constants.POST, path, null, body.toString()); } /** @@ -110,9 +161,9 @@ public Response post(String path, Params params) throws ResponseException { * * @hides as only used internally */ - public Response unauthenticatedRequest(String verb, String path, Params params, - String bearerToken) throws ResponseException { - Request request = buildRequest(verb, path, params, bearerToken); + public Response unauthenticatedRequest(String verb, String path, Params params, String body, + String bearerToken) throws ResponseException { + Request request = buildRequest(verb, path, params, body, bearerToken); log(request); return execute(request); } @@ -198,13 +249,15 @@ public Resource[] last(Resource resource) throws ResponseException { } // A generic method for making requests of any verb. - protected Response request(String verb, String path, Params params) throws ResponseException { - return unauthenticatedRequest(verb, path, params, accessToken.getBearerToken()); + protected Response request(String verb, String path, Params params, String body) + throws ResponseException { + return unauthenticatedRequest(verb, path, params, body, accessToken.getBearerToken()); } // Builds a request - protected Request buildRequest(String verb, String path, Params params, String bearerToken) { - return new Request(verb, path, params, bearerToken, this); + protected Request buildRequest(String verb, String path, Params params, String body, + String bearerToken) { + return new Request(verb, path, params, body, bearerToken, this); } // A simple log that only triggers if we are in debug mode @@ -238,6 +291,8 @@ private Request fetch(Request request) throws NetworkException { // Writes the parameters to the request. private void write(Request request) throws IOException { + + if (request.getVerb() == Constants.POST && request.getParams() != null) { OutputStream os = request.getConnection().getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); @@ -246,6 +301,16 @@ private void write(Request request) throws IOException { writer.close(); os.close(); } + if (request.getVerb() == Constants.POST && request.getParams() == null) { + OutputStream os = request.getConnection().getOutputStream(); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); + if (request.getBody() != null) { + writer.write(request.getBody()); + } + writer.flush(); + writer.close(); + os.close(); + } } /** @@ -263,7 +328,7 @@ protected Response page(String pageName, Response response) throws ResponseExcep Params params = (Params) request.getParams().clone(); params.put("page[offset]", pageNumber); - return request(request.getVerb(), request.getPath(), params); + return request(request.getVerb(), request.getPath(), params, "emptyBody"); } catch (NullPointerException e) { return null; } @@ -277,4 +342,4 @@ protected Resource[] page(String pageName, Resource resource) throws ResponseExc Response response = page(pageName, resource.getResponse()); return Resource.fromArray(response, resource.getDeSerializationClass()); } -} \ No newline at end of file +} diff --git a/src/main/java/com/amadeus/Request.java b/src/main/java/com/amadeus/Request.java index 278ac95d..7f86a354 100644 --- a/src/main/java/com/amadeus/Request.java +++ b/src/main/java/com/amadeus/Request.java @@ -2,6 +2,7 @@ import com.amadeus.Constants; +import com.google.gson.JsonElement; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; @@ -40,6 +41,10 @@ public class Request { * The params to send to the API endpoint. */ private @Getter Params params; + /** + * The body to send to the API endpoint. + */ + private @Getter String body; /** * The bearer token used to authenticate the API call. */ @@ -80,7 +85,7 @@ public class Request { // The connection used to make the API call. private @Getter HttpURLConnection connection; - protected Request(String verb, String path, Params params, String bearerToken, + protected Request(String verb, String path, Params params, String body, String bearerToken, HTTPClient client) { Configuration config = client.getConfiguration(); @@ -88,6 +93,7 @@ protected Request(String verb, String path, Params params, String bearerToken, this.host = config.getHost(); this.path = path; this.params = params; + this.body = body; this.bearerToken = bearerToken; this.languageVersion = System.getProperty("java.version"); this.clientVersion = Amadeus.VERSION; @@ -130,6 +136,7 @@ private void prepareHeaders() { this.headers = new HashMap(); headers.put(Constants.USER_AGENT, buildUserAgent()); headers.put(Constants.ACCEPT, "application/json, application/vnd.amadeus+json"); + if (bearerToken != null) { headers.put(Constants.AUTHORIZATION, bearerToken); } diff --git a/src/main/java/com/amadeus/client/AccessToken.java b/src/main/java/com/amadeus/client/AccessToken.java index 524d71c6..d5f93339 100644 --- a/src/main/java/com/amadeus/client/AccessToken.java +++ b/src/main/java/com/amadeus/client/AccessToken.java @@ -70,7 +70,8 @@ private Response fetchAccessToken() throws ResponseException { Params.with(Constants.GRANT_TYPE, Constants.CLIENT_CREDENTIALS) .and(Constants.CLIENT_ID, config.getClientId()) .and(Constants.CLIENT_SECRET, config.getClientSecret()), - null + null, + null ); } diff --git a/src/main/java/com/amadeus/resources/FlightOffer.java b/src/main/java/com/amadeus/resources/FlightOffer.java index cb31cc0d..912e5b7f 100644 --- a/src/main/java/com/amadeus/resources/FlightOffer.java +++ b/src/main/java/com/amadeus/resources/FlightOffer.java @@ -15,6 +15,7 @@ protected FlightOffer() {} private @Getter String type; private @Getter String id; private @Getter OfferItem[] offerItems; + private @Getter String choiceProbability; /** * An FlightOffer-related object as returned by the FlightOffers API. diff --git a/src/main/java/com/amadeus/shopping/FlightOffers.java b/src/main/java/com/amadeus/shopping/FlightOffers.java index 897404de..c1f53aa7 100644 --- a/src/main/java/com/amadeus/shopping/FlightOffers.java +++ b/src/main/java/com/amadeus/shopping/FlightOffers.java @@ -6,6 +6,7 @@ import com.amadeus.exceptions.ResponseException; import com.amadeus.resources.FlightOffer; import com.amadeus.resources.Resource; +import com.amadeus.shopping.flightOffers.Prediction; import com.google.gson.Gson; /** @@ -24,6 +25,7 @@ */ public class FlightOffers { private Amadeus client; + public Prediction prediction; /** * Constructor. @@ -31,6 +33,7 @@ public class FlightOffers { */ public FlightOffers(Amadeus client) { this.client = client; + this.prediction = new Prediction(client); } /** diff --git a/src/main/java/com/amadeus/shopping/flightOffers/Prediction.java b/src/main/java/com/amadeus/shopping/flightOffers/Prediction.java new file mode 100644 index 00000000..a66895c2 --- /dev/null +++ b/src/main/java/com/amadeus/shopping/flightOffers/Prediction.java @@ -0,0 +1,88 @@ +package com.amadeus.shopping.flightOffers; + +import com.amadeus.Amadeus; +import com.amadeus.Response; +import com.amadeus.exceptions.ResponseException; +import com.amadeus.resources.FlightOffer; +import com.amadeus.resources.Resource; + +import com.google.gson.JsonObject; + + + + + +/** + *

+ * A namespaced client for the + * /v1/shopping/flight-offers/prediction endpoints. + *

+ * + *

+ * Access via the Amadeus client object. + *

+ * + *
+ * Amadeus amadeus = Amadeus.builder(API_KEY, API_SECRET).build();
+ * amadeus.shopping.flightOffers.prediction;
+ */ +public class Prediction { + + private Amadeus client; + + /** + * Constructor. + * + * @hide + */ + public Prediction(Amadeus client) { + this.client = client; + } + + /** + *

+ * This machine learning API is based on a prediction model that takes the response of a flight + * search as input (Flight Low-fare Search) and predict, for each itinerary, the probably for a + * travel to select it. + *

+ * + *
+   * amadeus.shopping.flightOffers.prediction.post(body);
+ * + * @param body the parameters to send to the API as a JSonObject + * @return an API resource + * @throws ResponseException when an exception occurs + */ + public FlightOffer[] post(JsonObject body) throws ResponseException { + Response response = client.post("/v1/shopping/flight-offers/prediction", body); + return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class); + } + + /** + *

+ * This machine learning API is based on a prediction model that takes the response of a flight + * search as input (Flight Low-fare Search) and predict, for each itinerary, the probably for a + * travel to select it. + *

+ * + *
+   * amadeus.shopping.flightOffers.prediction.post(body);
+ * + * @param body the parameters to send to the API as a String + * @return an API resource + * @throws ResponseException when an exception occurs + */ + public FlightOffer[] post(String body) throws ResponseException { + Response response = client.post("/v1/shopping/flight-offers/prediction", body); + return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class); + } + + /** + * Convenience method for calling post without any parameters. + * + * @see Prediction#post() + */ + public FlightOffer[] post() throws ResponseException { + return post((String) null); + } +} diff --git a/src/test/java/com/amadeus/AccessTokenTest.java b/src/test/java/com/amadeus/AccessTokenTest.java index afb0dfd5..42b1341f 100644 --- a/src/test/java/com/amadeus/AccessTokenTest.java +++ b/src/test/java/com/amadeus/AccessTokenTest.java @@ -37,7 +37,7 @@ public class AccessTokenTest { .with("grant_type", "client_credentials") .and("client_id", "client_id") .and("client_secret", "client_secret"), - null)).thenReturn(response); + null, null)).thenReturn(response); } @Test public void testNewToken() throws ResponseException { @@ -50,7 +50,8 @@ public class AccessTokenTest { accessToken.getBearerToken(); verify(client, times(1)) - .unauthenticatedRequest(anyString(), anyString(), any(Params.class), (String) isNull()); + .unauthenticatedRequest(anyString(), anyString(), + any(Params.class), (String) isNull(), (String) isNull()); } @Test public void testExpiredToken() throws ResponseException { @@ -61,6 +62,7 @@ public class AccessTokenTest { accessToken.getBearerToken(); verify(client, times(2)) - .unauthenticatedRequest(anyString(), anyString(), any(Params.class), (String) isNull()); + .unauthenticatedRequest(anyString(), anyString(), + any(Params.class), (String) isNull(), (String) isNull()); } } diff --git a/src/test/java/com/amadeus/HTTPClientTest.java b/src/test/java/com/amadeus/HTTPClientTest.java index debbcbc6..46400b07 100644 --- a/src/test/java/com/amadeus/HTTPClientTest.java +++ b/src/test/java/com/amadeus/HTTPClientTest.java @@ -35,6 +35,7 @@ public class HTTPClientTest { Request request; HttpURLConnection connection; Logger logger; + String body; @Before public void setup() { client = mock(Amadeus.class); @@ -44,6 +45,7 @@ public class HTTPClientTest { request = mock(Request.class); connection = mock(HttpURLConnection.class); logger = mock(Logger.class); + body = "[{}]"; when(client.getConfiguration()).thenReturn(configuration); when(configuration.getLogger()).thenReturn(logger); @@ -53,33 +55,40 @@ public class HTTPClientTest { @Test public void testGetWithoutParams() throws ResponseException { when(client.get(anyString())).thenCallRealMethod(); client.get("/foo"); - verify(client, times(1)).request("GET", "/foo", null); + verify(client, times(1)).request("GET", "/foo", null, null); } @Test public void testGetWithParams() throws ResponseException { when(client.get(anyString(), any(Params.class))).thenCallRealMethod(); client.get("/foo", params); - verify(client, times(1)).request("GET", "/foo", params); + verify(client, times(1)).request("GET", "/foo", params, null); } - @Test public void testPostWithoutParams() throws ResponseException { + @Test public void testPostWithoutParamsWithoutBody() throws ResponseException { when(client.post(anyString())).thenCallRealMethod(); client.post("/foo"); - verify(client, times(1)).request("POST", "/foo", null); + verify(client, times(1)).request("POST", "/foo", null, null); } - @Test public void testPostWitParams() throws ResponseException { + @Test public void testPostWithParamsWithoutBody() throws ResponseException { when(client.post(anyString(), any(Params.class))).thenCallRealMethod(); client.post("/foo", params); - verify(client, times(1)).request("POST", "/foo", params); + verify(client, times(1)).request("POST", "/foo", params, null); + } + + @Test public void testPostWithoutParamsWithBody() throws ResponseException { + when(client.post(anyString(), anyString())).thenCallRealMethod(); + client.post("/foo", "[{}]"); + verify(client, times(1)).request("POST", "/foo", null, body); } @Test public void testRequest() throws ResponseException { client.accessToken = accessToken; when(accessToken.getBearerToken()).thenReturn("token"); - when(client.request(anyString(), anyString(), any(Params.class))).thenCallRealMethod(); - client.request("GET","/foo", params); - verify(client, times(1)).unauthenticatedRequest("GET", "/foo", params, "token"); + when(client.request(anyString(), anyString(), + any(Params.class), anyString())).thenCallRealMethod(); + client.request("GET","/foo", params, body); + verify(client, times(1)).unauthenticatedRequest("GET", "/foo", params, body, "token"); } @Test public void testUnauthenticatedGetRequest() throws ResponseException, IOException { @@ -93,10 +102,10 @@ public class HTTPClientTest { when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("GET", "/foo", params, null)).thenReturn(request); - when(client.unauthenticatedRequest("GET", "/foo", params, null)).thenCallRealMethod(); + when(client.buildRequest("GET", "/foo", params, null,null)).thenReturn(request); + when(client.unauthenticatedRequest("GET", "/foo", params, null,null)).thenCallRealMethod(); - Response response = client.unauthenticatedRequest("GET", "/foo", params, null); + Response response = client.unauthenticatedRequest("GET", "/foo", params, null, null); assertTrue(response.isParsed()); assertEquals(((JsonArray) response.getData()).size(), 1); @@ -114,18 +123,20 @@ public class HTTPClientTest { when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("POST", "/foo", params, null)).thenReturn(request); - when(client.unauthenticatedRequest("POST", "/foo", params, null)).thenCallRealMethod(); + when(client.buildRequest("POST", "/foo", params, null, null)).thenReturn(request); + when(client.unauthenticatedRequest("POST", "/foo", + params, null,null)).thenCallRealMethod(); - Response response = client.unauthenticatedRequest("POST", "/foo", params, null); + Response response = client.unauthenticatedRequest("POST", "/foo", + params, null,null); assertTrue(response.isParsed()); assertEquals(((JsonArray) response.getData()).size(), 1); } - @Test public void testUnauthenticatedPostWithoutParams() throws ResponseException, IOException { + @Test public void testUnauthenticatedPostWithoutBody() throws ResponseException, IOException { when(request.getVerb()).thenReturn("POST"); - when(request.getParams()).thenReturn(null); + when(request.getBody()).thenReturn(null); when(request.getConnection()).thenReturn(connection); when(connection.getOutputStream()).thenReturn(mock(OutputStream.class)); @@ -135,10 +146,10 @@ public class HTTPClientTest { when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("POST", "/foo", null, null)).thenReturn(request); - when(client.unauthenticatedRequest("POST", "/foo", null, null)).thenCallRealMethod(); + when(client.buildRequest("POST", "/foo", null, null,null)).thenReturn(request); + when(client.unauthenticatedRequest("POST", "/foo", null, null,null)).thenCallRealMethod(); - Response response = client.unauthenticatedRequest("POST", "/foo", null, null); + Response response = client.unauthenticatedRequest("POST", "/foo", null, null,null); assertTrue(response.isParsed()); assertEquals(((JsonArray) response.getData()).size(), 1); @@ -158,10 +169,10 @@ public void tesFailedUnauthenticatedPostRequest() throws ResponseException, IOEx when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("POST", "/foo", params, null)).thenReturn(request); - when(client.unauthenticatedRequest("POST", "/foo", params, null)).thenCallRealMethod(); + when(client.buildRequest("POST", "/foo", params, null,null)).thenReturn(request); + when(client.unauthenticatedRequest("POST", "/foo", params, null,null)).thenCallRealMethod(); - client.unauthenticatedRequest("POST", "/foo", params, null); + client.unauthenticatedRequest("POST", "/foo", params, null,null); } @Test public void testLogIfDebug() throws ResponseException, IOException { @@ -176,10 +187,10 @@ public void tesFailedUnauthenticatedPostRequest() throws ResponseException, IOEx when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("GET", "/foo", null, null)).thenReturn(request); - when(client.unauthenticatedRequest("GET", "/foo", null, null)).thenCallRealMethod(); + when(client.buildRequest("GET", "/foo", null, null,null)).thenReturn(request); + when(client.unauthenticatedRequest("GET", "/foo", null, null,null)).thenCallRealMethod(); - client.unauthenticatedRequest("GET", "/foo", null, null); + client.unauthenticatedRequest("GET", "/foo", null, null,null); verify(logger, times(2)).info(anyString()); } @@ -195,10 +206,10 @@ public void tesFailedUnauthenticatedPostRequest() throws ResponseException, IOEx when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("GET", "/foo", null, null)).thenReturn(request); - when(client.unauthenticatedRequest("GET", "/foo", null, null)).thenCallRealMethod(); + when(client.buildRequest("GET", "/foo", null, null,null)).thenReturn(request); + when(client.unauthenticatedRequest("GET", "/foo", null, null,null)).thenCallRealMethod(); - client.unauthenticatedRequest("GET", "/foo", null, null); + client.unauthenticatedRequest("GET", "/foo", null, null,null); verify(logger, times(0)).info(anyString()); } @@ -215,17 +226,17 @@ public void tesFailedUnauthenticatedPostRequest() throws ResponseException, IOEx when(connection.getInputStream()).thenReturn( new ByteArrayInputStream("{ \"data\": [{}]}".getBytes())); - when(client.buildRequest("GET", "/foo", null, null)).thenReturn(request); - when(client.unauthenticatedRequest("GET", "/foo", null, null)).thenCallRealMethod(); + when(client.buildRequest("GET", "/foo", null, null,null)).thenReturn(request); + when(client.unauthenticatedRequest("GET", "/foo", null, null,null)).thenCallRealMethod(); - client.unauthenticatedRequest("GET", "/foo", null, null); + client.unauthenticatedRequest("GET", "/foo", null, null,null); verify(logger, times(0)).info(anyString()); } @Test public void testBuildRequest() { - when(client.buildRequest("GET", "/foo", null, null)).thenCallRealMethod(); - Request request = client.buildRequest("GET", "/foo", null, null); + when(client.buildRequest("GET", "/foo", null, null,null)).thenCallRealMethod(); + Request request = client.buildRequest("GET", "/foo", null, null,null); assertNotNull(request); } @@ -306,11 +317,13 @@ public void tesFailedUnauthenticatedPostRequest() throws ResponseException, IOEx when(request.getPath()).thenReturn("/foo"); when(request.getParams()).thenReturn(Params.with("foo", "bar")); - when(client.request(anyString(), anyString(), any(Params.class))).thenReturn(response); + when(client.request(anyString(), anyString(), + any(Params.class), anyString())).thenReturn(response); + client.accessToken = accessToken; + when(accessToken.getBearerToken()).thenReturn("token"); when(client.page("next", response)).thenCallRealMethod(); Response nextResponse = client.page("next", response); - assertNotNull(nextResponse); } @@ -327,7 +340,8 @@ public void tesFailedUnauthenticatedPostRequest() throws ResponseException, IOEx when(request.getPath()).thenReturn("/foo"); when(request.getParams()).thenReturn(Params.with("foo", "bar")); - when(client.request(anyString(), anyString(), any(Params.class))).thenReturn(response); + when(client.request(anyString(), anyString(), + any(Params.class), anyString())).thenReturn(response); when(client.page("next", response)).thenCallRealMethod(); Response nextResponse = client.page("next", response); diff --git a/src/test/java/com/amadeus/NamespaceTest.java b/src/test/java/com/amadeus/NamespaceTest.java index c06009ca..5ecb118f 100644 --- a/src/test/java/com/amadeus/NamespaceTest.java +++ b/src/test/java/com/amadeus/NamespaceTest.java @@ -14,6 +14,7 @@ import com.amadeus.shopping.HotelOffer; import com.amadeus.shopping.HotelOffers; import com.amadeus.shopping.HotelOffersByHotel; +import com.amadeus.shopping.flightOffers.Prediction; import com.amadeus.travel.analytics.airTraffic.Booked; import com.amadeus.travel.analytics.airTraffic.BusiestPeriod; import com.amadeus.travel.analytics.airTraffic.Searched; @@ -32,6 +33,7 @@ public class NamespaceTest { private Params params; private Response singleResponse; private Response multiResponse; + private String body; @Test public void testAllNamespacesExist() { @@ -49,6 +51,7 @@ public void testAllNamespacesExist() { TestCase.assertNotNull(client.shopping.flightDates); TestCase.assertNotNull(client.shopping.flightDestinations); TestCase.assertNotNull(client.shopping.flightOffers); + TestCase.assertNotNull(client.shopping.flightOffers.prediction); TestCase.assertNotNull(client.shopping.hotelOffers); TestCase.assertNotNull(client.shopping.hotelOffersByHotel); TestCase.assertNotNull(client.shopping.hotelOffer("XXX")); @@ -58,6 +61,7 @@ public void testAllNamespacesExist() { public void setup() { client = Mockito.mock(Amadeus.class); params = Params.with("airline", "1X"); + body = "{ \"data\": [{}]}"; // Prepare a plural response JsonArray jsonArray = new JsonArray(); @@ -254,4 +258,17 @@ public void testGetMethods() throws ResponseException { TestCase.assertNotNull(hotelOffer.get()); TestCase.assertNotNull(hotelOffer.get(params)); } + + @Test + public void testPostMethods() throws ResponseException { + // Testing flight choice prediction + Mockito.when(client.post("/v1/shopping/flight-offers/prediction", (String) null)) + .thenReturn(multiResponse); + Mockito.when(client.post("/v1/shopping/flight-offers/prediction", body)) + .thenReturn(multiResponse); + Prediction flightOffersPrediction = new Prediction(client); + TestCase.assertNotNull(flightOffersPrediction.post()); + TestCase.assertNotNull(flightOffersPrediction.post(body)); + TestCase.assertEquals(flightOffersPrediction.post().length, 2); + } } diff --git a/src/test/java/com/amadeus/RequestTest.java b/src/test/java/com/amadeus/RequestTest.java index e3f35628..8b045134 100644 --- a/src/test/java/com/amadeus/RequestTest.java +++ b/src/test/java/com/amadeus/RequestTest.java @@ -11,7 +11,7 @@ public class RequestTest { @Test public void testInitializer() { Amadeus amadeus = Amadeus.builder("123", "234").build(); Params params = Params.with("foo", "bar"); - Request request = new Request("GET", "/foo/bar", params, "token", amadeus); + Request request = new Request("GET", "/foo/bar", params, null,"token", amadeus); assertEquals(request.getVerb(), "GET"); assertEquals(request.getHost(), "test.api.amadeus.com"); @@ -35,7 +35,7 @@ public class RequestTest { @Test public void testInitializerWithoutBearerToken() { Amadeus amadeus = Amadeus.builder("123", "234").build(); Params params = Params.with("foo", "bar"); - Request request = new Request("GET", "/foo/bar", params, null, amadeus); + Request request = new Request("GET", "/foo/bar", params, null, null, amadeus); assertEquals(request.getHeaders().size(), 2); assertEquals(request.getHeaders().get("Authorization"), null); @@ -47,7 +47,7 @@ public class RequestTest { .setCustomAppId("amadeus-cli") .build(); Params params = Params.with("foo", "bar"); - Request request = new Request("GET", "/foo/bar", params, "token", amadeus); + Request request = new Request("GET", "/foo/bar", params, null,"token", amadeus); assertTrue(request.getHeaders() .get("User-Agent").matches("amadeus-java/.* java/.* amadeus-cli/.*")); @@ -57,7 +57,7 @@ public class RequestTest { Amadeus amadeus = Amadeus.builder("123", "234") .setSsl(false) .build(); - Request request = new Request("GET", "/foo/bar", null, "token", amadeus); + Request request = new Request("GET", "/foo/bar", null, null, "token", amadeus); assertEquals(request.getScheme(), "http"); } @@ -65,27 +65,27 @@ public class RequestTest { @Test public void testBuildUriForGetRequest() { Amadeus amadeus = Amadeus.builder("123", "234").build(); Params params = Params.with("foo", "bar"); - Request request = new Request("GET", "/foo/bar", params, null, amadeus); + Request request = new Request("GET", "/foo/bar", params, null,null, amadeus); assertEquals(request.getUri(), "https://test.api.amadeus.com:443/foo/bar?foo=bar"); } @Test public void testBuildUriForGetRequestWithoutParams() { Amadeus amadeus = Amadeus.builder("123", "234").build(); - Request request = new Request("GET", "/foo/bar", null, null, amadeus); + Request request = new Request("GET", "/foo/bar", null, null,null, amadeus); assertEquals(request.getUri(), "https://test.api.amadeus.com:443/foo/bar?"); } @Test public void testBuildUriForPostRequest() { Amadeus amadeus = Amadeus.builder("123", "234").build(); Params params = Params.with("foo", "bar"); - Request request = new Request("POST", "/foo/bar", params, null, amadeus); + Request request = new Request("POST", "/foo/bar", params, null,null, amadeus); assertEquals(request.getUri(), "https://test.api.amadeus.com:443/foo/bar?"); } @Test public void testToString() { Amadeus amadeus = Amadeus.builder("123", "234").build(); - Request request = new Request("GET", "/foo/bar", null, null, amadeus); + Request request = new Request("GET", "/foo/bar", null, null,null, amadeus); assertTrue(request.toString() .startsWith("Request(verb=GET, scheme=https, host=test.api.amadeus.com")); @@ -93,7 +93,7 @@ public class RequestTest { @Test public void testEstablishConnection() throws IOException { Amadeus amadeus = Amadeus.builder("123", "234").build(); - Request request = new Request("POST", "/v1/security/oauth2/token", null, null, amadeus); + Request request = new Request("POST", "/v1/security/oauth2/token", null, null,null, amadeus); request.establishConnection(); assertNotNull(request.getConnection()); }