diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentVidController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentVidController.java index 16bf16cbdc3..dc89de8c8d6 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentVidController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentVidController.java @@ -1,25 +1,6 @@ package io.mosip.resident.controller; -import java.util.List; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PatchMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RestController; - -import io.mosip.kernel.core.exception.ServiceError; import io.mosip.kernel.core.logger.spi.Logger; -import io.mosip.kernel.core.util.DateUtils; import io.mosip.resident.config.LoggerConfiguration; import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.constant.ResidentErrorCode; @@ -50,8 +31,25 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; import reactor.util.function.Tuple2; +import java.io.IOException; +import java.util.List; +import java.util.Map; + /** * Resident VID controller class. * @@ -176,7 +174,7 @@ private ResponseEntity generateVidV2Version(IVidRequestDto requestDto @ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(hidden = true))) }) public ResponseEntity revokeVid( @RequestBody(required = true) RequestWrapper requestDto, @PathVariable String vid) - throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { return revokeVid(requestDto, vid, true); } @@ -193,13 +191,13 @@ public ResponseEntity revokeVid( @ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(hidden = true))) }) public ResponseEntity revokeVidV2( @RequestBody(required = true) RequestWrapper requestDto, @PathVariable String vid) - throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { return revokeVidV2Version(requestDto, vid, false); } @SuppressWarnings("unused") private ResponseEntity revokeVid(RequestWrapper requestDto, String vid, - boolean isOtpValidationRequired) throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + boolean isOtpValidationRequired) throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { logger.debug("ResidentVidController::revokeVid()::entry"); String residentIndividualId = !(requestDto.getRequest() instanceof VidRevokeRequestDTO)? null : ((VidRevokeRequestDTO)requestDto.getRequest()).getIndividualId(); @@ -221,7 +219,7 @@ private ResponseEntity revokeVid(RequestWrapper revokeVidV2Version(RequestWrapper requestDto, String vid, - boolean isOtpValidationRequired) throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + boolean isOtpValidationRequired) throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { logger.debug("ResidentVidController::revokeVidV2Version()::entry"); String residentIndividualId = !(requestDto.getRequest() instanceof VidRevokeRequestDTO)? null : ((VidRevokeRequestDTO)requestDto.getRequest()).getIndividualId(); diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentVidService.java b/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentVidService.java index cc7be79746c..67495a407de 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentVidService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentVidService.java @@ -1,28 +1,27 @@ package io.mosip.resident.service; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import org.springframework.stereotype.Service; - import io.mosip.resident.dto.BaseVidRequestDto; import io.mosip.resident.dto.BaseVidRevokeRequestDTO; -import io.mosip.resident.dto.IdentityDTO; import io.mosip.resident.dto.ResponseWrapper; import io.mosip.resident.dto.VidResponseDto; import io.mosip.resident.dto.VidRevokeResponseDTO; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.OtpValidationFailedException; import io.mosip.resident.exception.ResidentServiceCheckedException; +import org.springframework.stereotype.Service; import reactor.util.function.Tuple2; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Optional; + @Service public interface ResidentVidService { public ResponseWrapper generateVid(BaseVidRequestDto requestDto, String individualId) throws OtpValidationFailedException, ResidentServiceCheckedException; - public ResponseWrapper revokeVid(BaseVidRevokeRequestDTO requestDto,String vid, String individualId) throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException; + public ResponseWrapper revokeVid(BaseVidRevokeRequestDTO requestDto,String vid, String individualId) throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException; public String getVidPolicy() throws ResidentServiceCheckedException; @@ -35,7 +34,7 @@ public Tuple2, String> generateVidV2(BaseVidRequ public Tuple2, String> revokeVidV2(BaseVidRevokeRequestDTO requestDto, String vid, String indivudalId) - throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException; + throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException; ResponseWrapper>> retrieveVidsfromUin(String uin, int timeZoneOffset, String locale) throws ResidentServiceCheckedException, ApisResourceAccessException; diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentVidServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentVidServiceImpl.java index af611134906..7a28dbb2559 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentVidServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentVidServiceImpl.java @@ -1,33 +1,7 @@ package io.mosip.resident.service.impl; -import static io.mosip.resident.constant.ResidentConstants.VID_POLICIES; -import static io.mosip.resident.constant.ResidentConstants.VID_POLICY; - -import java.io.IOException; -import java.net.URL; -import java.security.NoSuchAlgorithmException; -import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.function.BiFunction; -import java.util.stream.Collectors; - -import javax.annotation.PostConstruct; - -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.env.Environment; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; - import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; - import io.mosip.idrepository.core.dto.VidPolicy; import io.mosip.kernel.core.exception.ServiceError; import io.mosip.kernel.core.logger.spi.Logger; @@ -81,10 +55,34 @@ import io.mosip.resident.service.ResidentVidService; import io.mosip.resident.util.EventEnum; import io.mosip.resident.util.ResidentServiceRestClient; +import io.mosip.resident.util.Utilities; import io.mosip.resident.util.Utility; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; import reactor.util.function.Tuple2; import reactor.util.function.Tuples; +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.net.URL; +import java.security.NoSuchAlgorithmException; +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.BiFunction; +import java.util.stream.Collectors; + +import static io.mosip.resident.constant.ResidentConstants.VID_POLICIES; +import static io.mosip.resident.constant.ResidentConstants.VID_POLICY; + @Component public class ResidentVidServiceImpl implements ResidentVidService { @@ -170,7 +168,10 @@ public class ResidentVidServiceImpl implements ResidentVidService { @Value("${perpatual.vid-type:PERPETUAL}") private String perpatualVidType; - + + @Autowired + private Utilities utilities; + @Override public ResponseWrapper generateVid(BaseVidRequestDto requestDto, String individualId) throws OtpValidationFailedException, ResidentServiceCheckedException { @@ -438,13 +439,13 @@ private VidGeneratorResponseDto vidGenerator(BaseVidRequestDto requestDto, Strin @Override public ResponseWrapper revokeVid(BaseVidRevokeRequestDTO requestDto, String vid, String indivudalId) - throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { return revokeVidV2(requestDto, vid, indivudalId).getT1(); } @Override public Tuple2, String> revokeVidV2(BaseVidRevokeRequestDTO requestDto, String vid, String indivudalId) - throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { boolean isV2Request = requestDto instanceof VidRevokeRequestDTOV2; ResponseWrapper responseDto = new ResponseWrapper<>(); NotificationRequestDto notificationRequestDto = isV2Request? new NotificationRequestDtoV2() : new NotificationRequestDto(); @@ -489,9 +490,8 @@ public Tuple2, String> revokeVidV2(BaseVid } } notificationRequestDto.setId(uin); - String idaTokenForIndividualId = identityServiceImpl.getIDAToken(uin); - String idaTokenForVid = identityServiceImpl.getIDATokenForIndividualId(vid); - if(idaTokenForVid == null || !idaTokenForIndividualId.equalsIgnoreCase(idaTokenForVid)) { + String uinForVid = utilities.getUinByVid(vid); + if(uinForVid != null && !uin.equalsIgnoreCase(uinForVid)) { if(Utility.isSecureSession()) { residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); residentTransactionEntity.setRequestSummary(String.format("%s - %s", RequestType.REVOKE_VID.getName(), ResidentConstants.FAILED)); diff --git a/resident/resident-service/src/test/java/io/mosip/resident/service/impl/ResidentVidServiceTest.java b/resident/resident-service/src/test/java/io/mosip/resident/service/impl/ResidentVidServiceTest.java index 0ab873c3bb6..576f675e40c 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/service/impl/ResidentVidServiceTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/service/impl/ResidentVidServiceTest.java @@ -19,6 +19,7 @@ import java.util.Optional; import java.util.UUID; +import io.mosip.resident.util.Utilities; import org.apache.commons.io.IOUtils; import org.assertj.core.util.Lists; import org.json.simple.JSONObject; @@ -101,6 +102,9 @@ public class ResidentVidServiceTest { @Mock private IdentityServiceImpl identityServiceImpl; + @Mock + private Utilities utilities; + @InjectMocks private ResidentVidServiceImpl residentVidService; @@ -180,6 +184,7 @@ public void setup() throws IOException, ResidentServiceCheckedException, ApisRes vid = "2038096257310540"; when(mapper.convertValue("1516239022", LocalDateTime.class)).thenReturn(LocalDateTime.now()); when(mapper.convertValue("1234343434", LocalDateTime.class)).thenReturn(LocalDateTime.now()); + Mockito.when(utilities.getUinByVid(Mockito.anyString())).thenReturn("123"); } @Test(expected = Exception.class) @@ -281,7 +286,7 @@ public void revokeVidSuccessTest() throws OtpValidationFailedException, IOExcept } @Test(expected = OtpValidationFailedException.class) - public void otpValidationFailedTest1() throws ResidentServiceCheckedException, OtpValidationFailedException, ApisResourceAccessException { + public void otpValidationFailedTest1() throws ResidentServiceCheckedException, OtpValidationFailedException, ApisResourceAccessException, IOException { String vid = "2038096257310540"; when(idAuthService.validateOtp(anyString(), anyString(), anyString())).thenReturn(Boolean.FALSE); @@ -289,7 +294,7 @@ public void otpValidationFailedTest1() throws ResidentServiceCheckedException, O } @Test(expected = Exception.class) - public void apiResourceAccessExceptionTest2() throws ResidentServiceCheckedException, OtpValidationFailedException, ApisResourceAccessException { + public void apiResourceAccessExceptionTest2() throws ResidentServiceCheckedException, OtpValidationFailedException, ApisResourceAccessException, IOException { String ERROR_CODE = "err"; String vid = "2038096257310540"; @@ -306,7 +311,7 @@ public void apiResourceAccessExceptionTest2() throws ResidentServiceCheckedExcep } @Test(expected = Exception.class) - public void idRepoAppExceptionTest() throws ResidentServiceCheckedException, OtpValidationFailedException, ApisResourceAccessException { + public void idRepoAppExceptionTest() throws ResidentServiceCheckedException, OtpValidationFailedException, ApisResourceAccessException, IOException { String ERROR_CODE = "err"; String vid = "2038096257310540"; @@ -468,8 +473,8 @@ public void testGenerateVidV2() throws OtpValidationFailedException, ResidentSer generateVid(vidRequestDtoV2, "123232323").getResponse().getVid()); } - @Test(expected = ResidentServiceCheckedException.class) - public void testRevokeVidV2Failed() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + @Test(expected = VidRevocationException.class) + public void testRevokeVidV2Failed() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { IdentityServiceTest.getAuthUserDetailsFromAuthentication(); Mockito.when(utility.createEntity(Mockito.any())).thenReturn(new ResidentTransactionEntity()); Mockito.when(identityServiceImpl.getIDAToken(Mockito.anyString())).thenReturn("123456789"); @@ -482,11 +487,10 @@ public void testRevokeVidV2Failed() throws OtpValidationFailedException, Residen } @Test(expected = VidRevocationException.class) - public void testRevokeVidV2VidRevocationException() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + public void testRevokeVidV2VidRevocationException() 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.getIDATokenForIndividualId(Mockito.any())).thenReturn("123456789"); Mockito.when(identityServiceImpl.getIDAToken(Mockito.anyString())).thenReturn("123456789"); when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse); VidRevokeRequestDTOV2 vidRevokeRequestDTOV2 = new VidRevokeRequestDTOV2(); @@ -496,7 +500,7 @@ public void testRevokeVidV2VidRevocationException() throws OtpValidationFailedEx } @Test - public void testRevokeVidV2Success() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException { + public void testRevokeVidV2Success() throws OtpValidationFailedException, ResidentServiceCheckedException, ApisResourceAccessException, IOException { IdentityServiceTest.getAuthUserDetailsFromAuthentication(); ResponseWrapper responseWrapper = new ResponseWrapper<>(); VidGeneratorResponseDto dto = new VidGeneratorResponseDto(); @@ -507,7 +511,6 @@ public void testRevokeVidV2Success() throws OtpValidationFailedException, Reside 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.getIDATokenForIndividualId(Mockito.any())).thenReturn("123456789"); Mockito.when(identityServiceImpl.getIDAToken(Mockito.anyString())).thenReturn("123456789"); when(residentServiceRestClient.getApi(Mockito.anyString(), Mockito.any())).thenReturn(vidResponse); VidRevokeRequestDTOV2 vidRevokeRequestDTOV2 = new VidRevokeRequestDTOV2();