Skip to content

Commit 2087306

Browse files
[MOSIP-32144] code coverage of authentication-service (mosip#1560) (mosip#1634)
* code coverage of authentication-service * code coverage of otp-service and auth service * fixed build failure * code coverage for auth service * code coverage for common and auth-internal service * code coverage for auth internal service * code coverage * addressed the PR comments --------- Signed-off-by: tarique-azeez <mdtarique2703@gmail.com>
1 parent 1677c2c commit 2087306

28 files changed

Lines changed: 2067 additions & 182 deletions

authentication/authentication-common/src/test/java/io/mosip/authentication/common/service/impl/AuthAnonymousProfileServiceImplTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ public void before() {
9090
ReflectionTestUtils.setField(anonymousProfileServiceImpl, "locationProfileAttribName","locationHierarchyForProfiling");
9191
ReflectionTestUtils.setField(anonymousProfileServiceImpl, "dateOfBirthPattern", "yyyy/MM/dd");
9292
}
93-
94-
@Ignore
93+
9594
@Test
9695
public void createAnonymousProfileWith_YourOfBirthTest() throws IdAuthenticationBusinessException {
9796
requestBody = new HashMap<>();
@@ -116,8 +115,7 @@ public void createAnonymousProfileWith_YourOfBirthTest() throws IdAuthentication
116115
requestBody, requestMetadata, responseMetadata, true, errorCodes);
117116
assertEquals(anonymousProfile.getYearOfBirth(), "1993");
118117
}
119-
120-
@Ignore
118+
121119
@Test
122120
public void createAnonymousProfileWith_PreferredLangTest() throws IdAuthenticationBusinessException {
123121
requestBody = new HashMap<>();
@@ -140,8 +138,7 @@ public void createAnonymousProfileWith_PreferredLangTest() throws IdAuthenticati
140138
AnonymousAuthenticationProfile anonymousProfile = ReflectionTestUtils.invokeMethod(anonymousProfileServiceImpl, "createAnonymousProfile",requestBody, requestMetadata, responseMetadata, true, errorCodes);
141139
assertEquals(List.of("eng"), anonymousProfile.getPreferredLanguages());
142140
}
143-
144-
@Ignore
141+
145142
@Test
146143
public void createAnonymousProfileWith_GenderTest() throws IdAuthenticationBusinessException {
147144
requestBody = new HashMap<>();
@@ -164,8 +161,7 @@ public void createAnonymousProfileWith_GenderTest() throws IdAuthenticationBusin
164161
AnonymousAuthenticationProfile anonymousProfile = ReflectionTestUtils.invokeMethod(anonymousProfileServiceImpl, "createAnonymousProfile",requestBody,requestMetadata, responseMetadata, true, errorCodes);
165162
assertEquals("Female", anonymousProfile.getGender());
166163
}
167-
168-
@Ignore
164+
169165
@Test
170166
public void createAnonymousProfileWith_LocationTest() throws IdAuthenticationBusinessException {
171167
requestBody = new HashMap<>();
@@ -193,8 +189,7 @@ public void createAnonymousProfileWith_LocationTest() throws IdAuthenticationBus
193189
AnonymousAuthenticationProfile anonymousProfile = ReflectionTestUtils.invokeMethod(anonymousProfileServiceImpl, "createAnonymousProfile",requestBody, requestMetadata, responseMetadata, true, errorCodes);
194190
assertEquals(List.of("zone1", "123456"), anonymousProfile.getLocation());
195191
}
196-
197-
@Ignore
192+
198193
@Test
199194
public void createAnonymousProfileWith_BiometricInfoTest() throws IdAuthenticationBusinessException, IOException {
200195
requestBody = new HashMap<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.mosip.authentication.common.service.impl;
2+
3+
import org.junit.runner.RunWith;
4+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
5+
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.TestContext;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
import org.springframework.test.util.ReflectionTestUtils;
9+
import org.springframework.web.context.WebApplicationContext;
10+
11+
import java.util.*;
12+
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
15+
import io.mosip.authentication.core.constant.IdAuthenticationErrorConstants;
16+
import io.mosip.authentication.core.exception.IdAuthenticationBusinessException;
17+
import io.mosip.authentication.core.spi.authtype.acramr.AuthMethodsRefValues;
18+
import io.mosip.authentication.core.spi.authtype.acramr.AuthenticationFactor;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.mockito.InjectMocks;
23+
import org.mockito.Mock;
24+
import org.mockito.MockitoAnnotations;
25+
import org.springframework.web.client.RestTemplate;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertNotNull;
29+
import static org.junit.Assert.assertNull;
30+
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assert.fail;
32+
import static org.mockito.Mockito.when;
33+
34+
@RunWith(SpringRunner.class)
35+
@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class })
36+
@WebMvcTest
37+
public class AuthContextClazzRefProviderTest {
38+
39+
@Mock
40+
private RestTemplate restTemplate;
41+
42+
private ObjectMapper objectMapper = new ObjectMapper();
43+
44+
@InjectMocks
45+
private AuthContextClazzRefProvider provider;
46+
47+
private final String mockUri = "http://mock-uri/config.json";
48+
49+
@Before
50+
public void setUp() {
51+
MockitoAnnotations.openMocks(this);
52+
ReflectionTestUtils.setField(provider, "objectMapper", objectMapper);
53+
ReflectionTestUtils.setField(provider, "amracrMappingUri", mockUri);
54+
}
55+
56+
@Test
57+
public void testInitSuccess() throws Exception {
58+
59+
String mockJson = """
60+
{
61+
"acr_amr": {
62+
"acr1": ["pwd"]
63+
},
64+
"amr": {
65+
"pwd": [
66+
{ "type": "PASSWORD", "count": 1, "subTypes": [] }
67+
]
68+
}
69+
}
70+
""";
71+
72+
when(restTemplate.getForObject(mockUri, String.class)).thenReturn(mockJson);
73+
74+
provider.init();
75+
76+
AuthMethodsRefValues result = provider.getAuthMethodsRefValues();
77+
assertNotNull(result);
78+
79+
Map<String, List<AuthenticationFactor>> map = result.getAuthMethodsRefValues();
80+
assertTrue(map.containsKey("acr1"));
81+
AuthenticationFactor factor = map.get("acr1").get(0);
82+
83+
assertEquals("PASSWORD", factor.getType());
84+
assertEquals(1, factor.getCount());
85+
assertTrue(factor.getSubTypes().isEmpty());
86+
}
87+
88+
@Test
89+
public void testInitFailure_InvalidJson() {
90+
String badJson = "{ invalid-json }";
91+
when(restTemplate.getForObject(mockUri, String.class)).thenReturn(badJson);
92+
93+
try {
94+
provider.init();
95+
fail("Expected IdAuthenticationBusinessException");
96+
} catch (IdAuthenticationBusinessException ex) {
97+
assertEquals(IdAuthenticationErrorConstants.DOWNLOAD_ERROR.getErrorCode(), ex.getErrorCode());
98+
assertEquals(IdAuthenticationErrorConstants.DOWNLOAD_ERROR.getErrorMessage(), ex.getErrorText());
99+
}
100+
}
101+
102+
@Test
103+
public void testGetterBeforeInit() {
104+
assertNull(provider.getAuthMethodsRefValues());
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package io.mosip.authentication.internal.service.batch;
2+
import io.mosip.authentication.common.service.entity.CredentialEventStore;
3+
import io.mosip.authentication.common.service.entity.IdentityEntity;
4+
import io.mosip.authentication.common.service.repository.CredentialEventStoreRepository;
5+
import io.mosip.authentication.common.service.repository.IdentityCacheRepository;
6+
import io.mosip.authentication.common.service.spi.idevent.CredentialStoreService;
7+
import io.mosip.authentication.common.service.transaction.manager.IdAuthSecurityManager;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.mockito.InjectMocks;
12+
import org.mockito.Mock;
13+
import org.mockito.MockitoAnnotations;
14+
import org.springframework.batch.core.StepContribution;
15+
import org.springframework.batch.core.scope.context.ChunkContext;
16+
import org.springframework.batch.repeat.RepeatStatus;
17+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
18+
import org.springframework.test.context.ContextConfiguration;
19+
import org.springframework.test.context.TestContext;
20+
import org.springframework.test.context.junit4.SpringRunner;
21+
import org.springframework.test.util.ReflectionTestUtils;
22+
import org.springframework.web.context.WebApplicationContext;
23+
24+
import java.util.Arrays;
25+
import java.util.Collections;
26+
import java.util.List;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.mockito.ArgumentMatchers.any;
30+
import static org.mockito.ArgumentMatchers.anyInt;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.never;
33+
import static org.mockito.Mockito.verify;
34+
import static org.powermock.api.mockito.PowerMockito.when;
35+
36+
@WebMvcTest
37+
@ContextConfiguration(classes = {TestContext.class, WebApplicationContext.class})
38+
@RunWith(SpringRunner.class)
39+
public class CredentialStoreTaskletTest {
40+
@InjectMocks
41+
private CredentialStoreTasklet tasklet;
42+
43+
@Mock
44+
private CredentialEventStoreRepository credentialEventRepo;
45+
46+
@Mock
47+
private IdentityCacheRepository identityCacheRepo;
48+
49+
@Mock
50+
private CredentialStoreService credentialStoreService;
51+
52+
@Mock
53+
private IdAuthSecurityManager securityManager;
54+
55+
@Mock
56+
private StepContribution contribution;
57+
58+
@Mock
59+
private ChunkContext chunkContext;
60+
61+
@Before
62+
public void setUp() {
63+
MockitoAnnotations.initMocks(this);
64+
ReflectionTestUtils.setField(tasklet, "threadCount", 4);
65+
tasklet.init();
66+
}
67+
@Test
68+
public void testExecute_withEmptyList_shouldNotCallSaveAll() throws Exception {
69+
when(credentialEventRepo.findNewOrFailedEvents(anyInt())).thenReturn(Collections.emptyList());
70+
71+
RepeatStatus status = tasklet.execute(contribution, chunkContext);
72+
73+
assertEquals(RepeatStatus.FINISHED, status);
74+
verify(credentialEventRepo, never()).saveAll(any());
75+
}
76+
77+
@Test
78+
public void testExecute_withValidEvents_shouldProcessAndSave() throws Exception {
79+
CredentialEventStore event = new CredentialEventStore();
80+
List<CredentialEventStore> events = Arrays.asList(event);
81+
82+
IdentityEntity identity = mock(IdentityEntity.class); // replace with actual type
83+
84+
when(credentialEventRepo.findNewOrFailedEvents(anyInt())).thenReturn(events);
85+
when(credentialStoreService.processCredentialStoreEvent(any())).thenReturn(identity);
86+
87+
RepeatStatus status = tasklet.execute(contribution, chunkContext);
88+
89+
assertEquals(RepeatStatus.FINISHED, status);
90+
verify(credentialStoreService).storeIdentityEntity(identity);
91+
verify(credentialEventRepo).saveAll(events);
92+
}
93+
@Test
94+
public void testExecute_withGenericException_shouldContinue() throws Exception {
95+
CredentialEventStore event = new CredentialEventStore();
96+
when(credentialEventRepo.findNewOrFailedEvents(anyInt())).thenReturn(Collections.singletonList(event));
97+
98+
when(credentialStoreService.processCredentialStoreEvent(any()))
99+
.thenThrow(new RuntimeException("Generic"));
100+
101+
RepeatStatus status = tasklet.execute(contribution, chunkContext);
102+
103+
assertEquals(RepeatStatus.FINISHED, status);
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.mosip.authentication.internal.service.config;
2+
3+
import org.junit.runner.RunWith;
4+
import org.springframework.test.context.junit4.SpringRunner;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.MockitoAnnotations;
11+
import org.springframework.batch.core.Job;
12+
import org.springframework.batch.core.JobExecution;
13+
import org.springframework.batch.core.JobParameters;
14+
import org.springframework.batch.core.launch.JobLauncher;
15+
16+
import static org.mockito.ArgumentMatchers.any;
17+
import static org.mockito.ArgumentMatchers.eq;
18+
import static org.mockito.Mockito.when;
19+
import static org.mockito.Mockito.times;
20+
import static org.mockito.Mockito.verify;
21+
import static org.mockito.Mockito.never;
22+
import static org.mockito.Mockito.mock;
23+
24+
25+
@RunWith(SpringRunner.class)
26+
public class BatchJobSchedulerConfigTest {
27+
28+
@Mock
29+
private JobLauncher jobLauncher;
30+
31+
@Mock
32+
private Job credentialStoreJob;
33+
34+
@Mock
35+
private Job retriggerMissingCredentials;
36+
37+
@InjectMocks
38+
private BatchJobSchedulerConfig batchJobSchedulerConfig;
39+
40+
@Before
41+
public void setup() {
42+
MockitoAnnotations.openMocks(this);
43+
}
44+
45+
@Test
46+
public void testScheduleCredentialStoreJob_Success() throws Exception {
47+
when(jobLauncher.run(eq(credentialStoreJob), any(JobParameters.class)))
48+
.thenReturn(mock(JobExecution.class));
49+
50+
batchJobSchedulerConfig.scheduleCredentialStoreJob();
51+
52+
verify(jobLauncher, times(1)).run(eq(credentialStoreJob), any(JobParameters.class));
53+
}
54+
55+
@Test
56+
public void testScheduleCredentialStoreJob_Exception() throws Exception {
57+
when(jobLauncher.run(eq(credentialStoreJob), any(JobParameters.class)))
58+
.thenThrow(new RuntimeException("some error"));
59+
60+
batchJobSchedulerConfig.scheduleCredentialStoreJob();
61+
62+
verify(jobLauncher, times(1)).run(eq(credentialStoreJob), any(JobParameters.class));
63+
}
64+
65+
@Test
66+
public void testRetriggerMissingCredentialsJob_Enabled() throws Exception {
67+
java.lang.reflect.Field field = BatchJobSchedulerConfig.class.getDeclaredField("enableMissingCredentialRetrigger");
68+
field.setAccessible(true);
69+
field.set(batchJobSchedulerConfig, true);
70+
71+
when(jobLauncher.run(eq(retriggerMissingCredentials), any(JobParameters.class)))
72+
.thenReturn(mock(JobExecution.class));
73+
74+
batchJobSchedulerConfig.retriggerMissingCredentialsJob();
75+
76+
verify(jobLauncher, times(1)).run(eq(retriggerMissingCredentials), any(JobParameters.class));
77+
}
78+
79+
@Test
80+
public void testRetriggerMissingCredentialsJob_Disabled() throws Exception {
81+
java.lang.reflect.Field field = BatchJobSchedulerConfig.class.getDeclaredField("enableMissingCredentialRetrigger");
82+
field.setAccessible(true);
83+
field.set(batchJobSchedulerConfig, false);
84+
85+
batchJobSchedulerConfig.retriggerMissingCredentialsJob();
86+
87+
verify(jobLauncher, never()).run(any(Job.class), any(JobParameters.class));
88+
}
89+
90+
@Test
91+
public void testRetriggerMissingCredentialsJob_Exception() throws Exception {
92+
java.lang.reflect.Field field = BatchJobSchedulerConfig.class.getDeclaredField("enableMissingCredentialRetrigger");
93+
field.setAccessible(true);
94+
field.set(batchJobSchedulerConfig, true);
95+
96+
when(jobLauncher.run(eq(retriggerMissingCredentials), any(JobParameters.class)))
97+
.thenThrow(new RuntimeException("retrigger error"));
98+
99+
batchJobSchedulerConfig.retriggerMissingCredentialsJob();
100+
101+
verify(jobLauncher, times(1)).run(eq(retriggerMissingCredentials), any(JobParameters.class));
102+
}
103+
}

0 commit comments

Comments
 (0)