@Test public void loginWithOauth2Authentication() throws MalformedURLException { String token = "12345678"; RestTemplate restTemplate = mock(RestTemplate.class); ClientHttpRequestFactory clientHttpRequestFactory = mock(ClientHttpRequestFactory.class); RestUtil restUtil = mock(RestUtil.class); OAuth2AccessToken oauthToken = mock(OAuth2AccessToken.class); when(restUtil.createRestTemplate(any(HttpProxyConfiguration.class))).thenReturn(restTemplate); when(restUtil.createRequestFactory(any(HttpProxyConfiguration.class))) .thenReturn(clientHttpRequestFactory); when(restTemplate.getForObject(eq("http://api.cloud.me/info"), any(Class.class))) .thenReturn(INFO_WITH_AUTH); when(restUtil.createOauthClient(any(URL.class), any(HttpProxyConfiguration.class))) .thenReturn(new OauthClient(new URL("http://uaa.cloud.me"), restTemplate)); when(restTemplate.execute( eq("http://uaa.cloud.me/oauth/authorize"), eq(HttpMethod.POST), any(RequestCallback.class), any(ResponseExtractor.class), any(Map.class))) .thenReturn(oauthToken); when(oauthToken.getValue()).thenReturn(token); when(oauthToken.getTokenType()).thenReturn("bearer"); // Run Test CloudControllerClientFactory ccf = new CloudControllerClientFactory(restUtil, null); CloudControllerClient ccc = ccf.newCloudController( new URL("http://api.cloud.me"), new CloudCredentials("*****@*****.**", "passwd"), null); String loginToken = ccc.login(); assertThat(loginToken, is("bearer " + token)); }
@Test public void loginWithWrongPassword() throws MalformedURLException { thrown.expect(CloudFoundryException.class); RestTemplate restTemplate = mock(RestTemplate.class); ClientHttpRequestFactory clientHttpRequestFactory = mock(ClientHttpRequestFactory.class); RestUtil restUtil = mock(RestUtil.class); when(restUtil.createRestTemplate(any(HttpProxyConfiguration.class))).thenReturn(restTemplate); when(restUtil.createRequestFactory(any(HttpProxyConfiguration.class))) .thenReturn(clientHttpRequestFactory); when(restTemplate.getForObject(eq("http://api.cloud.me/info"), any(Class.class))) .thenReturn(INFO_WITH_AUTH); when(restUtil.createOauthClient(any(URL.class), any(HttpProxyConfiguration.class))) .thenReturn(new OauthClient(new URL("http://uaa.cloud.me"), restTemplate)); when(restTemplate.execute( eq("http://uaa.cloud.me/oauth/authorize"), eq(HttpMethod.POST), any(RequestCallback.class), any(ResponseExtractor.class), any(Map.class))) .thenThrow( new CloudFoundryException(HttpStatus.UNAUTHORIZED, "Error requesting access token.")); // Run Test CloudControllerClientFactory ccf = new CloudControllerClientFactory(restUtil, null); CloudControllerClient ccc = ccf.newCloudController( new URL("http://api.cloud.me"), new CloudCredentials("*****@*****.**", "badpasswd"), null); ccc.login(); }
@Override public Restaurant getRestaurant(String restaurantId) { String url = this.urlStub + "/" + restaurantId; RestaurantExtractor extractor = new RestaurantExtractor(Restaurant.class, RestaurantResultObject.class); RestaurantResultObject restaurantResult = restClient.execute(url, HttpMethod.GET, null, extractor); if (restaurantResult.getError() != null) { throw new RuntimeException("Error: " + restaurantResult.getError().getMessage()); } return restaurantResult.getPayload(); }
private void readStream(RestTemplate restTemplate) { restTemplate.execute( buildUri("statuses/sample.json"), HttpMethod.GET, new RequestCallback() { public void doWithRequest(ClientHttpRequest request) throws IOException {} }, new ResponseExtractor<String>() { public String extractData(ClientHttpResponse response) throws IOException { InputStream inputStream = response.getBody(); LineNumberReader reader = new LineNumberReader(new InputStreamReader(inputStream)); while (running.get()) { String line = reader.readLine(); if (!StringUtils.hasText(line)) { break; } sendMessage(MessageBuilder.withPayload(line).build()); } return null; } }); }
@Test public void loginTestForOauthClient() throws MalformedURLException { String token = "12345678"; RestTemplate restTemplate = mock(RestTemplate.class); ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class); OAuth2AccessToken oauthToken = mock(OAuth2AccessToken.class); when(restTemplate.getRequestFactory()).thenReturn(requestFactory); when(restTemplate.execute( anyString(), any(HttpMethod.class), any(RequestCallback.class), any(ResponseExtractor.class), any(Map.class))) .thenReturn(oauthToken); when(oauthToken.getValue()).thenReturn(token); when(oauthToken.getTokenType()).thenReturn("bearer"); // Run Test OauthClient oauthClient = new OauthClient(new URL("http://uaa.cloud.me"), restTemplate); OAuth2AccessToken ouathToken = oauthClient.getToken("*****@*****.**", "passwd"); String loginToken = oauthToken.getValue(); assertThat(loginToken, is(token)); }
public HttpStatus getStatusCode(String path, final HttpHeaders headers) { RequestCallback requestCallback = new NullRequestCallback(); if (headers != null) { requestCallback = new RequestCallback() { public void doWithRequest(ClientHttpRequest request) throws IOException { request.getHeaders().putAll(headers); } }; } return client .execute( getUrl(path), HttpMethod.GET, requestCallback, new ResponseExtractor<ResponseEntity<String>>() { public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException { return new ResponseEntity<String>(response.getStatusCode()); } }) .getStatusCode(); }
public void delete(URI url) throws RestClientException { execute(url, HttpMethod.DELETE, null, null); }
public void delete(String url, Map<String, ?> urlVariables) throws RestClientException { execute(url, HttpMethod.DELETE, null, null, urlVariables); }
public void put(URI url, Object request) throws RestClientException { HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request); execute(url, HttpMethod.PUT, requestCallback, null); }
public void put(String url, Object request, Map<String, ?> urlVariables) throws RestClientException { HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request); execute(url, HttpMethod.PUT, requestCallback, null, urlVariables); }