Skip to content

Commit

Permalink
Add unit test for UniqueGroupId in post-update role name listener
Browse files Browse the repository at this point in the history
  • Loading branch information
HasiniSama committed Feb 6, 2025
1 parent d29840b commit aadc756
Showing 1 changed file with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2017, WSO2 LLC. (http://www.wso2.org)
* Copyright (c) 2017-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand Down Expand Up @@ -39,6 +39,7 @@
import org.wso2.carbon.user.api.RealmConfiguration;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.common.User;
import org.wso2.carbon.user.core.common.UserStore;
import org.wso2.carbon.user.core.util.UserCoreUtil;
Expand All @@ -50,6 +51,7 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
Expand All @@ -59,6 +61,7 @@
import static org.mockito.MockitoAnnotations.initMocks;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;


Expand All @@ -76,6 +79,7 @@ public class SCIMUserOperationListenerTest {
private String profile = "testProfile";
private String claimURI = "http://wso2.org/claims/country";
private String claimValue = "dummyValue";
private String domainName = "testDomain";
private boolean isAuthenticated = true;
SCIMUserOperationListener scimUserOperationListener;

Expand Down Expand Up @@ -283,12 +287,13 @@ public void testDoPostDeleteUserClaimValue() throws Exception {

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

return new Object[][]{
{true, true, true, "testDomain"},
{true, true, true, domainName},
{true, true, false, null},
{false, false, true, "testDomain"},
{true, false, false, "testDomain"},
{false, true, true, "testDomain"}
{false, false, true, domainName},
{true, false, false, domainName},
{false, true, true, domainName}
};
}

Expand All @@ -311,7 +316,8 @@ public void testDoPostAddRole1() throws Exception {

@Test(expectedExceptions = UserStoreException.class)
public void testDoPostAddRole2() throws Exception {
mockTestEnvironment(true, true, "testDomain");

mockTestEnvironment(true, true, domainName);
try (MockedConstruction<GroupDAO> mockedGroupDAO = Mockito.mockConstruction(GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(anyString(), anyInt()))
Expand All @@ -331,7 +337,8 @@ public void testDoPreDeleteRole1() throws Exception {

@Test(expectedExceptions = UserStoreException.class)
public void testDoPreDeleteRole2() throws Exception {
mockTestEnvironment(true, true, "testDomain");

mockTestEnvironment(true, true, domainName);
try (MockedConstruction<GroupDAO> mockedGroupDAO = Mockito.mockConstruction(GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(nullable(String.class), anyInt()))
Expand All @@ -353,12 +360,13 @@ public void testDoPreUpdateRoleName() throws Exception {

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

return new Object[][]{
{true, true, "testDomain"},
{true, true, domainName},
{true, true, null},
{false, false, "testDomain"},
{true, false, "testDomain"},
{false, true, "testDomain"}
{false, false, domainName},
{true, false, domainName},
{false, true, domainName}
};
}

Expand All @@ -383,7 +391,8 @@ public void testDoPostUpdateRoleName1() throws Exception {

@Test(expectedExceptions = UserStoreException.class)
public void testDoPostUpdateRoleName2() throws Exception {
mockTestEnvironment(true, true, "testDomain");

mockTestEnvironment(true, true, domainName);
try (MockedConstruction<GroupDAO> mockedGroupDAO = Mockito.mockConstruction(GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(anyString(), anyInt()))
Expand All @@ -393,6 +402,44 @@ public void testDoPostUpdateRoleName2() throws Exception {
}
}

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

return new Object[][]{
{true}, {false}
};
}

@Test(dataProvider = "testDoPostUpdateRoleNameForUniqueGroupIdFlag")
public void testDoPostUpdateRoleNameForUniqueGroupIdFlag(boolean isUniqueGroupIdEnabled) throws Exception {

try (MockedConstruction<GroupDAO> mockedGroupDAO = Mockito.mockConstruction(GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(anyString(), anyInt())).thenReturn(true);
})) {
AbstractUserStoreManager mockUserStoreManager = Mockito.mock(AbstractUserStoreManager.class);
when(mockUserStoreManager.isSCIMEnabled()).thenReturn(true);
when(mockUserStoreManager.isUniqueGroupIdEnabled()).thenReturn(isUniqueGroupIdEnabled);
when(scimUserOperationListener.isEnable()).thenReturn(true);

userCoreUtil.when(() -> UserCoreUtil.addDomainToName(anyString(), anyString()))
.thenReturn(domainName);

assertTrue(scimUserOperationListener.doPostUpdateRoleName(roleName, roleName, mockUserStoreManager));

GroupDAO groupDAO = mockedGroupDAO.constructed().stream()
.findFirst()
.orElse(null);

if (isUniqueGroupIdEnabled) {
assertNull(groupDAO, "GroupDAO instance should have not been created");
} else {
assertNotNull(groupDAO, "GroupDAO instance should have been created");
Mockito.verify(groupDAO, Mockito.times(1)).updateRoleName(anyInt(), anyString(), anyString());
}
}
}

@Test
public void testDoPreUpdateUserListOfRole() throws Exception {
assertTrue(scimUserOperationListener.doPreUpdateUserListOfRole(anyString(), any(String[].class),
Expand Down

0 comments on commit aadc756

Please sign in to comment.