Skip to content

Commit 505a094

Browse files
committed
MOSIP-42804 - increasing the code coverage
Signed-off-by: Youssef MAHTAT <youssef.mahtat.as.developer@gmail.com>
1 parent be7fac9 commit 505a094

4 files changed

Lines changed: 620 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.mosip.admin.validator;
2+
3+
import jakarta.validation.ConstraintValidatorContext;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.springframework.test.util.ReflectionTestUtils;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class LanguageCharacterValidatorTest {
11+
12+
private LanguageCharacterValidator validator;
13+
14+
@Before
15+
public void setUp() {
16+
validator = new LanguageCharacterValidator();
17+
}
18+
19+
@Test
20+
public void isValid_shouldReturnTrue_whenAllowedCharactersRegexIsNull() {
21+
// given
22+
ReflectionTestUtils.setField(validator, "allowedCharactersRegex", null);
23+
// when
24+
boolean result = validator.isValid("AnyValue123", (ConstraintValidatorContext) null);
25+
// then
26+
assertTrue(result);
27+
}
28+
29+
@Test
30+
public void isValid_shouldReturnTrue_whenValueIsNull() {
31+
// given
32+
ReflectionTestUtils.setField(validator, "allowedCharactersRegex", "[^a-zA-Z]");
33+
// when
34+
boolean result = validator.isValid(null, (ConstraintValidatorContext) null);
35+
// then
36+
assertTrue(result);
37+
}
38+
39+
@Test
40+
public void isValid_shouldReturnTrue_whenValueIsEmpty() {
41+
// given
42+
ReflectionTestUtils.setField(validator, "allowedCharactersRegex", "[^a-zA-Z]");
43+
// when
44+
boolean result = validator.isValid("", (ConstraintValidatorContext) null);
45+
// then
46+
assertTrue(result);
47+
}
48+
49+
@Test
50+
public void isValid_shouldReturnTrue_whenValueContainsOnlyAllowedCharacters() {
51+
// given
52+
ReflectionTestUtils.setField(validator, "allowedCharactersRegex", "[^a-zA-Z]");
53+
// when
54+
boolean result = validator.isValid("AlphaBeta", (ConstraintValidatorContext) null);
55+
// then
56+
assertTrue(result);
57+
}
58+
59+
@Test
60+
public void isValid_shouldReturnFalse_whenValueContainsNotAllowedCharacters() {
61+
// given
62+
ReflectionTestUtils.setField(validator, "allowedCharactersRegex", "[^a-zA-Z]");
63+
// when
64+
boolean result = validator.isValid("Alpha1", (ConstraintValidatorContext) null);
65+
// then
66+
assertFalse(result);
67+
}
68+
69+
@Test
70+
public void isValid_shouldTrimValueBeforeValidation() {
71+
// given
72+
ReflectionTestUtils.setField(validator, "allowedCharactersRegex", "[^a-zA-Z]");
73+
// when
74+
boolean result = validator.isValid(" Alpha ", (ConstraintValidatorContext) null);
75+
// then
76+
assertTrue(result);
77+
}
78+
79+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)