Skip to content

Commit

Permalink
Merge pull request #1786 from Khuddusshariff0022/develop-Mosip_27859
Browse files Browse the repository at this point in the history
[ MOSIP-29896 ] Adding pkt_cr_dtimes and setting the packet date and …
  • Loading branch information
vishwa-vyom authored Nov 7, 2023
2 parents 85d3492 + 6602518 commit 770217f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.mosip.kernel.core.util.DateUtils;
import io.mosip.registration.processor.packet.storage.utils.Utilities;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
Expand Down Expand Up @@ -168,11 +172,13 @@ public class PacketValidateProcessor {
@Autowired
private NotificationUtility notificationUtility;

@Value("${mosip.registration.processor.datetime.pattern}")
private String dateformat;

public MessageDTO process(MessageDTO object, String stageName) {
TrimExceptionMessage trimMessage = new TrimExceptionMessage();
LogDescription description = new LogDescription();
PacketValidationDto packetValidationDto = new PacketValidationDto();

InternalRegistrationStatusDto registrationStatusDto = new InternalRegistrationStatusDto();
try {
registrationStatusDto
Expand All @@ -185,10 +191,9 @@ public MessageDTO process(MessageDTO object, String stageName) {
"", "PacketValidateProcessor::process()::entry");
registrationId = object.getRid();
packetValidationDto.setTransactionSuccessful(false);

registrationStatusDto = registrationStatusService.getRegistrationStatus(
registrationId, object.getReg_type(), object.getIteration(), object.getWorkflowInstanceId());

setPacketCreatedDateTime(registrationStatusDto);
registrationStatusDto
.setLatestTransactionTypeCode(RegistrationTransactionTypeCode.VALIDATE_PACKET.toString());
registrationStatusDto.setRegistrationStageName(stageName);
Expand Down Expand Up @@ -447,6 +452,33 @@ public MessageDTO process(MessageDTO object, String stageName) {

}


private void setPacketCreatedDateTime(InternalRegistrationStatusDto registrationStatusDto) throws ApisResourceAccessException, PacketManagerException, JsonProcessingException, IOException {
try {
Map<String, String> metaInfo = packetManagerService.getMetaInfo(
registrationStatusDto.getRefId(), registrationStatusDto.getRegistrationType(), ProviderStageName.PACKET_VALIDATOR);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateformat);
String packetCreatedDateTime = metaInfo.get(JsonConstant.CREATIONDATE);
if (packetCreatedDateTime != null && !packetCreatedDateTime.isEmpty()) {
LocalDateTime dateTime = DateUtils.parseToLocalDateTime(packetCreatedDateTime);
registrationStatusDto.setPacketCreateDateTime(dateTime);
} else {
regProcLogger.warn(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(),
" -- " + registrationStatusDto.getRefId(),
PlatformErrorMessages.RPR_PVM_PACKET_CREATED_DATE_TIME_EMPTY_OR_NULL.getMessage());
}
} catch (DateTimeParseException e) {
regProcLogger.warn(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(),
" -- " + registrationStatusDto.getRefId(),
PlatformErrorMessages.RPR_PVM_PACKET_CREATED_DATE_TIME_PARSE_EXCEPTION.getMessage() + e.getMessage());
}catch (IllegalArgumentException ex)
{
regProcLogger.warn(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(),
" -- " + registrationStatusDto.getRefId(),
PlatformErrorMessages.RPR_PVM_INVALID_ARGUMENT_EXCEPTION.getMessage() + ex.getMessage());
}
}

private boolean isValidSupervisorStatus(MessageDTO messageDTO) {
SyncRegistrationEntity regEntity = syncRegistrationService.findByWorkflowInstanceId(messageDTO.getWorkflowInstanceId());
if (regEntity.getSupervisorStatus().equalsIgnoreCase(APPROVED)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@
import io.mosip.registration.processor.status.service.SyncRegistrationService;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand All @@ -64,17 +62,19 @@
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyBoolean;
import static org.junit.Assert.*;


/**
Expand Down Expand Up @@ -127,6 +127,9 @@ public class PacketValidateProcessorTest {

@Mock
private NotificationUtility notificationUtility;

@Mock
DateTimeFormatter dateTimeFormatter;

private MessageDTO messageDTO;
private String stageName;
Expand All @@ -136,6 +139,7 @@ public class PacketValidateProcessorTest {
@Before
public void setup() throws Exception {
ReflectionTestUtils.setField(packetValidateProcessor, "notificationTypes", "SMS|EMAIL");
ReflectionTestUtils.setField(packetValidateProcessor, "dateformat", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
messageDTO=new MessageDTO();
messageDTO.setRid("123456789");
messageDTO.setInternalError(false);
Expand Down Expand Up @@ -245,43 +249,64 @@ public void setup() throws Exception {
Map<String, String> metamap = new HashMap<>();
org.json.JSONArray jsonArray = new org.json.JSONArray();
org.json.JSONObject jsonObject1 = new org.json.JSONObject();
metamap.put(JsonConstant.CREATIONDATE,"2023-10-17T03:01:09.893");
metamap.put("creationDate","2023-10-17T03:01:09.893Z");
jsonObject1.put("preRegistrationId", "12345");
jsonArray.put(0, jsonObject1);
metamap.put(JsonConstant.METADATA, jsonArray.toString());
Mockito.when(packetManagerService.getMetaInfo(anyString(), any(), any())).thenReturn(metamap);

Mockito.when(packetManagerService.getMetaInfo(any(), any(), any())).thenReturn(metamap);
Mockito.when(objectMapper.readValue(anyString(), any(Class.class))).thenReturn(new FieldValue("preRegistrationId", "12345"));
}


@Test
public void PacketValidationSuccessTest() throws PacketManagerException, ApisResourceAccessException, IOException, JsonProcessingException {
Map<String, String> metainfo1 = new HashMap<>();
metainfo1.put(JsonConstant.CREATIONDATE,"2023-10-17T03:01:09.893");
MessageDTO object = packetValidateProcessor.process(messageDTO, stageName);
ArgumentCaptor<InternalRegistrationStatusDto> argument = ArgumentCaptor
.forClass(InternalRegistrationStatusDto.class);
Mockito.verify(registrationStatusService,Mockito.atLeastOnce()).updateRegistrationStatus(argument.capture(), Mockito.any(),
Mockito.any());
Assert.assertEquals(LocalDateTime.parse(metainfo1.get(JsonConstant.CREATIONDATE)), argument.getAllValues().get(0).getPacketCreateDateTime());
Assert.assertTrue(object.getIsValid());
Assert.assertFalse(object.getInternalError());
}

@Test
public void PacketValidationSuccessTest() {
public void PacketValidationSuccessTestwithPacketCreatedDateTimeNull() throws PacketManagerException, ApisResourceAccessException, IOException, JsonProcessingException {
Map<String, String> metainfo = new HashMap<>();
metainfo.put(JsonConstant.CREATIONDATE,null);
Mockito.when(packetManagerService.getMetaInfo(any(), any(), any())).thenReturn(metainfo);
MessageDTO object = packetValidateProcessor.process(messageDTO, stageName);
ArgumentCaptor<InternalRegistrationStatusDto> argument = ArgumentCaptor
.forClass(InternalRegistrationStatusDto.class);
Mockito.verify(registrationStatusService,Mockito.atLeastOnce()).updateRegistrationStatus(argument.capture(), Mockito.any(),
Mockito.any());
assertEquals(metainfo.get(JsonConstant.CREATIONDATE), argument.getAllValues().get(0).getPacketCreateDateTime());
assertTrue(object.getIsValid());
assertFalse(object.getInternalError());
}

@Test
public void PacketValidationFailureTest() throws PacketValidatorException, ApisResourceAccessException, JsonProcessingException, RegistrationProcessorCheckedException, IOException, PacketManagerException {
registrationStatusDto.setRetryCount(1);
Mockito.when(packetValidator.validate(any(), any(),any())).thenReturn(false);
public void PacketValidationSuccessTestwithPacketCreatedDateTimeInvalidFormat() throws PacketManagerException, ApisResourceAccessException, IOException, JsonProcessingException {
Map<String, String> metainfo = new HashMap<>();
metainfo.put(JsonConstant.CREATIONDATE,"2023-10-1703:01:09.893");
Mockito.when(packetManagerService.getMetaInfo(any(), any(), any())).thenReturn(metainfo);
MessageDTO object = packetValidateProcessor.process(messageDTO, stageName);
assertFalse(object.getIsValid());
assertFalse(object.getInternalError());
ArgumentCaptor<InternalRegistrationStatusDto> argument = ArgumentCaptor
.forClass(InternalRegistrationStatusDto.class);
Mockito.verify(registrationStatusService,Mockito.atLeastOnce()).updateRegistrationStatus(argument.capture(),any(),any());
assertEquals(null, argument.getAllValues().get(0).getPacketCreateDateTime());
}

@Test
public void invalidSupervisorStatusTest() throws PacketValidatorException {
public void PacketValidationFailureTest() throws PacketValidatorException, ApisResourceAccessException, JsonProcessingException, RegistrationProcessorCheckedException, IOException, PacketManagerException {
registrationStatusDto.setRetryCount(1);
regEntity=new SyncRegistrationEntity();
regEntity.setSupervisorStatus("REJECTED");
Mockito.when(syncRegistrationService.findByWorkflowInstanceId(any())).thenReturn(regEntity);
Mockito.when(packetValidator.validate(any(), any(),any())).thenReturn(false);
MessageDTO object = packetValidateProcessor.process(messageDTO, stageName);
assertFalse(object.getIsValid());
assertFalse(object.getInternalError());
}

@Test
public void PacketValidationPacketManagerFailedTest()
throws ApisResourceAccessException, PacketManagerException, JsonProcessingException, IOException {
Expand All @@ -293,7 +318,17 @@ public void PacketValidationPacketManagerFailedTest()
assertTrue(object.getIsValid());
assertTrue(object.getInternalError());
}


@Test
public void invalidSupervisorStatusTest() throws PacketValidatorException {
registrationStatusDto.setRetryCount(1);
regEntity=new SyncRegistrationEntity();
regEntity.setSupervisorStatus("REJECTED");
Mockito.when(syncRegistrationService.findByWorkflowInstanceId(any())).thenReturn(regEntity);
MessageDTO object = packetValidateProcessor.process(messageDTO, stageName);
assertFalse(object.getIsValid());
assertFalse(object.getInternalError());
}
@Test
public void PacketValidationParsingFailedTest()
throws ApisResourceAccessException, PacketManagerException, JsonProcessingException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ public enum PlatformErrorMessages {
RPR_PVM_PACKET_REJECTED(PlatformConstants.RPR_PACKET_VALIDATOR_MODULE + "011",
"Rejected by Supervisor"),

RPR_PVM_PACKET_CREATED_DATE_TIME_EMPTY_OR_NULL(PlatformConstants.RPR_PACKET_VALIDATOR_MODULE + "017",
"Packet Created Date time is Null or Empty"),

RPR_PVM_PACKET_CREATED_DATE_TIME_PARSE_EXCEPTION(PlatformConstants.RPR_PACKET_VALIDATOR_MODULE + "018",
"Packet Created Date time is not in correct format"),

RPR_PVM_INVALID_ARGUMENT_EXCEPTION(PlatformConstants.RPR_PACKET_VALIDATOR_MODULE + "019",
"Invalid Argument"),


/** The packet classification failed. */
PACKET_CLASSIFICATION_FAILED(PlatformConstants.RPR_PACKET_CLASSIFIER_MODULE + "000", "Packet Classification failed"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public class InternalRegistrationStatusDto implements Serializable {
/** The update date time. */
private LocalDateTime updateDateTime;

/** Packet Created Date and Time**/
private LocalDateTime packetCreateDateTime;

public LocalDateTime getPacketCreateDateTime() {
return packetCreateDateTime;
}

public void setPacketCreateDateTime(LocalDateTime packetCreateDateTime) {
this.packetCreateDateTime = packetCreateDateTime;
}

/** The is deleted. */
private Boolean isDeleted;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public class RegistrationStatusEntity extends BaseRegistrationEntity<BaseRegistr

private LocalDateTime createDateTime;

public LocalDateTime getPacketCreatedDateTime() {
return packetCreatedDateTime;
}

public void setPacketCreatedDateTime(LocalDateTime packetCreatedDateTime) {
this.packetCreatedDateTime = packetCreatedDateTime;
}

/** packet created date and time */
@Column(name = "pkt_cr_dtimes")
private LocalDateTime packetCreatedDateTime;

/** The updated by. */
@Column(name = "upd_by")
private String updatedBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ private InternalRegistrationStatusDto convertEntityToDto(RegistrationStatusEntit
registrationStatusDto.setSource(entity.getSource());
registrationStatusDto.setIteration(entity.getIteration());
registrationStatusDto.setWorkflowInstanceId(entity.getId().getWorkflowInstanceId());
registrationStatusDto.setPacketCreateDateTime(entity.getPacketCreatedDateTime());
return registrationStatusDto;
}

Expand Down Expand Up @@ -702,6 +703,7 @@ private RegistrationStatusEntity convertDtoToEntity(InternalRegistrationStatusDt
registrationStatusEntity.setLastSuccessStageName(dto.getRegistrationStageName());
else
registrationStatusEntity.setLastSuccessStageName(existingLastSuccessStageName);
registrationStatusEntity.setPacketCreatedDateTime(dto.getPacketCreateDateTime());
return registrationStatusEntity;
}

Expand Down

0 comments on commit 770217f

Please sign in to comment.