Example usage for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getMessage

List of usage examples for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getMessage

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java

/**
 * Test using a bad username//from w  w w  .  ja va 2s  .  c  o m
 */
@Test
public void testBadPassword() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, "badPassword");
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof InvalidGrantException) {
            InvalidGrantException clientException = (InvalidGrantException) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected InvalidGrantException. Got %s", ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad secret.
 *//*from w w w  .ja v a2 s .c o  m*/
@Test
public void testBadClientSecret() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadSecret();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof OAuth2Exception) {
            OAuth2Exception clientException = (OAuth2Exception) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad client Id.
 *///from w ww .j a v  a  2s .c om
@Test
public void testBadClientId() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadClientId();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof HttpClientErrorException) {
            HttpClientErrorException clientException = (HttpClientErrorException) ex.getCause();
            assertEquals(HttpStatus.UNAUTHORIZED, clientException.getStatusCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad scope.
 *//*w  w w.j  a va2 s . c o m*/
@Test
public void testBadScope() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadScope();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof InvalidScopeException) {
            InvalidScopeException clientException = (InvalidScopeException) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}