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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations