TracingEndpointCaller implements EndpointCaller { @Override public String callEndpoint(Map<String, String> requestProperties, URL url) throws ODataClientException { LOG.debug("Preparing the call endpoint for given url: {}", url); CloseableHttpResponse closeableResponse = null; try { RequestBuilder requestBuilder = RequestBuilder.get() .setUri(url.toURI()); getRequestHeaders(requestProperties, -1, null, XML).entrySet().stream() .forEach(entry -> requestBuilder.addHeader(entry.getKey(), entry.getValue())); closeableResponse = closeableHttpClient.execute(requestBuilder.build()); String response = EntityUtils.toString(closeableResponse.getEntity(), "UTF-8"); if (closeableResponse.getStatusLine().getStatusCode() >= HTTP_BAD_REQUEST) { throw buildException(response, closeableResponse.getStatusLine().getStatusCode()); } return response; } catch (URISyntaxException e) { throw new ODataClientException(WRONG_URL_MESSAGE, e); } catch (IOException e) { throw new ODataClientRuntimeException(REQUEST_FAILED_MESSAGE + url, e); } finally { closeIfNecessary(closeableResponse); } } TracingEndpointCaller(Properties properties); @Override String callEndpoint(Map<String, String> requestProperties, URL url); @Override InputStream getInputStream(Map<String, String> requestProperties, URL url); @Override String doPostEntity(Map<String, String> requestProperties, URL url, String body,
                               MediaType contentType, MediaType acceptType); @Override String doPutEntity(Map<String, String> requestProperties, URL url, String body, MediaType type); @Override void doDeleteEntity(Map<String, String> requestProperties, URL url);  }
@Test public void callEndpoint() throws ODataClientException, MalformedURLException { String response = caller.callEndpoint(singletonMap("Accept", JSON.getType()), new URL(basePath + RESPONSE)); String expected = URLTestUtils.loadTextFile(RESPONSE); assertThat(response, equalTo(expected.substring(0, expected.lastIndexOf(lineSeparator())))); }
@Test public void callEndpointWithoutResponse() throws ODataClientException, MalformedURLException { thrown.expect(ODataClientHttpError.class); caller.callEndpoint(singletonMap("Accept", JSON.getType()), new URL(basePath)); }
@Test public void testEmptyProxyPortNumber() throws ODataClientException, MalformedURLException { Properties properties = new Properties(); properties.setProperty(CLIENT_SERVICE_PROXY_PORT, ""); EndpointCaller proxifiedCaller = new TracingEndpointCaller(properties); String response = proxifiedCaller.callEndpoint(emptyMap(), new URL(basePath + RESPONSE)); String result = URLTestUtils.loadTextFile(RESPONSE); assertThat(response, equalTo(result.substring(0, result.lastIndexOf(lineSeparator())))); }
@Test public void unauthorizedGet() throws MalformedURLException, ODataClientException { thrown.expect(ODataClientNotAuthorized.class); caller.callEndpoint(emptyMap(), buildUrlToCall(HTTP_UNAUTHORIZED)); }
@Test public void forbiddenGet() throws MalformedURLException, ODataClientException { thrown.expect(ODataClientHttpError.class); caller.callEndpoint(emptyMap(), buildUrlToCall(HTTP_FORBIDDEN)); }
@Test public void timeoutGet() throws MalformedURLException, ODataClientException { thrown.expect(ODataClientTimeout.class); caller.callEndpoint(emptyMap(), buildUrlToCall(HTTP_CLIENT_TIMEOUT)); }
@Test public void testIOExceptionOnOpenConnection() throws IOException, ODataClientException { URL ioExceptionOnConnectUrl = URLTestUtils.getIOExceptionThrowingUrl(true); Properties properties = new Properties(); properties.setProperty(CLIENT_SERVICE_PROXY_HOST_NAME, "localhost"); properties.setProperty(CLIENT_SERVICE_PROXY_PORT, "9999"); EndpointCaller proxifiedCaller = new TracingEndpointCaller(properties); thrown.expect(ODataClientRuntimeException.class); thrown.expectCause(isA(IOException.class)); proxifiedCaller.callEndpoint(emptyMap(), ioExceptionOnConnectUrl); }