Search in sources :

Example 11 with IdentityOAuth2ScopeException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImplTest method testUpdateScope.

@Test(dataProvider = "BuildUpdateScope")
public void testUpdateScope(Response.Status expectation, Throwable throwable) throws Exception {
    ScopeToUpdateDTO scopeToUpdateDTO = new ScopeToUpdateDTO();
    scopeToUpdateDTO.setDescription("some description");
    scopeToUpdateDTO.setBindings(Collections.<String>emptyList());
    if (Response.Status.OK.equals(expectation)) {
        when(ScopeUtils.getScopeDTO(any(Scope.class))).thenReturn(any(ScopeDTO.class));
        assertEquals(scopesApiService.updateScope(scopeToUpdateDTO, someScopeName).getStatus(), Response.Status.OK.getStatusCode(), "Error occurred while updating scopes");
    } else if (Response.Status.BAD_REQUEST.equals(expectation)) {
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeClientException.class);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "Cannot find HTTP Response, Bad Request in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.BAD_REQUEST.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Bad Request");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.NOT_FOUND.equals(expectation)) {
        ((IdentityOAuth2ScopeException) throwable).setErrorCode(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_FOUND_SCOPE.getCode());
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Cannot find HTTP Response, Not Found in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.NOT_FOUND.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Not Found");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeException.class);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Cannot find HTTP Response, Internal Server Error in case of " + "IdentityOAuth2ScopeException");
            assertNull(e.getResponse().getEntity(), "Do not include error message in case of " + "Server Exception");
        } finally {
            reset(oAuth2ScopeService);
        }
    }
}
Also used : ScopeEndpointException(org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) ErrorDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO) ScopeToUpdateDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO) IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 12 with IdentityOAuth2ScopeException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImplTest method buildUpdateApplication.

@DataProvider(name = "BuildUpdateScope")
public Object[][] buildUpdateApplication() {
    IdentityOAuth2ScopeClientException identityOAuth2ScopeClientException = new IdentityOAuth2ScopeClientException("Oauth2 Scope Client Exception");
    IdentityOAuth2ScopeException identityOAuth2ScopeException = new IdentityOAuth2ScopeException("Oauth2 Scope " + "Exception");
    return new Object[][] { { Response.Status.OK, null }, { Response.Status.BAD_REQUEST, identityOAuth2ScopeClientException }, { Response.Status.NOT_FOUND, identityOAuth2ScopeClientException }, { Response.Status.INTERNAL_SERVER_ERROR, identityOAuth2ScopeException } };
}
Also used : IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) DataProvider(org.testng.annotations.DataProvider)

Example 13 with IdentityOAuth2ScopeException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeMgtDAOTest method getAllScopes.

@Test(dataProvider = "getAllScopesDataProvider")
public void getAllScopes(List<Object> scopes, int tenantId) throws SQLException, IdentityOAuth2ScopeException {
    try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
        mockStatic(IdentityDatabaseUtil.class);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection);
        assertTrue(scopes != null && !scopes.isEmpty(), "Failed to retrieve scopes.");
        addScopes(scopes, tenantId);
        // Clean after test
        deleteScopes(scopes, tenantId);
    }
}
Also used : Connection(java.sql.Connection) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IdentityBaseTest(org.wso2.carbon.identity.testutil.IdentityBaseTest)

Example 14 with IdentityOAuth2ScopeException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeMgtDAOTest method getScopeIDByName.

@Test(dataProvider = "getScopeIDByNameDataProvider")
public void getScopeIDByName(Object scope, int tenantId) throws IdentityOAuth2ScopeException, SQLException {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME)) {
        mockStatic(IdentityDatabaseUtil.class);
        addScopes(Collections.singletonList(scope), tenantId);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection1);
        assertTrue(oAuthScopeDAO.getScopeIDByName(((Scope) scope).getName(), tenantId) != Oauth2ScopeConstants.INVALID_SCOPE_ID, "Failed to retrieve the scope id.");
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection2);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertTrue(oAuthScopeDAO.getScopeIDByName("invalidScopeName", tenantId) == Oauth2ScopeConstants.INVALID_SCOPE_ID, "Failed to retrieve the scope id.");
        // Clean after test
        deleteScopes(Collections.singletonList(scope), tenantId);
    }
}
Also used : Connection(java.sql.Connection) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IdentityBaseTest(org.wso2.carbon.identity.testutil.IdentityBaseTest)

Example 15 with IdentityOAuth2ScopeException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeMgtDAOTest method deleteScopeByName.

@Test(dataProvider = "deleteScopeByNameDataProvider")
public void deleteScopeByName(Object scope, int tenantId) throws IdentityOAuth2ScopeException, SQLException {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME)) {
        mockStatic(IdentityDatabaseUtil.class);
        addScopes(Collections.singletonList(scope), tenantId);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection1);
        oAuthScopeDAO.deleteScopeByName(((Scope) scope).getName(), tenantId);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection2);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertNull(oAuthScopeDAO.getScopeByName(((Scope) scope).getName(), tenantId), "Failed to delete the scope" + " by name.");
    }
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope) Connection(java.sql.Connection) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IdentityBaseTest(org.wso2.carbon.identity.testutil.IdentityBaseTest)

Aggregations

Scope (org.wso2.carbon.identity.oauth2.bean.Scope)14 Test (org.testng.annotations.Test)12 IdentityOAuth2ScopeException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 Connection (java.sql.Connection)9 IdentityOAuth2ScopeClientException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException)8 IdentityBaseTest (org.wso2.carbon.identity.testutil.IdentityBaseTest)8 OAuthScopeCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthScopeCacheKey)6 DataProvider (org.testng.annotations.DataProvider)5 UserApplicationScopeConsentDO (org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO)5 ScopeDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO)3 ScopeEndpointException (org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException)3 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)3 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)2 ErrorDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO)2 OAuth2ScopeConsentResponse (org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1