Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public class AppModuleTest {

@Mock LocationValidationService locationValidationService;

@Mock LocalConfigService localConfigService;

private AppModule appModule;
private MockedStatic<ConfigService> configServiceMock;

Expand Down Expand Up @@ -180,7 +182,8 @@ public void testProvideMasterDataService() {
registrationCenterRepository, documentTypeRepository, applicantValidDocRepository, templateRepository,
dynamicFieldRepository, locationRepository, globalParamRepository, identitySchemaRepository,
blocklistedWordRepository, syncJobDefRepository, userDetailRepository, certificateManagerService,
languageRepository, jobManagerService, fileSignatureDao, jobTransactionService, permittedLocalConfigRepository, localConfigDAO
languageRepository, jobManagerService, fileSignatureDao, jobTransactionService, permittedLocalConfigRepository, localConfigDAO,
auditManagerService
);
assertNotNull(service);
assertTrue(service instanceof MasterDataServiceImpl);
Expand All @@ -202,7 +205,8 @@ public void testProvideLoginService() {
public void testProvideRegistrationService() {
RegistrationService service = appModule.provideRegistrationService(
packetWriterService, registrationRepository, mock(MasterDataService.class), identitySchemaRepository,
clientCryptoManagerService, keyStoreRepository, globalParamRepository, auditManagerService,registrationCenterRepository,locationValidationService, preRegistrationDataSyncServiceProvider,biometricService
clientCryptoManagerService, keyStoreRepository, globalParamRepository, auditManagerService, registrationCenterRepository, locationValidationService, preRegistrationDataSyncServiceProvider, biometricService,
mock(PacketService.class), mock(PreCheckValidatorService.class)
);
assertNotNull(service);
assertTrue(service instanceof RegistrationServiceImpl);
Expand All @@ -217,7 +221,7 @@ public void testProvideUserInterfaceHelperService() {
@Test
public void testProvidePacketService() {
PacketService service = appModule.providePacketService(
registrationRepository, iPacketCryptoService, syncRestService, mock(MasterDataService.class), globalParamRepository
registrationRepository, iPacketCryptoService, syncRestService, mock(MasterDataService.class), globalParamRepository, auditManagerService
);
assertNotNull(service);
assertTrue(service instanceof PacketServiceImpl);
Expand Down Expand Up @@ -245,7 +249,7 @@ public void testProvideAuditManagerService() {

@Test
public void testProvideJobManagerService() {
JobManagerService service = appModule.provideJobManagerService(syncJobDefRepository, mock(JobTransactionService.class), dateUtil);
JobManagerService service = appModule.provideJobManagerService(syncJobDefRepository, mock(JobTransactionService.class), dateUtil, localConfigService);
assertNotNull(service);
assertTrue(service instanceof JobManagerServiceImpl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.util.*;

import static org.junit.Assert.*;
Expand All @@ -31,7 +32,12 @@ public class SessionManagerTest {
SharedPreferences.Editor mockEditor;

@Before
public void setUp() {
public void setUp() throws Exception {
// Reset singleton so each test gets SessionManager with current test's mocks
Field managerField = SessionManager.class.getDeclaredField("manager");
managerField.setAccessible(true);
managerField.set(null, null);

lenient().when(mockContext.getString(anyInt())).thenReturn("app_name");
lenient().when(mockContext.getSharedPreferences(eq("app_name"), eq(Context.MODE_PRIVATE))).thenReturn(mockPrefs);
lenient().when(mockPrefs.edit()).thenReturn(mockEditor);
Expand Down Expand Up @@ -93,7 +99,6 @@ private Claim mockClaim(Object value) {
return claim;
}

@Ignore
@Test
public void test_fetch_auth_token_returns_null_when_no_token_exists() {
Mockito.when(mockContext.getString(R.string.app_name)).thenReturn("app_name");
Expand All @@ -106,7 +111,6 @@ public void test_fetch_auth_token_returns_null_when_no_token_exists() {
Assertions.assertNull(token);
}

@Ignore
@Test
public void test_fetch_auth_token_retrieves_saved_token() throws Exception {
lenient().when(mockContext.getString(R.string.app_name)).thenReturn("app_name");
Expand All @@ -123,7 +127,6 @@ public void test_fetch_auth_token_retrieves_saved_token() throws Exception {
sessionManager.fetchAuthToken();
}

@Ignore
@Test
public void test_fetch_auth_token_uses_correct_shared_preferences_name() {
lenient().when(mockContext.getString(R.string.app_name)).thenReturn("app_name");
Expand All @@ -133,7 +136,6 @@ public void test_fetch_auth_token_uses_correct_shared_preferences_name() {
sessionManager.fetchAuthToken();
}

@Ignore
@Test
public void test_clear_auth_token_removes_all_user_session_data() {
lenient().when(mockContext.getString(R.string.app_name)).thenReturn("app_name");
Expand All @@ -152,19 +154,10 @@ public void test_clear_auth_token_removes_all_user_session_data() {
@Test
public void test_expired_token_throws_exception() {
String expiredToken = "expired.jwt.token";
JWT mockJwt = mock(JWT.class);
Date expiryDate = new Date(System.currentTimeMillis() - 1000);

lenient().when(mockJwt.isExpired(15)).thenReturn(true);
lenient().when(mockJwt.getExpiresAt()).thenReturn(expiryDate);

SessionManager sessionManager = SessionManager.getSessionManager(mockContext);

assertThrows(Exception.class, () -> {
sessionManager.saveAuthToken(expiredToken);
});

verify(mockContext, never()).getSharedPreferences(anyString(), anyInt());
assertThrows(Exception.class, () -> sessionManager.saveAuthToken(expiredToken));
Comment on lines 155 to +160
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

This no longer proves the expired-token branch.

assertThrows(Exception.class, ...) on a raw "expired.jwt.token" string will also pass for other saveAuthToken() failures, so this can succeed without ever hitting the expiry guard. Keep the controlled JWT mock here, or assert the specific expired-token message.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@android/clientmanager/src/test/java/io/mosip/registration/clientmanager/config/SessionManagerTest.java`
around lines 155 - 160, The test test_expired_token_throws_exception no longer
guarantees it exercised the expired-token branch because passing a raw string
"expired.jwt.token" can trigger other failures; update the test to use a
controlled expired JWT payload (or a JWT factory/mock) and call
SessionManager.saveAuthToken on that token, then assert the specific exception
type/message indicating token expiry (or assert the exact error text) instead of
a generic assertThrows(Exception.class); reference
SessionManager.getSessionManager(mockContext) and SessionManager.saveAuthToken
when implementing the controlled token or tightened assertion.

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.mosip.registration.clientmanager.jobs;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockedStatic;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import dagger.android.AndroidInjection;
import io.mosip.registration.clientmanager.spi.PacketService;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

@RunWith(RobolectricTestRunner.class)
public class RegistrationDeletionJobTest {

@Mock
private PacketService packetService;

private AutoCloseable closeable;

@Before
public void setUp() {
closeable = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
if (closeable != null) {
closeable.close();
}
}

private RegistrationDeletionJob createJobSpy() {
RegistrationDeletionJob job = spy(new RegistrationDeletionJob());
job.packetService = packetService;
doNothing().when(job).logJobTransaction(anyInt(), anyLong());
return job;
}

@Test
public void onCreate_invokesAndroidInjection() {
try (MockedStatic<AndroidInjection> injectionMock = org.mockito.Mockito.mockStatic(AndroidInjection.class)) {
Robolectric.buildService(RegistrationDeletionJob.class).create().get();
injectionMock.verify(() -> AndroidInjection.inject(org.mockito.ArgumentMatchers.any(RegistrationDeletionJob.class)),
org.mockito.Mockito.atLeastOnce());
}
}

@Test
public void triggerJob_whenDeletionSucceeds_logsTransactionAndReturnsTrue() {
RegistrationDeletionJob job = createJobSpy();
doNothing().when(packetService).deleteRegistrationPackets();

boolean result = job.triggerJob(101);

assertTrue(result);
verify(packetService).deleteRegistrationPackets();
verify(job).logJobTransaction(eq(101), anyLong());
}

@Test
public void triggerJob_whenDeletionThrows_returnsFalseAndDoesNotLogTransaction() {
RegistrationDeletionJob job = createJobSpy();
doThrow(new RuntimeException("Deletion failed")).when(packetService).deleteRegistrationPackets();

boolean result = job.triggerJob(202);

assertFalse(result);
verify(packetService).deleteRegistrationPackets();
verify(job, never()).logJobTransaction(anyInt(), anyLong());
}

@Test
public void triggerJob_withDifferentJobIds_logsCorrectJobId() {
RegistrationDeletionJob job = createJobSpy();
doNothing().when(packetService).deleteRegistrationPackets();

job.triggerJob(501);
verify(job).logJobTransaction(eq(501), anyLong());

job.triggerJob(502);
verify(job).logJobTransaction(eq(502), anyLong());
}
}
Loading