diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/SearchServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/SearchServiceImpl.java index 5cb7c8d87544d6b17ba29183a079ea8f1ff3c633..51f39344987c7b6ff700bc59b3ae10ed8aa5335f 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/SearchServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/SearchServiceImpl.java @@ -21,8 +21,8 @@ import com.google.gson.Gson; import org.opengroup.osdu.core.common.http.FetchServiceHttpRequest; import org.opengroup.osdu.core.common.http.IUrlFetchService; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo; import org.opengroup.osdu.core.common.model.http.HttpResponse; +import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo; import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.opengroup.osdu.indexer.model.SearchRequest; import org.opengroup.osdu.indexer.model.SearchResponse; @@ -63,25 +63,33 @@ public class SearchServiceImpl implements SearchService { return new SearchResponse(); } - String body = this.gson.toJson(searchRequest); - String url = String.format("%s/%s", configurationProperties.getSearchHost(), path); - FetchServiceHttpRequest request = FetchServiceHttpRequest.builder() - .httpMethod(HttpMethods.POST) - .url(url) - .headers(this.requestInfo.getHeaders()) - .body(body) - .build(); - HttpResponse response = this.urlFetchService.sendRequest(request); + try { + String body = this.gson.toJson(searchRequest); + String url = String.format("%s/%s", configurationProperties.getSearchHost(), path); + FetchServiceHttpRequest request = FetchServiceHttpRequest.builder() + .httpMethod(HttpMethods.POST) + .url(url) + .headers(this.requestInfo.getHeaders()) + .body(body) + .build(); + HttpResponse response = this.urlFetchService.sendRequest(request); - if(response != null && response.getResponseCode() == OK_CODE) { - return gson.fromJson(response.getBody(), SearchResponse.class); + if (response != null && response.getResponseCode() == OK_CODE) { + return gson.fromJson(response.getBody(), SearchResponse.class); + } else { + if (response != null) + jaxRsDpsLog.error(String.format("Search service: failed to call the search service: %d", response.getResponseCode())); + else + jaxRsDpsLog.error(String.format("Search service: failed to call the search service. The response is null.")); + return new SearchResponse(); + } } - else { - if(response != null) - jaxRsDpsLog.error(String.format("Search service: failed to call the search service: %d", response.getResponseCode())); - else - jaxRsDpsLog.error(String.format("Search service: failed to call the search service. The response is null.")); - return new SearchResponse(); + catch(URISyntaxException ex) { + throw ex; + } + catch(Exception ex) { + jaxRsDpsLog.error(String.format("Search service: failed to call the search service", ex)); + throw new URISyntaxException(ex.getMessage(), "Unexpected exception type: " + ex.getClass().getName()); } } } diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/SearchServiceImplTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/SearchServiceImplTest.java index 2018809bfec26c654bc5040669c85c041528d248..93ee08260785537a679529039d0966dbbee88fb7 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/SearchServiceImplTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/SearchServiceImplTest.java @@ -7,12 +7,15 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.opengroup.osdu.core.common.http.IUrlFetchService; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.HttpResponse; import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo; import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.opengroup.osdu.indexer.model.SearchRequest; import org.opengroup.osdu.indexer.model.SearchResponse; import org.powermock.modules.junit4.PowerMockRunner; + +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import java.net.URISyntaxException; @@ -143,4 +146,11 @@ public class SearchServiceImplTest { Assert.assertNotNull(searchResponse); Assert.assertNull(searchResponse.getResults()); } + + @Test + public void query_with_exception() throws URISyntaxException { + when(this.configurationProperties.getSearchHost()).thenReturn(searchHost); + when(this.urlFetchService.sendRequest(any())).thenThrow(new AppException(415, "upstream server responded with unsupported media type: text/plain", "Unsupported media type" )); + assertThrows(URISyntaxException.class, () -> sut.query(new SearchRequest())); + } }