Skip to content

Commit 3e7d425

Browse files
authored
Merge pull request #96 from amadeus4dev/fix-parse-exception-204
avoid parser exception on 204 no content
2 parents bd1adf4 + e7061d8 commit 3e7d425

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ FlightOfferSearch[] flightOffersSearches = amadeus.shopping.flightOffersSearch.p
196196
// Retrieve a flight order
197197
FlightOrder order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").get();
198198
// Cancel a flight order
199-
FlightOrder order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
199+
Response order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
200200

201201
// Flight Offers price
202202
FlightPrice[] flightPricing = amadeus.shopping.flightOffersSearch.pricing.post(

src/main/java/com/amadeus/Response.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.amadeus;
22

3-
import com.amadeus.Constants;
43
import com.amadeus.exceptions.AuthenticationException;
54
import com.amadeus.exceptions.ClientException;
65
import com.amadeus.exceptions.NotFoundException;
@@ -61,7 +60,9 @@ protected Response(Request request) {
6160
// Tries to parse the raw response from the request.
6261
protected void parse(HTTPClient client) {
6362
parseStatusCode();
64-
parseData(client);
63+
if (this.statusCode != 204) {
64+
parseData(client);
65+
}
6566
}
6667

6768
// Detects of any exceptions have occured and throws the appropriate exceptions.
@@ -75,6 +76,8 @@ protected void detectError(HTTPClient client) throws ResponseException {
7576
exception = new AuthenticationException(this);
7677
} else if (statusCode >= 400) {
7778
exception = new ClientException(this);
79+
} else if (statusCode == 204) {
80+
return;
7881
} else if (!parsed) {
7982
exception = new ParserException(this);
8083
}

src/main/java/com/amadeus/booking/FlightOrder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public com.amadeus.resources.FlightOrder get(Params params) throws ResponseExcep
5353
String path = String.format("/v1/booking/flight-orders/%s", flightOfferId);
5454
Response response = client.get(path, params);
5555
return (com.amadeus.resources.FlightOrder) Resource.fromObject(
56-
response, com.amadeus.resources.FlightOrder.class);
56+
response, com.amadeus.resources.FlightOrder.class);
5757
}
5858

5959
/**
@@ -72,26 +72,25 @@ public com.amadeus.resources.FlightOrder get() throws ResponseException {
7272
* </p>
7373
*
7474
* <pre>
75-
* FlightOrder order = amadeus.booking.flightOrder.(
75+
* Response order = amadeus.booking.flightOrder.(
7676
* "eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
7777
* </pre>
7878
* @param params the parameters to send to the API
7979
* @return an API response object
8080
* @throws ResponseException when an exception occurs
8181
*/
8282

83-
public com.amadeus.resources.FlightOrder delete(Params params) throws ResponseException {
83+
public Response delete(Params params) throws ResponseException {
8484
String path = String.format("/v1/booking/flight-orders/%s", flightOfferId);
8585
Response response = client.delete(path, params);
86-
return (com.amadeus.resources.FlightOrder) Resource.fromObject(
87-
response, com.amadeus.resources.FlightOrder.class);
86+
return response;
8887
}
89-
88+
9089
/**
9190
* Convenience method for calling <code>delete</code> without any parameters.
9291
* @see com.amadeus.booking.FlightOrder#delete()
9392
*/
94-
public com.amadeus.resources.FlightOrder delete() throws ResponseException {
93+
public Response delete() throws ResponseException {
9594
return delete(null);
9695
}
97-
}
96+
}

src/test/java/com/amadeus/ResponseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ public class ResponseTest {
107107
assertNull(response.getData());
108108
}
109109

110+
@Test public void testNoContent() throws IOException {
111+
when(connection.getResponseCode()).thenReturn(204);
112+
when(connection.getHeaderField("Content-Type")).thenReturn(
113+
"application/json");
114+
when(connection.getInputStream()).thenReturn(
115+
new ByteArrayInputStream("".getBytes()));
116+
117+
response.parse(client);
118+
119+
assertEquals(response.getStatusCode(), 204);
120+
assertEquals(response.getBody(), null);
121+
assertFalse(response.isParsed());
122+
assertNull(response.getResult());
123+
assertNull(response.getData());
124+
}
125+
110126
@Test public void testEmptyConnection() throws IOException {
111127
InputStream stream = mock(ByteArrayInputStream.class);
112128
when(connection.getResponseCode()).thenThrow(new IOException());
@@ -212,6 +228,16 @@ public void detectParserException() throws ResponseException, IOException {
212228
response.detectError(client);
213229
}
214230

231+
@Test public void detectNoExceptionNoContent() throws ResponseException, IOException {
232+
when(connection.getResponseCode()).thenReturn(204);
233+
when(connection.getHeaderField("Content-Type")).thenReturn(
234+
"application/json");
235+
when(connection.getInputStream()).thenReturn(
236+
new ByteArrayInputStream("{}".getBytes()));
237+
response.parse(client);
238+
response.detectError(client);
239+
}
240+
215241
@Test public void testToString() {
216242
assertTrue(response.toString().startsWith("Response("));
217243
}

0 commit comments

Comments
 (0)