1+ package io .mosip .kernel .masterdata .test .impl ;
2+
3+ import io .mosip .kernel .masterdata .service .impl .CacheManagementServiceImpl ;
4+ import io .mosip .kernel .masterdata .utils .CacheName ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .mockito .InjectMocks ;
8+ import org .mockito .Mock ;
9+ import org .mockito .junit .MockitoJUnitRunner ;
10+ import org .springframework .cache .Cache ;
11+ import org .springframework .cache .CacheManager ;
12+
13+ import java .util .Arrays ;
14+ import java .util .HashSet ;
15+ import java .util .Set ;
16+
17+ import static org .mockito .Mockito .*;
18+
19+
20+ @ RunWith (MockitoJUnitRunner .class )
21+ public class CacheManagementServiceImplTest {
22+
23+ @ Mock
24+ private CacheManager cacheManager ;
25+
26+ @ Mock
27+ private Cache cache1 ;
28+
29+ @ Mock
30+ private Cache cache2 ;
31+
32+ @ InjectMocks
33+ private CacheManagementServiceImpl cacheManagementService ;
34+
35+
36+ @ Test
37+ public void clearCacheByCacheName_shouldClearMatchingCache () {
38+ // given
39+ CacheName cacheName = CacheName .DOCUMENT_CATEGORY ;
40+ Set <String > cacheNames = new HashSet <>();
41+ cacheNames .add (cacheName .getName ());
42+ when (cacheManager .getCacheNames ()).thenReturn (cacheNames );
43+ when (cacheManager .getCache (cacheName .getName ())).thenReturn (cache1 );
44+ // when
45+ cacheManagementService .clearCacheByCacheName (cacheName );
46+ // then
47+ verify (cacheManager , times (1 )).getCacheNames ();
48+ verify (cacheManager , times (1 )).getCache (cacheName .getName ());
49+ verify (cache1 , times (1 )).clear ();
50+ verifyNoMoreInteractions (cache1 );
51+ }
52+
53+ @ Test
54+ public void clearCacheByCacheName_shouldNotFailWhenCacheIsNull () {
55+ // given
56+ CacheName cacheName = CacheName .BLOCK_LISTED_WORDS ;
57+ Set <String > cacheNames = new HashSet <>();
58+ cacheNames .add (cacheName .getName ());
59+ when (cacheManager .getCacheNames ()).thenReturn (cacheNames );
60+ when (cacheManager .getCache (cacheName .getName ())).thenReturn (null );
61+ // when
62+ cacheManagementService .clearCacheByCacheName (cacheName );
63+ // then
64+ verify (cacheManager , times (1 )).getCacheNames ();
65+ verify (cacheManager , times (1 )).getCache (cacheName .getName ());
66+ verifyNoInteractions (cache1 , cache2 );
67+ }
68+
69+ @ Test
70+ public void clearCacheByCacheName_shouldDoNothingWhenNoNameMatches () {
71+ // given
72+ CacheName cacheName = CacheName .DYNAMIC_FIELD ;
73+ Set <String > cacheNames = new HashSet <>(Arrays .asList ("OTHER_CACHE_1" , "OTHER_CACHE_2" ));
74+ when (cacheManager .getCacheNames ()).thenReturn (cacheNames );
75+ when (cacheManager .getCache ("OTHER_CACHE_1" )).thenReturn (cache1 );
76+ when (cacheManager .getCache ("OTHER_CACHE_2" )).thenReturn (cache2 );
77+ // when
78+ cacheManagementService .clearCacheByCacheName (cacheName );
79+ // then
80+ verify (cacheManager , times (1 )).getCacheNames ();
81+ verify (cacheManager , times (1 )).getCache ("OTHER_CACHE_1" );
82+ verify (cacheManager , times (1 )).getCache ("OTHER_CACHE_2" );
83+ verify (cache1 , never ()).clear ();
84+ verify (cache2 , never ()).clear ();
85+ }
86+
87+ @ Test
88+ public void clearCache_shouldClearAllNonNullCaches () {
89+ // given
90+ Set <String > cacheNames = new HashSet <>(Arrays .asList ("cache1" , "cache2" ));
91+ when (cacheManager .getCacheNames ()).thenReturn (cacheNames );
92+ when (cacheManager .getCache ("cache1" )).thenReturn (cache1 );
93+ when (cacheManager .getCache ("cache2" )).thenReturn (cache2 );
94+ // when
95+ cacheManagementService .clearCache ();
96+ // then
97+ verify (cacheManager , times (1 )).getCacheNames ();
98+ verify (cacheManager , times (1 )).getCache ("cache1" );
99+ verify (cacheManager , times (1 )).getCache ("cache2" );
100+ verify (cache1 , times (1 )).clear ();
101+ verify (cache2 , times (1 )).clear ();
102+ }
103+
104+ @ Test
105+ public void clearCache_shouldIgnoreNullCaches () {
106+ // given
107+ Set <String > cacheNames = new HashSet <>(Arrays .asList ("cache1" , "cache2" ));
108+ when (cacheManager .getCacheNames ()).thenReturn (cacheNames );
109+ when (cacheManager .getCache ("cache1" )).thenReturn (cache1 );
110+ when (cacheManager .getCache ("cache2" )).thenReturn (null );
111+ // when
112+ cacheManagementService .clearCache ();
113+ // then
114+ verify (cacheManager , times (1 )).getCacheNames ();
115+ verify (cacheManager , times (1 )).getCache ("cache1" );
116+ verify (cacheManager , times (1 )).getCache ("cache2" );
117+ verify (cache1 , times (1 )).clear (); // seul cache1 non null
118+ verifyNoMoreInteractions (cache1 );
119+ }
120+
121+ }
0 commit comments