Skip to content

Commit

Permalink
Merge pull request #96 from amadeus4dev/fix-parse-exception-204
Browse files Browse the repository at this point in the history
avoid parser exception on 204 no content
  • Loading branch information
tsolakoua authored Feb 5, 2021
2 parents bd1adf4 + e7061d8 commit 3e7d425
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ FlightOfferSearch[] flightOffersSearches = amadeus.shopping.flightOffersSearch.p
// Retrieve a flight order
FlightOrder order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").get();
// Cancel a flight order
FlightOrder order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
Response order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();

// Flight Offers price
FlightPrice[] flightPricing = amadeus.shopping.flightOffersSearch.pricing.post(
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/amadeus/Response.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.amadeus;

import com.amadeus.Constants;
import com.amadeus.exceptions.AuthenticationException;
import com.amadeus.exceptions.ClientException;
import com.amadeus.exceptions.NotFoundException;
Expand Down Expand Up @@ -61,7 +60,9 @@ protected Response(Request request) {
// Tries to parse the raw response from the request.
protected void parse(HTTPClient client) {
parseStatusCode();
parseData(client);
if (this.statusCode != 204) {
parseData(client);
}
}

// Detects of any exceptions have occured and throws the appropriate exceptions.
Expand All @@ -75,6 +76,8 @@ protected void detectError(HTTPClient client) throws ResponseException {
exception = new AuthenticationException(this);
} else if (statusCode >= 400) {
exception = new ClientException(this);
} else if (statusCode == 204) {
return;
} else if (!parsed) {
exception = new ParserException(this);
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/amadeus/booking/FlightOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public com.amadeus.resources.FlightOrder get(Params params) throws ResponseExcep
String path = String.format("/v1/booking/flight-orders/%s", flightOfferId);
Response response = client.get(path, params);
return (com.amadeus.resources.FlightOrder) Resource.fromObject(
response, com.amadeus.resources.FlightOrder.class);
response, com.amadeus.resources.FlightOrder.class);
}

/**
Expand All @@ -72,26 +72,25 @@ public com.amadeus.resources.FlightOrder get() throws ResponseException {
* </p>
*
* <pre>
* FlightOrder order = amadeus.booking.flightOrder.(
* Response order = amadeus.booking.flightOrder.(
* "eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
* </pre>
* @param params the parameters to send to the API
* @return an API response object
* @throws ResponseException when an exception occurs
*/

public com.amadeus.resources.FlightOrder delete(Params params) throws ResponseException {
public Response delete(Params params) throws ResponseException {
String path = String.format("/v1/booking/flight-orders/%s", flightOfferId);
Response response = client.delete(path, params);
return (com.amadeus.resources.FlightOrder) Resource.fromObject(
response, com.amadeus.resources.FlightOrder.class);
return response;
}

/**
* Convenience method for calling <code>delete</code> without any parameters.
* @see com.amadeus.booking.FlightOrder#delete()
*/
public com.amadeus.resources.FlightOrder delete() throws ResponseException {
public Response delete() throws ResponseException {
return delete(null);
}
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/amadeus/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ public class ResponseTest {
assertNull(response.getData());
}

@Test public void testNoContent() throws IOException {
when(connection.getResponseCode()).thenReturn(204);
when(connection.getHeaderField("Content-Type")).thenReturn(
"application/json");
when(connection.getInputStream()).thenReturn(
new ByteArrayInputStream("".getBytes()));

response.parse(client);

assertEquals(response.getStatusCode(), 204);
assertEquals(response.getBody(), null);
assertFalse(response.isParsed());
assertNull(response.getResult());
assertNull(response.getData());
}

@Test public void testEmptyConnection() throws IOException {
InputStream stream = mock(ByteArrayInputStream.class);
when(connection.getResponseCode()).thenThrow(new IOException());
Expand Down Expand Up @@ -212,6 +228,16 @@ public void detectParserException() throws ResponseException, IOException {
response.detectError(client);
}

@Test public void detectNoExceptionNoContent() throws ResponseException, IOException {
when(connection.getResponseCode()).thenReturn(204);
when(connection.getHeaderField("Content-Type")).thenReturn(
"application/json");
when(connection.getInputStream()).thenReturn(
new ByteArrayInputStream("{}".getBytes()));
response.parse(client);
response.detectError(client);
}

@Test public void testToString() {
assertTrue(response.toString().startsWith("Response("));
}
Expand Down

0 comments on commit 3e7d425

Please sign in to comment.