Skip to content

Commit

Permalink
Merge pull request #1848 from Khuddusshariff0022/develop-mosip-31365
Browse files Browse the repository at this point in the history
[MOSIP-31365] Discarding draft when update, extraction, publish …
  • Loading branch information
vishwa-vyom authored Mar 13, 2024
2 parents fd11532 + 4b5e81c commit f7157bd
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ protected String getPropertyPrefix() {
* Deploy verticle.
*/
public void deployVerticle() {

mosipEventBus = this.getEventBus(this, clusterManagerUrl, workerPoolSize);
this.consumeAndSend(mosipEventBus, MessageBusAddress.BIOMETRIC_EXTRACTION_BUS_IN,
MessageBusAddress.BIOMETRIC_EXTRACTION_BUS_OUT, messageExpiryTimeLimit);
Expand Down Expand Up @@ -172,8 +171,6 @@ public MessageDTO process(MessageDTO object) {
registrationStatusDto
.setLatestTransactionTypeCode(RegistrationTransactionTypeCode.BIOMETRIC_EXTRACTION.toString());
registrationStatusDto.setRegistrationStageName(getStageName());


if(!idrepoDraftService.idrepoHasDraft(registrationStatusDto.getRegistrationId())) {
registrationStatusDto.setStatusCode(RegistrationStatusCode.FAILED.toString());
registrationStatusDto.setLatestTransactionStatusCode(registrationStatusMapperUtil
Expand All @@ -196,9 +193,9 @@ public MessageDTO process(MessageDTO object) {
else {
ExtractorsDto extractorsDto=getExtractors(registrationStatusDto.getRegistrationId());
if(extractorsDto.getExtractors()!=null && !extractorsDto.getExtractors().isEmpty()) {
for(ExtractorDto dto:extractorsDto.getExtractors()) {
addBiometricExtractiontoIdRepository(dto,registrationStatusDto.getRegistrationId());
}
for(ExtractorDto dto:extractorsDto.getExtractors()) {
addBiometricExtractiontoIdRepository(dto,registrationStatusDto.getRegistrationId());
}
}
else {
throw new RegistrationProcessorCheckedException(PlatformErrorMessages.RPR_PMS_BIOMETRIC_EXTRACTION_NULL_RESPONSE.getCode(),
Expand Down Expand Up @@ -361,13 +358,13 @@ private IdResponseDTO addBiometricExtractiontoIdRepository(ExtractorDto dto,
}
List<String> segments=List.of(registrationId);
IdResponseDTO response= (IdResponseDTO) registrationProcessorRestClientService.putApi(ApiName.IDREPOEXTRACTBIOMETRICS, segments, extractionFormat, dto.getAttributeName(), null, IdResponseDTO.class, null);

if (response.getErrors() != null && !response.getErrors().isEmpty()) {
ErrorDTO error = response.getErrors().get(0);
regProcLogger.error("Error occured while updating draft for id : " + registrationId, error.toString());
if (response.getErrors().get(0).getErrorCode().equalsIgnoreCase(ID_REPO_KEY_MANAGER_ERROR)) {
throw new IdrepoDraftReprocessableException(error.getErrorCode(), error.getMessage());
} else {
idrepoDraftService.idrepoDiscardDraft(registrationId);
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.yarn.webapp.hamlet.HamletSpec;
import org.assertj.core.util.Lists;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -170,8 +172,6 @@ public void setUp() throws Exception {
dto.setReg_type("UPDATE");

MockitoAnnotations.initMocks(this);


ResponseWrapper<AuditResponseDto> responseWrapper = new ResponseWrapper<>();
Mockito.doReturn(responseWrapper).when(auditLogRequestBuilder).createAuditRequestBuilder(
"test case description", EventId.RPR_405.toString(), EventName.UPDATE.toString(),
Expand Down Expand Up @@ -356,4 +356,31 @@ public void testIdrepoDraftReprocessableException() throws Exception {
assertTrue(result.getInternalError());
assertTrue(result.getIsValid());
}
@Test
public void testBiometricExtractionDraftExceptionAndDiscardDreaft() throws Exception {
MessageDTO messageDTO = new MessageDTO();
messageDTO.setRid("27847657360002520181210094052");
messageDTO.setReg_type(RegistrationType.NEW.name());
messageDTO.setWorkflowInstanceId("123er");
messageDTO.setIteration(1);
when(idrepoDraftService.idrepoHasDraft(anyString())).thenReturn(true);
IdResponseDTO idResponseDTO=new IdResponseDTO();
List<io.mosip.registration.processor.core.common.rest.dto.ErrorDTO> errorList=new ArrayList<>();
io.mosip.registration.processor.core.common.rest.dto.ErrorDTO dto=new io.mosip.registration.processor.core.common.rest.dto.ErrorDTO();
dto.setErrorCode("aa");
dto.setMessage("bb");
errorList.add(dto);
idResponseDTO.setId("mosip.id.read");
idResponseDTO.setResponse(null);
idResponseDTO.setErrors(errorList);
idResponseDTO.setVersion("1.0");
List<String> segment=new ArrayList<>();
segment.add(messageDTO.getRid());
when(registrationProcessorRestClientService.putApi(Mockito.eq(ApiName.IDREPOEXTRACTBIOMETRICS), any(), anyString(), anyString(), Mockito.isNull(), Mockito.eq(IdResponseDTO.class), isNull())).thenReturn(idResponseDTO);
MessageDTO result = biometricExtractionStage.process(messageDTO);
verify(idrepoDraftService,atLeastOnce()).idrepoDiscardDraft(anyString());

assertTrue(result.getInternalError());
assertFalse(result.getIsValid());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.registration.processor.stages.finalization.stage;

import io.mosip.registration.processor.core.common.rest.dto.ErrorDTO;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public enum ApiName {
IDREPOUPDATEDRAFT,
IDREPOPUBLISHDRAFT,
IDREPOEXTRACTBIOMETRICS,
IDREPODISCARDDRAFT,
CREDENTIALREQUESTV2;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@ public T postApi(String url, MediaType mediaType, List<String> pathsegments, Lis
T requestedData, Class<?> responseType) throws ApisResourceAccessException;

public Integer headApi(ApiName apiName, List<String> pathsegments, List<String> queryParamName, List<Object> queryParamValue) throws ApisResourceAccessException;
}

public T deleteApi(ApiName apiName, List<String> pathsegments, String queryParam, String queryParamValue,
Class<?> responseType) throws ApisResourceAccessException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public class IdrepoDraftService {
private static final Integer IDREPO_DRAFT_FOUND = 200;
private static final Integer IDREPO_DRAFT_NOT_FOUND = 204;
private static Logger regProcLogger = RegProcessorLogger.getLogger(IdrepoDraftService.class);
private static final String ID_REPO_KEY_MANAGER_ERROR = "IDR-IDS-003";
private static final String ID_REPO_KEY_MANAGER_ERROR = "IDR-IDS-003";

@Autowired
private ObjectMapper mapper;

/** The registration processor rest client service. */
/**
* The registration processor rest client service.
*/
@Autowired
private RegistrationProcessorRestClientService<Object> registrationProcessorRestClientService;

Expand All @@ -58,28 +60,37 @@ public boolean idrepoHasDraft(String id) throws ApisResourceAccessException, Idr
return hasDraft;
}

public ResponseDTO idrepoGetDraft(String id) throws ApisResourceAccessException {
public ResponseDTO idrepoGetDraft(String id) throws ApisResourceAccessException, IdrepoDraftException {
regProcLogger.debug("idrepoGetDraft entry " + id);
IdResponseDTO idResponseDTO = (IdResponseDTO) registrationProcessorRestClientService.getApi(
ApiName.IDREPOGETDRAFT, Lists.newArrayList(id), Lists.emptyList(), null, IdResponseDTO.class);
if (idResponseDTO.getErrors() != null && !idResponseDTO.getErrors().isEmpty()) {
ErrorDTO error = idResponseDTO.getErrors().get(0);
regProcLogger.error("Error occured while getting draft for id : " + id, error.toString());
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}
regProcLogger.debug("idrepoGetDraft exit " + id);
return idResponseDTO.getResponse();
}

regProcLogger.debug("idrepoGetDraft exit " + id);
return idResponseDTO.getResponse();
}

public boolean idrepoCreateDraft(String id, String uin) throws ApisResourceAccessException {
public boolean idrepoCreateDraft(String id, String uin) throws ApisResourceAccessException, IdrepoDraftException {
regProcLogger.debug("idrepoCreateDraft entry " + id);
String queryParam = uin != null ? UIN : null;
String queryParamValue = uin != null ? uin : null;

ResponseWrapper response = (ResponseWrapper) registrationProcessorRestClientService.postApi(
ApiName.IDREPOCREATEDRAFT, Lists.newArrayList(id), queryParam, queryParamValue, null, ResponseWrapper.class);

return (response.getErrors() == null || response.getErrors().isEmpty());
if (response.getErrors() != null && !response.getErrors().isEmpty())
{
regProcLogger.error("Error while creating draft for id " + id);
throw new IdrepoDraftException(PlatformErrorMessages.IDREPO_DRAFT_EXCEPTION.getCode(), PlatformErrorMessages.IDREPO_DRAFT_EXCEPTION.getMessage());
}
return (response.getErrors() == null || response.getErrors().isEmpty());
}

public IdResponseDTO idrepoUpdateDraft(String id, String uin, IdRequestDto idRequestDto)
throws ApisResourceAccessException, IdrepoDraftException, IOException, IdrepoDraftReprocessableException {
public IdResponseDTO idrepoUpdateDraft(String id, String uin, IdRequestDto idRequestDto)
throws ApisResourceAccessException, IdrepoDraftException, IOException, IdrepoDraftReprocessableException {
regProcLogger.debug("idrepoUpdateDraft entry " + id);
if (!idrepoHasDraft(id)) {
regProcLogger.info("Existing draft not found for id " + id + ". Creating new draft.");
Expand All @@ -93,49 +104,69 @@ public IdResponseDTO idrepoUpdateDraft(String id, String uin, IdRequestDto idReq
JSONObject existingIdentity = mapper.readValue(mapper.writeValueAsString(responseDTO.getIdentity()), JSONObject.class);
JSONObject newIdentity = mapper.readValue(mapper.writeValueAsString(idRequestDto.getRequest().getIdentity()), JSONObject.class);
newIdentity.put(UIN, existingIdentity.get(UIN));
idRequestDto.getRequest().setIdentity(newIdentity);
// setting the identity to request while updating the draft.
requestDto.setIdentity(newIdentity);
requestDto.setRegistrationId(responseDTO.getRegistrationId());
requestDto.setStatus(responseDTO.getStatus());
requestDto.setUin(responseDTO.getUin());
idRequestDto.setRequest(requestDto);

}

IdResponseDTO response = (IdResponseDTO) registrationProcessorRestClientService.patchApi(
ApiName.IDREPOUPDATEDRAFT, Lists.newArrayList(id), null, null, idRequestDto, IdResponseDTO.class);
if (response.getErrors() != null && !response.getErrors().isEmpty()) {
ErrorDTO error = response.getErrors().get(0);
regProcLogger.error("Error occured while updating draft for id : " + id, error.toString());
if (response.getErrors().get(0).getErrorCode().equalsIgnoreCase(ID_REPO_KEY_MANAGER_ERROR)) {
throw new IdrepoDraftReprocessableException(error.getErrorCode(), error.getMessage());
} else {
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}
ApiName.IDREPOUPDATEDRAFT, Lists.newArrayList(id), null, null, idRequestDto, IdResponseDTO.class);
if (response.getErrors() != null && !response.getErrors().isEmpty()) {
regProcLogger.info("Error while updating the drant " + id);
idrepoDiscardDraft(id);
ErrorDTO error = response.getErrors().get(0);
regProcLogger.error("Error occured while updating draft for id : " + id, error.toString());
if (response.getErrors().get(0).getErrorCode().equalsIgnoreCase(ID_REPO_KEY_MANAGER_ERROR)) {
regProcLogger.error("Error occured Deleting the Draft : " + id, error.toString());
throw new IdrepoDraftReprocessableException(error.getErrorCode(), error.getMessage());
} else {
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}
}

regProcLogger.debug("idrepoUpdateDraft exit " + id);
return response;
}

public IdResponseDTO idrepoPublishDraft(String id)
throws ApisResourceAccessException, IdrepoDraftException, IdrepoDraftReprocessableException {
regProcLogger.debug("idrepoPublishDraft entry " + id);
List<String> pathsegments=new ArrayList<String>();
pathsegments.add(id);
IdResponseDTO response = (IdResponseDTO) registrationProcessorRestClientService.
getApi(ApiName.IDREPOPUBLISHDRAFT, pathsegments, "", "", IdResponseDTO.class);
if (response.getErrors() != null && !response.getErrors().isEmpty()) {
ErrorDTO error = response.getErrors().get(0);
regProcLogger.error("Error occured while updating draft for id : " + id, error.toString());
if (response.getErrors().get(0).getErrorCode().equalsIgnoreCase(ID_REPO_KEY_MANAGER_ERROR)) {
throw new IdrepoDraftReprocessableException(error.getErrorCode(), error.getMessage());
} else {
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}

public IdResponseDTO idrepoPublishDraft(String id)
throws ApisResourceAccessException, IdrepoDraftException, IdrepoDraftReprocessableException {
regProcLogger.debug("idrepoPublishDraft entry " + id);
List<String> pathsegments = new ArrayList<String>();
pathsegments.add(id);
IdResponseDTO response = (IdResponseDTO) registrationProcessorRestClientService.
getApi(ApiName.IDREPOPUBLISHDRAFT, pathsegments, "", "", IdResponseDTO.class);

if(response.getErrors()!=null && !response.getErrors().isEmpty())
{
ErrorDTO error=response.getErrors().get(0);
regProcLogger.error("Error occured while publishing the Draft : " + id, error.toString());
if (response.getErrors().get(0).getErrorCode().equalsIgnoreCase(ID_REPO_KEY_MANAGER_ERROR)) {
throw new IdrepoDraftReprocessableException(error.getErrorCode(), error.getMessage());
} else {
idrepoDiscardDraft(id);
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}
}

regProcLogger.debug("idrepoPublishDraft exit " + id);
return response;
}
}

public Boolean idrepoDiscardDraft(String id) throws ApisResourceAccessException, IdrepoDraftReprocessableException, IdrepoDraftException {
regProcLogger.debug("idrepoDiscardDraft entry " + id);
List<String> pathsegments = new ArrayList<String>();
pathsegments.add(id);
IdResponseDTO response = (IdResponseDTO) registrationProcessorRestClientService.
deleteApi(ApiName.IDREPODISCARDDRAFT, pathsegments, "", "", IdResponseDTO.class);
if (response.getErrors() != null && !response.getErrors().isEmpty()) {
ErrorDTO error = response.getErrors().get(0);
regProcLogger.error("Error occured while discarding draft for id : " + id, error.toString());
if (response.getErrors().get(0).getErrorCode().equalsIgnoreCase(ID_REPO_KEY_MANAGER_ERROR)) {
throw new IdrepoDraftReprocessableException(error.getErrorCode(), error.getMessage());
} else {
throw new IdrepoDraftException(error.getErrorCode(), error.getMessage());
}
}
return true;
}
}
Loading

0 comments on commit f7157bd

Please sign in to comment.