diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImpl.java index 107168cb..4d043862 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImpl.java @@ -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; @@ -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()); @@ -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; diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java b/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java index c3d1f73e..8397570c 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java @@ -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);