Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,28 +237,14 @@ public MessageDTO process(MessageDTO object) {
List<String> defaultFields = idSchemaUtil.getDefaultFields(Double.valueOf(schemaVersion));

final String regTypeForCreatedOn = registrationStatusDto.getRegistrationType();
CompletableFuture<String> createdOnFuture = null;

// Start retrieveCreatedDateFromPacket in parallel if schema contains the packetCreatedOn and packet type NEW or UPDATE.
if (defaultFields.contains(MappingJsonConstants.PACKET_CREATED_ON)) {
if (RegistrationType.NEW.toString().equalsIgnoreCase(object.getReg_type()) ||
RegistrationType.UPDATE.toString().equalsIgnoreCase(object.getReg_type())) {
createdOnFuture = CompletableFuture.supplyAsync(() -> {
try {
return utility.retrieveCreatedDateFromPacket(registrationId, regTypeForCreatedOn, ProviderStageName.CREATE_DRAFT);
} catch (Exception e) {
throw new CompletionException(e);
}
}, uinExecutor);
// Always retrieve packetCreatedOn — it is required for the stale-packet check.
CompletableFuture<String> createdOnFuture = CompletableFuture.supplyAsync(() -> {
try {
return utility.retrieveCreatedDateFromPacket(registrationId, regTypeForCreatedOn, ProviderStageName.CREATE_DRAFT);
} catch (Exception e) {
throw new CompletionException(e);
}
} else {
regProcLogger.info(
LoggerFileConstant.SESSIONID.toString(),
LoggerFileConstant.REGISTRATIONID.toString(),
registrationId,
"packetCreatedOn not found in packet idSchemaVersion " + schemaVersion
+ ". Skipping retrieveCreatedDateFromPacket.");
}
}, uinExecutor);

Map<String, String> fieldMap = packetManagerService.getFields(registrationId,
defaultFields, registrationStatusDto.getRegistrationType(), ProviderStageName.CREATE_DRAFT);
Expand All @@ -268,9 +254,7 @@ public MessageDTO process(MessageDTO object) {
String packetCreatedOn = null;
try {
uinField = uinFuture.join();
if (createdOnFuture != null) {
packetCreatedOn = createdOnFuture.join();
}
packetCreatedOn = createdOnFuture.join();
} catch (CompletionException e) {
Throwable cause = e.getCause();
while (cause instanceof CompletionException && cause.getCause() != null) cause = cause.getCause();
Expand All @@ -289,7 +273,19 @@ public MessageDTO process(MessageDTO object) {

loadDemographicIdentity(fieldMap, demographicIdentity);

updatePacketCreatedOnInDemographicIdentity(registrationId, registrationStatusDto, demographicIdentity, object, packetCreatedOn);
// Store packetCreatedOn in demographic identity only for NEW/UPDATE when the ID schema includes the field.
if (defaultFields.contains(MappingJsonConstants.PACKET_CREATED_ON)
&& (RegistrationType.NEW.toString().equalsIgnoreCase(object.getReg_type())
|| RegistrationType.UPDATE.toString().equalsIgnoreCase(object.getReg_type()))) {
updatePacketCreatedOnInDemographicIdentity(registrationId, registrationStatusDto, demographicIdentity, object, packetCreatedOn);
} else {
regProcLogger.info(
LoggerFileConstant.SESSIONID.toString(),
LoggerFileConstant.REGISTRATIONID.toString(),
registrationId,
"Skipping update of packetCreatedOn in demographic identity. packet type : "
+ object.getReg_type() + ", idSchemaVersion : " + schemaVersion);
}

if (StringUtils.isEmpty(uinField) || uinField.equalsIgnoreCase("null") ) {

Expand Down Expand Up @@ -1226,7 +1222,6 @@ private void updatePacketCreatedOnInDemographicIdentity(String registrationId,
InternalRegistrationStatusDto registrationStatusDto,
Map<String, Object> demographicIdentity, MessageDTO object,
String packetCreatedOn) throws IOException {
// packetCreatedOn is only fetched for NEW and UPDATE — null means not applicable
if (packetCreatedOn == null) {
regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), registrationId,
"Unable to find the packetCreatedOn from packet for registrationType: {}. Skipping update of packetCreatedOn. ", object.getReg_type());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,10 @@ public void testLostPacket_IgnoresStaleCheck() throws Exception {

MessageDTO result = createDraftStage.process(messageDTO);

// LOST path does not run handleStaleCheck — draft update still proceeds
// LOST path does not run handleStaleCheck or retrieve packetCreatedOn — draft update still proceeds
assertTrue(result.getIsValid());
verify(utility, never()).isLatestPacket(nullable(String.class), nullable(String.class), anyString());
verify(utility, never()).retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class));
verify(idrepoDraftService, times(1)).idrepoUpdateDraftV2(eq(REG_ID), isNull(), any(), eq(false));
}

Expand Down Expand Up @@ -852,6 +853,8 @@ public void testUinFieldStringNull_UsesNewDraftPath() throws Exception {

// -----------------------------------------------------------------------
// packetCreatedOn
// Always fetched for stale check. Stored in demographic identity only for
// NEW/UPDATE when the ID schema default fields include PACKET_CREATED_ON.
// -----------------------------------------------------------------------

@Test
Expand All @@ -868,6 +871,7 @@ public void testNewPacketFetchesPacketCreatedOnWhenInSchema() throws Exception {
assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("NEW"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(isNull(), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
assertEquals("2019-01-17T06:29:01.940Z", captureNewPacketIdentity().get("packetCreatedOn"));
}

Expand All @@ -887,57 +891,123 @@ public void testUpdatePacketFetchesPacketCreatedOnWhenInSchema() throws Exceptio
assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("UPDATE"),
eq(ProviderStageName.CREATE_DRAFT));
ArgumentCaptor<IdRequestDto> requestCaptor = ArgumentCaptor.forClass(IdRequestDto.class);
verify(idrepoDraftService).idrepoUpdateDraftV2(eq(REG_ID), eq(EXISTING_UIN), requestCaptor.capture(), eq(true));
JSONObject identity = (JSONObject) requestCaptor.getValue().getRequest().getIdentity();
verify(utility).isLatestPacket(eq(EXISTING_UIN), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
JSONObject identity = captureUpdatePacketIdentity();
assertEquals("2019-01-17T06:29:01.940Z", identity.get("packetCreatedOn"));
}

@Test
public void testResUpdateDoesNotFetchPacketCreatedOn() throws Exception {
public void testNewPacketFetchesPacketCreatedOnWhenNotInSchema_DoesNotAddToIdentity() throws Exception {
messageDTO.setReg_type("NEW");
when(idSchemaUtil.getDefaultFields(anyDouble())).thenReturn(Arrays.asList("fullName", "dateOfBirth"));
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenReturn("2019-01-17T06:29:01.940Z");

MessageDTO result = createDraftStage.process(messageDTO);

assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("NEW"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(isNull(), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
verify(utility, never()).getMappedFieldName(MappingJsonConstants.PACKET_CREATED_ON);
JSONObject identity = captureNewPacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
public void testUpdatePacketFetchesPacketCreatedOnWhenNotInSchema_DoesNotAddToIdentity() throws Exception {
messageDTO.setReg_type("UPDATE");
registrationStatusDto.setRegistrationType("UPDATE");
when(utility.getUIn(anyString(), anyString(), any(ProviderStageName.class))).thenReturn(EXISTING_UIN);
when(idSchemaUtil.getDefaultFields(anyDouble())).thenReturn(Arrays.asList("fullName", "dateOfBirth"));
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenReturn("2019-01-17T06:29:01.940Z");

MessageDTO result = createDraftStage.process(messageDTO);

assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("UPDATE"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(eq(EXISTING_UIN), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
verify(utility, never()).getMappedFieldName(MappingJsonConstants.PACKET_CREATED_ON);
JSONObject identity = captureUpdatePacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
public void testResUpdateFetchesPacketCreatedOnForStaleCheck_DoesNotAddToIdentityEvenWhenInSchema() throws Exception {
messageDTO.setReg_type("RES_UPDATE");
registrationStatusDto.setRegistrationType("RES_UPDATE");
when(utility.getUIn(anyString(), anyString(), any(ProviderStageName.class))).thenReturn(EXISTING_UIN);
when(idSchemaUtil.getDefaultFields(anyDouble()))
.thenReturn(Arrays.asList(MappingJsonConstants.PACKET_CREATED_ON));
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenReturn("2019-01-17T06:29:01.940Z");

createDraftStage.process(messageDTO);
MessageDTO result = createDraftStage.process(messageDTO);

verify(utility, never()).retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class));
assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("RES_UPDATE"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(eq(EXISTING_UIN), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
verify(utility, never()).getMappedFieldName(MappingJsonConstants.PACKET_CREATED_ON);
JSONObject identity = captureUpdatePacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
public void testActivatedDoesNotFetchPacketCreatedOn() throws Exception {
public void testActivatedFetchesPacketCreatedOnForStaleCheck_DoesNotAddToIdentityEvenWhenInSchema() throws Exception {
messageDTO.setReg_type("ACTIVATED");
registrationStatusDto.setRegistrationType("ACTIVATED");
when(utility.getUIn(anyString(), anyString(), any(ProviderStageName.class))).thenReturn(EXISTING_UIN);
when(idSchemaUtil.getDefaultFields(anyDouble()))
.thenReturn(Arrays.asList(MappingJsonConstants.PACKET_CREATED_ON));
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenReturn("2019-01-17T06:29:01.940Z");
when(registrationProcessorRestClientService.getApi(eq(ApiName.IDREPOGETIDBYUIN), any(), anyString(), anyString(),
eq(IdResponseDTO.class))).thenReturn(idResponseWithStatus("DEACTIVATED"));
when(idrepoDraftService.idrepoUpdateDraftV2(anyString(), eq(EXISTING_UIN), any(), eq(true)))
.thenReturn(idResponseWithStatus("ACTIVATED"));

createDraftStage.process(messageDTO);
MessageDTO result = createDraftStage.process(messageDTO);

verify(utility, never()).retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class));
assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("ACTIVATED"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(eq(EXISTING_UIN), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
verify(utility, never()).getMappedFieldName(MappingJsonConstants.PACKET_CREATED_ON);
JSONObject identity = captureUpdatePacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
public void testDeactivatedDoesNotFetchPacketCreatedOn() throws Exception {
public void testDeactivatedFetchesPacketCreatedOnForStaleCheck_DoesNotAddToIdentityEvenWhenInSchema() throws Exception {
messageDTO.setReg_type("DEACTIVATED");
registrationStatusDto.setRegistrationType("DEACTIVATED");
when(utility.getUIn(anyString(), anyString(), any(ProviderStageName.class))).thenReturn(EXISTING_UIN);
when(idSchemaUtil.getDefaultFields(anyDouble()))
.thenReturn(Arrays.asList(MappingJsonConstants.PACKET_CREATED_ON));
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenReturn("2019-01-17T06:29:01.940Z");
when(registrationProcessorRestClientService.getApi(eq(ApiName.IDREPOGETIDBYUIN), any(), anyString(), anyString(),
eq(IdResponseDTO.class))).thenReturn(idResponseWithStatus("ACTIVATED"));
when(idrepoDraftService.idrepoUpdateDraftV2(anyString(), eq(EXISTING_UIN), any(), eq(true)))
.thenReturn(idResponseWithStatus("DEACTIVATED"));

createDraftStage.process(messageDTO);
MessageDTO result = createDraftStage.process(messageDTO);

verify(utility, never()).retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class));
assertTrue(result.getIsValid());
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("DEACTIVATED"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(eq(EXISTING_UIN), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
verify(utility, never()).getMappedFieldName(MappingJsonConstants.PACKET_CREATED_ON);
JSONObject identity = captureUpdatePacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
Expand All @@ -951,14 +1021,36 @@ public void testPacketCreatedOnSkippedWhenMappingMissing() throws Exception {

createDraftStage.process(messageDTO);

assertNull(captureNewPacketIdentity().get("packetCreatedOn"));
verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("NEW"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(isNull(), eq("2019-01-17T06:29:01.940Z"), eq(REG_ID));
JSONObject identity = captureNewPacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
public void testRetrieveCreatedDateThrows_MarksPacketManagerReprocess() throws Exception {
public void testPacketCreatedOnSkippedWhenValueMissingEvenIfInSchema() throws Exception {
messageDTO.setReg_type("NEW");
when(idSchemaUtil.getDefaultFields(anyDouble()))
.thenReturn(Arrays.asList(MappingJsonConstants.PACKET_CREATED_ON));
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenReturn(null);

createDraftStage.process(messageDTO);

verify(utility, times(1)).retrieveCreatedDateFromPacket(eq(REG_ID), eq("NEW"),
eq(ProviderStageName.CREATE_DRAFT));
verify(utility).isLatestPacket(isNull(), isNull(String.class), eq(REG_ID));
verify(utility, never()).getMappedFieldName(MappingJsonConstants.PACKET_CREATED_ON);
JSONObject identity = captureNewPacketIdentity();
assertFalse(identity.containsKey("packetCreatedOn"));
assertNull(identity.get("packetCreatedOn"));
}

@Test
public void testRetrieveCreatedDateThrows_MarksPacketManagerReprocess() throws Exception {
messageDTO.setReg_type("NEW");
when(utility.retrieveCreatedDateFromPacket(anyString(), anyString(), any(ProviderStageName.class)))
.thenThrow(new PacketManagerException("RPR-PKM-001", "metaInfo failed"));

Expand Down Expand Up @@ -1454,6 +1546,12 @@ private JSONObject captureNewPacketIdentity() throws Exception {
return (JSONObject) requestCaptor.getValue().getRequest().getIdentity();
}

private JSONObject captureUpdatePacketIdentity() throws Exception {
ArgumentCaptor<IdRequestDto> requestCaptor = ArgumentCaptor.forClass(IdRequestDto.class);
verify(idrepoDraftService).idrepoUpdateDraftV2(eq(REG_ID), eq(EXISTING_UIN), requestCaptor.capture(), eq(true));
return (JSONObject) requestCaptor.getValue().getRequest().getIdentity();
}

@SuppressWarnings("unchecked")
private void putIdentityMapping(String mappingKey, String actualFieldName) {
LinkedHashMap<String, Object> inner = new LinkedHashMap<>();
Expand Down
Loading