Search in sources :

Example 16 with IdentityOAuth2ScopeException

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

the class OAuthScopeDAOImpl method addScope.

/**
 * Add a scope
 *
 * @param scope    Scope
 * @param tenantID tenant ID
 * @throws IdentityOAuth2ScopeException IdentityOAuth2ScopeException
 */
@Override
public void addScope(Scope scope, int tenantID) throws IdentityOAuth2ScopeException {
    if (scope == null || scope.getName() == null) {
        if (log.isDebugEnabled()) {
            log.debug("Scope is not defined");
        }
        throw Oauth2ScopeUtils.generateClientException(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_BAD_REQUEST_SCOPE_NAME_NOT_SPECIFIED, null);
    }
    if (scope.getName().startsWith(INTERNAL_SCOPE_PREFIX) && Oauth2ScopeUtils.isSystemLevelInternalSystemScopeManagementEnabled()) {
        if (log.isDebugEnabled()) {
            log.debug("Internal Scopes can't be added per tenant as they are managed at system level.");
        }
        throw Oauth2ScopeUtils.generateClientException(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_INTERNAL_SCOPE_MANAGED_AT_SYSTEM_LEVEL, null);
    }
    if (log.isDebugEnabled()) {
        log.debug("Adding scope :" + scope.getName());
    }
    try (Connection conn = IdentityDatabaseUtil.getDBConnection()) {
        try {
            addScope(scope, conn, tenantID);
            IdentityDatabaseUtil.commitTransaction(conn);
        } catch (SQLException e1) {
            IdentityDatabaseUtil.rollbackTransaction(conn);
            String msg = "SQL error occurred while creating scope :" + scope.getName();
            throw new IdentityOAuth2ScopeServerException(msg, e1);
        }
    } catch (SQLException e) {
        String msg = "Error occurred while creating scope :" + scope.getName();
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 17 with IdentityOAuth2ScopeException

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

the class ScopeMgtDAOTest method updateScopeByName.

@Test(dataProvider = "updateScopeByNameDataProvider")
public void updateScopeByName(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);
        Scope updatedScope = (Scope) scope;
        updatedScope.setDisplayName("updateScopeName");
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection1);
        oAuthScopeDAO.updateScopeByName(updatedScope, tenantId);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection2);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertNotNull(oAuthScopeDAO.getScopeByName(updatedScope.getName(), tenantId), "Failed to update scope.");
        // Clean after test
        deleteScopes(Collections.singletonList(scope), tenantId);
    }
}
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)

Example 18 with IdentityOAuth2ScopeException

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

the class ScopeMgtDAOTest method addScope.

@Test(dataProvider = "addScopeDataProvider")
public void addScope(Object scope, int tenantId) throws IdentityOAuth2ScopeException, SQLException {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME);
        Connection connection3 = DAOUtils.getConnection(DB_NAME)) {
        mockStatic(IdentityDatabaseUtil.class);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection1);
        oAuthScopeDAO.addScope((Scope) scope, tenantId);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection2);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertNotNull(oAuthScopeDAO.getScopeByName(((Scope) scope).getName(), tenantId), "Failed to persist scope.");
        // Clean after test
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection3);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection3);
        oAuthScopeDAO.deleteScopeByName(((Scope) scope).getName(), tenantId);
    }
}
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)

Example 19 with IdentityOAuth2ScopeException

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

the class ScopeMgtDAOTest method isScopeExists.

@Test(dataProvider = "isScopeExistsDataProvider")
public void isScopeExists(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.isScopeExists(((Scope) scope).getName(), tenantId), "Failed to check existence " + "by scope name.");
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection2);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection2);
        assertFalse(oAuthScopeDAO.isScopeExists("invalidScopeName", tenantId), "Failed to check existence " + "by scope name.");
        // Clean after test
        deleteScopes(Collections.singletonList(scope), tenantId);
    }
}
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)

Example 20 with IdentityOAuth2ScopeException

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

the class ScopeMgtDAOTest method getScopeByName.

@Test(dataProvider = "getScopeByNameDataProvider")
public void getScopeByName(Object scope, int tenantId) throws IdentityOAuth2ScopeException, SQLException {
    try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
        mockStatic(IdentityDatabaseUtil.class);
        addScopes(Collections.singletonList(scope), tenantId);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
        when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection);
        assertNotNull(oAuthScopeDAO.getScopeByName(((Scope) scope).getName(), tenantId), "Failed to retrieve by scope name.");
        // Clean after test
        deleteScopes(Collections.singletonList(scope), tenantId);
    }
}
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