Skip to content

Commit

Permalink
CNDIT 1558: RTR increase code coverage (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sveselev authored Sep 5, 2024
1 parent a5a4e56 commit 0fc8f8a
Show file tree
Hide file tree
Showing 74 changed files with 1,212 additions and 1,274 deletions.
7 changes: 7 additions & 0 deletions investigation-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ jacocoTestReport {
csv.required = false
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}

afterEvaluate {
classDirectories.from = files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ['**/*_$$_javassist_*', '**/LogDynamic*.class', '**/*Application.class', '**/*DataSourceConfig.class'])
})
}
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions investigation-service/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import jakarta.persistence.*;
import lombok.Data;


@Data
@Entity
@Table(name = "nrt_page_case_answer")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package gov.cdc.etldatapipeline.investigation.repository.model.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class InvestigationConfirmationMethod {
private Long publicHealthCaseUid;
private String confirmationMethodCd;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package gov.cdc.etldatapipeline.investigation.repository.model.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class InvestigationConfirmationMethodKey {

private Long publicHealthCaseUid;;
private Long publicHealthCaseUid;
private String confirmationMethodCd;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Data;

@Entity
@Data
public class InvestigationNotifications {
@JsonProperty("source_act_uid")
@Id
@Column(name = "source_act_uid")
private Long sourceActUid;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package gov.cdc.etldatapipeline.investigation.repository.model.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class InvestigationObservation {
private Long publicHealthCaseUid;
private Long observationId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package gov.cdc.etldatapipeline.investigation.repository.model.reporting;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class InvestigationReporting {
private Long publicHealthCaseUid;
private Long programJurisdictionOid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void processMessage(String message,
processInvestigation(message);
}

public String processInvestigation(String value) {
public void processInvestigation(String value) {
String publicHealthCaseUid = "";
try {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
Expand All @@ -110,7 +110,6 @@ public String processInvestigation(String value) {
logger.info("Investigation data (uid={}) sent to {}", investigation.getPublicHealthCaseUid(), investigationTopicReporting))
.thenRunAsync(() -> processDataUtil.processNotifications(investigation.getInvestigationNotifications(), objectMapper))
.join();
return objectMapper.writeValueAsString(investigation);
} else {
throw new NoDataException("No investigation data found for id: " + publicHealthCaseUid);
}
Expand All @@ -124,7 +123,6 @@ public String processInvestigation(String value) {
logger.error(msg, e.getMessage());
throw new RuntimeException(e);
}
return null;
}

private void processPhcFactDatamart(String publicHealthCaseUid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gov.cdc.etldatapipeline.investigation.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import gov.cdc.etldatapipeline.commonutil.json.CustomJsonGeneratorImpl;
import gov.cdc.etldatapipeline.investigation.repository.model.dto.*;
import gov.cdc.etldatapipeline.investigation.repository.rdb.InvestigationCaseAnswerRepository;
import lombok.Setter;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
Expand All @@ -13,13 +15,12 @@
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
@Setter
public class ProcessInvestigationDataUtil {
private static final Logger logger = LoggerFactory.getLogger(ProcessInvestigationDataUtil.class);

Expand Down Expand Up @@ -56,10 +57,10 @@ public InvestigationTransformed transformInvestigationData(Investigation investi

public void processNotifications(String investigationNotifications, ObjectMapper objectMapper) {
try {
JsonNode investigationNotificationsJsonArray = investigationNotifications != null ? objectMapper.readTree(investigationNotifications) : null;
InvestigationNotificationsKey investigationNotificationsKey = new InvestigationNotificationsKey();
JsonNode investigationNotificationsJsonArray = parseJsonArray(investigationNotifications, objectMapper);

if (investigationNotificationsJsonArray != null && investigationNotificationsJsonArray.isArray()) {
if (investigationNotificationsJsonArray != null) {
InvestigationNotificationsKey investigationNotificationsKey = new InvestigationNotificationsKey();
for (JsonNode node : investigationNotificationsJsonArray) {
Long notificationUid = node.get("notification_uid").asLong();
investigationNotificationsKey.setNotificationUid(notificationUid);
Expand All @@ -80,25 +81,25 @@ public void processNotifications(String investigationNotifications, ObjectMapper
}
}


private void transformPersonParticipations(String personParticipations, InvestigationTransformed investigationTransformed, ObjectMapper objectMapper) {
try {
JsonNode personParticipationsJsonArray = personParticipations != null ? objectMapper.readTree(personParticipations) : null;
JsonNode personParticipationsJsonArray = parseJsonArray(personParticipations, objectMapper);

if(personParticipationsJsonArray != null && personParticipationsJsonArray.isArray()) {
for(JsonNode node : personParticipationsJsonArray) {
if (personParticipationsJsonArray != null) {
for (JsonNode node : personParticipationsJsonArray) {
String typeCode = node.get("type_cd").asText();
String subjectClassCode = node.get("subject_class_cd").asText();
String personCode = node.get("person_cd").asText();
Long entityId = node.get("entity_id").asLong();

if(typeCode.equals("InvestgrOfPHC") && subjectClassCode.equals("PSN") && personCode.equals("PRV")) {
investigationTransformed.setInvestigatorId(node.get("entity_id").asLong());
if (typeCode.equals("InvestgrOfPHC") && subjectClassCode.equals("PSN") && personCode.equals("PRV")) {
investigationTransformed.setInvestigatorId(entityId);
}
if(typeCode.equals("PhysicianOfPHC") && subjectClassCode.equals("PSN") && personCode.equals("PRV")) {
investigationTransformed.setPhysicianId(node.get("entity_id").asLong());
if (typeCode.equals("PhysicianOfPHC") && subjectClassCode.equals("PSN") && personCode.equals("PRV")) {
investigationTransformed.setPhysicianId(entityId);
}
if(typeCode.equals("SubjOfPHC") && subjectClassCode.equals("PSN") && personCode.equals("PAT")) {
investigationTransformed.setPatientId(node.get("entity_id").asLong());
if (typeCode.equals("SubjOfPHC") && subjectClassCode.equals("PSN") && personCode.equals("PAT")) {
investigationTransformed.setPatientId(entityId);
}
}
}
Expand All @@ -112,9 +113,9 @@ private void transformPersonParticipations(String personParticipations, Investig

private void transformOrganizationParticipations(String organizationParticipations, InvestigationTransformed investigationTransformed, ObjectMapper objectMapper) {
try {
JsonNode organizationParticipationsJsonArray = organizationParticipations != null ? objectMapper.readTree(organizationParticipations) : null;
JsonNode organizationParticipationsJsonArray = parseJsonArray(organizationParticipations, objectMapper);

if(organizationParticipationsJsonArray != null && organizationParticipationsJsonArray.isArray()) {
if(organizationParticipationsJsonArray != null) {
for(JsonNode node : organizationParticipationsJsonArray) {
String typeCode = node.get("type_cd").asText();
String subjectClassCode = node.get("subject_class_cd").asText();
Expand All @@ -134,21 +135,22 @@ private void transformOrganizationParticipations(String organizationParticipatio

private void transformActIds(String actIds, InvestigationTransformed investigationTransformed, ObjectMapper objectMapper) {
try {
JsonNode actIdsJsonArray = actIds != null ? objectMapper.readTree(actIds) : null;
JsonNode actIdsJsonArray = parseJsonArray(actIds, objectMapper);

if(actIdsJsonArray != null && actIdsJsonArray.isArray()) {
if(actIdsJsonArray != null) {
for(JsonNode node : actIdsJsonArray) {
int actIdSeq = node.get("act_id_seq").asInt();
String typeCode = node.get("type_cd").asText();
String rootExtension = node.get("root_extension_txt").asText();

if(typeCode.equals("STATE") && actIdSeq == 1) {
investigationTransformed.setInvStateCaseId(node.get("root_extension_txt").asText());
investigationTransformed.setInvStateCaseId(rootExtension);
}
if(typeCode.equals("CITY") && actIdSeq == 2) {
investigationTransformed.setCityCountyCaseNbr(node.get("root_extension_txt").asText());
investigationTransformed.setCityCountyCaseNbr(rootExtension);
}
if(typeCode.equals("LEGACY") && actIdSeq == 3) {
investigationTransformed.setLegacyCaseId(node.get("root_extension_txt").asText());
investigationTransformed.setLegacyCaseId(rootExtension);
}
}
}
Expand All @@ -162,11 +164,11 @@ private void transformActIds(String actIds, InvestigationTransformed investigati

private void transformObservationIds(String observationNotificationIds, InvestigationTransformed investigationTransformed, ObjectMapper objectMapper) {
try {
JsonNode investigationObservationIdsJsonArray = observationNotificationIds != null ? objectMapper.readTree(observationNotificationIds) : null;
JsonNode investigationObservationIdsJsonArray = parseJsonArray(observationNotificationIds, objectMapper);
InvestigationObservation investigationObservation = new InvestigationObservation();
List<Long> observationIds = new ArrayList<>();

if(investigationObservationIdsJsonArray != null && investigationObservationIdsJsonArray.isArray()) {
if(investigationObservationIdsJsonArray != null) {
for(JsonNode node : investigationObservationIdsJsonArray) {
String sourceClassCode = node.get("source_class_cd").asText();
String actTypeCode = node.get("act_type_cd").asText();
Expand Down Expand Up @@ -199,47 +201,35 @@ private void transformObservationIds(String observationNotificationIds, Investig

private void transformInvestigationConfirmationMethod(String investigationConfirmationMethod, ObjectMapper objectMapper) {
try {
JsonNode investigationConfirmationMethodJsonArray = investigationConfirmationMethod != null ? objectMapper.readTree(investigationConfirmationMethod) : null;
InvestigationConfirmationMethodKey investigationConfirmationMethodKey = new InvestigationConfirmationMethodKey();
InvestigationConfirmationMethod investigationConfirmation = new InvestigationConfirmationMethod();
Long publicHealthCaseUid;
Map<String, String> confirmationMethodMap = new HashMap<>();
Instant confirmationMethodTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
JsonNode investigationConfirmationMethodJsonArray = parseJsonArray(investigationConfirmationMethod, objectMapper);

// Redundant time variable in case if confirmation_method_time is null in all rows of the array
String phcLastChgTime = null;
if(investigationConfirmationMethodJsonArray != null) {
InvestigationConfirmationMethodKey investigationConfirmationMethodKey = new InvestigationConfirmationMethodKey();
InvestigationConfirmationMethod investigationConfirmation = new InvestigationConfirmationMethod();
Map<String, String> confirmationMethodMap = new HashMap<>();
String confirmationMethodTime = null;

// Redundant time variable in case if confirmation_method_time is null in all rows of the array
String phcLastChgTime = investigationConfirmationMethodJsonArray.get(0).get("phc_last_chg_time").asText();
Long publicHealthCaseUid = investigationConfirmationMethodJsonArray.get(0).get("public_health_case_uid").asLong();

if(investigationConfirmationMethodJsonArray != null && investigationConfirmationMethodJsonArray.isArray()) {
publicHealthCaseUid = investigationConfirmationMethodJsonArray.get(0).get("public_health_case_uid").asLong();
phcLastChgTime = investigationConfirmationMethodJsonArray.get(0).get("phc_last_chg_time").asText();
for(JsonNode node : investigationConfirmationMethodJsonArray) {
String confirmationMethodTimeString = node.get("confirmation_method_time").asText();
Instant currentInstant = null;

// While getting data from JSON node, it is considered as literal String and that is why the null check
// has equals for null String instead of null value.
if(confirmationMethodTimeString != null && !confirmationMethodTimeString.equals("null")) {
Date dateTime = sdf.parse(confirmationMethodTimeString);
currentInstant = dateTime.toInstant();
}
if (confirmationMethodTime == null || currentInstant.isAfter(confirmationMethodTime)) {
confirmationMethodTime = currentInstant;
JsonNode timeNode = node.get("confirmation_method_time");
if (timeNode != null && !timeNode.isNull()) {
confirmationMethodTime = timeNode.asText();
}
confirmationMethodMap.put(node.get("confirmation_method_cd").asText(), node.get("confirmation_method_desc_txt").asText());
}
investigationConfirmation.setPublicHealthCaseUid(publicHealthCaseUid);
investigationConfirmationMethodKey.setPublicHealthCaseUid(publicHealthCaseUid);

investigationConfirmation.setConfirmationMethodTime(
confirmationMethodTime == null ? phcLastChgTime : confirmationMethodTime);

if(confirmationMethodTime == null) {
investigationConfirmation.setConfirmationMethodTime(phcLastChgTime);
}
for(String key : confirmationMethodMap.keySet()) {
investigationConfirmation.setConfirmationMethodCd(key);
investigationConfirmation.setConfirmationMethodDescTxt(confirmationMethodMap.get(key));
investigationConfirmationMethodKey.setConfirmationMethodCd(key);
for(Map.Entry<String, String> entry : confirmationMethodMap.entrySet()) {
investigationConfirmation.setConfirmationMethodCd(entry.getKey());
investigationConfirmation.setConfirmationMethodDescTxt(entry.getValue());
investigationConfirmationMethodKey.setConfirmationMethodCd(entry.getKey());
String jsonKey = jsonGenerator.generateStringJson(investigationConfirmationMethodKey);
String jsonValue = jsonGenerator.generateStringJson(investigationConfirmation);
kafkaTemplate.send(investigationConfirmationOutputTopicName, jsonKey, jsonValue);
Expand All @@ -255,9 +245,9 @@ private void transformInvestigationConfirmationMethod(String investigationConfir

private void processInvestigationPageCaseAnswer(String investigationCaseAnswer, InvestigationTransformed investigationTransformed, ObjectMapper objectMapper) {
try {
JsonNode investigationCaseAnswerJsonArray = investigationCaseAnswer != null ? objectMapper.readTree(investigationCaseAnswer) : null;
JsonNode investigationCaseAnswerJsonArray = parseJsonArray(investigationCaseAnswer, objectMapper);

if(investigationCaseAnswerJsonArray != null && investigationCaseAnswerJsonArray.isArray()) {
if(investigationCaseAnswerJsonArray != null) {
Long actUid = investigationCaseAnswerJsonArray.get(0).get("act_uid").asLong();
List<InvestigationCaseAnswer> investigationCaseAnswerDataIfPresent = investigationCaseAnswerRepository.findByActUid(actUid);
List<InvestigationCaseAnswer> investigationCaseAnswerList = new ArrayList<>();
Expand Down Expand Up @@ -288,4 +278,13 @@ private void processInvestigationPageCaseAnswer(String investigationCaseAnswer,
logger.error("Error processing investigation case answer JSON array from investigation data: {}", e.getMessage());
}
}

private JsonNode parseJsonArray(String jsonString, ObjectMapper objectMapper) throws JsonProcessingException {
JsonNode jsonArray = jsonString != null ? objectMapper.readTree(jsonString) : null;
if (jsonArray != null) {
return jsonArray.isArray() ? jsonArray : null;
} else {
return null;
}
}
}
Loading

0 comments on commit 0fc8f8a

Please sign in to comment.