Skip to content

Commit

Permalink
Merge pull request #152 from sadilchamishka/improve-error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sadilchamishka authored Sep 20, 2024
2 parents 18ef420 + d137ed6 commit 151b138
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants;
import org.wso2.carbon.identity.organization.management.service.dao.OrganizationManagementDAO;
Expand Down Expand Up @@ -155,6 +157,8 @@
*/
public class OrganizationManagerImpl implements OrganizationManager {

private static final Log LOG = LogFactory.getLog(OrganizationManagerImpl.class);

private final OrganizationManagementDAO organizationManagementDAO =
new CacheBackedOrganizationManagementDAO(new OrganizationManagementDAOImpl());

Expand All @@ -178,7 +182,12 @@ public Organization addOrganization(Organization organization) throws Organizati
getListener().postAddOrganization(organization);
} catch (OrganizationManagementException e) {
// Rollback created organization.
deleteOrganization(organization.getId());
try {
deleteOrganization(organization.getId());
} catch (OrganizationManagementException exception) {
LOG.error("The server encountered an error while deleting the organization due to a rollback " +
"after a failed organization creation.", exception);
}
throw e;
}
return organization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,22 @@ public void testGetParentOrganizationId() throws OrganizationManagementException
Assert.assertEquals(organizationManager.getParentOrganizationId(ORG2_ID), ORG1_ID);
}

@Test(expectedExceptions = OrganizationManagementException.class,
expectedExceptionsMessageRegExp = ".*Server encountered error while executing post listeners.*")
public void testAddOrganizationFailWhileRoleBack() throws Exception {

Organization sampleOrganization = getOrganization(UUID.randomUUID().toString(), NEW_ORG_NAME, ORG_DESCRIPTION,
SUPER_ORG_ID, STRUCTURAL.toString());
TestUtils.mockCarbonContext(SUPER_ORG_ID);
OrganizationManagerListener mockOrgMgtListener = OrganizationManagementDataHolder.getInstance()
.getOrganizationManagerListener();
Mockito.doThrow(new OrganizationManagementException("Server encountered error while executing post listeners."))
.when(mockOrgMgtListener).postAddOrganization(sampleOrganization);
Mockito.doThrow(new OrganizationManagementException("Server encountered error while deleting organization."))
.when(mockOrgMgtListener).preDeleteOrganization(sampleOrganization.getId());
organizationManager.addOrganization(sampleOrganization);
}

private void setOrganizationAttributes(Organization organization, String key, String value) {

OrganizationAttribute organizationAttribute = new OrganizationAttribute(key, value);
Expand Down

0 comments on commit 151b138

Please sign in to comment.