Skip to content

Commit

Permalink
Mock OrganizationManagementUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
PasinduYeshan committed Oct 19, 2023
1 parent 489e6dd commit e15763f
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil;
import org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo;
import org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException;
import org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException;
Expand All @@ -51,6 +53,7 @@
import org.wso2.charon3.core.utils.codeutils.Node;
import org.wso2.charon3.core.utils.codeutils.OperationNode;
import org.wso2.charon3.core.utils.codeutils.SearchRequest;
import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -85,7 +88,7 @@
/**
* Contains the unit test cases for SCIMRoleManager.
*/
@PrepareForTest({SCIMCommonUtils.class})
@PrepareForTest({SCIMCommonUtils.class, OrganizationManagementUtil.class})
public class SCIMRoleManagerTest extends PowerMockTestCase {

private static final String SAMPLE_TENANT_DOMAIN = "carbon.super";
Expand Down Expand Up @@ -129,6 +132,7 @@ public void setUpClass() {
public void setUpMethod() {

mockStatic(SCIMCommonUtils.class);
mockStatic(OrganizationManagementUtil.class);
when(SCIMCommonUtils.getSCIMRoleURL(nullable(String.class))).thenReturn(DUMMY_SCIM_URL);
when(mockRoleManagementService.getSystemRoles()).thenReturn(SYSTEM_ROLES);
}
Expand All @@ -144,30 +148,35 @@ public Object[][] dataProviderForCreateRoleExistingRole() {

@Test(dataProvider = "dataProviderForCreateRoleExistingRole")
public void testCreateRoleExistingRole(String roleId, String roleDisplayName)
throws IdentityRoleManagementException, BadRequestException, CharonException {
throws IdentityRoleManagementException, BadRequestException, CharonException,
OrganizationManagementException {

Role role = getDummyRole(roleId, roleDisplayName);
when(mockRoleManagementService.isExistingRole(anyString(), anyString())).thenReturn(true);
when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(false);
SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, SAMPLE_TENANT_DOMAIN2);
assertThrows(ConflictException.class, () -> scimRoleManager.createRole(role));
}

@Test
public void testCreateRoleAddRoleExistingRoleName()
throws BadRequestException, CharonException, IdentityRoleManagementException {
throws BadRequestException, CharonException, IdentityRoleManagementException,
OrganizationManagementException {

Role role = getDummyRole(SAMPLE_VALID_ROLE_ID2, SAMPLE_EXISTING_ROLE_NAME);
when(mockRoleManagementService.addRole(anyString(), anyListOf(String.class), anyListOf(String.class),
anyListOf(String.class), anyString())).thenThrow(
new IdentityRoleManagementException(ROLE_ALREADY_EXISTS.getCode(),
"Role already exist for the role name: " + SAMPLE_EXISTING_ROLE_NAME));
when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(false);
SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, SAMPLE_TENANT_DOMAIN);
assertThrows(ConflictException.class, () -> scimRoleManager.createRole(role));
}

@Test
public void testCreateRoleAddRoleInvalidRoleName()
throws BadRequestException, CharonException, IdentityRoleManagementException {
throws BadRequestException, CharonException, IdentityRoleManagementException,
OrganizationManagementException {

Role role = getDummyRole(SAMPLE_VALID_ROLE_ID, SAMPLE_INVALID_ROLE_NAME);

Expand All @@ -177,6 +186,7 @@ public void testCreateRoleAddRoleInvalidRoleName()
String.format("Invalid role name: %s. Role names with the prefix: %s, is not allowed"
+ " to be created from externally in the system.", SAMPLE_INVALID_ROLE_NAME,
UserCoreConstants.INTERNAL_SYSTEM_ROLE_PREFIX)));
when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(false);
SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, SAMPLE_TENANT_DOMAIN);
assertThrows(BadRequestException.class, () -> scimRoleManager.createRole(role));
}
Expand All @@ -193,13 +203,15 @@ public Object[][] dataProviderForCreateRoleUnexpectedServerError() {
@Test(dataProvider = "dataProviderForCreateRoleUnexpectedServerError")
public void testCreateRoleUnexpectedServerError(String roleId, String roleDisplayName, String tenantDomain,
String sError)
throws BadRequestException, CharonException, IdentityRoleManagementException {
throws BadRequestException, CharonException, IdentityRoleManagementException,
OrganizationManagementException {

Role role = getDummyRole(roleId, roleDisplayName);
when(mockRoleManagementService.addRole(anyString(), anyListOf(String.class), anyListOf(String.class),
anyListOf(String.class), anyString())).
thenThrow(unExpectedErrorThrower(tenantDomain, sError,
"Error while creating the role: %s in the tenantDomain: %s", roleDisplayName));
when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(false);
SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, tenantDomain);
assertThrows(CharonException.class, () -> scimRoleManager.createRole(role));
}
Expand All @@ -220,11 +232,13 @@ public Object[][] dataProviderForCreateRolePositive() {

@Test(dataProvider = "dataProviderForCreateRolePositive")
public void testCreateRolePositive(String roleId, String roleDisplayName, String tenantDomain)
throws IdentityRoleManagementException, BadRequestException, CharonException, ConflictException {
throws IdentityRoleManagementException, BadRequestException, CharonException, ConflictException,
OrganizationManagementException {

Role role = getDummyRole(roleId, roleDisplayName);
when(mockRoleManagementService.addRole(nullable(String.class), anyListOf(String.class), anyListOf(String.class),
anyListOf(String.class), anyString())).thenReturn(new RoleBasicInfo(roleId, roleDisplayName));
when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(false);
SCIMRoleManager scimRoleManager = new SCIMRoleManager(mockRoleManagementService, tenantDomain);
Role createdRole = scimRoleManager.createRole(role);
assertEquals(createdRole.getDisplayName(), roleDisplayName);
Expand Down

0 comments on commit e15763f

Please sign in to comment.