@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();
  }
  @Test
  public void loginWithNonOauthAuthentication() throws MalformedURLException {
    String token = "12345678";
    Map<String, String> tokenResponse = new HashMap<String, String>();
    tokenResponse.put("token", token);
    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_WITHOUT_AUTH);
    when(restTemplate.postForObject(
            eq("http://api.cloud.me/users/{id}/tokens"),
            any(Object.class),
            any(Class.class),
            any(Object[].class)))
        .thenReturn(tokenResponse);

    // 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(token));
  }
  public AbstractCloudControllerClient(
      URL cloudControllerUrl,
      RestUtil restUtil,
      CloudCredentials cloudCredentials,
      URL authorizationEndpoint,
      HttpProxyConfiguration httpProxyConfiguration) {
    Assert.notNull(cloudControllerUrl, "CloudControllerUrl cannot be null");
    Assert.notNull(restUtil, "RestUtil cannot be null");
    this.restUtil = restUtil;
    this.cloudCredentials = cloudCredentials;
    if (cloudCredentials != null && cloudCredentials.getToken() != null) {
      this.token = cloudCredentials.getToken();
    }
    this.cloudControllerUrl = cloudControllerUrl;
    if (authorizationEndpoint != null) {
      this.authorizationEndpoint =
          determineAuthorizationEndPointToUse(authorizationEndpoint, cloudControllerUrl);
    } else {
      this.authorizationEndpoint = null;
    }
    this.restTemplate = restUtil.createRestTemplate(httpProxyConfiguration);
    configureCloudFoundryRequestFactory(restTemplate);

    this.restTemplate.setErrorHandler(new ErrorHandler());
    this.restTemplate.setMessageConverters(getHttpMessageConverters());
  }
  @Test
  public void loginWithNullPassword() throws MalformedURLException {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Password cannot be null or empty");
    RestTemplate restTemplate = mock(RestTemplate.class);
    RestUtil restUtil = mock(RestUtil.class);
    when(restUtil.createRestTemplate(any(HttpProxyConfiguration.class))).thenReturn(restTemplate);

    // Run Test
    CloudControllerClient ccc =
        new CloudControllerClientV1(
            new URL("http://api.cloud.me"),
            restUtil,
            new CloudCredentials("*****@*****.**", null),
            null,
            null);
    ccc.login();
  }
 public void updateHttpProxyConfiguration(HttpProxyConfiguration httpProxyConfiguration) {
   ClientHttpRequestFactory requestFactory = restUtil.createRequestFactory(httpProxyConfiguration);
   restTemplate.setRequestFactory(requestFactory);
   configureCloudFoundryRequestFactory(restTemplate);
 }