Skip to content

Commit 106c85f

Browse files
committed
NPE Fix: Return Empty List
1 parent be19e63 commit 106c85f

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/main/java/com/amadeus/resources/Resource.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.amadeus.Response;
44
import com.google.gson.Gson;
55
import com.google.gson.GsonBuilder;
6+
import com.google.gson.JsonArray;
7+
import com.google.gson.JsonElement;
68
import lombok.Getter;
79

810
/**
@@ -28,7 +30,11 @@ protected Resource() {}
2830
*/
2931
public static Resource[] fromArray(Response response, Class klass) {
3032
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
31-
Resource[] resources = (Resource[]) gson.fromJson(response.getData(), klass);
33+
JsonElement responseData = response.getData();
34+
if (responseData == null) {
35+
responseData = new JsonArray(0);
36+
}
37+
Resource[] resources = (Resource[]) gson.fromJson(responseData, klass);
3238
for (Resource resource : resources) {
3339
resource.response = response;
3440
resource.deSerializationClass = klass;

src/test/java/com/amadeus/referenceData/locations/CitiesIT.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.amadeus.referencedata.locations;
22

33
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
45
import static com.github.tomakehurst.wiremock.client.WireMock.get;
56
import static com.github.tomakehurst.wiremock.client.WireMock.post;
67
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
8+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
79
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
811
import static org.junit.jupiter.api.Assertions.assertNotEquals;
912

1013
import com.amadeus.Amadeus;
@@ -74,6 +77,27 @@ public void givenClientWhenCallCitySearchWithParamsThenOK()
7477
assertNotEquals(0, result.length);
7578
}
7679

80+
@Test
81+
public void givenClientWhenCallCitySearchWithParamsThenEmpty() throws ResponseException {
82+
83+
// Given
84+
String address = "/v1/reference-data/locations/cities";
85+
wireMockServer.stubFor(get(urlPathEqualTo(address))
86+
.withQueryParam("keyword", equalTo("SANC"))
87+
.withQueryParam("countryCode", equalTo("AZ"))
88+
.willReturn(aResponse().withHeader("Content-Type", "application/json")
89+
.withStatus(200)
90+
.withBodyFile("city_search_response_empty.json")));
91+
92+
Params params = Params.with("keyword", "SANC").and("countryCode", "AZ");
93+
94+
// When
95+
City[] result = amadeus.referenceData.locations.cities.get(params);
96+
97+
// Then
98+
assertEquals(0, result.length);
99+
}
100+
77101
@Test
78102
public void givenClientWhenCallCitySearchWithoutParamsThenOK()
79103
throws ResponseException {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"warnings": [
3+
{
4+
"code": 1797,
5+
"title": "DATA NOT FOUND",
6+
"detail": "No Cities available",
7+
"source": {
8+
"parameter": "keyword"
9+
},
10+
"status": 200
11+
}
12+
],
13+
"meta": {
14+
"count": 0,
15+
"links": {
16+
"self": "https://test.api.amadeus.com/v1/reference-data/locations/cities?countryCode=AZ&keyword=SANC&max=1000"
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)