-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* client manager unit test cases Signed-off-by: Sachin S P <[email protected]> * unit test cases Signed-off-by: Sachin S P <[email protected]> * unit test cases Signed-off-by: Sachin S P <[email protected]> * added unit test cases for packet manager Signed-off-by: Sachin S P <[email protected]> * unit test cases null handle Signed-off-by: Sachin S P <[email protected]> --------- Signed-off-by: Sachin S P <[email protected]> Co-authored-by: Sachin S P <[email protected]>
- Loading branch information
1 parent
b21ab3c
commit 8e48b39
Showing
25 changed files
with
1,361 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...r/src/test/java/io/mosip/registration/clientmanager/service/Biometrics095ServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.mosip.registration.clientmanager.service; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.Assert.*; | ||
|
||
import android.content.Context; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.*; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.mosip.registration.clientmanager.dto.sbi.CaptureBioDetail; | ||
import io.mosip.registration.clientmanager.exception.BiometricsServiceException; | ||
import io.mosip.registration.clientmanager.spi.AuditManagerService; | ||
import io.mosip.registration.clientmanager.repository.GlobalParamRepository; | ||
import io.mosip.registration.clientmanager.repository.UserBiometricRepository; | ||
import io.mosip.registration.keymanager.dto.JWTSignatureVerifyRequestDto; | ||
import io.mosip.registration.keymanager.dto.JWTSignatureVerifyResponseDto; | ||
import io.mosip.registration.keymanager.spi.ClientCryptoManagerService; | ||
import io.mosip.registration.clientmanager.dto.sbi.CaptureRequest; | ||
import io.mosip.registration.clientmanager.constant.Modality; | ||
import io.mosip.registration.keymanager.util.KeyManagerConstant; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.*; | ||
|
||
public class Biometrics095ServiceTest { | ||
|
||
@Mock private Context context; | ||
@Mock private ObjectMapper objectMapper; | ||
@Mock private AuditManagerService auditManagerService; | ||
@Mock private GlobalParamRepository globalParamRepository; | ||
@Mock private ClientCryptoManagerService clientCryptoManagerService; | ||
@Mock private UserBiometricRepository userBiometricRepository; | ||
|
||
private Biometrics095Service biometrics095Service; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
biometrics095Service = new Biometrics095Service( | ||
context, objectMapper, auditManagerService, globalParamRepository, | ||
clientCryptoManagerService, userBiometricRepository); | ||
} | ||
|
||
@Test | ||
public void getRCaptureRequest_Test() { | ||
String deviceId = "device123"; | ||
Modality modality = Modality.FACE; | ||
List<String> exceptionAttributes = new ArrayList<>(); | ||
|
||
CaptureRequest captureRequest = biometrics095Service.getRCaptureRequest(modality, deviceId, exceptionAttributes); | ||
|
||
assertNotNull(captureRequest); | ||
assertEquals("Registration", captureRequest.getPurpose()); | ||
assertEquals(10000, captureRequest.getTimeout()); | ||
assertNotNull(captureRequest.getBio()); | ||
assertEquals(1, captureRequest.getBio().size()); | ||
CaptureBioDetail bioDetail = captureRequest.getBio().get(0); | ||
assertEquals(deviceId, bioDetail.getDeviceId()); | ||
assertEquals(modality.getSingleType().value(), bioDetail.getType()); | ||
} | ||
|
||
@Test(expected = BiometricsServiceException.class) | ||
public void handleRCaptureResponse_Error_Test() throws Exception { | ||
String responseJson = "{\"biometrics\": [{\"error\": {\"errorCode\": \"SOME_ERROR\", \"errorInfo\": \"Some error\"}}]}"; // Error response | ||
InputStream responseStream = new ByteArrayInputStream(responseJson.getBytes()); | ||
Modality modality = Modality.FACE; | ||
List<String> exceptionAttributes = new ArrayList<>(); | ||
biometrics095Service.handleRCaptureResponse(modality, responseStream, exceptionAttributes); | ||
} | ||
|
||
@Test | ||
public void validateJWTResponse_Success_Test() throws Exception { | ||
JWTSignatureVerifyResponseDto mockResponse = mock(JWTSignatureVerifyResponseDto.class); | ||
when(mockResponse.isSignatureValid()).thenReturn(true); | ||
when(mockResponse.getTrustValid()).thenReturn(KeyManagerConstant.TRUST_VALID); | ||
when(clientCryptoManagerService.jwtVerify(any(JWTSignatureVerifyRequestDto.class))).thenReturn(mockResponse); | ||
|
||
biometrics095Service.validateJWTResponse("some-signed-data", "DEVICE"); | ||
} | ||
|
||
@Test(expected = BiometricsServiceException.class) | ||
public void validateJWTResponse_Failure_Test() throws Exception { | ||
JWTSignatureVerifyResponseDto mockResponse = mock(JWTSignatureVerifyResponseDto.class); | ||
when(mockResponse.isSignatureValid()).thenReturn(false); | ||
when(clientCryptoManagerService.jwtVerify(any(JWTSignatureVerifyRequestDto.class))).thenReturn(mockResponse); | ||
|
||
biometrics095Service.validateJWTResponse("some-invalid-signed-data", "DEVICE"); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
.../src/test/java/io/mosip/registration/clientmanager/service/JobManagerServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.mosip.registration.clientmanager.service; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.Assert.*; | ||
|
||
import android.app.job.JobInfo; | ||
import android.app.job.JobScheduler; | ||
import android.content.Context; | ||
|
||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.*; | ||
|
||
import io.mosip.registration.clientmanager.repository.SyncJobDefRepository; | ||
import io.mosip.registration.clientmanager.spi.JobTransactionService; | ||
import io.mosip.registration.clientmanager.util.DateUtil; | ||
|
||
public class JobManagerServiceImplTest { | ||
|
||
@Mock | ||
private Context mockContext; | ||
|
||
@Mock | ||
private JobScheduler mockJobScheduler; | ||
|
||
@Mock | ||
private SyncJobDefRepository mockSyncJobDefRepository; | ||
|
||
@Mock | ||
private JobTransactionService mockJobTransactionService; | ||
|
||
@Mock | ||
private DateUtil mockDateUtil; | ||
|
||
private JobManagerServiceImpl jobManagerService; | ||
|
||
private static final String JOB_ID = "mosip.syncJobId"; | ||
@Mock | ||
private JobInfo.Builder mockJobInfoBuilder; | ||
|
||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
jobManagerService = new JobManagerServiceImpl(mockContext, mockSyncJobDefRepository, mockJobTransactionService, mockDateUtil); | ||
when(mockContext.getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn(mockJobScheduler); | ||
|
||
mockJobInfoBuilder = mock(JobInfo.Builder.class); | ||
when(mockJobInfoBuilder.setRequiresCharging(anyBoolean())).thenReturn(mockJobInfoBuilder); | ||
} | ||
|
||
|
||
@Test(expected = NotImplementedException.class) | ||
public void scheduleJob_NotImplementedJob_Test() { | ||
int jobId = 1; | ||
String apiName = "nonExistentJob"; | ||
|
||
jobManagerService.scheduleJob(jobId, apiName, null); | ||
} | ||
|
||
@Test | ||
public void getLastSyncTime_Test() { | ||
int jobId = 1; | ||
long lastSyncTime = 1609459200L; // Example timestamp | ||
|
||
when(mockJobTransactionService.getLastSyncTime(jobId)).thenReturn(lastSyncTime); | ||
when(mockDateUtil.getDateTime(lastSyncTime)).thenReturn("2024-11-27 00:00:00"); | ||
|
||
String lastSync = jobManagerService.getLastSyncTime(jobId); | ||
|
||
assertEquals("2024-11-27 00:00:00", lastSync); | ||
} | ||
|
||
@Test | ||
public void getNextSyncTime_Test() { | ||
int jobId = 1; | ||
long lastSyncTime = 1609459200L; // Example timestamp | ||
long nextSyncTime = lastSyncTime + 15 * 60; | ||
|
||
when(mockJobTransactionService.getLastSyncTime(jobId)).thenReturn(lastSyncTime); | ||
when(mockDateUtil.getDateTime(nextSyncTime)).thenReturn("2024-11-27 00:15:00"); | ||
|
||
String nextSync = jobManagerService.getNextSyncTime(jobId); | ||
|
||
assertEquals("2024-11-27 00:15:00", nextSync); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
.../test/java/io/mosip/registration/clientmanager/service/JobTransactionServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.mosip.registration.clientmanager.service; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
import io.mosip.registration.clientmanager.entity.JobTransaction; | ||
import io.mosip.registration.clientmanager.repository.JobTransactionRepository; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.*; | ||
|
||
public class JobTransactionServiceImplTest { | ||
|
||
@Mock | ||
private JobTransactionRepository jobTransactionRepository; | ||
|
||
@InjectMocks | ||
private JobTransactionServiceImpl jobTransactionService; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); // Initialize mocks | ||
} | ||
|
||
@Test | ||
public void logJobTransaction_WhenJobTransactionIsNotFound_Test() { | ||
|
||
int jobId = 1; | ||
long syncTime = 1000L; | ||
|
||
when(jobTransactionRepository.getJobTransaction(jobId)).thenReturn(null); // JobTransaction not found | ||
|
||
jobTransactionService.LogJobTransaction(jobId, syncTime); | ||
|
||
verify(jobTransactionRepository).createJobTransaction(any(JobTransaction.class)); // Verifying createJobTransaction is called | ||
} | ||
|
||
@Test | ||
public void logJobTransaction_WhenJobTransactionIsFound_Test() { | ||
|
||
int jobId = 1; | ||
long syncTime = 1000L; | ||
JobTransaction existingJobTransaction = new JobTransaction(jobId, 0L); | ||
|
||
when(jobTransactionRepository.getJobTransaction(jobId)).thenReturn(existingJobTransaction); // JobTransaction found | ||
|
||
jobTransactionService.LogJobTransaction(jobId, syncTime); | ||
|
||
verify(jobTransactionRepository).updateJobTransaction(jobId, syncTime); // Verifying updateJobTransaction is called | ||
} | ||
|
||
@Test | ||
public void logJobTransaction_WhenJobTransactionIsNull_Test() { | ||
|
||
int jobId = 2; | ||
long syncTime = 2000L; | ||
|
||
when(jobTransactionRepository.getJobTransaction(jobId)).thenReturn(null); // No transaction found | ||
|
||
jobTransactionService.LogJobTransaction(jobId, syncTime); | ||
|
||
verify(jobTransactionRepository, times(1)).createJobTransaction(any(JobTransaction.class)); // Ensure create is called once | ||
} | ||
|
||
@Test | ||
public void getLastSyncTime_WhenJobTransactionDoesNotExist_Test() { | ||
|
||
int jobId = 1; | ||
when(jobTransactionRepository.getJobTransaction(jobId)).thenReturn(null); | ||
|
||
long actualLastSyncTime = jobTransactionService.getLastSyncTime(jobId); | ||
|
||
assertEquals(0, actualLastSyncTime); | ||
verify(jobTransactionRepository, times(1)).getJobTransaction(jobId); // Ensure repository method was called once | ||
} | ||
} |
Oops, something went wrong.