From 106c85fb7fca012ec2a4c25b148ad0ae32e127da Mon Sep 17 00:00:00 2001 From: Siddhartha Dutta Date: Sat, 4 Jan 2025 15:31:02 +0000 Subject: [PATCH] NPE Fix: Return Empty List --- .../java/com/amadeus/resources/Resource.java | 8 ++++++- .../referenceData/locations/CitiesIT.java | 24 +++++++++++++++++++ .../__files/city_search_response_empty.json | 19 +++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/__files/city_search_response_empty.json diff --git a/src/main/java/com/amadeus/resources/Resource.java b/src/main/java/com/amadeus/resources/Resource.java index efe4e7bd..91c29ddd 100644 --- a/src/main/java/com/amadeus/resources/Resource.java +++ b/src/main/java/com/amadeus/resources/Resource.java @@ -3,6 +3,8 @@ import com.amadeus.Response; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import lombok.Getter; /** @@ -28,7 +30,11 @@ protected Resource() {} */ public static Resource[] fromArray(Response response, Class klass) { Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); - Resource[] resources = (Resource[]) gson.fromJson(response.getData(), klass); + JsonElement responseData = response.getData(); + if (responseData == null) { + responseData = new JsonArray(0); + } + Resource[] resources = (Resource[]) gson.fromJson(responseData, klass); for (Resource resource : resources) { resource.response = response; resource.deSerializationClass = klass; diff --git a/src/test/java/com/amadeus/referenceData/locations/CitiesIT.java b/src/test/java/com/amadeus/referenceData/locations/CitiesIT.java index 31035a0e..60f2286b 100644 --- a/src/test/java/com/amadeus/referenceData/locations/CitiesIT.java +++ b/src/test/java/com/amadeus/referenceData/locations/CitiesIT.java @@ -1,10 +1,13 @@ package com.amadeus.referencedata.locations; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import com.amadeus.Amadeus; @@ -74,6 +77,27 @@ public void givenClientWhenCallCitySearchWithParamsThenOK() assertNotEquals(0, result.length); } + @Test + public void givenClientWhenCallCitySearchWithParamsThenEmpty() throws ResponseException { + + // Given + String address = "/v1/reference-data/locations/cities"; + wireMockServer.stubFor(get(urlPathEqualTo(address)) + .withQueryParam("keyword", equalTo("SANC")) + .withQueryParam("countryCode", equalTo("AZ")) + .willReturn(aResponse().withHeader("Content-Type", "application/json") + .withStatus(200) + .withBodyFile("city_search_response_empty.json"))); + + Params params = Params.with("keyword", "SANC").and("countryCode", "AZ"); + + // When + City[] result = amadeus.referenceData.locations.cities.get(params); + + // Then + assertEquals(0, result.length); + } + @Test public void givenClientWhenCallCitySearchWithoutParamsThenOK() throws ResponseException { diff --git a/src/test/resources/__files/city_search_response_empty.json b/src/test/resources/__files/city_search_response_empty.json new file mode 100644 index 00000000..0ad7728a --- /dev/null +++ b/src/test/resources/__files/city_search_response_empty.json @@ -0,0 +1,19 @@ +{ + "warnings": [ + { + "code": 1797, + "title": "DATA NOT FOUND", + "detail": "No Cities available", + "source": { + "parameter": "keyword" + }, + "status": 200 + } + ], + "meta": { + "count": 0, + "links": { + "self": "https://test.api.amadeus.com/v1/reference-data/locations/cities?countryCode=AZ&keyword=SANC&max=1000" + } + } +}