Skip to content

Commit 15d1011

Browse files
authored
Add DELETE support for Flight Order Management (#63)
* manipulate client to accept delete and add support for cancel flight order * add tests for httpclient and delete method * fix indentation errors * fix docs
1 parent 0d7d3f6 commit 15d1011

File tree

6 files changed

+119
-2
lines changed

6 files changed

+119
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ FlightOfferSearch[] flightOffersSearches = amadeus.shopping.flightOffersSearch.p
239239

240240
// Flight Order Management
241241
// The flightOrderID comes from the Flight Create Orders (in test environment it's temporary)
242+
// Retrieve a flight order
242243
FlightOrder order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").get();
244+
// Cancel a flight order
245+
FlightOrder order = amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
243246

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

src/main/java/com/amadeus/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public final class Constants {
1212
public static final String GET = "GET";
1313
public static final String POST = "POST";
1414
public static final String PUT = "PUT";
15+
public static final String DELETE = "DELETE";
1516

1617

1718
public static final String HTTPS = "https";

src/main/java/com/amadeus/HTTPClient.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,43 @@ public Response get(String path, Params params) throws ResponseException {
6767
return request(Constants.GET, path, params, null);
6868
}
6969

70+
/**
71+
* <p>
72+
* A helper module for making generic DELETE requests calls. It is used by
73+
* every namespaced API DELETE method.
74+
* </p>
75+
*
76+
* <pre>
77+
* amadeus.booking.flightOrder("eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
78+
* </pre>
79+
*
80+
* <p>
81+
* It can be used to make any generic API call that is automatically
82+
* authenticated using your API credentials:
83+
* </p>
84+
*
85+
* <pre>
86+
* amadeus.delete("/v1/foo/bar", Params.with("airline", "1X"));
87+
* </pre>
88+
*
89+
* @param path The full path for the API call
90+
* @param params The optional DELETE params to pass to the API
91+
* @return a Response object containing the status code, body, and parsed data.
92+
*/
93+
public Response delete(String path, Params params) throws ResponseException {
94+
return request(Constants.DELETE, path, params, null);
95+
}
96+
97+
/**
98+
* A helper module for making generic DELETE requests calls. It is used by
99+
* every namespaced API DELETE method.
100+
*
101+
* @see Amadeus#delete(String, Params)
102+
*/
103+
public Response delete(String path) throws ResponseException {
104+
return request(Constants.DELETE, path, null,null);
105+
}
106+
70107
/**
71108
* A helper module for making generic POST requests calls. It is used by
72109
* every namespaced API POST method.

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,43 @@ public com.amadeus.resources.FlightOrder get(Params params) throws ResponseExcep
5555
return (com.amadeus.resources.FlightOrder) Resource.fromObject(
5656
response, com.amadeus.resources.FlightOrder.class);
5757
}
58-
58+
5959
/**
6060
* Convenience method for calling <code>get</code> without any parameters.
6161
* @see com.amadeus.booking.FlightOrder#get()
6262
*/
6363
public com.amadeus.resources.FlightOrder get() throws ResponseException {
6464
return get(null);
6565
}
66+
67+
/**
68+
* <p>
69+
* Allows you to cancel a flight order. The flightOfferId
70+
* used is an example for educational purposes. In test enviromnent
71+
* it's temporary.
72+
* </p>
73+
*
74+
* <pre>
75+
* FlightOrder order = amadeus.booking.flightOrder.(
76+
* "eJzTd9f3NjIJdzUGAAp%2fAiY=").delete();
77+
* </pre>
78+
* @param params the parameters to send to the API
79+
* @return an API response object
80+
* @throws ResponseException when an exception occurs
81+
*/
82+
83+
public com.amadeus.resources.FlightOrder delete(Params params) throws ResponseException {
84+
String path = String.format("/v1/booking/flight-orders/%s", flightOfferId);
85+
Response response = client.delete(path, params);
86+
return (com.amadeus.resources.FlightOrder) Resource.fromObject(
87+
response, com.amadeus.resources.FlightOrder.class);
88+
}
89+
90+
/**
91+
* Convenience method for calling <code>delete</code> without any parameters.
92+
* @see com.amadeus.booking.FlightOrder#delete()
93+
*/
94+
public com.amadeus.resources.FlightOrder delete() throws ResponseException {
95+
return delete(null);
96+
}
6697
}

src/test/java/com/amadeus/HTTPClientTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public class HTTPClientTest {
6464
verify(client, times(1)).request("GET", "/foo", params, null);
6565
}
6666

67+
@Test public void testDeleteWithoutParams() throws ResponseException {
68+
when(client.delete(anyString())).thenCallRealMethod();
69+
client.delete("/foo");
70+
verify(client, times(1)).request("DELETE", "/foo", null, null);
71+
}
72+
73+
@Test public void testDeletetWithParams() throws ResponseException {
74+
when(client.delete(anyString(), any(Params.class))).thenCallRealMethod();
75+
client.delete("/foo", params);
76+
verify(client, times(1)).request("DELETE", "/foo", params, null);
77+
}
78+
6779
@Test public void testPostWithoutParamsWithoutBody() throws ResponseException {
6880
when(client.post(anyString())).thenCallRealMethod();
6981
client.post("/foo");
@@ -111,6 +123,26 @@ public class HTTPClientTest {
111123
assertEquals(((JsonArray) response.getData()).size(), 1);
112124
}
113125

126+
@Test public void testUnauthenticatedDeleteRequest() throws ResponseException, IOException {
127+
when(request.getVerb()).thenReturn("DELETE");
128+
when(request.getParams()).thenReturn(params);
129+
when(request.getConnection()).thenReturn(connection);
130+
131+
when(connection.getResponseCode()).thenReturn(200);
132+
when(connection.getHeaderField("Content-Type")).thenReturn(
133+
"application/json");
134+
when(connection.getInputStream()).thenReturn(
135+
new ByteArrayInputStream("{ \"data\": [{}]}".getBytes()));
136+
137+
when(client.buildRequest("DELETE", "/foo", params, null,null)).thenReturn(request);
138+
when(client.unauthenticatedRequest("DELETE", "/foo", params, null,null)).thenCallRealMethod();
139+
140+
Response response = client.unauthenticatedRequest("DELETE", "/foo", params, null, null);
141+
142+
assertTrue(response.isParsed());
143+
assertEquals(((JsonArray) response.getData()).size(), 1);
144+
}
145+
114146
@Test public void testUnauthenticatedPostRequest() throws ResponseException, IOException {
115147
when(request.getVerb()).thenReturn("POST");
116148
when(request.getParams()).thenReturn(params);

src/test/java/com/amadeus/NamespaceTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,17 @@ public void testPostMethods() throws ResponseException {
377377
TestCase.assertNotNull(seatmap.post());
378378
TestCase.assertNotNull(seatmap.post(body));
379379
}
380-
}
380+
381+
@Test
382+
public void testDeleteMethods() throws ResponseException {
383+
// Test deleting a specific offer
384+
Mockito.when(client.delete("/v1/booking/flight-orders/XXX", null))
385+
.thenReturn(singleResponse);
386+
Mockito.when(client.delete("/v1/booking/flight-orders/XXX", params))
387+
.thenReturn(singleResponse);
388+
FlightOrder flightOrder = new FlightOrder(client, "XXX");
389+
TestCase.assertNotNull(flightOrder.delete());
390+
TestCase.assertNotNull(flightOrder.delete(params));
391+
392+
}
393+
}

0 commit comments

Comments
 (0)