Skip to content

Commit

Permalink
MOSIP-24939 Improved code coverage of ResidentVidServiceImpl. (#979)
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.
  • Loading branch information
kameshsr authored Jun 23, 2023
1 parent fa81ac4 commit eaf7146
Showing 1 changed file with 95 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
package io.mosip.resident.service.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.assertj.core.util.Lists;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.ReflectionTestUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.mosip.idrepository.core.dto.VidPolicy;
import io.mosip.kernel.core.exception.ServiceError;
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.resident.constant.ResidentConstants;
import io.mosip.resident.constant.TemplateVariablesConstants;
import io.mosip.resident.dto.IdentityDTO;
import io.mosip.resident.dto.NotificationRequestDto;
import io.mosip.resident.dto.NotificationResponseDTO;
import io.mosip.resident.dto.ResponseWrapper;
import io.mosip.resident.dto.VidGeneratorResponseDto;
import io.mosip.resident.dto.VidRequestDto;
import io.mosip.resident.dto.VidRequestDtoV2;
import io.mosip.resident.dto.VidResponseDto;
import io.mosip.resident.dto.VidRevokeRequestDTO;
import io.mosip.resident.dto.VidRevokeResponseDTO;
Expand All @@ -63,6 +30,40 @@
import io.mosip.resident.util.JsonUtil;
import io.mosip.resident.util.ResidentServiceRestClient;
import io.mosip.resident.util.Utility;
import org.apache.commons.io.IOUtils;
import org.assertj.core.util.Lists;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@RefreshScope
Expand Down Expand Up @@ -142,7 +143,7 @@ public void setup() throws IOException, ResidentServiceCheckedException, ApisRes
identityValue = new IdentityDTO();
identityValue.setEmail("[email protected]");
identityValue.setPhone("987654321");
identityValue.setUIN("1234567890");
identityValue.setUIN("123");
when(identityServiceImpl.getIdentity(Mockito.anyString())).thenReturn(identityValue);

ClassLoader classLoader = getClass().getClassLoader();
Expand Down Expand Up @@ -179,7 +180,8 @@ public void setup() throws IOException, ResidentServiceCheckedException, ApisRes
vidList.add(vidDetails);
vidResponse.setResponse(vidList);
vid = "2038096257310540";
when(mapper.convertValue((Object) any(), (Class<Object>) any())).thenReturn(LocalDateTime.now());
when(mapper.convertValue("1516239022", LocalDateTime.class)).thenReturn(LocalDateTime.now());
when(mapper.convertValue("1234343434", LocalDateTime.class)).thenReturn(LocalDateTime.now());
when(identityServiceImpl.getIdentity(Mockito.anyString())).thenReturn(identityValue);
}

Expand Down Expand Up @@ -417,4 +419,59 @@ public void getPerpatualVidTest() throws ResidentServiceCheckedException, ApisRe
assertEquals(perpetualVid, response);
}

@Test
public void testGenerateVidV2() 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("Ka**g.com");
notificationResponseDTO.setMaskedPhone("88**09");
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("1236547899874563",residentVidService.
generateVidV2(vidRequestDtoV2, "123232323").getT2());
}

}

0 comments on commit eaf7146

Please sign in to comment.