From 9a1b1524d6f0bf20b657476708d89e3ca6a89e9c Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Mon, 28 Nov 2022 13:50:22 +0530 Subject: [PATCH 01/15] added error for invalid user --- .../resident/constant/ResidentErrorCode.java | 3 ++- .../service/impl/ResidentServiceImpl.java | 12 ++++++++--- .../ResidentServiceGetEventStatusTest.java | 21 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentErrorCode.java b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentErrorCode.java index dae2cb375f3..bf272fe70e2 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentErrorCode.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentErrorCode.java @@ -109,7 +109,8 @@ public enum ResidentErrorCode { VID_REQUEST_CARD_FAILED("RES-SER-480", "Error in Request card from vid"), PATNER_NOT_FOUND("RES-SER-481","Partner not found."), REDIRECT_URL_NOT_FOUND("RES-SER-482","Redirect url not found."), - CARD_NOT_READY("RES_SER_509", "The card is not ready for download."); + CARD_NOT_READY("RES_SER_509", "The card is not ready for download."), + EID_NOT_BELONG_TO_SESSION("RES-SER-510", "The entered EID is not associated with the UIN/VID used to log in."); private final String errorCode; private final String errorMessage; diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java index 6f0a2c8caab..bedb18b5344 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java @@ -1910,6 +1910,10 @@ public ResponseWrapper getEventStatus(String eventId, St String requestTypeCode; String statusCode; if (residentTransactionEntity.isPresent()) { + String idaToken = identityServiceImpl.getResidentIdaToken(); + if (!idaToken.equals(residentTransactionEntity.get().getTokenId())) { + throw new ResidentServiceCheckedException(ResidentErrorCode.EID_NOT_BELONG_TO_SESSION); + } requestTypeCode = residentTransactionEntity.get().getRequestTypeCode(); statusCode = getEventStatusCode(residentTransactionEntity.get().getStatusCode()); } else { @@ -1938,6 +1942,7 @@ public ResponseWrapper getEventStatus(String eventId, St eventStatusMap.remove(TemplateVariablesConstants.INDIVIDUAL_ID); eventStatusMap.remove(TemplateVariablesConstants.SUMMARY); eventStatusMap.remove(TemplateVariablesConstants.TIMESTAMP); + eventStatusMap.remove(TemplateVariablesConstants.TRACK_SERVICE_REQUEST_LINK); String name = identityServiceImpl.getClaimFromIdToken(env.getProperty(NAME)); eventStatusMap.put(env.getProperty(ResidentConstants.APPLICANT_NAME_PROPERTY), name); @@ -1956,11 +1961,12 @@ public ResponseWrapper getEventStatus(String eventId, St responseWrapper.setVersion(serviceEventVersion); responseWrapper.setResponsetime(DateUtils.getUTCCurrentDateTime()); responseWrapper.setResponse(eventStatusResponseDTO); - - } catch (Exception e) { + + } catch (ApisResourceAccessException e) { logger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), "ResidentServiceImpl::getEventStatus():: Exception"); - throw new ResidentServiceCheckedException(ResidentErrorCode.EVENT_STATUS_NOT_FOUND); + throw new ResidentServiceCheckedException(ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION.getErrorCode(), + ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION.getErrorMessage(), e); } return responseWrapper; } diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceGetEventStatusTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceGetEventStatusTest.java index 6083d000950..fd3b82ac514 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceGetEventStatusTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceGetEventStatusTest.java @@ -94,6 +94,7 @@ public void setup() throws ResidentServiceCheckedException, ApisResourceAccessEx residentTransactionEntity.get().setRequestSummary("requestSummary"); residentTransactionEntity.get().setRequestTypeCode(requestType.name()); residentTransactionEntity.get().setCrDtimes(LocalDateTime.now()); + residentTransactionEntity.get().setTokenId("123456789"); Mockito.when(residentTransactionRepository.findById(Mockito.anyString())).thenReturn(residentTransactionEntity); templateVariables.put("eventId", eventId); templateVariables.put("authenticationMode", "OTP"); @@ -101,6 +102,7 @@ public void setup() throws ResidentServiceCheckedException, ApisResourceAccessEx templateVariables.put("purpose", "authentication"); Mockito.when(requestType.getAckTemplateVariables(templateUtil, Mockito.anyString())).thenReturn(templateVariables); Mockito.when(identityServiceImpl.getResidentIndvidualId()).thenReturn("123456789"); + Mockito.when(identityServiceImpl.getResidentIdaToken()).thenReturn("123456789"); Mockito.doNothing().when(audit).setAuditRequestDto(Mockito.any()); Mockito.when(templateUtil.getPurposeTemplateTypeCode(Mockito.any(), Mockito.any())).thenReturn("template-type-code"); Mockito.when(templateUtil.getSummaryTemplateTypeCode(Mockito.any(), Mockito.any())).thenReturn("template-type-code"); @@ -152,4 +154,23 @@ public void getEventStatusTestException() throws ResidentServiceCheckedException Mockito.when(residentTransactionRepository.findById(Mockito.anyString())).thenReturn(Optional.empty()); residentService.getEventStatus(eventId, langCode); } + + @Test(expected = ResidentServiceCheckedException.class) + public void getEventStatusNestedIfTest() throws ResidentServiceCheckedException, ApisResourceAccessException { + Mockito.when(identityServiceImpl.getResidentIdaToken()).thenReturn("abcd"); + residentService.getEventStatus(eventId, langCode); + } + + @Test(expected = ResidentServiceCheckedException.class) + public void getEventStatusThrowsExceptionTest() throws ResidentServiceCheckedException, ApisResourceAccessException { + Mockito.when(identityServiceImpl.getResidentIdaToken()).thenThrow(new ApisResourceAccessException()); + residentService.getEventStatus(eventId, langCode); + } + + @Test + public void getEventStatusServiceTypeNotMappedTest() throws ResidentServiceCheckedException { + residentTransactionEntity.get().setRequestTypeCode(RequestType.SEND_OTP.name()); + ResponseWrapper resultResponseWrapper =residentService.getEventStatus(eventId, langCode); + assert resultResponseWrapper.getResponse().getEventId().equals(eventId); + } } From 8477e16f6c5fb25a1d6397a9263eab80320e5ad7 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Tue, 29 Nov 2022 13:40:45 +0530 Subject: [PATCH 02/15] added status in 2 apis --- .../main/java/io/mosip/resident/constant/ResidentConstants.java | 2 ++ .../io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java | 1 + .../java/io/mosip/resident/dto/VidDownloadCardResponseDto.java | 1 + .../io/mosip/resident/service/impl/DownloadCardServiceImpl.java | 1 + .../resident/service/impl/ResidentCredentialServiceImpl.java | 2 ++ 5 files changed, 7 insertions(+) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java index ae3fa503e30..905a797630e 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java @@ -137,5 +137,7 @@ private ResidentConstants() { public static final String ACK_NAMING_CONVENTION_PROPERTY = "mosip.resident.ack.name.convention"; public static final String UIN_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.uin.card.name.convention"; public static final String VID_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.vid.card.name.convention"; + + public static final String SUCCESS = "Success"; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java index a0bb1ee9045..bc07aeea7d0 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java @@ -11,5 +11,6 @@ public class ResidentCredentialResponseDtoV2 { private String eventId; + private String status; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/VidDownloadCardResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/VidDownloadCardResponseDto.java index 645a8475fe4..8345b07e5bf 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/dto/VidDownloadCardResponseDto.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/VidDownloadCardResponseDto.java @@ -10,4 +10,5 @@ public class VidDownloadCardResponseDto { String eventId; + String status; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java index 19b07796957..fed1eea9c19 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java @@ -275,6 +275,7 @@ public ResponseWrapper getVidCardEventId(String vid) ResidentCredentialResponseDto.class); eventId =insertDataForVidCard(residentCredentialResponseDto, vid); vidDownloadCardResponseDto.setEventId(eventId); + vidDownloadCardResponseDto.setStatus(ResidentConstants.SUCCESS); }else{ throw new ResidentServiceCheckedException(String.valueOf(ResidentErrorCode.VID_REQUEST_CARD_FAILED), ResidentErrorCode.VID_REQUEST_CARD_FAILED.getErrorMessage()); diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java index 6d7072b745e..531aea50978 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java @@ -34,6 +34,7 @@ import io.mosip.resident.constant.LoggerFileConstant; import io.mosip.resident.constant.NotificationTemplateCode; import io.mosip.resident.constant.RequestType; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.constant.ResidentErrorCode; import io.mosip.resident.constant.TemplateType; import io.mosip.resident.dto.CredentialCancelRequestResponseDto; @@ -276,6 +277,7 @@ public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequest updateResidentTransaction(dto, residentCredentialResponseDto, residentTransactionEntity); residentCredentialResponseDtoV2.setEventId(residentTransactionEntity.getEventId()); + residentCredentialResponseDtoV2.setStatus(ResidentConstants.SUCCESS); } catch (ResidentServiceCheckedException | ApisResourceAccessException e) { if (residentTransactionEntity != null) { residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); From 11ee99fe94675592225dbe81a8ff3cb9a3f46b52 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Tue, 29 Nov 2022 14:27:11 +0530 Subject: [PATCH 03/15] changed response of auth-lock-unlock api --- .../resident/dto/AuthLockUnlockResponseDto.java | 17 +++++++++++++++++ .../service/impl/ResidentServiceImpl.java | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java new file mode 100644 index 00000000000..865e92ce542 --- /dev/null +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java @@ -0,0 +1,17 @@ +package io.mosip.resident.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * The Auth Lock Unlock response dto. + * + * @author Ritik Jain + */ +@Data +@EqualsAndHashCode(callSuper=true) +public class AuthLockUnlockResponseDto extends ResponseDTO { + + private String eventId; + +} diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java index 00155f1cfaf..a7643adf968 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java @@ -96,6 +96,7 @@ import io.mosip.resident.dto.AuthLockOrUnLockRequestDto; import io.mosip.resident.dto.AuthLockOrUnLockRequestDtoV2; import io.mosip.resident.dto.AuthLockStatusResponseDtoV2; +import io.mosip.resident.dto.AuthLockUnlockResponseDto; import io.mosip.resident.dto.AuthTxnDetailsDTO; import io.mosip.resident.dto.AuthTypeStatusDtoV2; import io.mosip.resident.dto.AuthUnLockRequestDTO; @@ -1126,7 +1127,7 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL throws ResidentServiceCheckedException, ApisResourceAccessException { logger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), "ResidentServiceImpl::reqAauthTypeStatusUpdate():: entry"); - ResponseDTO response = new ResponseDTO(); + AuthLockUnlockResponseDto response = new AuthLockUnlockResponseDto(); String individualId = identityServiceImpl.getResidentIndvidualId(); boolean isTransactionSuccessful = false; List residentTransactionEntities = List.of(); @@ -1213,6 +1214,8 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL "Request for auth " + authLockOrUnLockRequestDtoV2.getAuthTypes() + " lock failed")); if (notificationResponseDTO != null) { response.setMessage(notificationResponseDTO.getMessage()); + response.setStatus(ResidentConstants.SUCCESS); + response.setEventId(residentTransactionEntities.get(0).getEventId()); } } logger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), From 4e7ca20d4ad39d6f238b83a9250a80216571121b Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Mon, 5 Dec 2022 13:09:52 +0530 Subject: [PATCH 04/15] updated response of validate otp --- .../resident/controller/IdAuthController.java | 17 +++-- .../dto/AuthLockUnlockResponseDto.java | 4 +- .../resident/dto/ValidateOtpResponseDto.java | 18 +++++ .../mosip/resident/service/IdAuthService.java | 4 + .../service/impl/IdAuthServiceImpl.java | 29 +++++-- .../test/controller/IdAuthControllerTest.java | 6 +- .../test/service/IdAuthServiceTest.java | 76 +++++++++++-------- 7 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 resident/resident-service/src/main/java/io/mosip/resident/dto/ValidateOtpResponseDto.java diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/IdAuthController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/IdAuthController.java index 50d21ab6563..75b1cefd6c3 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/IdAuthController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/IdAuthController.java @@ -9,9 +9,11 @@ import io.mosip.kernel.core.http.ResponseWrapper; import io.mosip.kernel.core.logger.spi.Logger; import io.mosip.resident.config.LoggerConfiguration; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.dto.IdAuthRequestDto; import io.mosip.resident.dto.IdAuthResponseDto; import io.mosip.resident.dto.RequestWrapper; +import io.mosip.resident.dto.ValidateOtpResponseDto; import io.mosip.resident.exception.OtpValidationFailedException; import io.mosip.resident.service.IdAuthService; import io.mosip.resident.util.AuditUtil; @@ -22,6 +24,7 @@ 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 reactor.util.function.Tuple2; /** * Resident IdAuth controller class. @@ -44,7 +47,7 @@ public class IdAuthController { * Validate OTP * * @param requestWrapper - * @return ResponseWrapper object + * @return ResponseWrapper object * @throws OtpValidationFailedException */ @ResponseFilter @@ -60,15 +63,17 @@ public ResponseWrapper validateOtp(@RequestBody RequestWrappe logger.debug("IdAuthController::validateOtp()::entry"); auditUtil.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.VALIDATE_OTP, requestWrapper.getRequest().getTransactionId(), "OTP Validate Request")); - Boolean authStatus = idAuthService.validateOtp(requestWrapper.getRequest().getTransactionId(), + Tuple2 tupleResponse = idAuthService.validateOtpV1(requestWrapper.getRequest().getTransactionId(), requestWrapper.getRequest().getIndividualId(), requestWrapper.getRequest().getOtp()); auditUtil.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.VALIDATE_OTP_SUCCESS, requestWrapper.getRequest().getTransactionId(), "OTP Validate Request Success")); ResponseWrapper responseWrapper = new ResponseWrapper(); - IdAuthResponseDto idAuthResponseDto = new IdAuthResponseDto(); - idAuthResponseDto.setAuthStatus(authStatus); - idAuthResponseDto.setTransactionId(requestWrapper.getRequest().getTransactionId()); - responseWrapper.setResponse(idAuthResponseDto); + ValidateOtpResponseDto validateOtpResponseDto = new ValidateOtpResponseDto(); + validateOtpResponseDto.setAuthStatus(tupleResponse.getT1()); + validateOtpResponseDto.setTransactionId(requestWrapper.getRequest().getTransactionId()); + validateOtpResponseDto.setEventId(tupleResponse.getT2()); + validateOtpResponseDto.setStatus(ResidentConstants.SUCCESS); + responseWrapper.setResponse(validateOtpResponseDto); logger.debug("IdAuthController::validateOtp()::exit"); return responseWrapper; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java index 865e92ce542..26f0ea6aff6 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java @@ -9,9 +9,9 @@ * @author Ritik Jain */ @Data -@EqualsAndHashCode(callSuper=true) +@EqualsAndHashCode(callSuper = true) public class AuthLockUnlockResponseDto extends ResponseDTO { - + private String eventId; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/ValidateOtpResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/ValidateOtpResponseDto.java new file mode 100644 index 00000000000..e1df37b6ea2 --- /dev/null +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/ValidateOtpResponseDto.java @@ -0,0 +1,18 @@ +package io.mosip.resident.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * The validate otp response dto. + * + * @author Ritik Jain + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class ValidateOtpResponseDto extends IdAuthResponseDto { + + private String eventId; + private String status; + +} diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/IdAuthService.java b/resident/resident-service/src/main/java/io/mosip/resident/service/IdAuthService.java index 69eed05bfc9..30c131efbb1 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/IdAuthService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/IdAuthService.java @@ -9,12 +9,16 @@ import io.mosip.resident.dto.AuthTxnDetailsDTO; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.OtpValidationFailedException; +import reactor.util.function.Tuple2; @Service public interface IdAuthService { public boolean validateOtp(String transactionID, String individualId, String otp) throws OtpValidationFailedException; + + public Tuple2 validateOtpV1(String transactionId, String individualId, String otp) + throws OtpValidationFailedException; public boolean authTypeStatusUpdate(String individualId, List authType, AuthTypeStatus authTypeStatus, Long unlockForSeconds) throws ApisResourceAccessException; diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/IdAuthServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/IdAuthServiceImpl.java index 45492b506bd..092e8ebb3b6 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/IdAuthServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/IdAuthServiceImpl.java @@ -22,8 +22,6 @@ import javax.crypto.SecretKey; -import io.mosip.resident.constant.EventStatusInProgress; -import io.mosip.resident.dto.*; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.bouncycastle.util.io.pem.PemObject; @@ -50,10 +48,21 @@ import io.mosip.resident.config.LoggerConfiguration; import io.mosip.resident.constant.ApiName; import io.mosip.resident.constant.AuthTypeStatus; +import io.mosip.resident.constant.EventStatusInProgress; import io.mosip.resident.constant.LoggerFileConstant; import io.mosip.resident.constant.RequestType; import io.mosip.resident.constant.ResidentErrorCode; import io.mosip.resident.constant.ServiceType; +import io.mosip.resident.dto.AuthRequestDTO; +import io.mosip.resident.dto.AuthResponseDTO; +import io.mosip.resident.dto.AuthTxnDetailsDTO; +import io.mosip.resident.dto.AuthTypeDTO; +import io.mosip.resident.dto.AuthTypeStatusRequestDto; +import io.mosip.resident.dto.AuthTypeStatusResponseDto; +import io.mosip.resident.dto.AutnTxnDto; +import io.mosip.resident.dto.AutnTxnResponseDto; +import io.mosip.resident.dto.OtpAuthRequestDTO; +import io.mosip.resident.dto.PublicKeyResponseDto; import io.mosip.resident.entity.ResidentTransactionEntity; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.CertificateException; @@ -61,6 +70,8 @@ import io.mosip.resident.repository.ResidentTransactionRepository; import io.mosip.resident.service.IdAuthService; import io.mosip.resident.util.ResidentServiceRestClient; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; @Component public class IdAuthServiceImpl implements IdAuthService { @@ -110,10 +121,17 @@ public class IdAuthServiceImpl implements IdAuthService { @Override public boolean validateOtp(String transactionId, String individualId, String otp) throws OtpValidationFailedException { + return validateOtpV1(transactionId, individualId, otp).getT1(); + } + + @Override + public Tuple2 validateOtpV1(String transactionId, String individualId, String otp) + throws OtpValidationFailedException { AuthResponseDTO response = null; + ResidentTransactionEntity residentTransactionEntity = null; try { response = internelOtpAuth(transactionId, individualId, otp); - updateResidentTransaction(response.getResponse().isAuthStatus(), transactionId, individualId); + residentTransactionEntity = updateResidentTransaction(response.getResponse().isAuthStatus(), transactionId, individualId); } catch (ApisResourceAccessException | InvalidKeySpecException | NoSuchAlgorithmException | IOException | JsonProcessingException | java.security.cert.CertificateException e) { logger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.USERID.toString(), null, @@ -128,10 +146,10 @@ public boolean validateOtp(String transactionId, String individualId, String otp } - return response.getResponse().isAuthStatus(); + return Tuples.of(response.getResponse().isAuthStatus(), residentTransactionEntity.getEventId()); } - private void updateResidentTransaction(boolean verified,String transactionId, String individualId) throws NoSuchAlgorithmException { + private ResidentTransactionEntity updateResidentTransaction(boolean verified,String transactionId, String individualId) throws NoSuchAlgorithmException { ResidentTransactionEntity residentTransactionEntity = residentTransactionRepository. findTopByRequestTrnIdAndTokenIdAndStatusCodeOrderByCrDtimesDesc(transactionId, identityService.getIDAToken(individualId) , EventStatusInProgress.OTP_REQUESTED.toString()); @@ -143,6 +161,7 @@ private void updateResidentTransaction(boolean verified,String transactionId, St residentTransactionEntity.setAuthTypeCode(ServiceType.SERVICE_REQUEST.name()); residentTransactionRepository.save(residentTransactionEntity); } + return residentTransactionEntity; } public AuthResponseDTO internelOtpAuth(String transactionId, String individualId, diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/IdAuthControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/IdAuthControllerTest.java index a0f8b621d14..45127be6af6 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/IdAuthControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/IdAuthControllerTest.java @@ -34,6 +34,7 @@ import io.mosip.resident.service.impl.IdAuthServiceImpl; import io.mosip.resident.test.ResidentTestBootApplication; import io.mosip.resident.util.AuditUtil; +import reactor.util.function.Tuples; /** * Resident IdAuth controller test class. @@ -93,9 +94,8 @@ public void setUp() { @Test public void testValidateOtp() throws Exception { - Boolean authStatus = true; - Mockito.when(idAuthService.validateOtp(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())) - .thenReturn(authStatus); + Mockito.when(idAuthService.validateOtpV1(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())) + .thenReturn(Tuples.of(true, "12345")); mockMvc.perform(MockMvcRequestBuilders.post("/validate-otp").contentType(MediaType.APPLICATION_JSON_VALUE) .content(reqJson.getBytes())).andExpect(status().isOk()); } diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/service/IdAuthServiceTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/service/IdAuthServiceTest.java index 90687b54274..e02b4f19b1d 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/service/IdAuthServiceTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/service/IdAuthServiceTest.java @@ -1,6 +1,42 @@ package io.mosip.resident.test.service; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.net.URI; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +import javax.crypto.SecretKey; + +import org.assertj.core.util.Lists; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; + import com.fasterxml.jackson.databind.ObjectMapper; + import io.mosip.kernel.core.crypto.spi.CryptoCoreSpec; import io.mosip.kernel.core.http.ResponseWrapper; import io.mosip.kernel.core.util.DateUtils; @@ -16,6 +52,7 @@ import io.mosip.resident.dto.ErrorDTO; import io.mosip.resident.dto.IdAuthResponseDto; import io.mosip.resident.dto.PublicKeyResponseDto; +import io.mosip.resident.entity.ResidentTransactionEntity; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.CertificateException; import io.mosip.resident.exception.OtpValidationFailedException; @@ -26,39 +63,6 @@ import io.mosip.resident.service.impl.IdAuthServiceImpl; import io.mosip.resident.service.impl.IdentityServiceImpl; import io.mosip.resident.util.ResidentServiceRestClient; -import org.assertj.core.util.Lists; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.core.env.Environment; -import org.springframework.test.context.ContextConfiguration; - -import javax.crypto.SecretKey; -import java.io.IOException; -import java.net.URI; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @RefreshScope @@ -169,6 +173,9 @@ public void validateOtpSuccessTest() throws IOException, ApisResourceAccessExcep String otp = "12345"; String request = "request"; + + ResidentTransactionEntity residentTransactionEntity = new ResidentTransactionEntity(); + residentTransactionEntity.setEventId("12345"); IdAuthResponseDto authResponse = new IdAuthResponseDto(); authResponse.setAuthStatus(true); @@ -215,6 +222,9 @@ public void validateOtpSuccessTest() throws IOException, ApisResourceAccessExcep when(encryptor.asymmetricEncrypt(any(), any())).thenReturn(request.getBytes()); when(restClient.postApi(any(), any(), any(), any(Class.class))).thenReturn(response); + when(identityService.getIDAToken(anyString())).thenReturn("346697314566835424394775924659202696"); + when(residentTransactionRepository.findTopByRequestTrnIdAndTokenIdAndStatusCodeOrderByCrDtimesDesc(anyString(), + anyString(), anyString())).thenReturn(residentTransactionEntity); boolean result = idAuthService.validateOtp(transactionID, individualId, otp); From b80c68b40e73a8d5971d14811cccca8ea2f646bd Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Wed, 7 Dec 2022 13:09:15 +0530 Subject: [PATCH 05/15] added eventid and status in response --- .../resident/dto/GenerateVidResponseDto.java | 22 +++++++++++++++++ .../resident/dto/RevokeVidResponseDto.java | 22 +++++++++++++++++ .../service/impl/ResidentVidServiceImpl.java | 24 ++++++++++++++----- 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 resident/resident-service/src/main/java/io/mosip/resident/dto/GenerateVidResponseDto.java create mode 100644 resident/resident-service/src/main/java/io/mosip/resident/dto/RevokeVidResponseDto.java diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/GenerateVidResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/GenerateVidResponseDto.java new file mode 100644 index 00000000000..7c23b23ce90 --- /dev/null +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/GenerateVidResponseDto.java @@ -0,0 +1,22 @@ +package io.mosip.resident.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * The generate vid response dto. + * + * @author Ritik Jain + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class GenerateVidResponseDto extends VidResponseDtoV2 { + + /** + * + */ + private static final long serialVersionUID = 5728940858748492895L; + private String eventId; + private String status; + +} diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/RevokeVidResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/RevokeVidResponseDto.java new file mode 100644 index 00000000000..1cf69c68e9b --- /dev/null +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/RevokeVidResponseDto.java @@ -0,0 +1,22 @@ +package io.mosip.resident.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * The revoke vid response dto. + * + * @author Ritik Jain + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class RevokeVidResponseDto extends VidRevokeResponseDTO { + + /** + * + */ + private static final long serialVersionUID = 1973880019812497700L; + private String eventId; + private String status; + +} 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 a5e0c9c9e3c..0f2eda3f94a 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 @@ -38,24 +38,26 @@ import io.mosip.resident.constant.LoggerFileConstant; import io.mosip.resident.constant.NotificationTemplateCode; import io.mosip.resident.constant.RequestType; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.constant.ResidentErrorCode; import io.mosip.resident.constant.TemplateEnum; import io.mosip.resident.constant.TemplateType; import io.mosip.resident.constant.TemplateVariablesConstants; import io.mosip.resident.dto.BaseVidRequestDto; import io.mosip.resident.dto.BaseVidRevokeRequestDTO; +import io.mosip.resident.dto.GenerateVidResponseDto; import io.mosip.resident.dto.IdentityDTO; import io.mosip.resident.dto.NotificationRequestDto; import io.mosip.resident.dto.NotificationRequestDtoV2; import io.mosip.resident.dto.NotificationResponseDTO; import io.mosip.resident.dto.RequestWrapper; import io.mosip.resident.dto.ResponseWrapper; +import io.mosip.resident.dto.RevokeVidResponseDto; import io.mosip.resident.dto.VidGeneratorRequestDto; 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.VidResponseDtoV2; import io.mosip.resident.dto.VidRevokeRequestDTO; import io.mosip.resident.dto.VidRevokeRequestDTOV2; import io.mosip.resident.dto.VidRevokeResponseDTO; @@ -218,10 +220,12 @@ public ResponseWrapper generateVid(BaseVidRequestDto requestDto, // create response dto VidResponseDto vidResponseDto; if(notificationResponseDTO.getMaskedEmail() != null || notificationResponseDTO.getMaskedPhone() != null) { - VidResponseDtoV2 vidResponseDtov2 = new VidResponseDtoV2(); - vidResponseDto = vidResponseDtov2; - vidResponseDtov2.setMaskedEmail(notificationResponseDTO.getMaskedEmail()); - vidResponseDtov2.setMaskedPhone(notificationResponseDTO.getMaskedPhone()); + GenerateVidResponseDto generateVidResponseDto = new GenerateVidResponseDto(); + vidResponseDto = generateVidResponseDto; + generateVidResponseDto.setMaskedEmail(notificationResponseDTO.getMaskedEmail()); + generateVidResponseDto.setMaskedPhone(notificationResponseDTO.getMaskedPhone()); + generateVidResponseDto.setEventId(residentTransactionEntity.getEventId()); + generateVidResponseDto.setStatus(ResidentConstants.SUCCESS); } else { vidResponseDto = new VidResponseDto(); } @@ -472,7 +476,15 @@ public ResponseWrapper revokeVid(BaseVidRevokeRequestDTO r audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.SEND_NOTIFICATION_SUCCESS, requestDto.getTransactionID(), "Request to revoke VID")); // create response dto - VidRevokeResponseDTO vidRevokeResponseDto = new VidRevokeResponseDTO(); + VidRevokeResponseDTO vidRevokeResponseDto; + if(isV2Request) { + RevokeVidResponseDto revokeVidResponseDto = new RevokeVidResponseDto(); + vidRevokeResponseDto = revokeVidResponseDto; + revokeVidResponseDto.setEventId(residentTransactionEntity.getEventId()); + revokeVidResponseDto.setStatus(ResidentConstants.SUCCESS); + } else { + vidRevokeResponseDto = new VidRevokeResponseDTO(); + } vidRevokeResponseDto.setMessage(notificationResponseDTO.getMessage()); responseDto.setResponse(vidRevokeResponseDto); if(Utilitiy.isSecureSession()) { From f9114c3a264f57f1ac52093e1bac96288339010e Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Wed, 7 Dec 2022 19:39:37 +0530 Subject: [PATCH 06/15] added eventId in response header for download eventid api --- .../java/io/mosip/resident/controller/ResidentController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java index 252b656c4c3..166e4fcdbd7 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java @@ -404,6 +404,7 @@ public ResponseEntity downloadCard( audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.RID_DIGITAL_CARD_REQ_SUCCESS, eventId)); return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf")) .header("Content-Disposition", "attachment; filename=\"" + residentService.getFileName(eventId) + ".pdf\"") + .header("eventId", eventId) .body(resource); } From 149fe3533f2134b33d77b5adea118dd727f11d4c Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Thu, 8 Dec 2022 13:14:36 +0530 Subject: [PATCH 07/15] fixed service history pdf file name --- .../resident/constant/ResidentConstants.java | 1 + .../resident/controller/ResidentController.java | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java index 8c2959c5eea..5be872ccdc4 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java @@ -140,5 +140,6 @@ private ResidentConstants() { public static final String VID_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.vid.card.name.convention"; public static final String RESIDENT_OTP_EXPIRY_TIME = "mosip.resident.otp.expiry.time"; public static final String SUCCESS = "Success"; + public static final String DOWNLOAD_SERVICE_HISTORY_FILE_NAME_CONVENTION_PROPERTY = "mosip.resident.download.service.history.file.name.convention"; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java index 166e4fcdbd7..d6502bf856b 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java @@ -8,6 +8,7 @@ import io.mosip.resident.config.LoggerConfiguration; import io.mosip.resident.constant.AuthTypeStatus; import io.mosip.resident.constant.IdType; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.constant.ResidentErrorCode; import io.mosip.resident.dto.AidStatusRequestDTO; import io.mosip.resident.dto.AidStatusResponseDTO; @@ -42,6 +43,7 @@ import io.mosip.resident.util.AuditUtil; import io.mosip.resident.util.EventEnum; import io.mosip.resident.util.JsonUtil; +import io.mosip.resident.util.Utilitiy; import io.mosip.resident.validator.RequestValidator; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -51,6 +53,7 @@ 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.core.env.Environment; import org.springframework.core.io.InputStreamResource; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; @@ -71,6 +74,7 @@ import java.io.IOException; import java.time.LocalDateTime; import java.util.List; +import java.util.Objects; @RestController @Tag(name = "resident-controller", description = "Resident Controller") @@ -87,6 +91,12 @@ public class ResidentController { @Autowired private IdentityServiceImpl identityServiceImpl; + + @Autowired + private Utilitiy utilitiy; + + @Autowired + private Environment environment; @Value("${resident.authLockStatusUpdateV2.id}") private String authLockStatusUpdateV2Id; @@ -544,11 +554,12 @@ public ResponseEntity downLoadServiceHistory( audit.setAuditRequestDto(EventEnum.DOWNLOAD_SERVICE_HISTORY_SUCCESS); logger.debug("AcknowledgementController::acknowledgement()::exit"); return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf")) - .header("Content-Disposition", "attachment; filename=\"" + "viewServiceHistory" + ".pdf\"") + .header("Content-Disposition", "attachment; filename=\"" + utilitiy.getFileName(null, + Objects.requireNonNull(this.environment.getProperty( + ResidentConstants.DOWNLOAD_SERVICE_HISTORY_FILE_NAME_CONVENTION_PROPERTY))) + ".pdf\"") .body(resource); } - @ResponseFilter @GetMapping("/profile") @Operation(summary = "get", description = "Get unread-service-list", tags = { "resident-controller" }) From bbc24e7186bf4ae3bfd53613e98703c1696bd333 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Thu, 8 Dec 2022 14:34:30 +0530 Subject: [PATCH 08/15] added eventid in response of download card api --- .../controller/DownloadCardController.java | 10 +++-- .../resident/service/DownloadCardService.java | 3 +- .../service/impl/DownloadCardServiceImpl.java | 12 ++++- .../DownloadCardControllerTest.java | 44 ++++++++++--------- .../test/service/DownloadCardServiceTest.java | 38 +++++++++------- 5 files changed, 65 insertions(+), 42 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java index 505bc42b11b..267743c02e9 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java @@ -14,6 +14,8 @@ import io.mosip.resident.util.EventEnum; import io.mosip.resident.validator.RequestValidator; import io.swagger.v3.oas.annotations.tags.Tag; +import reactor.util.function.Tuple2; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpStatus; @@ -54,9 +56,9 @@ public ResponseEntity downloadCard(@Validated @RequestBody MainRequestDT logger.debug("DownloadCardController::downloadCard()::entry"); auditUtil.setAuditRequestDto(EventEnum.REQ_CARD); requestValidator.validateDownloadCardRequest(downloadCardRequestDTOMainRequestDTO); - byte[] pdfBytes = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(pdfBytes)); - if(pdfBytes.length==0){ + Tuple2 tupleResponse = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(tupleResponse.getT1())); + if(tupleResponse.getT1().length==0){ throw new CardNotReadyException(); } auditUtil.setAuditRequestDto(EventEnum.GET_ACKNOWLEDGEMENT_DOWNLOAD_URL_SUCCESS); @@ -64,8 +66,10 @@ public ResponseEntity downloadCard(@Validated @RequestBody MainRequestDT return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf")) .header("Content-Disposition", "attachment; filename=\"" + downloadCardRequestDTOMainRequestDTO.getRequest().getIndividualId() + ".pdf\"") + .header("eventId", tupleResponse.getT2()) .body(resource); } + @PreAuthorize("@scopeValidator.hasAllScopes(" + "@authorizedScopes.getPostPersonalizedCard()" + ")") @PostMapping("/download/personalized-card") public ResponseEntity downloadPersonalizedCard(@Validated @RequestBody MainRequestDTO downloadPersonalizedCardMainRequestDTO){ diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java b/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java index d51b2595a32..da16b7074f4 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java @@ -6,13 +6,14 @@ import io.mosip.resident.dto.MainRequestDTO; import io.mosip.resident.dto.ResponseWrapper; import io.mosip.resident.dto.VidDownloadCardResponseDto; +import reactor.util.function.Tuple2; /** * This class is used to create service class to download uin card. * @Author Kamesh Shekhar Prasad */ public interface DownloadCardService { - byte[] getDownloadCardPDF(MainRequestDTO downloadCardRequestDTOMainRequestDTO); + Tuple2 getDownloadCardPDF(MainRequestDTO downloadCardRequestDTOMainRequestDTO); byte[] downloadPersonalizedCard(MainRequestDTO downloadPersonalizedCardMainRequestDTO); diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java index 0085654757a..cbb2cd78bfa 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java @@ -38,6 +38,9 @@ import io.mosip.resident.util.TemplateUtil; import io.mosip.resident.util.Utilities; import io.mosip.resident.util.Utilitiy; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; + import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; @@ -120,8 +123,9 @@ public class DownloadCardServiceImpl implements DownloadCardService { private static final Logger logger = LoggerConfiguration.logConfig(DownloadCardServiceImpl.class); @Override - public byte[] getDownloadCardPDF(MainRequestDTO downloadCardRequestDTOMainRequestDTO) { + public Tuple2 getDownloadCardPDF(MainRequestDTO downloadCardRequestDTOMainRequestDTO) { String rid = null; + String eventId = ""; try { if (idAuthService.validateOtp(downloadCardRequestDTOMainRequestDTO.getRequest().getTransactionId(), getUINForIndividualId(downloadCardRequestDTOMainRequestDTO.getRequest().getIndividualId()) @@ -133,6 +137,10 @@ public byte[] getDownloadCardPDF(MainRequestDTO download } else { rid = utilities.getRidByIndividualId(individualId); } + ResidentTransactionEntity residentTransactionEntity = residentTransactionRepository.findByAid(rid); + if (residentTransactionEntity != null) { + eventId = residentTransactionEntity.getEventId(); + } } else { logger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), @@ -151,7 +159,7 @@ public byte[] getDownloadCardPDF(MainRequestDTO download throw new ResidentServiceException(ResidentErrorCode.OTP_VALIDATION_FAILED.getErrorCode(), e.getErrorText(), e); } - return residentService.getUINCard(rid); + return Tuples.of(residentService.getUINCard(rid), eventId); } @Override diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java index e761c4d14e5..8fa3622e6e7 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java @@ -1,20 +1,12 @@ package io.mosip.resident.test.controller; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import io.mosip.kernel.core.crypto.spi.CryptoCoreSpec; -import io.mosip.resident.controller.DownloadCardController; -import io.mosip.resident.dto.DownloadCardRequestDTO; -import io.mosip.resident.dto.DownloadPersonalizedCardDto; -import io.mosip.resident.dto.MainRequestDTO; -import io.mosip.resident.dto.VidDownloadCardResponseDto; -import io.mosip.resident.helper.ObjectStoreHelper; -import io.mosip.resident.service.DownloadCardService; -import io.mosip.resident.service.ResidentVidService; -import io.mosip.resident.service.impl.IdentityServiceImpl; -import io.mosip.resident.test.ResidentTestBootApplication; -import io.mosip.resident.util.AuditUtil; -import io.mosip.resident.validator.RequestValidator; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.security.PrivateKey; +import java.security.PublicKey; + +import javax.crypto.SecretKey; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,11 +27,23 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.client.RestTemplate; -import javax.crypto.SecretKey; -import java.security.PrivateKey; -import java.security.PublicKey; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import io.mosip.kernel.core.crypto.spi.CryptoCoreSpec; +import io.mosip.resident.controller.DownloadCardController; +import io.mosip.resident.dto.DownloadCardRequestDTO; +import io.mosip.resident.dto.DownloadPersonalizedCardDto; +import io.mosip.resident.dto.MainRequestDTO; +import io.mosip.resident.dto.VidDownloadCardResponseDto; +import io.mosip.resident.helper.ObjectStoreHelper; +import io.mosip.resident.service.DownloadCardService; +import io.mosip.resident.service.ResidentVidService; +import io.mosip.resident.service.impl.IdentityServiceImpl; +import io.mosip.resident.test.ResidentTestBootApplication; +import io.mosip.resident.util.AuditUtil; +import io.mosip.resident.validator.RequestValidator; +import reactor.util.function.Tuples; /** * @author Kamesh Shekhar Prasad @@ -106,7 +110,7 @@ public void setup() throws Exception { @Test public void testGetCardSuccess() throws Exception { - Mockito.when(downloadCardService.getDownloadCardPDF(Mockito.any())).thenReturn(pdfbytes); + Mockito.when(downloadCardService.getDownloadCardPDF(Mockito.any())).thenReturn(Tuples.of(pdfbytes, "12345")); mockMvc.perform(MockMvcRequestBuilders.post("/download-card").contentType(MediaType.APPLICATION_JSON_VALUE) .content(reqJson.getBytes())).andExpect(status().isOk()); } diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java index f9621eea071..d90519c93df 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java @@ -28,6 +28,8 @@ import io.mosip.resident.util.TemplateUtil; import io.mosip.resident.util.Utilities; import io.mosip.resident.util.Utilitiy; +import reactor.util.function.Tuple2; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -124,6 +126,10 @@ public void setup() throws Exception { Mockito.when(identityService.getIndividualIdType(Mockito.anyString())).thenReturn("UIN"); Mockito.when(identityService.getIndividualIdForAid(Mockito.anyString())).thenReturn("7841261580"); + ResidentTransactionEntity residentTransactionEntity = new ResidentTransactionEntity(); + residentTransactionEntity.setEventId("12345"); + Mockito.when(residentTransactionRepository.findByAid(Mockito.anyString())).thenReturn(residentTransactionEntity); + downloadPersonalizedCardMainRequestDTO= new MainRequestDTO<>(); DownloadPersonalizedCardDto downloadPersonalizedCardDto = new DownloadPersonalizedCardDto(); @@ -149,22 +155,22 @@ public void setup() throws Exception { @Test public void testDownloadCardServiceTest() { - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test public void testGetDownloadCardPdfVID(){ Mockito.when(identityService.getIndividualIdType(Mockito.anyString())).thenReturn("VID"); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test public void testGetDownloadCardPdfAID(){ Mockito.when(identityService.getIndividualIdType(Mockito.anyString())).thenReturn("AID"); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) @@ -172,8 +178,8 @@ public void testGetDownloadCardPdfResidentServiceExceptionTest() throws Resident Mockito.when(identityService.getIndividualIdForAid(Mockito.anyString())).thenThrow( new ResidentServiceCheckedException()); Mockito.when(identityService.getIndividualIdType(Mockito.anyString())).thenReturn("AID"); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) @@ -181,31 +187,31 @@ public void testGetDownloadCardPdfApisResourceAccessExceptionTest() throws Resid Mockito.when(identityService.getIndividualIdForAid(Mockito.anyString())).thenThrow( new ApisResourceAccessException()); Mockito.when(identityService.getIndividualIdType(Mockito.anyString())).thenReturn("AID"); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) public void testGetDownloadCardPdfOtpValidationFailedTest() throws ResidentServiceCheckedException, ApisResourceAccessException, OtpValidationFailedException { Mockito.when(idAuthService.validateOtp(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(false); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) public void testGetDownloadCardPdfApiResourceException() throws OtpValidationFailedException, ApisResourceAccessException { Mockito.when(idAuthService.validateOtp(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true); Mockito.when(utilities.getRidByIndividualId(Mockito.anyString())).thenThrow(new ApisResourceAccessException()); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) public void testGetDownloadCardPdfOtpValidationException() throws OtpValidationFailedException, ApisResourceAccessException { Mockito.when(idAuthService.validateOtp(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())) .thenThrow(new OtpValidationFailedException()); - byte[] actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.getDownloadCardPDF(downloadCardRequestDTOMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test From 3bd3dd1931830bbcc003f2ae6728a7fd317812bc Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Mon, 12 Dec 2022 12:10:02 +0530 Subject: [PATCH 09/15] convert eventId to constant --- .../io/mosip/resident/controller/DownloadCardController.java | 3 ++- .../java/io/mosip/resident/controller/ResidentController.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java index 267743c02e9..6209fed73d0 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java @@ -3,6 +3,7 @@ import io.mosip.kernel.core.exception.BaseCheckedException; import io.mosip.kernel.core.logger.spi.Logger; import io.mosip.resident.config.LoggerConfiguration; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.dto.DownloadCardRequestDTO; import io.mosip.resident.dto.DownloadPersonalizedCardDto; import io.mosip.resident.dto.MainRequestDTO; @@ -66,7 +67,7 @@ public ResponseEntity downloadCard(@Validated @RequestBody MainRequestDT return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf")) .header("Content-Disposition", "attachment; filename=\"" + downloadCardRequestDTOMainRequestDTO.getRequest().getIndividualId() + ".pdf\"") - .header("eventId", tupleResponse.getT2()) + .header(ResidentConstants.EVENT_ID, tupleResponse.getT2()) .body(resource); } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java index d6502bf856b..c3d4012dbb4 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java @@ -414,7 +414,7 @@ public ResponseEntity downloadCard( audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.RID_DIGITAL_CARD_REQ_SUCCESS, eventId)); return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf")) .header("Content-Disposition", "attachment; filename=\"" + residentService.getFileName(eventId) + ".pdf\"") - .header("eventId", eventId) + .header(ResidentConstants.EVENT_ID, eventId) .body(resource); } From dbbb7ffc59f1fdeef7d0afbfb363b59b0c5070f7 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Wed, 14 Dec 2022 17:07:20 +0530 Subject: [PATCH 10/15] added eventId for error scenarios --- .../resident/constant/ResidentConstants.java | 1 + .../controller/DownloadCardController.java | 23 +++- .../exception/ApiExceptionHandler.java | 12 ++ .../exception/ResidentServiceException.java | 20 +++- .../resident/service/DownloadCardService.java | 4 +- .../service/impl/DownloadCardServiceImpl.java | 107 +++++++++--------- .../resident/util/ObjectWithMetadata.java | 14 +++ .../DownloadCardControllerTest.java | 12 +- .../test/service/DownloadCardServiceTest.java | 57 +++------- 9 files changed, 143 insertions(+), 107 deletions(-) create mode 100644 resident/resident-service/src/main/java/io/mosip/resident/util/ObjectWithMetadata.java diff --git a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java index e2c8fc96241..81e19118520 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java @@ -139,6 +139,7 @@ private ResidentConstants() { public static final String UIN_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.uin.card.name.convention"; public static final String VID_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.vid.card.name.convention"; public static final String SUCCESS = "Success"; + public static final String FAILED = "Failed"; public static final String REGISTRATION_CENTRE_TEMPLATE_PROPERTY = "resident.template.registration.centers.list"; public static final String SUPPORTING_DOCS_TEMPLATE_PROPERTY = "resident.template.support-docs-list"; public static final String FROM_DATE_TIME = "fromDateTime"; diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java index 6209fed73d0..e70743c3bbf 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/DownloadCardController.java @@ -13,11 +13,13 @@ import io.mosip.resident.service.DownloadCardService; import io.mosip.resident.util.AuditUtil; import io.mosip.resident.util.EventEnum; +import io.mosip.resident.util.Utilitiy; import io.mosip.resident.validator.RequestValidator; import io.swagger.v3.oas.annotations.tags.Tag; import reactor.util.function.Tuple2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -31,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController; import java.io.ByteArrayInputStream; +import java.util.Objects; /** * @author Kamesh Shekhar Prasad @@ -49,6 +52,12 @@ public class DownloadCardController { @Autowired DownloadCardService downloadCardService; + + @Autowired + private Utilitiy utilitiy; + + @Autowired + private Environment environment; private static final Logger logger = LoggerConfiguration.logConfig(DownloadCardController.class); @@ -77,14 +86,18 @@ public ResponseEntity downloadPersonalizedCard(@Validated @RequestBody M logger.debug("DownloadCardController::downloadPersonalizedCard()::entry"); auditUtil.setAuditRequestDto(EventEnum.DOWNLOAD_PERSONALIZED_CARD); requestValidator.validateDownloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - byte[] pdfBytes = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(pdfBytes)); - if(pdfBytes.length==0){ + Tuple2 tupleResponse = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(tupleResponse.getT1())); + if(tupleResponse.getT1().length==0){ return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf")) - .header("Content-Disposition", "attachment; filename=\"" + - downloadCardService.getFileName() + ".pdf\"") + .header("Content-Disposition", "attachment; filename=\"" + + utilitiy.getFileName(tupleResponse.getT2(), + Objects.requireNonNull(this.environment.getProperty( + ResidentConstants.DOWNLOAD_PERSONALIZED_CARD_NAMING_CONVENTION_PROPERTY))) + + ".pdf\"") + .header(ResidentConstants.EVENT_ID, tupleResponse.getT2()) .body(resource); } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/exception/ApiExceptionHandler.java b/resident/resident-service/src/main/java/io/mosip/resident/exception/ApiExceptionHandler.java index c4a4645d114..43cee75ef6b 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/exception/ApiExceptionHandler.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/exception/ApiExceptionHandler.java @@ -13,13 +13,17 @@ import io.mosip.resident.mock.exception.PaymentCanceledException; import io.mosip.resident.mock.exception.PaymentFailedException; import io.mosip.resident.mock.exception.TechnicalErrorException; +import io.mosip.resident.util.ObjectWithMetadata; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.util.MultiValueMap; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -40,6 +44,7 @@ import io.mosip.kernel.core.util.DateUtils; import io.mosip.kernel.core.util.EmptyCheckUtils; import io.mosip.resident.config.LoggerConfiguration; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.constant.ResidentErrorCode; @RestControllerAdvice @@ -102,6 +107,13 @@ private ResponseEntity> getErrorResponseEntity(Htt ServiceError error = new ServiceError(e.getErrorCode(), e.getErrorText()); ResponseWrapper errorResponse = setErrors(httpServletRequest); errorResponse.getErrors().add(error); + if (e instanceof ObjectWithMetadata && ((ObjectWithMetadata) e).getMetadata() != null + && ((ObjectWithMetadata) e).getMetadata().containsKey(ResidentConstants.EVENT_ID)) { + MultiValueMap headers = new HttpHeaders(); + headers.add(ResidentConstants.EVENT_ID, + (String) ((ObjectWithMetadata) e).getMetadata().get(ResidentConstants.EVENT_ID)); + return new ResponseEntity<>(errorResponse, headers, httpStatus); + } return new ResponseEntity<>(errorResponse, httpStatus); } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java index d274c144d77..085e7ce1822 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java @@ -1,15 +1,20 @@ package io.mosip.resident.exception; +import java.util.Map; + import io.mosip.kernel.core.exception.BaseUncheckedException; import io.mosip.resident.constant.ResidentErrorCode; +import io.mosip.resident.util.ObjectWithMetadata; /** * The Class ResidentServiceException. */ -public class ResidentServiceException extends BaseUncheckedException { +public class ResidentServiceException extends BaseUncheckedException implements ObjectWithMetadata { /** Generated serial version id. */ private static final long serialVersionUID = 8621530697947108810L; + + private Map metadata; /** * Constructor the initialize Handler exception. @@ -54,5 +59,18 @@ public ResidentServiceException(String errorCode, String errorMessage, Throwable public ResidentServiceException(ResidentErrorCode err, Throwable rootCause) { this(err.getErrorCode(), err.getErrorMessage(), rootCause); } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public ResidentServiceException(ResidentErrorCode err, Throwable rootCause, Map metadata) { + this(err, rootCause); + this.metadata=metadata; + } } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java b/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java index da16b7074f4..a59863b95cc 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/DownloadCardService.java @@ -15,9 +15,7 @@ public interface DownloadCardService { Tuple2 getDownloadCardPDF(MainRequestDTO downloadCardRequestDTOMainRequestDTO); - byte[] downloadPersonalizedCard(MainRequestDTO downloadPersonalizedCardMainRequestDTO); - - String getFileName(); + Tuple2 downloadPersonalizedCard(MainRequestDTO downloadPersonalizedCardMainRequestDTO); ResponseWrapper getVidCardEventId(String vid) throws BaseCheckedException; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java index cbb2cd78bfa..eb86b09b24e 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java @@ -1,12 +1,32 @@ package io.mosip.resident.service.impl; +import static io.mosip.resident.constant.RegistrationConstants.SUCCESS; +import static io.mosip.resident.constant.TemplateVariablesConstants.NAME; +import static io.mosip.resident.constant.TemplateVariablesConstants.VID; +import static io.mosip.resident.constant.TemplateVariablesConstants.VID_TYPE; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; + import com.fasterxml.jackson.databind.ObjectMapper; + import io.mosip.kernel.core.exception.BaseCheckedException; import io.mosip.kernel.core.logger.spi.Logger; import io.mosip.kernel.core.util.CryptoUtil; import io.mosip.kernel.core.util.DateUtils; import io.mosip.resident.config.LoggerConfiguration; import io.mosip.resident.constant.ApiName; +import io.mosip.resident.constant.EventStatusFailure; +import io.mosip.resident.constant.EventStatusInProgress; import io.mosip.resident.constant.IdType; import io.mosip.resident.constant.LoggerFileConstant; import io.mosip.resident.constant.RequestType; @@ -41,25 +61,6 @@ import reactor.util.function.Tuple2; import reactor.util.function.Tuples; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Service; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import static io.mosip.resident.constant.RegistrationConstants.SUCCESS; -import static io.mosip.resident.constant.TemplateVariablesConstants.NAME; -import static io.mosip.resident.constant.TemplateVariablesConstants.VID; -import static io.mosip.resident.constant.TemplateVariablesConstants.VID_TYPE; - /** * @author Kamesh Shekhar Prasad * This class is used to create service class implementation of download card api. @@ -163,26 +164,53 @@ public Tuple2 getDownloadCardPDF(MainRequestDTO downloadPersonalizedCardMainRequestDTO) { + public Tuple2 downloadPersonalizedCard(MainRequestDTO downloadPersonalizedCardMainRequestDTO) { String encodeHtml = downloadPersonalizedCardMainRequestDTO.getRequest().getHtml(); byte[] decodedData; String password=null; + String eventId = null; + ResidentTransactionEntity residentTransactionEntity = null; try { + residentTransactionEntity = createResidentTransactionEntity(); + if (residentTransactionEntity != null) { + eventId = residentTransactionEntity.getEventId(); + } decodedData = CryptoUtil.decodePlainBase64(encodeHtml); List attributeValues = getAttributeList(); if(Boolean.parseBoolean(this.environment.getProperty(ResidentConstants.IS_PASSWORD_FLAG_ENABLED))){ password = utilitiy.getPassword(attributeValues); } + residentTransactionEntity.setRequestSummary(SUCCESS); + residentTransactionEntity.setStatusCode(EventStatusInProgress.NEW.name()); } catch (Exception e) { + residentTransactionEntity.setRequestSummary(ResidentConstants.FAILED); + residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); audit.setAuditRequestDto(EventEnum.DOWNLOAD_PERSONALIZED_CARD); logger.error("Unable to convert html to pdf RootCause- "+e); - throw new ResidentServiceException(ResidentErrorCode.DOWNLOAD_PERSONALIZED_CARD, e); - } - return utilitiy.signPdf(new ByteArrayInputStream(decodedData), password); + throw new ResidentServiceException(ResidentErrorCode.DOWNLOAD_PERSONALIZED_CARD, e, Map.of(ResidentConstants.EVENT_ID, eventId)); + } finally { + //if the status code will come as null, it will set it as failed. + if(residentTransactionEntity.getStatusCode()==null) { + residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); + residentTransactionEntity.setRequestSummary(ResidentConstants.FAILED); + } + residentTransactionRepository.save(residentTransactionEntity); + } + return Tuples.of(utilitiy.signPdf(new ByteArrayInputStream(decodedData), password), eventId); } - private List getAttributeList() throws ApisResourceAccessException, IOException { + private ResidentTransactionEntity createResidentTransactionEntity() throws ApisResourceAccessException, ResidentServiceCheckedException { + ResidentTransactionEntity residentTransactionEntity = utilitiy.createEntity(); + String eventId = utilitiy.createEventId(); + residentTransactionEntity.setEventId(eventId); + residentTransactionEntity.setRequestTypeCode(RequestType.DOWNLOAD_PERSONALIZED_CARD.name()); + residentTransactionEntity.setRefId(utilitiy.convertToMaskDataFormat(identityService.getResidentIndvidualId())); + residentTransactionEntity.setTokenId(identityService.getResidentIdaToken()); + return residentTransactionEntity; + } + + private List getAttributeList() throws ApisResourceAccessException, IOException { return getAttributeList(identityService.getResidentIndvidualId()); } @@ -227,35 +255,6 @@ private List getAttributeList(String individualId) throws IOException, A return attributeValues; } - @Override - public String getFileName() { - ResidentTransactionEntity residentTransactionEntity = utilitiy.createEntity(); - String eventId = utilitiy.createEventId(); - residentTransactionEntity.setEventId(eventId); - residentTransactionEntity.setRequestTypeCode(RequestType.DOWNLOAD_PERSONALIZED_CARD.name()); - try { - residentTransactionEntity.setRefId(utilitiy.convertToMaskDataFormat(identityService.getResidentIndvidualId())); - residentTransactionEntity.setTokenId(identityService.getResidentIdaToken()); - } catch (ApisResourceAccessException e) { - logger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), - LoggerFileConstant.APPLICATIONID.toString(), - ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorCode() - + ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorMessage() - + ExceptionUtils.getStackTrace(e)); - audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.API_NOT_AVAILABLE, - eventId, "Download personalized card")); - throw new ResidentServiceException(ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorCode(), - ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorMessage(), e); - } catch (ResidentServiceCheckedException e) { - throw new RuntimeException(e); - } - residentTransactionEntity.setRequestSummary(SUCCESS); - residentTransactionEntity.setStatusCode(NEW); - residentTransactionRepository.save(residentTransactionEntity); - return utilitiy.getFileName(eventId, Objects.requireNonNull(this.environment.getProperty - (ResidentConstants.DOWNLOAD_PERSONALIZED_CARD_NAMING_CONVENTION_PROPERTY))); - } - @Override public ResponseWrapper getVidCardEventId(String vid) throws BaseCheckedException { ResponseWrapper responseWrapper= new ResponseWrapper<>(); @@ -314,7 +313,7 @@ private String insertDataForVidCard(ResidentCredentialResponseDto responseDto, S residentTransactionEntity.setRefId(utilitiy.convertToMaskDataFormat(uin)); residentTransactionEntity.setTokenId(identityService.getIDAToken(uin)); residentTransactionEntity.setCredentialRequestId(responseDto.getRequestId()); - residentTransactionEntity.setStatusCode(NEW); + residentTransactionEntity.setStatusCode(EventStatusInProgress.NEW.name()); residentTransactionEntity.setRequestSummary(RequestType.VID_CARD_DOWNLOAD.name()); /** * Here we are setting vid in aid column. diff --git a/resident/resident-service/src/main/java/io/mosip/resident/util/ObjectWithMetadata.java b/resident/resident-service/src/main/java/io/mosip/resident/util/ObjectWithMetadata.java new file mode 100644 index 00000000000..b74859e43de --- /dev/null +++ b/resident/resident-service/src/main/java/io/mosip/resident/util/ObjectWithMetadata.java @@ -0,0 +1,14 @@ +package io.mosip.resident.util; + +import java.util.Map; + +/** + * @author Ritik Jain + */ +public interface ObjectWithMetadata { + + public Map getMetadata(); + + public void setMetadata(Map metadata); + +} diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java index 8fa3622e6e7..a636500b2b8 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/DownloadCardControllerTest.java @@ -19,6 +19,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.env.Environment; import org.springframework.http.MediaType; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -42,6 +43,7 @@ import io.mosip.resident.service.impl.IdentityServiceImpl; import io.mosip.resident.test.ResidentTestBootApplication; import io.mosip.resident.util.AuditUtil; +import io.mosip.resident.util.Utilitiy; import io.mosip.resident.validator.RequestValidator; import reactor.util.function.Tuples; @@ -60,6 +62,12 @@ public class DownloadCardControllerTest { @Mock private AuditUtil audit; + + @Mock + private Environment environment; + + @Mock + private Utilitiy utilitiy; @MockBean private ObjectStoreHelper objectStore; @@ -106,6 +114,8 @@ public void setup() throws Exception { downloadCardRequestDTOMainRequestDTO.setId("mosip.resident.download.uin.card"); reqJson = gson.toJson(downloadCardRequestDTOMainRequestDTO); pdfbytes = "uin".getBytes(); + Mockito.when(utilitiy.getFileName(Mockito.anyString(), Mockito.anyString())).thenReturn("file"); + Mockito.when(environment.getProperty(Mockito.anyString())).thenReturn("property"); } @Test @@ -117,7 +127,7 @@ public void testGetCardSuccess() throws Exception { @Test public void testDownloadPersonalizedCard() throws Exception { - Mockito.when(downloadCardService.downloadPersonalizedCard(Mockito.any())).thenReturn(pdfbytes); + Mockito.when(downloadCardService.downloadPersonalizedCard(Mockito.any())).thenReturn(Tuples.of(pdfbytes, "12345")); MainRequestDTO downloadPersonalizedCardMainRequestDTO = new MainRequestDTO<>(); DownloadPersonalizedCardDto downloadPersonalizedCardDto = diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java index d90519c93df..7c45a65cfa6 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/service/DownloadCardServiceTest.java @@ -125,6 +125,8 @@ public void setup() throws Exception { Mockito.when(residentService.getUINCard(Mockito.anyString())).thenReturn(pdfbytes); Mockito.when(identityService.getIndividualIdType(Mockito.anyString())).thenReturn("UIN"); Mockito.when(identityService.getIndividualIdForAid(Mockito.anyString())).thenReturn("7841261580"); + Mockito.when(utilitiy.createEntity()).thenReturn(new ResidentTransactionEntity()); + Mockito.when(utilitiy.createEventId()).thenReturn("123"); ResidentTransactionEntity residentTransactionEntity = new ResidentTransactionEntity(); residentTransactionEntity.setEventId("12345"); @@ -138,7 +140,6 @@ public void setup() throws Exception { Mockito.when(environment.getProperty(ResidentConstants.IS_PASSWORD_FLAG_ENABLED)).thenReturn(String.valueOf(false)); Mockito.when(environment.getProperty(ResidentConstants.RESIDENT_IDENTITY_SCHEMATYPE)).thenReturn("personalized-card"); Mockito.when(environment.getProperty(ResidentConstants.PASSWORD_ATTRIBUTE)).thenReturn("dateOfBirth"); - Mockito.when(environment.getProperty(ResidentConstants.DOWNLOAD_PERSONALIZED_CARD_NAMING_CONVENTION_PROPERTY)).thenReturn("dateOfBirth"); Mockito.when(environment.getProperty(ResidentConstants.MOSIP_CREDENTIAL_TYPE_PROPERTY)).thenReturn("credentialType"); Mockito.when(environment.getProperty(ResidentConstants.CREDENTIAL_ISSUER)).thenReturn("credentialType"); Mockito.when(environment.getProperty(ResidentConstants.CREDENTIAL_ENCRYPTION_FLAG)).thenReturn("true"); @@ -216,33 +217,27 @@ public void testGetDownloadCardPdfOtpValidationException() throws OtpValidationF @Test public void testDownloadPersonalizedCardSuccess() { - byte[] actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test public void testDownloadPersonalizedCardSuccessWithListAttributes() throws ResidentServiceCheckedException, IOException { Mockito.when(environment.getProperty(ResidentConstants.PASSWORD_ATTRIBUTE)).thenReturn("firstName"); - identityMap = new LinkedHashMap(); Map name = new HashMap<>(); name.put("language", "eng"); name.put("value", "kamesh"); - identityMap.put("UIN", "8251649601"); - identityMap.put("email", "manojvsp12@gmail.com"); - identityMap.put("phone", "9395910872"); - identityMap.put("dateOfBirth", "1970"); identityMap.put("firstName", List.of(name)); Mockito.when(utilities.getLanguageCode()).thenReturn("eng"); - Mockito.when(identityService.getIdentityAttributes(Mockito.anyString(),Mockito.anyString())).thenReturn(identityMap); - byte[] actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test public void testDownloadPersonalizedCardPassword(){ Mockito.when(environment.getProperty(ResidentConstants.IS_PASSWORD_FLAG_ENABLED)).thenReturn(String.valueOf(true)); - byte[] actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) @@ -250,48 +245,24 @@ public void testDownloadPersonalizedCardPasswordFailed(){ Mockito.when(environment.getProperty(ResidentConstants.IS_PASSWORD_FLAG_ENABLED)).thenReturn(String.valueOf(true)); Mockito.when(utilitiy.getPassword(Mockito.anyList())).thenThrow( new ResidentServiceException(ResidentErrorCode.DOWNLOAD_PERSONALIZED_CARD)); - byte[] actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) public void testDownloadPersonalizedCardResidentServiceCheckedException() throws ResidentServiceCheckedException, IOException { Mockito.when(identityService.getIdentityAttributes(Mockito.anyString(), Mockito.anyString())).thenThrow( new ResidentServiceCheckedException()); - byte[] actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - assertEquals(pdfbytes, actualResult); + Tuple2 actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test(expected = ResidentServiceException.class) public void testDownloadPersonalizedCardIOException() throws ResidentServiceCheckedException, IOException { Mockito.when(identityService.getIdentityAttributes(Mockito.anyString(), Mockito.anyString())).thenThrow( new IOException()); - byte[] actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); - assertEquals(pdfbytes, actualResult); - } - - @Test - public void testGetFileNameSuccess(){ - Mockito.when(utilitiy.createEntity()).thenReturn(new ResidentTransactionEntity()); - Mockito.when(utilitiy.createEventId()).thenReturn("123"); - Mockito.when(utilitiy.getFileName(Mockito.anyString(), Mockito.anyString())).thenReturn("file"); - assertEquals("file", downloadCardService.getFileName()); - } - - @Test(expected = ResidentServiceException.class) - public void testGetFileNameApiResourceException() throws ResidentServiceCheckedException, ApisResourceAccessException { - Mockito.when(utilitiy.createEntity()).thenReturn(new ResidentTransactionEntity()); - Mockito.when(utilitiy.createEventId()).thenReturn("123"); - Mockito.when(identityService.getResidentIdaToken()).thenThrow(new ApisResourceAccessException()); - assertEquals("file", downloadCardService.getFileName()); - } - - @Test(expected = RuntimeException.class) - public void testGetFileNameResidentServiceCheckedException() throws ResidentServiceCheckedException, ApisResourceAccessException { - Mockito.when(utilitiy.createEntity()).thenReturn(new ResidentTransactionEntity()); - Mockito.when(utilitiy.createEventId()).thenReturn("123"); - Mockito.when(identityService.getResidentIdaToken()).thenThrow(new ResidentServiceCheckedException()); - assertEquals("file", downloadCardService.getFileName()); + Tuple2 actualResult = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO); + assertEquals(pdfbytes, actualResult.getT1()); } @Test From 788b9aafdf8616fba8800ff3773afa6bd1cd1c1b Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Thu, 15 Dec 2022 12:21:54 +0530 Subject: [PATCH 11/15] fixed reliability bug --- .../service/impl/DownloadCardServiceImpl.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java index eb86b09b24e..95e8bc5ce10 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/DownloadCardServiceImpl.java @@ -184,18 +184,23 @@ public Tuple2 downloadPersonalizedCard(MainRequestDTO Date: Thu, 15 Dec 2022 12:25:28 +0530 Subject: [PATCH 12/15] fixed response of share credential API --- .../ResidentCredentialController.java | 12 +++- .../dto/ResidentCredentialResponseDtoV2.java | 1 - .../ResidentCredentialServiceException.java | 21 ++++++- .../service/ResidentCredentialService.java | 5 +- .../impl/ResidentCredentialServiceImpl.java | 27 ++++---- .../ResidentCredentialControllerTest.java | 4 +- .../ResidentCredentialServiceTest.java | 62 ++++++++++--------- 7 files changed, 83 insertions(+), 49 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentCredentialController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentCredentialController.java index bddb9c2ad8b..fd2e46c819b 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentCredentialController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentCredentialController.java @@ -29,6 +29,7 @@ import io.mosip.kernel.core.http.ResponseWrapper; import io.mosip.resident.constant.RequestIdType; import io.mosip.resident.constant.RequestType; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.dto.CredentialCancelRequestResponseDto; import io.mosip.resident.dto.CredentialRequestStatusResponseDto; import io.mosip.resident.dto.CredentialTypeResponse; @@ -52,6 +53,7 @@ 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 reactor.util.function.Tuple2; @RestController @Tag(name = "resident-credential-controller", description = "Resident Credential Controller") @@ -116,15 +118,19 @@ public ResponseEntity requestShareCredWithPartner( request.setRequest(credentialRequestDto); buildAdditionalMetadata(requestDTO, request); ResponseWrapper response = new ResponseWrapper<>(); + Tuple2 tupleResponse; if(purpose != null) { - response.setResponse(residentCredentialService.shareCredential(request.getRequest(), RequestType.SHARE_CRED_WITH_PARTNER.name(),purpose)); + tupleResponse = residentCredentialService.shareCredential(request.getRequest(), RequestType.SHARE_CRED_WITH_PARTNER.name(),purpose); }else { - response.setResponse(residentCredentialService.shareCredential(request.getRequest(), RequestType.SHARE_CRED_WITH_PARTNER.name())); + tupleResponse = residentCredentialService.shareCredential(request.getRequest(), RequestType.SHARE_CRED_WITH_PARTNER.name()); } response.setId(shareCredentialId); response.setVersion(shareCredentialVersion); + response.setResponse(tupleResponse.getT1()); audit.setAuditRequestDto(EventEnum.CREDENTIAL_REQ_SUCCESS); - return ResponseEntity.status(HttpStatus.OK).body(response); + return ResponseEntity.status(HttpStatus.OK) + .header(ResidentConstants.EVENT_ID, tupleResponse.getT2()) + .body(response); } @GetMapping(value = "req/credential/status/{requestId}") diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java index bc07aeea7d0..e736ee6761e 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/dto/ResidentCredentialResponseDtoV2.java @@ -10,7 +10,6 @@ @Data public class ResidentCredentialResponseDtoV2 { - private String eventId; private String status; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java index 984666a0a43..59da788a829 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java @@ -1,13 +1,19 @@ package io.mosip.resident.exception; +import java.util.Map; + import io.mosip.kernel.core.exception.BaseUncheckedException; +import io.mosip.resident.constant.ResidentErrorCode; +import io.mosip.resident.util.ObjectWithMetadata; -public class ResidentCredentialServiceException extends BaseUncheckedException { +public class ResidentCredentialServiceException extends BaseUncheckedException implements ObjectWithMetadata { /** * Generated serial version id */ private static final long serialVersionUID = 8621530697947108810L; + + private Map metadata; /** * Constructor the initialize Handler exception @@ -29,4 +35,17 @@ public ResidentCredentialServiceException(String errorCode, String errorMessage) public ResidentCredentialServiceException(String errorCode, String errorMessage, Throwable rootCause) { super(errorCode, errorMessage, rootCause); } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public ResidentCredentialServiceException(ResidentErrorCode err, Throwable rootCause, Map metadata) { + this(err.getErrorCode(), err.getErrorMessage(), rootCause); + this.metadata=metadata; + } } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentCredentialService.java b/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentCredentialService.java index b2f0399c7a5..f339f806418 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentCredentialService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentCredentialService.java @@ -10,6 +10,7 @@ import io.mosip.resident.dto.ResponseWrapper; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.ResidentServiceCheckedException; +import reactor.util.function.Tuple2; public interface ResidentCredentialService { @@ -17,9 +18,9 @@ public interface ResidentCredentialService { public ResidentCredentialResponseDto reqCredential(ResidentCredentialRequestDto request, String id) throws ResidentServiceCheckedException; - public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequestDto request, String requestType) throws ResidentServiceCheckedException, ApisResourceAccessException; + public Tuple2 shareCredential(ResidentCredentialRequestDto request, String requestType) throws ResidentServiceCheckedException, ApisResourceAccessException; - public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequestDto request, String requestType, String purpose) throws ResidentServiceCheckedException, ApisResourceAccessException; + public Tuple2 shareCredential(ResidentCredentialRequestDto request, String requestType, String purpose) throws ResidentServiceCheckedException, ApisResourceAccessException; public CredentialRequestStatusResponseDto getStatus(String requestId) throws ResidentServiceCheckedException; diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java index 08d93ebad74..0251dec1bc5 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentCredentialServiceImpl.java @@ -69,6 +69,8 @@ import io.mosip.resident.util.JsonUtil; import io.mosip.resident.util.ResidentServiceRestClient; import io.mosip.resident.util.Utilitiy; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; @Service public class ResidentCredentialServiceImpl implements ResidentCredentialService { @@ -221,13 +223,13 @@ public ResidentCredentialResponseDto reqCredential(ResidentCredentialRequestDto } @Override - public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequestDto dto, String requestType) + public Tuple2 shareCredential(ResidentCredentialRequestDto dto, String requestType) throws ResidentServiceCheckedException, ApisResourceAccessException { return shareCredential(dto, requestType, null); } @Override - public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequestDto dto, String requestType, + public Tuple2 shareCredential(ResidentCredentialRequestDto dto, String requestType, String purpose) throws ResidentServiceCheckedException, ApisResourceAccessException { ResidentCredentialResponseDto residentCredentialResponseDto = new ResidentCredentialResponseDto(); ResidentCredentialResponseDtoV2 residentCredentialResponseDtoV2=new ResidentCredentialResponseDtoV2(); @@ -239,10 +241,14 @@ public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequest String partnerUrl = env.getProperty(ApiName.PARTNER_API_URL.name()) + "/" + dto.getIssuer(); URI partnerUri = URI.create(partnerUrl); String individualId = identityServiceImpl.getResidentIndvidualId(); + String eventId = null; ResidentTransactionEntity residentTransactionEntity = null; try { residentTransactionEntity = createResidentTransactionEntity(dto, requestType, individualId); + if (residentTransactionEntity != null) { + eventId = residentTransactionEntity.getEventId(); + } if (dto.getConsent() == null || dto.getConsent().equalsIgnoreCase(ConsentStatusType.DENIED.name()) || dto.getConsent().trim().isEmpty() || dto.getConsent().equals("null") || !dto.getConsent().equalsIgnoreCase(ConsentStatusType.ACCEPTED.name())) { residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); @@ -273,27 +279,26 @@ public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequest } additionalAttributes.put("RID", residentCredentialResponseDto.getRequestId()); sendNotificationV2(individualId, RequestType.valueOf(requestType), TemplateType.REQUEST_RECEIVED, - residentTransactionEntity.getEventId(), additionalAttributes); + eventId, additionalAttributes); updateResidentTransaction(dto, residentCredentialResponseDto, residentTransactionEntity); - residentCredentialResponseDtoV2.setEventId(residentTransactionEntity.getEventId()); residentCredentialResponseDtoV2.setStatus(ResidentConstants.SUCCESS); } catch (ResidentServiceCheckedException | ApisResourceAccessException e) { if (residentTransactionEntity != null) { residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); sendNotificationV2(individualId, RequestType.valueOf(requestType), TemplateType.FAILURE, - residentTransactionEntity.getEventId(), additionalAttributes); + eventId, additionalAttributes); } audit.setAuditRequestDto(EventEnum.CREDENTIAL_REQ_EXCEPTION); - throw new ResidentCredentialServiceException(ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION.getErrorCode(), - ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION.getErrorMessage(), e); + throw new ResidentCredentialServiceException(ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION, e, + Map.of(ResidentConstants.EVENT_ID, eventId)); } catch (IOException e) { residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name()); sendNotificationV2(individualId, RequestType.valueOf(requestType), TemplateType.FAILURE, - residentTransactionEntity.getEventId(), additionalAttributes); + eventId, additionalAttributes); audit.setAuditRequestDto(EventEnum.CREDENTIAL_REQ_EXCEPTION); - throw new ResidentCredentialServiceException(ResidentErrorCode.IO_EXCEPTION.getErrorCode(), - ResidentErrorCode.IO_EXCEPTION.getErrorMessage(), e); + throw new ResidentCredentialServiceException(ResidentErrorCode.IO_EXCEPTION, e, + Map.of(ResidentConstants.EVENT_ID, eventId)); } finally { if (Utilitiy.isSecureSession() && residentTransactionEntity != null) { //if the status code will come as null, it will set it as failed. @@ -304,7 +309,7 @@ public ResidentCredentialResponseDtoV2 shareCredential(ResidentCredentialRequest residentTransactionRepository.save(residentTransactionEntity); } } - return residentCredentialResponseDtoV2; + return Tuples.of(residentCredentialResponseDtoV2, eventId); } private ResidentTransactionEntity createResidentTransactionEntity(ResidentCredentialRequestDto dto, diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java index 881a2407dd2..27f48b09585 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java @@ -53,6 +53,7 @@ import io.mosip.resident.test.ResidentTestBootApplication; import io.mosip.resident.util.AuditUtil; import io.mosip.resident.validator.RequestValidator; +import reactor.util.function.Tuples; @RunWith(SpringRunner.class) @SpringBootTest(classes = ResidentTestBootApplication.class) @@ -193,7 +194,8 @@ public void testPartnerIdCredentialType() throws Exception { @Test public void testRequestShareCredWithPartner() throws Exception { - Mockito.when(residentCredentialService.shareCredential(Mockito.any(),Mockito.anyString())).thenReturn(new ResidentCredentialResponseDtoV2()); + Mockito.when(residentCredentialService.shareCredential(Mockito.any(), Mockito.anyString())) + .thenReturn(Tuples.of(new ResidentCredentialResponseDtoV2(), "12345")); ShareCredentialRequestDto request = new ShareCredentialRequestDto(); SharableAttributesDTO attr = new SharableAttributesDTO(); attr.setAttributeName("name"); diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentCredentialServiceTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentCredentialServiceTest.java index 3d016dd68b0..5c918df93cb 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentCredentialServiceTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentCredentialServiceTest.java @@ -1,6 +1,31 @@ package io.mosip.resident.test.service; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.net.URI; +import java.security.SecureRandom; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +import org.json.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.junit.MockitoJUnitRunner; +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.databind.ObjectMapper; + import io.mosip.kernel.core.exception.ServiceError; import io.mosip.kernel.core.util.CryptoUtil; import io.mosip.kernel.core.util.DateUtils; @@ -38,30 +63,7 @@ import io.mosip.resident.util.AuditUtil; import io.mosip.resident.util.ResidentServiceRestClient; import io.mosip.resident.util.Utilitiy; -import org.json.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.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.IOException; -import java.net.URI; -import java.security.SecureRandom; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; +import reactor.util.function.Tuple2; @RunWith(MockitoJUnitRunner.class) @RefreshScope @@ -260,8 +262,8 @@ public void testShareCredential() throws IOException, ApisResourceAccessExceptio when(residentServiceRestClient.getApi(partnerUri, ResponseWrapper.class)).thenReturn(partnerResponseDtoResponseWrapper); when(residentServiceRestClient.postApi(any(), any(), any(), any())).thenReturn(response); - ResidentCredentialResponseDtoV2 credentialResponseDto = residentCredentialService.shareCredential(residentCredentialRequestDto,"SHARE_CRED_WITH_PARTNER"); - assertNotNull(credentialResponseDto.getEventId()); + Tuple2 credentialResponseDto = residentCredentialService.shareCredential(residentCredentialRequestDto,"SHARE_CRED_WITH_PARTNER"); + assertNotNull(credentialResponseDto.getT1().getStatus()); } @Test @@ -289,8 +291,8 @@ public void testShareCredentialPurpose() throws IOException, ApisResourceAccessE when(residentServiceRestClient.getApi(partnerUri, ResponseWrapper.class)).thenReturn(partnerResponseDtoResponseWrapper); when(residentServiceRestClient.postApi(any(), any(), any(), any())).thenReturn(response); - ResidentCredentialResponseDtoV2 credentialResponseDto = residentCredentialService.shareCredential(residentCredentialRequestDto,"SHARE_CRED_WITH_PARTNER","Banking"); - assertNotNull(credentialResponseDto.getEventId()); + Tuple2 credentialResponseDto = residentCredentialService.shareCredential(residentCredentialRequestDto,"SHARE_CRED_WITH_PARTNER","Banking"); + assertNotNull(credentialResponseDto.getT1().getStatus()); } @Test @@ -319,8 +321,8 @@ public void testShareCredentialWithEncryptionKeyNull() throws IOException, ApisR when(residentServiceRestClient.getApi(partnerUri, ResponseWrapper.class)).thenReturn(partnerResponseDtoResponseWrapper); when(residentServiceRestClient.postApi(any(), any(), any(), any())).thenReturn(response); - ResidentCredentialResponseDtoV2 credentialResponseDto = residentCredentialService.shareCredential(residentCredentialRequestDto,"SHARE_CRED_WITH_PARTNER"); - assertNotNull(credentialResponseDto.getEventId()); + Tuple2 credentialResponseDto = residentCredentialService.shareCredential(residentCredentialRequestDto,"SHARE_CRED_WITH_PARTNER"); + assertNotNull(credentialResponseDto.getT1().getStatus()); } @Test(expected = ResidentCredentialServiceException.class) From e202a8aa92c66989b17dc07b23d576d65077b283 Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Thu, 15 Dec 2022 14:12:43 +0530 Subject: [PATCH 13/15] fixed test failure --- .../test/controller/ResidentCredentialControllerTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java index 27f48b09585..0062597ab1d 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java @@ -115,6 +115,8 @@ public class ResidentCredentialControllerTest { CredentialRequestStatusResponseDto credentialReqStatusResponse; PartnerCredentialTypePolicyDto partnerCredentialTypeReqResponse; + + ResidentCredentialResponseDtoV2 dtoV2; String reqCredentialEventJson; @@ -126,6 +128,7 @@ public void setup() throws Exception { credentialCancelReqResponse = new CredentialCancelRequestResponseDto(); credentialReqResponse = new ResidentCredentialResponseDto(); partnerCredentialTypeReqResponse = new PartnerCredentialTypePolicyDto(); + dtoV2 = new ResidentCredentialResponseDtoV2(); MockitoAnnotations.initMocks(this); this.mockMvc = MockMvcBuilders.standaloneSetup(residentCredentialController).build(); ResidentCredentialRequestDto credentialRequestDto = new ResidentCredentialRequestDto(); @@ -194,8 +197,8 @@ public void testPartnerIdCredentialType() throws Exception { @Test public void testRequestShareCredWithPartner() throws Exception { - Mockito.when(residentCredentialService.shareCredential(Mockito.any(), Mockito.anyString())) - .thenReturn(Tuples.of(new ResidentCredentialResponseDtoV2(), "12345")); + Mockito.when(residentCredentialService.shareCredential(Mockito.any(), Mockito.anyString(), Mockito.anyString())) + .thenReturn(Tuples.of(dtoV2, "12345")); ShareCredentialRequestDto request = new ShareCredentialRequestDto(); SharableAttributesDTO attr = new SharableAttributesDTO(); attr.setAttributeName("name"); From a182937c05514bfbe41ff69f2be1f41f340c883a Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Thu, 15 Dec 2022 18:39:05 +0530 Subject: [PATCH 14/15] updated response for auth lock unlock API --- .../controller/ResidentController.java | 11 ++++++--- .../dto/AuthLockUnlockResponseDto.java | 17 ------------- .../exception/ResidentServiceException.java | 7 +++++- .../resident/service/ResidentService.java | 3 ++- .../service/impl/ResidentServiceImpl.java | 24 +++++++++++-------- .../controller/ResidentControllerTest.java | 5 ++-- ...esidentServiceRequestTypeAuthLockTest.java | 18 +++++++------- 7 files changed, 42 insertions(+), 43 deletions(-) delete mode 100644 resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java diff --git a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java index c3d4012dbb4..c3b92924d92 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/controller/ResidentController.java @@ -51,6 +51,8 @@ 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 reactor.util.function.Tuple2; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; @@ -228,7 +230,7 @@ public ResponseWrapper reqAuthUnlock( @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(schema = @Schema(hidden = true))), @ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(hidden = true))), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(schema = @Schema(hidden = true))) }) - public ResponseWrapper reqAauthTypeStatusUpdateV2( + public ResponseEntity reqAauthTypeStatusUpdateV2( @Valid @RequestBody RequestWrapper requestDTO) throws ResidentServiceCheckedException, ApisResourceAccessException { audit.setAuditRequestDto( @@ -237,11 +239,14 @@ public ResponseWrapper reqAauthTypeStatusUpdateV2( validator.validateAuthLockOrUnlockRequestV2(requestDTO); audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.REQ_AUTH_LOCK, individualId)); ResponseWrapper response = new ResponseWrapper<>(); - response.setResponse(residentService.reqAauthTypeStatusUpdateV2(requestDTO.getRequest())); + Tuple2 tupleResponse = residentService.reqAauthTypeStatusUpdateV2(requestDTO.getRequest()); + response.setResponse(tupleResponse.getT1()); response.setId(authLockStatusUpdateV2Id); response.setVersion(authLockStatusUpdateV2Version); audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.REQ_AUTH_LOCK_SUCCESS, individualId)); - return response; + return ResponseEntity.ok() + .header(ResidentConstants.EVENT_ID, tupleResponse.getT2()) + .body(response); } @ResponseFilter diff --git a/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java b/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java deleted file mode 100644 index 26f0ea6aff6..00000000000 --- a/resident/resident-service/src/main/java/io/mosip/resident/dto/AuthLockUnlockResponseDto.java +++ /dev/null @@ -1,17 +0,0 @@ -package io.mosip.resident.dto; - -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * The Auth Lock Unlock response dto. - * - * @author Ritik Jain - */ -@Data -@EqualsAndHashCode(callSuper = true) -public class AuthLockUnlockResponseDto extends ResponseDTO { - - private String eventId; - -} diff --git a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java index 085e7ce1822..1e7cd04f676 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentServiceException.java @@ -70,7 +70,12 @@ public void setMetadata(Map metadata) { public ResidentServiceException(ResidentErrorCode err, Throwable rootCause, Map metadata) { this(err, rootCause); - this.metadata=metadata; + this.metadata = metadata; + } + + public ResidentServiceException(ResidentErrorCode err, Map metadata) { + this(err); + this.metadata = metadata; } } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentService.java b/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentService.java index 80e2f369cc3..caf1dbc5603 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/ResidentService.java @@ -32,6 +32,7 @@ import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.OtpValidationFailedException; import io.mosip.resident.exception.ResidentServiceCheckedException; +import reactor.util.function.Tuple2; public interface ResidentService { @@ -50,7 +51,7 @@ public ResponseDTO reqAauthTypeStatusUpdate(AuthLockOrUnLockRequestDto dto, Auth public Object reqUinUpdate(ResidentUpdateRequestDto dto, JSONObject demographicJsonObject) throws ResidentServiceCheckedException; - ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 request) + public Tuple2 reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 request) throws ResidentServiceCheckedException, ApisResourceAccessException; public ResponseWrapper getAuthLockStatus(String individualId) throws ResidentServiceCheckedException;; diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java index 840a9aa11e9..56c56cc5817 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java @@ -78,7 +78,6 @@ import io.mosip.resident.dto.AuthLockOrUnLockRequestDto; import io.mosip.resident.dto.AuthLockOrUnLockRequestDtoV2; import io.mosip.resident.dto.AuthLockStatusResponseDtoV2; -import io.mosip.resident.dto.AuthLockUnlockResponseDto; import io.mosip.resident.dto.AuthTxnDetailsDTO; import io.mosip.resident.dto.AuthTypeStatusDtoV2; import io.mosip.resident.dto.AuthUnLockRequestDTO; @@ -152,6 +151,8 @@ import io.mosip.resident.util.UINCardDownloadService; import io.mosip.resident.util.Utilities; import io.mosip.resident.util.Utilitiy; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; @Service public class ResidentServiceImpl implements ResidentService { @@ -1122,14 +1123,15 @@ private List getResidentDocuments(ResidentUpdateRequestDto dt } @Override - public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authLockOrUnLockRequestDtoV2) + public Tuple2 reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authLockOrUnLockRequestDtoV2) throws ResidentServiceCheckedException, ApisResourceAccessException { logger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), "ResidentServiceImpl::reqAauthTypeStatusUpdate():: entry"); - AuthLockUnlockResponseDto response = new AuthLockUnlockResponseDto(); + ResponseDTO response = new ResponseDTO(); String individualId = identityServiceImpl.getResidentIndvidualId(); boolean isTransactionSuccessful = false; List residentTransactionEntities = List.of(); + String eventId = null; try { audit.setAuditRequestDto( EventEnum.getEventEnumWithValue(EventEnum.REQ_AUTH_TYPE_LOCK, "Request for Auth Type Lock")); @@ -1144,6 +1146,9 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL throw new RuntimeException(e); } }).collect(Collectors.toList()); + if (!residentTransactionEntities.isEmpty()) { + eventId = residentTransactionEntities.get(0).getEventId(); + } List authTypesStatusList = authLockOrUnLockRequestDtoV2.getAuthTypes(); String authType = authTypesStatusList.stream().map(ResidentServiceImpl::getAuthTypeBasedOnConfigV2) @@ -1176,8 +1181,8 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL } else { audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.REQUEST_FAILED, "Request for auth " + authLockOrUnLockRequestDtoV2.getAuthTypes() + " lock failed")); - throw new ResidentServiceException(ResidentErrorCode.REQUEST_FAILED.getErrorCode(), - ResidentErrorCode.REQUEST_FAILED.getErrorMessage()); + throw new ResidentServiceException(ResidentErrorCode.REQUEST_FAILED, + Map.of(ResidentConstants.EVENT_ID, eventId)); } } catch (ApisResourceAccessException e) { @@ -1193,8 +1198,8 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL + ExceptionUtils.getStackTrace(e)); audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.API_NOT_AVAILABLE, "Request for auth" + authLockOrUnLockRequestDtoV2.getAuthTypes() + " lock failed")); - throw new ResidentServiceException(ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorCode(), - ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorMessage(), e); + throw new ResidentServiceException(ResidentErrorCode.API_RESOURCE_UNAVAILABLE, e, + Map.of(ResidentConstants.EVENT_ID, eventId)); } finally { residentTransactionRepository.saveAll(residentTransactionEntities); @@ -1202,7 +1207,7 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL TemplateType templateType = isTransactionSuccessful ? TemplateType.REQUEST_RECEIVED : TemplateType.FAILURE; NotificationResponseDTO notificationResponseDTO = sendNotificationV2(individualId, requestType, - templateType, residentTransactionEntities.get(0).getEventId(), null); + templateType, eventId, null); if (isTransactionSuccessful) audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.SEND_NOTIFICATION_SUCCESS, @@ -1213,12 +1218,11 @@ public ResponseDTO reqAauthTypeStatusUpdateV2(AuthLockOrUnLockRequestDtoV2 authL if (notificationResponseDTO != null) { response.setMessage(notificationResponseDTO.getMessage()); response.setStatus(ResidentConstants.SUCCESS); - response.setEventId(residentTransactionEntities.get(0).getEventId()); } } logger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(), "ResidentServiceImpl::reqAauthTypeStatusUpdate():: exit"); - return response; + return Tuples.of(response, eventId); } private ResidentTransactionEntity createResidentTransactionEntity(String individualId, String partnerId) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentControllerTest.java index a310b134651..33e019b9c3d 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentControllerTest.java @@ -93,6 +93,7 @@ import io.mosip.resident.util.AuditUtil; import io.mosip.resident.util.JsonUtil; import io.mosip.resident.validator.RequestValidator; +import reactor.util.function.Tuples; /** * @author Sowmya Ujjappa Banakar @@ -235,7 +236,7 @@ public void testReqAuthTypeLock() throws Exception { ResponseDTO responseDto = new ResponseDTO(); responseDto.setStatus("success"); doNothing().when(validator).validateAuthLockOrUnlockRequestV2(Mockito.any()); - Mockito.doReturn(responseDto).when(residentService).reqAauthTypeStatusUpdateV2(Mockito.any()); + Mockito.doReturn(Tuples.of(responseDto, "12345")).when(residentService).reqAauthTypeStatusUpdateV2(Mockito.any()); residentController.reqAauthTypeStatusUpdateV2(authTypeStatusRequest); validator.validateAuthLockOrUnlockRequestV2(authTypeStatusRequest); this.mockMvc.perform( @@ -248,7 +249,7 @@ public void testReqAuthTypeLock() throws Exception { public void testReqAuthTypeLockBadRequest() throws Exception { ResponseDTO responseDto = new ResponseDTO(); doNothing().when(validator).validateAuthLockOrUnlockRequest(Mockito.any(), Mockito.any()); - Mockito.doReturn(responseDto).when(residentService).reqAauthTypeStatusUpdateV2(Mockito.any()); + Mockito.doReturn(Tuples.of(responseDto, "12345")).when(residentService).reqAauthTypeStatusUpdateV2(Mockito.any()); MvcResult result = this.mockMvc .perform(post("/auth-lock-unlock").contentType(MediaType.APPLICATION_JSON).content("")) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceRequestTypeAuthLockTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceRequestTypeAuthLockTest.java index 209828069e6..14dbe0fe52e 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceRequestTypeAuthLockTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/service/ResidentServiceRequestTypeAuthLockTest.java @@ -28,6 +28,8 @@ import io.mosip.resident.util.AuditUtil; import io.mosip.resident.util.UINCardDownloadService; import io.mosip.resident.util.Utilitiy; +import reactor.util.function.Tuple2; + import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Test; @@ -35,14 +37,13 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.util.ReflectionTestUtils; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -110,15 +111,17 @@ public class ResidentServiceRequestTypeAuthLockTest { public void setup() throws ApisResourceAccessException, ResidentServiceCheckedException { notificationResponseDTO = new NotificationResponseDTO(); - notificationResponseDTO.setStatus("Notification success"); + notificationResponseDTO.setStatus("success"); + notificationResponseDTO.setMessage("Notification success"); Mockito.doNothing().when(audit).setAuditRequestDto(Mockito.any()); individualId = identityServiceImpl.getResidentIndvidualId(); List residentTransactionEntities=new ArrayList<>(); ResidentTransactionEntity residentTransactionEntity = new ResidentTransactionEntity(); - residentTransactionEntity.setEventId(UUID.randomUUID().toString()); + residentTransactionEntity.setEventId("12345"); when(utility.createEntity()).thenReturn(residentTransactionEntity); residentTransactionEntities.add(residentTransactionEntity); + Mockito.when(utility.createEventId()).thenReturn("12345"); ArrayList partnerIds = new ArrayList<>(); partnerIds.add("m-partner-default-auth"); when(partnerService.getPartnerDetails(Mockito.anyString())).thenReturn(partnerIds); @@ -139,13 +142,10 @@ public void testReqAuthTypeStatusUpdateSuccess() authLockOrUnLockRequestDtoV2.setAuthTypes(authTypeStatusDtoList); for (AuthTypeStatusDto authTypeStatusDto1 : authLockOrUnLockRequestDtoV2.getAuthTypes()) { Mockito.when(idAuthService.authTypeStatusUpdateForRequestId(any(), any(), any())).thenReturn("123"); - ResponseDTO response = new ResponseDTO(); - response.setMessage("Notification success"); Mockito.when(notificationService.sendNotification(Mockito.any())).thenReturn(notificationResponseDTO); - ResponseDTO authLockResponse = residentService.reqAauthTypeStatusUpdateV2(authLockOrUnLockRequestDtoV2); - assertEquals(authLockResponse.getMessage(), authLockResponse.getMessage()); + Tuple2 authLockResponse = residentService.reqAauthTypeStatusUpdateV2(authLockOrUnLockRequestDtoV2); + assertEquals(notificationResponseDTO.getMessage(), authLockResponse.getT1().getMessage()); } - } @Test(expected = ResidentServiceException.class) From ace76d1996788c26b5b76fc20074e70aa82f4ffa Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Thu, 15 Dec 2022 19:07:52 +0530 Subject: [PATCH 15/15] covered code for one method --- .../ResidentCredentialServiceException.java | 4 ++-- .../controller/ResidentCredentialControllerTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java index 59da788a829..3eb00bdf0dc 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/exception/ResidentCredentialServiceException.java @@ -44,8 +44,8 @@ public void setMetadata(Map metadata) { this.metadata = metadata; } - public ResidentCredentialServiceException(ResidentErrorCode err, Throwable rootCause, Map metadata) { + public ResidentCredentialServiceException(ResidentErrorCode err, Throwable rootCause, Map metadata) { this(err.getErrorCode(), err.getErrorMessage(), rootCause); - this.metadata=metadata; + this.metadata = metadata; } } diff --git a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java index 0062597ab1d..52ed3ede3c6 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/test/controller/ResidentCredentialControllerTest.java @@ -210,4 +210,16 @@ public void testRequestShareCredWithPartner() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/share-credential").contentType(MediaType.APPLICATION_JSON_VALUE) .content(gson.toJson(requestWrapper).getBytes())).andExpect(status().isOk()); } + + @Test + public void testRequestShareCredWithPartnerWithoutPurpose() throws Exception { + Mockito.when(residentCredentialService.shareCredential(Mockito.any(), Mockito.anyString())) + .thenReturn(Tuples.of(dtoV2, "12345")); + ShareCredentialRequestDto request = new ShareCredentialRequestDto(); + request.setSharableAttributes(null); + RequestWrapper requestWrapper = new RequestWrapper<>(); + requestWrapper.setRequest(request); + mockMvc.perform(MockMvcRequestBuilders.post("/share-credential").contentType(MediaType.APPLICATION_JSON_VALUE) + .content(gson.toJson(requestWrapper).getBytes())).andExpect(status().isOk()); + } }