public BasicRestClient build() { return new BasicRestClient(this); } }
@Test public void testExecuteRequestStatusCodeIsNotOk() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_503_STATUSLINE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_503_STATUSLINE)); }
protected BasicRestClient(final CloseableHttpClient client, final HttpClientContext clientContex, final String host) { this.client = client; clientContext = clientContex; clientContext.setTargetHost(buildHttpHost(host)); }
private CloseableHttpResponse execute(final HttpUriRequest request, final int previousStatusCode) throws CloudstackRESTException { if (counter.hasReachedExecutionLimit()) { throw new CloudstackRESTException("Reached max executions limit of " + executionLimit); } counter.incrementExecutionCounter(); s_logger.debug("Executing " + request.getMethod() + " request [execution count = " + counter.getValue() + "]"); final CloseableHttpResponse response = super.execute(request); final StatusLine statusLine = response.getStatusLine(); final int statusCode = statusLine.getStatusCode(); s_logger.debug("Status of last request: " + statusLine.toString()); if (HttpStatusCodeHelper.isUnauthorized(statusCode)) { return handleUnauthorizedResponse(request, previousStatusCode, response, statusCode); } else if (HttpStatusCodeHelper.isSuccess(statusCode)) { return handleSuccessResponse(request, response); } else if (HttpStatusCodeHelper.isConflict(statusCode)) { throw new CloudstackRESTException("Conflict: " + statusLine.getReasonPhrase(), statusCode); } else { throw new CloudstackRESTException("Unexpected status code: " + statusCode, statusCode); } }
@Override public CloseableHttpResponse execute(final HttpUriRequest request) throws CloudstackRESTException { logRequestExecution(request); try { return client.execute(clientContext.getTargetHost(), request, clientContext); } catch (final IOException e) { throw new CloudstackRESTException("Could not execute request " + request, e); } }
private CloseableHttpResponse handleUnauthorizedResponse(final HttpUriRequest request, final int previousStatusCode, final CloseableHttpResponse response, final int statusCode) throws CloudstackRESTException { super.closeResponse(response); if (HttpStatusCodeHelper.isUnauthorized(previousStatusCode)) { s_logger.error(responseToErrorMessage(response)); throw new CloudstackRESTException("Two consecutive failed attempts to authenticate against REST server"); } final HttpUriRequest authenticateRequest = createAuthenticationRequest(); final CloseableHttpResponse loginResponse = execute(authenticateRequest, statusCode); final int loginStatusCode = loginResponse.getStatusLine().getStatusCode(); super.closeResponse(loginResponse); return execute(request, loginStatusCode); }
@Override public CloseableHttpResponse execute(final HttpUriRequest request) throws CosmicRESTException { logRequestExecution(request); try { return client.execute(clientContext.getTargetHost(), request, clientContext); } catch (final IOException e) { throw new CosmicRESTException("Could not execute request " + request, e); } }
@Test public void testExecuteDeleteObject() throws Exception { final HttpEntity entity = mock(HttpEntity.class); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(entity); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); connector.executeDeleteObject("/somepath"); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("DELETE"), any(HttpClientContext.class)); verify(response).close(); }
@Test public void testExecuteRequest() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_200_REPSONSE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_200_REPSONSE)); }
private BasicRestClient(final Builder<?> builder) { client = builder.client; clientContext = builder.clientContext; clientContext.setTargetHost(buildHttpHost(builder.host)); }
@Test(expected = JsonParseException.class) public void testCustomDeserializerTypeMismatch() throws Exception { final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); when(response.getEntity()).thenReturn(new StringEntity("[{somethig_not_type : \"WrongType\"}]")); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder() .client(restClient) .classToDeserializerEntry(TestPojo.class, new TestPojoDeserializer()) .build(); connector.executeRetrieveObject(TestPojo.class, "/somepath"); }
@Test(expected = CloudstackRESTException.class) public void testExecuteRequestWhenClientThrowsIOException() throws Exception { final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(HttpClientHelper.createHttpClient(5)) .build(); final CloseableHttpResponse response = restClient.execute(request); fail("A CloudstackRESTException should have been thrown by now."+ "\nWe got " + response.getStatusLine() + "\nMake sure you cannot reach '" + request.getURI() + "' by method " + request.getMethod()); }
protected BasicRestClient(final CloseableHttpClient client, final HttpClientContext clientContex, final String host) { this.client = client; clientContext = clientContex; clientContext.setTargetHost(buildHttpHost(host)); }
@Test public void testCustomDeserializerForCustomLists() throws Exception { final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); when(response.getEntity()).thenReturn(new StringEntity("{results: [{field : \"SomeValue\"}], results_count: 1}")); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final Class<? extends CollectionType> clazzListOfTestPojo = new ObjectMapper().getTypeFactory().constructCollectionType(List.class, TestPojo.class).getClass(); final RESTServiceConnector connector = new RESTServiceConnector.Builder() .client(restClient) .classToDeserializerEntry(clazzListOfTestPojo, new CustomListDeserializer<TestPojoDeserializer>()) .build(); connector.executeRetrieveObject(TestPojo.class, "/somepath"); }
@Test public void testExecuteRequestStatusCodeIsNotOk() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_503_STATUSLINE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_503_STATUSLINE)); }
private BasicRestClient(final Builder<?> builder) { client = builder.client; clientContext = builder.clientContext; clientContext.setTargetHost(buildHttpHost(builder.host)); }
@Test public void testExecuteUpdateObject() throws Exception { final TestPojo newObject = new TestPojo(); newObject.setField("newValue"); final String newObjectJson = gson.toJson(newObject); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); connector.executeUpdateObject(newObject, "/somepath"); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("PUT"), any(HttpClientContext.class)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPayloadMatcher.aPayload(newObjectJson), any(HttpClientContext.class)); }
@Test public void testExecuteRequest() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_200_REPSONSE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_200_REPSONSE)); }
@Test public void testExecuteRetrieveObject() throws Exception { final TestPojo existingObject = new TestPojo(); existingObject.setField("existingValue"); final String newObjectJson = gson.toJson(existingObject); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(new StringEntity(newObjectJson)); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); final TestPojo object = connector.executeRetrieveObject(TestPojo.class, "/somepath"); assertThat(object, notNullValue()); assertThat(object, equalTo(existingObject)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); verify(response).close(); }
@Test(expected = CosmicRESTException.class) public void testExecuteRequestWhenClientThrowsIOException() throws Exception { final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(HttpClientHelper.createHttpClient(5)) .build(); final CloseableHttpResponse response = restClient.execute(request); fail("A CloudstackRESTException should have been thrown by now." + "\nWe got " + response.getStatusLine() + "\nMake sure you cannot reach '" + request.getURI() + "' by method " + request.getMethod()); } }