Skip to content

Commit

Permalink
MOSIP-24939 Improved code coverage of ResidentVidServiceImpl. (#980)
Browse files Browse the repository at this point in the history
* Fixed service history issue.

* MOSIP-27716 Moved identity-data-formatter.mvel to credentialdata.mvel.

* Fixed Null pointer exception in notification method.

* MOSIP-24939 Improved code coverage of ResidentVidServiceImpl.

* MOSIP-24939 Improved code coverage of ResidentVidServiceImpl.

* MOSIP-24939 Improved code coverage of ResidentVidServiceImpl.

* MOSIP-24939 Improved code coverage of ResidentVidServiceImpl.
  • Loading branch information
kameshsr authored Jun 23, 2023
1 parent eaf7146 commit d153996
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public boolean validateOtp(String otp, String userId, String transactionId) thr

public Tuple2<Object, String> updateUserId(String userId, String transactionId) throws ApisResourceAccessException, ResidentServiceCheckedException, IOException {
ResidentUpdateRequestDto residentUpdateRequestDto = new ResidentUpdateRequestDto();
String individualId= identityService.getResidentIndvidualIdFromSession();
String individualId= identityService.getUinForIndividualId(identityService.getResidentIndvidualIdFromSession());
String individualIdType = templateUtil.getIndividualIdType();
residentUpdateRequestDto.setIndividualId(individualId);
residentUpdateRequestDto.setConsent(ACCEPTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import io.mosip.resident.dto.VidRequestDtoV2;
import io.mosip.resident.dto.VidResponseDto;
import io.mosip.resident.dto.VidRevokeRequestDTO;
import io.mosip.resident.dto.VidRevokeRequestDTOV2;
import io.mosip.resident.dto.VidRevokeResponseDTO;
import io.mosip.resident.entity.ResidentTransactionEntity;
import io.mosip.resident.exception.ApisResourceAccessException;
import io.mosip.resident.exception.OtpValidationFailedException;
import io.mosip.resident.exception.ResidentServiceCheckedException;
import io.mosip.resident.exception.VidCreationException;
import io.mosip.resident.exception.VidRevocationException;
import io.mosip.resident.repository.ResidentTransactionRepository;
import io.mosip.resident.service.IdAuthService;
import io.mosip.resident.service.NotificationService;
Expand Down Expand Up @@ -470,8 +473,163 @@ public void testGenerateVidV2() throws OtpValidationFailedException, ResidentSer
vidRequestDtoV2.setVidType("PERPETUAL");
vidRequestDtoV2.setChannels(List.of("EMAIL"));
vidRequestDtoV2.setTransactionID("3434232323");
assertEquals("1236547899874563",residentVidService.
generateVidV2(vidRequestDtoV2, "123232323").getT2());
assertEquals("12345",residentVidService.
generateVid(vidRequestDtoV2, "123232323").getResponse().getVid());
}

@Test(expected = ResidentServiceCheckedException.class)
public void testRevokeVidV2Failed() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException {
IdentityServiceTest.getAuthUserDetailsFromAuthentication();
Mockito.when(utility.createEntity(Mockito.any())).thenReturn(new ResidentTransactionEntity());
Mockito.when(utility.createEventId()).thenReturn("1236547899874563");
when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse);
VidRevokeRequestDTOV2 vidRevokeRequestDTOV2 = new VidRevokeRequestDTOV2();
vidRevokeRequestDTOV2.setTransactionID("1234567896");
vidRevokeRequestDTOV2.setVidStatus("Active");
residentVidService.revokeVid(vidRevokeRequestDTOV2, "2076439409167031", "2037293183 ");
}

@Test(expected = VidRevocationException.class)
public void testRevokeVidV2VidRevocationException() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException {
IdentityServiceTest.getAuthUserDetailsFromAuthentication();
Mockito.when(utility.createEntity(Mockito.any())).thenReturn(new ResidentTransactionEntity());
Mockito.when(utility.createEventId()).thenReturn("1236547899874563");
Mockito.when(identityServiceImpl.getResidentIdaToken()).thenReturn("123456789");
Mockito.when(identityServiceImpl.getIDATokenForIndividualId(Mockito.any())).thenReturn("123456789");
when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse);
VidRevokeRequestDTOV2 vidRevokeRequestDTOV2 = new VidRevokeRequestDTOV2();
vidRevokeRequestDTOV2.setTransactionID("1234567896");
vidRevokeRequestDTOV2.setVidStatus("Active");
residentVidService.revokeVid(vidRevokeRequestDTOV2, "2076439409167031", "2037293183 ");
}

@Test
public void testRevokeVidV2Success() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException {
IdentityServiceTest.getAuthUserDetailsFromAuthentication();
ResponseWrapper<VidGeneratorResponseDto> responseWrapper = new ResponseWrapper<>();
VidGeneratorResponseDto dto = new VidGeneratorResponseDto();
dto.setVidStatus("Deactive");
responseWrapper.setResponse(dto);
responseWrapper.setVersion("v1");
responseWrapper.setResponsetime(DateUtils.getCurrentDateTimeString());
when(residentServiceRestClient.patchApi(any(), any(), any(), any())).thenReturn(responseWrapper);
Mockito.when(utility.createEntity(Mockito.any())).thenReturn(new ResidentTransactionEntity());
Mockito.when(utility.createEventId()).thenReturn("1236547899874563");
Mockito.when(identityServiceImpl.getResidentIdaToken()).thenReturn("123456789");
Mockito.when(identityServiceImpl.getIDATokenForIndividualId(Mockito.any())).thenReturn("123456789");
when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse);
VidRevokeRequestDTOV2 vidRevokeRequestDTOV2 = new VidRevokeRequestDTOV2();
vidRevokeRequestDTOV2.setTransactionID("1234567896");
vidRevokeRequestDTOV2.setVidStatus("Active");
assertEquals("Vid successfully generated",
residentVidService.revokeVid(vidRevokeRequestDTOV2, "2076439409167031", "2037293183 ").getResponse().getMessage());
}

@Test
public void testGenerateVidV2NullNotificationResponse() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException {
IdentityServiceTest.getAuthUserDetailsFromAuthentication();
Mockito.when(utility.createEntity(Mockito.any())).thenReturn(new ResidentTransactionEntity());
Mockito.when(utility.createEventId()).thenReturn("1236547899874563");
Mockito.when(identityServiceImpl.getIndividualIdType(Mockito.anyString())).thenReturn("VID");
Mockito.when(identityServiceImpl.getUinForIndividualId(Mockito.anyString())).thenReturn("123");
when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse);
String vidPolicyURL = "https://dev.mosip.net";
ReflectionTestUtils.setField(residentVidService, "vidPolicyUrl", vidPolicyURL);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode policy = objectMapper.readValue(this.getClass().getClassLoader().getResource("vid_policy.json"),
ObjectNode.class);
when(mapper.readValue(Mockito.any(URL.class), Mockito.any(Class.class))).thenReturn(policy);
Map<Object, Object> vidPolicyMap = new HashMap<>();
List<Map<String, String>> vidList = new ArrayList<>();
Map<String, String> vids= new HashMap<>();
vids.put(TemplateVariablesConstants.VID_TYPE,ResidentConstants.PERPETUAL);
vids.put("vidPolicy", "vidPolicy");
vidList.add(vids);
vidPolicyMap.put("vidPolicies",vidList);
when(mapper.readValue("{\"vidPolicies\":[{\"vidType\":\"Perpetual\",\"vidPolicy\":{\"validForInMinutes\":null,\"transactionsAllowed\":null,\"instancesAllowed\":1,\"autoRestoreAllowed\":true,\"restoreOnAction\":\"REVOKE\"}},{\"vidType\":\"Temporary\",\"vidPolicy\":{\"validForInMinutes\":30,\"transactionsAllowed\":1,\"instancesAllowed\":5,\"autoRestoreAllowed\":false,\"restoreOnAction\":\"REGENERATE\"}}]}",
Map.class)).thenReturn(vidPolicyMap);
VidPolicy vidPolicy = new VidPolicy();
vidPolicy.setAllowedInstances(1);
vidPolicy.setAutoRestoreAllowed(true);
vidPolicy.setRestoreOnAction("true");

when(mapper.convertValue("vidPolicy", VidPolicy.class)).thenReturn(vidPolicy);
when(env.getProperty(Mockito.anyString())).thenReturn("false");

String vid = "12345";
VidGeneratorResponseDto vidGeneratorResponseDto = new VidGeneratorResponseDto();
vidGeneratorResponseDto.setVidStatus("Active");
vidGeneratorResponseDto.setVID(vid);
ResponseWrapper<VidGeneratorResponseDto> response = new ResponseWrapper<>();
response.setResponsetime(DateUtils.getCurrentDateTimeString());
response.setResponse(vidGeneratorResponseDto);
when(mapper.writeValueAsString(Mockito.any())).thenReturn("response");
when(mapper.readValue("response", VidGeneratorResponseDto.class)).thenReturn(vidGeneratorResponseDto);
when(residentServiceRestClient.postApi(any(), any(), any(), any())).thenReturn(response);
NotificationResponseDTO notificationResponseDTO = new NotificationResponseDTO();
notificationResponseDTO.setMaskedEmail(null);
notificationResponseDTO.setMaskedPhone(null);
notificationResponseDTO.setStatus("SUCCESS");
notificationResponseDTO.setMessage("SUCCESS");
when(notificationService.sendNotification(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(notificationResponseDTO);
VidRequestDtoV2 vidRequestDtoV2 = new VidRequestDtoV2();
vidRequestDtoV2.setVidType("PERPETUAL");
vidRequestDtoV2.setChannels(List.of("EMAIL"));
vidRequestDtoV2.setTransactionID("3434232323");
assertEquals("12345",residentVidService.
generateVid(vidRequestDtoV2, "123232323").getResponse().getVid());
}

@Test(expected = VidCreationException.class)
public void testGenerateVidV2NullNotificationResponseApiResourceException() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException {
IdentityServiceTest.getAuthUserDetailsFromAuthentication();
Mockito.when(utility.createEntity(Mockito.any())).thenReturn(new ResidentTransactionEntity());
Mockito.when(utility.createEventId()).thenReturn("1236547899874563");
Mockito.when(identityServiceImpl.getIndividualIdType(Mockito.anyString())).thenReturn("VID");
Mockito.when(identityServiceImpl.getUinForIndividualId(Mockito.anyString())).thenReturn("123");
when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse);
String vidPolicyURL = "https://dev.mosip.net";
ReflectionTestUtils.setField(residentVidService, "vidPolicyUrl", vidPolicyURL);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode policy = objectMapper.readValue(this.getClass().getClassLoader().getResource("vid_policy.json"),
ObjectNode.class);
when(mapper.readValue(Mockito.any(URL.class), Mockito.any(Class.class))).thenReturn(policy);
Map<Object, Object> vidPolicyMap = new HashMap<>();
List<Map<String, String>> vidList = new ArrayList<>();
Map<String, String> vids= new HashMap<>();
vids.put(TemplateVariablesConstants.VID_TYPE,ResidentConstants.PERPETUAL);
vids.put("vidPolicy", "vidPolicy");
vidList.add(vids);
vidPolicyMap.put("vidPolicies",vidList);
when(mapper.readValue("{\"vidPolicies\":[{\"vidType\":\"Perpetual\",\"vidPolicy\":{\"validForInMinutes\":null,\"transactionsAllowed\":null,\"instancesAllowed\":1,\"autoRestoreAllowed\":true,\"restoreOnAction\":\"REVOKE\"}},{\"vidType\":\"Temporary\",\"vidPolicy\":{\"validForInMinutes\":30,\"transactionsAllowed\":1,\"instancesAllowed\":5,\"autoRestoreAllowed\":false,\"restoreOnAction\":\"REGENERATE\"}}]}",
Map.class)).thenReturn(vidPolicyMap);
VidPolicy vidPolicy = new VidPolicy();
vidPolicy.setAllowedInstances(1);
vidPolicy.setAutoRestoreAllowed(true);
vidPolicy.setRestoreOnAction("true");

when(mapper.convertValue("vidPolicy", VidPolicy.class)).thenReturn(vidPolicy);
when(env.getProperty(Mockito.anyString())).thenReturn("false");

String vid = "12345";
VidGeneratorResponseDto vidGeneratorResponseDto = new VidGeneratorResponseDto();
vidGeneratorResponseDto.setVidStatus("Active");
vidGeneratorResponseDto.setVID(vid);
ResponseWrapper<VidGeneratorResponseDto> response = new ResponseWrapper<>();
response.setResponsetime(DateUtils.getCurrentDateTimeString());
response.setResponse(vidGeneratorResponseDto);
when(residentServiceRestClient.postApi(any(), any(), any(), any())).thenThrow(new ApisResourceAccessException());
NotificationResponseDTO notificationResponseDTO = new NotificationResponseDTO();
notificationResponseDTO.setMaskedEmail(null);
notificationResponseDTO.setMaskedPhone(null);
notificationResponseDTO.setStatus("SUCCESS");
notificationResponseDTO.setMessage("SUCCESS");
VidRequestDtoV2 vidRequestDtoV2 = new VidRequestDtoV2();
vidRequestDtoV2.setVidType("PERPETUAL");
vidRequestDtoV2.setChannels(List.of("EMAIL"));
vidRequestDtoV2.setTransactionID("3434232323");
assertEquals("12345",residentVidService.
generateVid(vidRequestDtoV2, "123232323").getResponse().getVid());
}

}

0 comments on commit d153996

Please sign in to comment.