diff --git a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/dao/LocalClaimDAOTest.java b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/dao/LocalClaimDAOTest.java index 0e82797be45b..3b89ce1c1957 100644 --- a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/dao/LocalClaimDAOTest.java +++ b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/dao/LocalClaimDAOTest.java @@ -15,7 +15,7 @@ */ package org.wso2.carbon.identity.claim.metadata.mgt.dao; -import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException; @@ -25,14 +25,18 @@ import org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants; import org.wso2.carbon.identity.common.testng.WithCarbonHome; import org.wso2.carbon.identity.common.testng.WithH2Database; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import java.sql.Connection; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; @Test @WithH2Database(jndiName = "jdbc/WSO2IdentityDB", @@ -58,7 +62,7 @@ public class LocalClaimDAOTest { Map claimProperties2; Map claimProperties3; - @BeforeClass + @BeforeMethod public void initTest() throws Exception { attributeMapping1 = new AttributeMapping("PRIMARY", "givenname"); @@ -193,4 +197,124 @@ public Object[][] testUpdateLocalClaimData() { }; } + + @Test + public void testAddLocalClaimWithUniqueProperty() throws Exception { + + // Add claim dialect + ClaimDialectDAO claimDialectDAO = new ClaimDialectDAO(); + ClaimDialect claimDialect = new ClaimDialect(ClaimConstants.LOCAL_CLAIM_DIALECT_URI); + claimDialectDAO.addClaimDialect(claimDialect, TEST_LOCAL_TENANT_ID); + + // Add claim + localClaim3.getClaimProperties().put(ClaimConstants.IS_UNIQUE_CLAIM_PROPERTY, "true"); + LocalClaimDAO localClaimDAO = new LocalClaimDAO(); + localClaimDAO.addLocalClaim(localClaim3, TEST_LOCAL_TENANT_ID); + + // Retrieve and verify + List retrievedClaims = localClaimDAO.getLocalClaims(TEST_LOCAL_TENANT_ID); + assertNotNull(retrievedClaims, "Retrieved claims should not be null"); + assertEquals(retrievedClaims.size(), 1, "Should have retrieved exactly one claim"); + + LocalClaim retrievedClaim = retrievedClaims.get(0); + Map retrievedProperties = retrievedClaim.getClaimProperties(); + + // Verify property transformation + verifyUniqueClaimProperties(retrievedProperties); + + // Clean up + localClaimDAO.removeLocalClaim(localClaim3.getClaimURI(), TEST_LOCAL_TENANT_ID); + claimDialectDAO.removeClaimDialect(claimDialect, TEST_LOCAL_TENANT_ID); + } + + @Test + public void testGetLocalClaimsWithUniqueProperty() throws Exception { + + // Add claim dialect + ClaimDialectDAO claimDialectDAO = new ClaimDialectDAO(); + ClaimDialect claimDialect = new ClaimDialect(ClaimConstants.LOCAL_CLAIM_DIALECT_URI); + claimDialectDAO.addClaimDialect(claimDialect, TEST_LOCAL_TENANT_ID); + + // Add initial claim + LocalClaimDAO localClaimDAO = new LocalClaimDAO(); + localClaimDAO.addLocalClaim(localClaim3, TEST_LOCAL_TENANT_ID); + + // Get claim ID and add isUnique property directly using ClaimDAO + try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) { + ClaimDAO claimDAO = new ClaimDAO(); + int claimId = claimDAO.getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, + localClaim3.getClaimURI(), TEST_LOCAL_TENANT_ID); + + Map uniqueProperty = new HashMap<>(); + uniqueProperty.put(ClaimConstants.IS_UNIQUE_CLAIM_PROPERTY, "true"); + claimDAO.addClaimProperties(connection, claimId, uniqueProperty, TEST_LOCAL_TENANT_ID); + + IdentityDatabaseUtil.commitTransaction(connection); + } + + // Retrieve and verify + List retrievedClaims = localClaimDAO.getLocalClaims(TEST_LOCAL_TENANT_ID); + assertNotNull(retrievedClaims, "Retrieved claims should not be null"); + assertEquals(retrievedClaims.size(), 1, "Should have retrieved exactly one claim"); + + LocalClaim retrievedClaim = retrievedClaims.get(0); + Map retrievedProperties = retrievedClaim.getClaimProperties(); + + // Verify property transformation + verifyUniqueClaimProperties(retrievedProperties); + + // Clean up + localClaimDAO.removeLocalClaim(localClaim3.getClaimURI(), TEST_LOCAL_TENANT_ID); + claimDialectDAO.removeClaimDialect(claimDialect, TEST_LOCAL_TENANT_ID); + } + + @Test + public void testUpdateLocalClaimWithUniqueProperty() throws ClaimMetadataException { + + // Setup initial claim without isUnique property + ClaimDialectDAO claimDialectDAO = new ClaimDialectDAO(); + ClaimDialect claimDialect = new ClaimDialect(ClaimConstants.LOCAL_CLAIM_DIALECT_URI); + claimDialectDAO.addClaimDialect(claimDialect, TEST_LOCAL_TENANT_ID); + + LocalClaimDAO localClaimDAO = new LocalClaimDAO(); + localClaimDAO.addLocalClaim(localClaim3, TEST_LOCAL_TENANT_ID); + + // Update claim with isUnique property + Map updatedProperties = new HashMap<>(localClaim3.getClaimProperties()); + updatedProperties.put(ClaimConstants.IS_UNIQUE_CLAIM_PROPERTY, "true"); + localClaim3.setClaimProperties(updatedProperties); + + localClaimDAO.updateLocalClaim(localClaim3, TEST_LOCAL_TENANT_ID); + + // Retrieve and verify + List retrievedClaims = localClaimDAO.getLocalClaims(TEST_LOCAL_TENANT_ID); + assertNotNull(retrievedClaims, "Retrieved claims should not be null"); + assertEquals(retrievedClaims.size(), 1, "Should have retrieved exactly one claim"); + + LocalClaim retrievedClaim = retrievedClaims.get(0); + Map retrievedProperties = retrievedClaim.getClaimProperties(); + + // Verify property transformation + verifyUniqueClaimProperties(retrievedProperties); + + // Clean up + localClaimDAO.removeLocalClaim(localClaim3.getClaimURI(), TEST_LOCAL_TENANT_ID); + claimDialectDAO.removeClaimDialect(claimDialect, TEST_LOCAL_TENANT_ID); + } + + /** + * Verifies that the claim properties have been correctly transformed for unique claims. + * + * @param retrievedProperties The map of claim properties to verify + */ + private void verifyUniqueClaimProperties(Map retrievedProperties) { + + assertFalse(retrievedProperties.containsKey(ClaimConstants.IS_UNIQUE_CLAIM_PROPERTY), + "isUnique property should have been removed"); + assertTrue(retrievedProperties.containsKey(ClaimConstants.CLAIM_UNIQUENESS_SCOPE_PROPERTY), + "UniquenessScope property should have been added"); + assertEquals(ClaimConstants.ClaimUniquenessScope.ACROSS_USERSTORES.toString(), + retrievedProperties.get(ClaimConstants.CLAIM_UNIQUENESS_SCOPE_PROPERTY), + "UniquenessScope should be set to ACROSS_USERSTORES"); + } } diff --git a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtilsTest.java b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtilsTest.java index 2edfece1fe64..eef0b1c4c762 100644 --- a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtilsTest.java +++ b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtilsTest.java @@ -15,8 +15,11 @@ */ package org.wso2.carbon.identity.claim.metadata.mgt.util; +import org.mockito.MockedStatic; import org.testng.Assert; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.wso2.carbon.identity.claim.metadata.mgt.dto.AttributeMappingDTO; @@ -28,6 +31,7 @@ import org.wso2.carbon.identity.claim.metadata.mgt.model.ClaimDialect; import org.wso2.carbon.identity.claim.metadata.mgt.model.ExternalClaim; import org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim; +import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.user.core.UserCoreConstants; import org.wso2.carbon.user.core.claim.ClaimMapping; @@ -37,6 +41,9 @@ import java.util.List; import java.util.Map; +import static org.mockito.Mockito.mockStatic; +import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.UNIQUENESS_VALIDATION_SCOPE; + /** * Covers unit tests for ClaimMetadataUtils class */ @@ -60,6 +67,8 @@ public class ClaimMetadataUtilsTest { ExternalClaimDTO externalClaimDTO; + private MockedStatic identityUtilMock; + @BeforeClass public void setUp() throws Exception { setUpClaimDialects(); @@ -67,6 +76,20 @@ public void setUp() throws Exception { setUpExternalClaims(); } + @BeforeMethod + public void setUpMocks() { + + identityUtilMock = mockStatic(IdentityUtil.class); + } + + @AfterMethod + public void tearDownMocks() { + + if (identityUtilMock != null) { + identityUtilMock.close(); + } + } + private void setUpClaimDialects() { String claimDialectURI = "testClaimDialectURI"; claimDialect = new ClaimDialect(claimDialectURI); @@ -504,4 +527,37 @@ public void testConvertExternalClaimToClaimMapping() throws Exception { } + @DataProvider(name = "uniquenessScopeData") + public Object[][] getUniquenessScopeData() { + return new Object[][]{ + {"true", ClaimConstants.ClaimUniquenessScope.WITHIN_USERSTORE}, + {"false", ClaimConstants.ClaimUniquenessScope.ACROSS_USERSTORES}, + {null, ClaimConstants.ClaimUniquenessScope.ACROSS_USERSTORES}, + {"", ClaimConstants.ClaimUniquenessScope.ACROSS_USERSTORES} + }; + } + + @Test(dataProvider = "uniquenessScopeData") + public void testGetServerLevelClaimUniquenessScope(String configValue, + ClaimConstants.ClaimUniquenessScope expectedScope) { + + identityUtilMock.when(() -> IdentityUtil.getProperty(UNIQUENESS_VALIDATION_SCOPE)).thenReturn(configValue); + + ClaimConstants.ClaimUniquenessScope actualScope = ClaimMetadataUtils.getServerLevelClaimUniquenessScope(); + + Assert.assertEquals(actualScope, expectedScope, + "Incorrect uniqueness scope for config value: " + configValue); + } + + @Test + public void testGetServerLevelClaimUniquenessScopeWithInvalidValue() { + + identityUtilMock.when(() -> IdentityUtil.getProperty(UNIQUENESS_VALIDATION_SCOPE)) + .thenReturn("invalid_value"); + + ClaimConstants.ClaimUniquenessScope actualScope = ClaimMetadataUtils.getServerLevelClaimUniquenessScope(); + + Assert.assertEquals(actualScope, ClaimConstants.ClaimUniquenessScope.ACROSS_USERSTORES, + "Should default to ACROSS_USERSTORES for invalid config value"); + } }