Skip to content

Commit 5ec17d7

Browse files
Add unit tests
1 parent 33999e2 commit 5ec17d7

File tree

4 files changed

+569
-0
lines changed

4 files changed

+569
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.wso2.carbon.identity.claim.metadata.mgt;
2+
3+
import org.mockito.MockedStatic;
4+
import org.testng.annotations.BeforeMethod;
5+
import org.testng.annotations.Test;
6+
import org.wso2.carbon.context.CarbonContext;
7+
import org.wso2.carbon.identity.claim.metadata.mgt.cache.LocalClaimCache;
8+
import org.wso2.carbon.identity.claim.metadata.mgt.model.AttributeMapping;
9+
import org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim;
10+
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
11+
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
import static org.mockito.ArgumentMatchers.anyInt;
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.mockStatic;
19+
import static org.mockito.Mockito.when;
20+
import static org.testng.Assert.assertEquals;
21+
import static org.testng.Assert.assertNotNull;
22+
23+
/**
24+
* Unit test class for LocalClaimCache.
25+
*/
26+
@WithCarbonHome
27+
public class LocalClaimCacheTest {
28+
29+
private static final String TENANT_DOMAIN = "carbon.super";
30+
31+
@BeforeMethod
32+
public void setUp() {
33+
// Any common setup logic can go here
34+
}
35+
36+
/**
37+
* Test to verify that the LocalClaimCache instance is not null.
38+
*/
39+
@Test
40+
public void testGetInstance() {
41+
42+
try (MockedStatic<CarbonContext> carbonContextMock = mockStatic(CarbonContext.class)) {
43+
initializeCarbonContextMock(carbonContextMock);
44+
assertNotNull(LocalClaimCache.getInstance(), "LocalClaimCache instance should not be null");
45+
}
46+
}
47+
48+
/**
49+
* Test to verify adding claims to the cache and retrieving them.
50+
*/
51+
@Test
52+
public void testAddClaimsToCache() {
53+
54+
try (MockedStatic<CarbonContext> carbonContextMock = mockStatic(CarbonContext.class);
55+
MockedStatic<IdentityTenantUtil> identityTenantUtilMock = mockStatic(IdentityTenantUtil.class)) {
56+
57+
initializeCarbonContextMocks(carbonContextMock, identityTenantUtilMock);
58+
LocalClaimCache claimCache = LocalClaimCache.getInstance();
59+
60+
ArrayList<LocalClaim> initialClaims = createLocalClaims("http://wso2.org/claims/firstName");
61+
claimCache.addToCache(1, initialClaims, 1);
62+
assertEquals(claimCache.getValueFromCache(1, 1), initialClaims, "Cache should store the initial claims");
63+
64+
ArrayList<LocalClaim> updatedClaims = createLocalClaims("http://wso2.org/claims/lastName");
65+
claimCache.addToCache(1, updatedClaims, 1);
66+
assertEquals(claimCache.getValueFromCache(1, 1), updatedClaims, "Cache should update with new claims");
67+
}
68+
}
69+
70+
/**
71+
* Test to verify adding claims to the cache with a tenant domain and retrieving them.
72+
*/
73+
@Test
74+
public void testAddClaimsToCacheWithTenantDomain() {
75+
76+
try (MockedStatic<CarbonContext> carbonContextMock = mockStatic(CarbonContext.class);
77+
MockedStatic<IdentityTenantUtil> identityTenantUtilMock = mockStatic(IdentityTenantUtil.class)) {
78+
79+
initializeCarbonContextMocks(carbonContextMock, identityTenantUtilMock);
80+
LocalClaimCache claimCache = LocalClaimCache.getInstance();
81+
82+
ArrayList<LocalClaim> initialClaims = createLocalClaims("http://wso2.org/claims/firstName");
83+
claimCache.addToCache(1, initialClaims, TENANT_DOMAIN);
84+
assertEquals(claimCache.getValueFromCache(1, TENANT_DOMAIN), initialClaims, "Cache should store the initial claims");
85+
86+
ArrayList<LocalClaim> updatedClaims = createLocalClaims("http://wso2.org/claims/lastName");
87+
claimCache.addToCache(1, updatedClaims, TENANT_DOMAIN);
88+
assertEquals(claimCache.getValueFromCache(1, TENANT_DOMAIN), updatedClaims, "Cache should update with new claims");
89+
}
90+
}
91+
92+
/**
93+
* Test to verify that adding claims to the cache with a null key does not overwrite existing values.
94+
*/
95+
@Test
96+
public void testAddToCacheWithNullKey() {
97+
98+
try (MockedStatic<CarbonContext> carbonContextMock = mockStatic(CarbonContext.class);
99+
MockedStatic<IdentityTenantUtil> identityTenantUtilMock = mockStatic(IdentityTenantUtil.class)) {
100+
101+
initializeCarbonContextMocks(carbonContextMock, identityTenantUtilMock);
102+
LocalClaimCache localClaimCache = LocalClaimCache.getInstance();
103+
104+
ArrayList<LocalClaim> localClaims = createLocalClaims("http://wso2.org/claims/firstName");
105+
localClaimCache.addToCache(1, localClaims, 1);
106+
assertNotNull(localClaimCache.getValueFromCache(1, 1), "Cache should store the initial claims");
107+
108+
ArrayList<LocalClaim> newLocalClaims = createLocalClaims("http://wso2.org/claims/lastName");
109+
localClaimCache.addToCache(null, newLocalClaims, 1);
110+
111+
List<LocalClaim> cachedValue = localClaimCache.getValueFromCache(1, 1);
112+
assertEquals(cachedValue, localClaims, "Cache should not overwrite with null key");
113+
}
114+
}
115+
116+
private void initializeCarbonContextMocks(MockedStatic<CarbonContext> carbonContextMock,
117+
MockedStatic<IdentityTenantUtil> identityTenantUtilMock) {
118+
119+
initializeCarbonContextMock(carbonContextMock);
120+
identityTenantUtilMock.when(() -> IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn(TENANT_DOMAIN);
121+
}
122+
123+
private void initializeCarbonContextMock(MockedStatic<CarbonContext> carbonContextMock) {
124+
125+
CarbonContext carbonContext = mock(CarbonContext.class);
126+
carbonContextMock.when(CarbonContext::getThreadLocalCarbonContext).thenReturn(carbonContext);
127+
when(carbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
128+
}
129+
130+
private ArrayList<LocalClaim> createLocalClaims(String claimURI) {
131+
132+
ArrayList<LocalClaim> localClaims = new ArrayList<>();
133+
LocalClaim localClaim = new LocalClaim(claimURI);
134+
localClaim.setMappedAttribute(new AttributeMapping("primary", claimURI));
135+
localClaims.add(localClaim);
136+
return localClaims;
137+
}
138+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.wso2.carbon.identity.claim.metadata.mgt.model;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static org.testng.Assert.assertEquals;
6+
import static org.testng.Assert.assertFalse;
7+
import static org.testng.Assert.assertNotEquals;
8+
9+
/**
10+
* Unit test class for AttributeMapping.
11+
*/
12+
public class AttributeMappingTest {
13+
14+
/**
15+
* Test to verify that two equal AttributeMapping objects are considered equal.
16+
*/
17+
@Test
18+
public void testEqualsReturnsTrueForEqualObjects() {
19+
20+
AttributeMapping mapping1 = new AttributeMapping("PRIMARY", "email");
21+
AttributeMapping mapping2 = new AttributeMapping("PRIMARY", "email");
22+
23+
assertEquals(mapping1, mapping2, "Equal AttributeMapping objects should be considered equal");
24+
}
25+
26+
/**
27+
* Test to verify that AttributeMapping objects with different user store domains are not equal.
28+
*/
29+
@Test
30+
public void testEqualsReturnsFalseForDifferentUserStoreDomains() {
31+
32+
AttributeMapping mapping1 = new AttributeMapping("PRIMARY", "email");
33+
AttributeMapping mapping2 = new AttributeMapping("SECONDARY", "email");
34+
35+
assertNotEquals(mapping1, mapping2,
36+
"AttributeMapping objects with different user store domains should not be equal");
37+
}
38+
39+
/**
40+
* Test to verify that AttributeMapping objects with different attribute names are not equal.
41+
*/
42+
@Test
43+
public void testEqualsReturnsFalseForDifferentAttributeNames() {
44+
45+
AttributeMapping mapping1 = new AttributeMapping("PRIMARY", "email");
46+
AttributeMapping mapping2 = new AttributeMapping("PRIMARY", "username");
47+
48+
assertNotEquals(mapping1, mapping2,
49+
"AttributeMapping objects with different attribute names should not be equal");
50+
}
51+
52+
/**
53+
* Test to verify that AttributeMapping is not equal to null.
54+
*/
55+
@Test
56+
public void testEqualsReturnsFalseForNullObject() {
57+
58+
AttributeMapping mapping = new AttributeMapping("PRIMARY", "email");
59+
60+
assertNotEquals(mapping, null, "AttributeMapping should not be equal to null");
61+
}
62+
63+
/**
64+
* Test to verify that AttributeMapping is not equal to an object of a different type.
65+
*/
66+
@Test
67+
public void testEqualsReturnsFalseForDifferentObjectType() {
68+
69+
AttributeMapping mapping = new AttributeMapping("PRIMARY", "email");
70+
71+
assertFalse(mapping.equals("SomeString"),
72+
"AttributeMapping should not be equal to an object of a different type");
73+
}
74+
75+
/**
76+
* Test to verify that the user store domain is returned in uppercase.
77+
*/
78+
@Test
79+
public void testGetUserStoreDomainReturnsUpperCaseValue() {
80+
81+
AttributeMapping mapping = new AttributeMapping("primary", "email");
82+
83+
assertEquals(mapping.getUserStoreDomain(), "PRIMARY",
84+
"User store domain should be converted to uppercase");
85+
}
86+
87+
/**
88+
* Test to verify that the attribute name is returned correctly.
89+
*/
90+
@Test
91+
public void testGetAttributeNameReturnsCorrectValue() {
92+
93+
AttributeMapping mapping = new AttributeMapping("PRIMARY", "email");
94+
95+
assertEquals(mapping.getAttributeName(), "email",
96+
"Attribute name should match the provided value");
97+
}
98+
99+
/**
100+
* Test to verify that AttributeMapping is not equal to a completely different object.
101+
*/
102+
@Test
103+
public void testEqualsReturnsFalseForDifferentObject() {
104+
105+
AttributeMapping mapping1 = new AttributeMapping("PRIMARY", "email");
106+
Object obj = new Object();
107+
108+
assertNotEquals(obj, mapping1,
109+
"AttributeMapping should not be equal to an object of a different type");
110+
}
111+
}

0 commit comments

Comments
 (0)