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
+ }
0 commit comments