Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaminduDilshan committed Jan 3, 2025
1 parent c78c3b2 commit 2117b8a
Showing 1 changed file with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.testng.annotations.Test;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.InboundProvisioningConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
Expand Down Expand Up @@ -91,6 +92,7 @@
import org.wso2.charon3.core.utils.codeutils.ExpressionNode;
import org.wso2.charon3.core.utils.codeutils.FilterTreeManager;
import org.wso2.charon3.core.utils.codeutils.Node;
import org.wso2.charon3.core.utils.codeutils.PatchOperation;
import org.wso2.charon3.core.utils.codeutils.SearchRequest;
import org.wso2.carbon.identity.configuration.mgt.core.model.Resource;

Expand Down Expand Up @@ -119,6 +121,8 @@
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -1156,6 +1160,121 @@ public void testGetEnterpriseUserSchemaWhenDisabled() throws Exception {
assertEquals(userManager.getEnterpriseUserSchema(), null);
}

@DataProvider(name = "patchGroupDataProvider")
public Object[][] patchGroupDataProvider() {

String currentGroupName = "CurrentGroupName";
String updatedGroupName = "UpdatedGroupName";
String userName = "user1";
String userId = "1234";

Map<String, List<PatchOperation>> patchOperations1 = new HashMap<>();
Map<String, List<PatchOperation>> patchOperations2 = new HashMap<>();
Map<String, List<PatchOperation>> patchOperations3 = new HashMap<>();
Map<String, List<PatchOperation>> patchOperations4 = new HashMap<>();

PatchOperation patchOperation1 = new PatchOperation();
patchOperation1.setOperation("add");
patchOperation1.setValues(updatedGroupName);
patchOperation1.setAttributeName("displayName");
patchOperation1.setExecutionOrder(1);
patchOperations1.put("add", new ArrayList<PatchOperation>() {{
add(patchOperation1);
}});
patchOperations3.put("add", new ArrayList<PatchOperation>() {{
add(patchOperation1);
}});

// Update with the same display name.
PatchOperation patchOperation2 = new PatchOperation();
patchOperation2.setOperation("add");
patchOperation2.setValues(currentGroupName);
patchOperation2.setAttributeName("displayName");
patchOperation2.setExecutionOrder(1);
patchOperations2.put("add", new ArrayList<PatchOperation>() {{
add(patchOperation2);
}});

PatchOperation patchOperation3 = new PatchOperation();
patchOperation3.setOperation("remove");
HashMap<String, String> groupUsers = new HashMap<>();
groupUsers.put("display", userName);
groupUsers.put("value", userId);
patchOperation3.setValues(groupUsers);
patchOperation3.setAttributeName("members");
patchOperation3.setExecutionOrder(2);
patchOperation3.setPath("members[display eq \"" + userName + "\"]");
patchOperations1.put("remove", new ArrayList<PatchOperation>() {{
add(patchOperation3);
}});
patchOperations2.put("remove", new ArrayList<PatchOperation>() {{
add(patchOperation3);
}});
patchOperations4.put("remove", new ArrayList<PatchOperation>() {{
add(patchOperation3);
}});

return new Object[][]{
{true, currentGroupName, updatedGroupName, patchOperations1},
{false, currentGroupName, updatedGroupName, patchOperations1},
{false, currentGroupName, currentGroupName, patchOperations2},
{false, currentGroupName, updatedGroupName, patchOperations3},
{false, currentGroupName, currentGroupName, patchOperations4},
};
}

@Test(dataProvider = "patchGroupDataProvider")
public void testPatchGroup(boolean isUniqueGroupIdEnabled, String currentGroupName, String updatedGroupName,
Map<String, List<PatchOperation>> patchOperations)
throws NotImplementedException, BadRequestException, NotFoundException, CharonException,
IdentityApplicationManagementException, UserStoreException {

String currentGroupNameWithDomain = "PRIMARY/" + currentGroupName;
String updatedGroupNameWithDomain = "PRIMARY/" + updatedGroupName;

scimCommonUtils.when(() -> SCIMCommonUtils.getGroupNameWithDomain(currentGroupName))
.thenReturn(currentGroupNameWithDomain);
scimCommonUtils.when(() -> SCIMCommonUtils.getGroupNameWithDomain(updatedGroupName))
.thenReturn(updatedGroupNameWithDomain);
when(IdentityUtil.extractDomainFromName(anyString())).thenReturn("PRIMARY");

InboundProvisioningConfig inboundProvisioningConfig = new InboundProvisioningConfig();
inboundProvisioningConfig.setProvisioningUserStore("PRIMARY");
ServiceProvider serviceProvider = new ServiceProvider();
serviceProvider.setInboundProvisioningConfig(inboundProvisioningConfig);
when(ApplicationManagementService.getInstance()).thenReturn(applicationManagementService);
when(applicationManagementService.getServiceProvider(anyString(), anyString())).thenReturn(serviceProvider);

mockedUserStoreManager = mock(AbstractUserStoreManager.class);
when(mockedUserStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(secondaryUserStoreManager);
when(mockedUserStoreManager.isUniqueGroupIdEnabled()).thenReturn(isUniqueGroupIdEnabled);
when(secondaryUserStoreManager.getUserIDFromProperties(USERNAME_LOCAL_CLAIM, "user1", "default"))
.thenReturn("1234");

try (MockedConstruction<SCIMGroupHandler> mockedGroupHandler = mockConstruction(SCIMGroupHandler.class)) {
SCIMUserManager scimUserManager = new SCIMUserManager(mockedUserStoreManager,
mockClaimMetadataManagementService, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
scimUserManager.patchGroup("123456", currentGroupName, patchOperations);

// Assert if mocks are called as expected.
if (currentGroupName.equals(updatedGroupName) || !patchOperations.containsKey("add")) {
verify(mockedUserStoreManager, times(0)).renameGroup("123456", updatedGroupNameWithDomain);
} else {
verify(mockedUserStoreManager, times(1)).renameGroup("123456", updatedGroupNameWithDomain);
}
if (patchOperations.containsKey("remove")) {
verify(mockedUserStoreManager, times(1)).updateUserListOfRoleWithID(anyString(), any(), any());
}
verify(mockedUserStoreManager, times(1)).updateGroupName(currentGroupName, updatedGroupName);

if (isUniqueGroupIdEnabled) {
assertEquals(mockedGroupHandler.constructed().size(), 0);
} else {
assertEquals(mockedGroupHandler.constructed().size(), 1);
}
}
}

@Test
public void testUpdateUserWithUsernameChange() throws Exception {

Expand Down

0 comments on commit 2117b8a

Please sign in to comment.