@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();
  }