Skip to content

Commit

Permalink
MAT-5716 transfer qdm test case expected values
Browse files Browse the repository at this point in the history
  • Loading branch information
CeciliaLiu8 committed Nov 16, 2023
1 parent 9e1e1da commit dd6c245
Show file tree
Hide file tree
Showing 11 changed files with 1,268 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cms.gov.madie.measure.repositories.MeasureRepository;
import cms.gov.madie.measure.services.TestCaseService;
import cms.gov.madie.measure.utils.QiCoreJsonUtil;
import cms.gov.madie.measure.utils.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import gov.cms.madie.models.common.ModelType;
import gov.cms.madie.models.measure.Measure;
Expand Down Expand Up @@ -41,7 +41,7 @@ public void updatedTestCaseJsonWithPatientUuid(
.filter(tc -> !StringUtils.isBlank(tc.getJson()))
.forEach(
testCase -> {
if (!QiCoreJsonUtil.isValidJson(testCase.getJson())) {
if (!JsonUtil.isValidJson(testCase.getJson())) {
log.warn(
"Skipping test case [{}] on measure [{}] as JSON is invalid",
testCase.getId(),
Expand Down Expand Up @@ -80,18 +80,18 @@ protected String updateJsonForUuids(
testCase.getId());
}
final String newPatientId = patientIdUuid.toString();
final String oldFullUrl = QiCoreJsonUtil.getPatientFullUrl(testCase.getJson());
final String oldFullUrl = JsonUtil.getPatientFullUrl(testCase.getJson());

// Refs update makes the assumption that the ref will start with
// Patient/
String updatedJson =
QiCoreJsonUtil.enforcePatientId(testCase, testCaseService.getMadieJsonResourcesBaseUri());
JsonUtil.enforcePatientId(testCase, testCaseService.getMadieJsonResourcesBaseUri());

final String previousJson = updatedJson;
updatedJson = QiCoreJsonUtil.replacePatientRefs(updatedJson, newPatientId);
updatedJson = JsonUtil.replacePatientRefs(updatedJson, newPatientId);

if (!StringUtils.isBlank(oldFullUrl)) {
updatedJson = QiCoreJsonUtil.replaceFullUrlRefs(updatedJson, oldFullUrl, newPatientId);
updatedJson = JsonUtil.replaceFullUrlRefs(updatedJson, oldFullUrl, newPatientId);
}

if (previousJson.equals(updatedJson)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cms.gov.madie.measure.repositories.MeasureRepository;
import cms.gov.madie.measure.services.TestCaseService;
import cms.gov.madie.measure.utils.QiCoreJsonUtil;
import cms.gov.madie.measure.utils.JsonUtil;
import gov.cms.madie.models.common.ModelType;
import gov.cms.madie.models.measure.Measure;
import gov.cms.madie.models.measure.TestCase;
Expand Down Expand Up @@ -39,7 +39,7 @@ public void updateTestCaseResourceFullUrls(
.filter(tc -> !StringUtils.isBlank(tc.getJson()))
.forEach(
testCase -> {
if (!QiCoreJsonUtil.isValidJson(testCase.getJson())) {
if (!JsonUtil.isValidJson(testCase.getJson())) {
log.warn(
"Skipping test case [{}] on measure [{}] as JSON is invalid",
testCase.getId(),
Expand All @@ -48,7 +48,7 @@ public void updateTestCaseResourceFullUrls(
}
try {
String updatedJson =
QiCoreJsonUtil.updateResourceFullUrls(
JsonUtil.updateResourceFullUrls(
testCase, testCaseService.getMadieJsonResourcesBaseUri());
testCase.setJson(updatedJson);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cms.gov.madie.measure.exceptions.ResourceNotFoundException;
import cms.gov.madie.measure.repositories.MeasureRepository;
import cms.gov.madie.measure.services.MeasureService;
import gov.cms.madie.models.common.ModelType;
import gov.cms.madie.models.measure.Measure;
import gov.cms.madie.models.measure.TestCase;
import cms.gov.madie.measure.services.TestCaseService;
Expand All @@ -22,6 +23,7 @@
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@RestController
Expand Down Expand Up @@ -137,7 +139,25 @@ public ResponseEntity<List<TestCaseImportOutcome>> importTestCases(
Principal principal) {
final String userName = principal.getName();
var testCaseImportOutcomes =
testCaseService.importTestCases(testCaseImportRequests, measureId, userName, accessToken);
testCaseService.importTestCases(
testCaseImportRequests, measureId, userName, accessToken, ModelType.QI_CORE.getValue());
return ResponseEntity.ok().body(testCaseImportOutcomes);
}

@PutMapping(ControllerUtil.TEST_CASES + "/imports/qdm")
public ResponseEntity<List<TestCaseImportOutcome>> importTestCasesQdm(
@RequestBody List<TestCaseImportRequest> testCaseImportRequests,
@PathVariable String measureId,
@RequestHeader("Authorization") String accessToken,
Principal principal) {
final String userName = principal.getName();

for (TestCaseImportRequest request : testCaseImportRequests) {
request.setPatientId(UUID.randomUUID());
}
var testCaseImportOutcomes =
testCaseService.importTestCases(
testCaseImportRequests, measureId, userName, accessToken, ModelType.QDM_5_6.getValue());
return ResponseEntity.ok().body(testCaseImportOutcomes);
}

Expand Down
100 changes: 75 additions & 25 deletions src/main/java/cms/gov/madie/measure/services/TestCaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import gov.cms.madie.models.measure.Group;
import cms.gov.madie.measure.exceptions.*;
import cms.gov.madie.measure.repositories.MeasureRepository;
import cms.gov.madie.measure.utils.QiCoreJsonUtil;
import cms.gov.madie.measure.utils.JsonUtil;
import cms.gov.madie.measure.utils.TestCaseServiceUtil;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -245,10 +245,7 @@ public TestCase validateTestCaseAsResource(
if (ModelType.QDM_5_6.equals(modelType)) {
return testCase == null
? null
: testCase
.toBuilder()
.validResource(QiCoreJsonUtil.isValidJson(testCase.getJson()))
.build();
: testCase.toBuilder().validResource(JsonUtil.isValidJson(testCase.getJson())).build();
} else {
final HapiOperationOutcome hapiOperationOutcome = validateTestCaseJson(testCase, accessToken);
return testCase == null
Expand Down Expand Up @@ -305,11 +302,11 @@ public TestCase updateTestCase(
testCase, ModelType.valueOfName(measure.getModel()), accessToken);
if (ModelType.QI_CORE.getValue().equalsIgnoreCase(measure.getModel())) {
validatedTestCase.setJson(
QiCoreJsonUtil.enforcePatientId(validatedTestCase, madieJsonResourcesBaseUri));
JsonUtil.enforcePatientId(validatedTestCase, madieJsonResourcesBaseUri));
validatedTestCase.setJson(
QiCoreJsonUtil.updateResourceFullUrls(validatedTestCase, madieJsonResourcesBaseUri));
JsonUtil.updateResourceFullUrls(validatedTestCase, madieJsonResourcesBaseUri));
validatedTestCase.setJson(
QiCoreJsonUtil.replacePatientRefs(
JsonUtil.replacePatientRefs(
validatedTestCase.getJson(), validatedTestCase.getPatientId().toString()));
}
measure.getTestCases().add(validatedTestCase);
Expand Down Expand Up @@ -424,7 +421,8 @@ public List<TestCaseImportOutcome> importTestCases(
List<TestCaseImportRequest> testCaseImportRequests,
String measureId,
String userName,
String accessToken) {
String accessToken,
String model) {
Measure measure = findMeasureById(measureId);
Set<UUID> checkedTestCases = new HashSet<>();
return testCaseImportRequests.stream()
Expand Down Expand Up @@ -457,7 +455,7 @@ public List<TestCaseImportOutcome> importTestCases(
}
if (isEmpty(measure.getTestCases())) {
return validateTestCaseJsonAndCreateTestCase(
testCaseImportRequest, measure, userName, accessToken);
testCaseImportRequest, measure, userName, accessToken, model);
}
Optional<TestCase> existingTestCase =
measure.getTestCases().stream()
Expand All @@ -472,10 +470,11 @@ public List<TestCaseImportOutcome> importTestCases(
measureId,
userName,
accessToken,
null);
null,
model);
} else {
return validateTestCaseJsonAndCreateTestCase(
testCaseImportRequest, measure, userName, accessToken);
testCaseImportRequest, measure, userName, accessToken, model);
}
})
.toList();
Expand All @@ -485,12 +484,11 @@ private TestCaseImportOutcome validateTestCaseJsonAndCreateTestCase(
TestCaseImportRequest testCaseImportRequest,
Measure measure,
String userName,
String accessToken) {
String accessToken,
String model) {
try {
String patientFamilyName =
QiCoreJsonUtil.getPatientName(testCaseImportRequest.getJson(), "family");
String patientGivenName =
QiCoreJsonUtil.getPatientName(testCaseImportRequest.getJson(), "given");
String patientFamilyName = getPatientFamilyName(model, testCaseImportRequest.getJson());
String patientGivenName = getPatientGivenName(model, testCaseImportRequest.getJson());
log.info(
"Test Case title + Test Case Group: {}", patientGivenName + " " + patientFamilyName);
if (StringUtils.isBlank(patientGivenName)) {
Expand All @@ -506,9 +504,10 @@ private TestCaseImportOutcome validateTestCaseJsonAndCreateTestCase(
.series(patientFamilyName)
.patientId(testCaseImportRequest.getPatientId())
.build();

List<TestCaseGroupPopulation> testCaseGroupPopulations =
QiCoreJsonUtil.getTestCaseGroupPopulationsFromMeasureReport(
testCaseImportRequest.getJson());
getTestCaseGroupPopulationsFromImportRequest(
model, testCaseImportRequest.getJson(), measure);
List<Group> groups = testCaseServiceUtil.getGroupsWithValidPopulations(measure.getGroups());
boolean matched =
testCaseServiceUtil.matchCriteriaGroups(testCaseGroupPopulations, groups, newTestCase);
Expand All @@ -524,7 +523,8 @@ private TestCaseImportOutcome validateTestCaseJsonAndCreateTestCase(
measure.getId(),
userName,
accessToken,
warningMessage);
warningMessage,
model);
} catch (JsonProcessingException ex) {
log.info(
"User {} is unable to import test case with patient id : "
Expand All @@ -542,23 +542,53 @@ private TestCaseImportOutcome validateTestCaseJsonAndCreateTestCase(
}
}

private String getPatientFamilyName(String model, String json) throws JsonProcessingException {
String patientFamilyName = null;
if (ModelType.QI_CORE.getValue().equalsIgnoreCase(model)) {
patientFamilyName = JsonUtil.getPatientName(json, "family");
} else if ((ModelType.QDM_5_6.getValue().equalsIgnoreCase(model))) {
patientFamilyName = JsonUtil.getPatientNameQdm(json, "familyName");
}
return patientFamilyName;
}

private String getPatientGivenName(String model, String json) throws JsonProcessingException {
String patientGivenName = null;
if (ModelType.QI_CORE.getValue().equalsIgnoreCase(model)) {
patientGivenName = JsonUtil.getPatientName(json, "given");
} else if ((ModelType.QDM_5_6.getValue().equalsIgnoreCase(model))) {
patientGivenName = JsonUtil.getPatientNameQdm(json, "givenNames");
}
return patientGivenName;
}

private List<TestCaseGroupPopulation> getTestCaseGroupPopulationsFromImportRequest(
String model, String json, Measure measure) throws JsonProcessingException {
List<TestCaseGroupPopulation> testCaseGroupPopulations = null;
if (ModelType.QI_CORE.getValue().equalsIgnoreCase(model)) {
testCaseGroupPopulations = JsonUtil.getTestCaseGroupPopulationsFromMeasureReport(json);
} else if (ModelType.QDM_5_6.getValue().equalsIgnoreCase(model)) {
testCaseGroupPopulations = JsonUtil.getTestCaseGroupPopulationsQdm(json, measure);
}
return testCaseGroupPopulations;
}

private TestCaseImportOutcome updateTestCaseJsonAndSaveTestCase(
TestCase existingTestCase,
TestCaseImportRequest testCaseImportRequest,
String measureId,
String userName,
String accessToken,
String warningMessage) {
String warningMessage,
String model) {
TestCaseImportOutcome failureOutcome =
TestCaseImportOutcome.builder()
.patientId(testCaseImportRequest.getPatientId())
.successful(false)
.build();
try {
String description = QiCoreJsonUtil.getTestDescription(testCaseImportRequest.getJson());
existingTestCase.setJson(
QiCoreJsonUtil.removeMeasureReportFromJson(testCaseImportRequest.getJson()));
existingTestCase.setDescription(description);
existingTestCase.setDescription(getDescription(model, testCaseImportRequest.getJson()));
existingTestCase.setJson(getJson(model, testCaseImportRequest.getJson()));
TestCase updatedTestCase = updateTestCase(existingTestCase, measureId, userName, accessToken);
log.info(
"User {} successfully imported test case with patient id : {}",
Expand Down Expand Up @@ -607,6 +637,26 @@ private TestCaseImportOutcome updateTestCaseJsonAndSaveTestCase(
}
}

private String getDescription(String model, String json) throws JsonProcessingException {
String description = null;
if (ModelType.QI_CORE.getValue().equalsIgnoreCase(model)) {
description = JsonUtil.getTestDescription(json);
} else if (ModelType.QDM_5_6.getValue().equalsIgnoreCase(model)) {
description = JsonUtil.getTestDescriptionQdm(json);
}
return description;
}

private String getJson(String model, String json) throws JsonProcessingException {
String jsonFromImportRequest = null;
if (ModelType.QI_CORE.getValue().equalsIgnoreCase(model)) {
jsonFromImportRequest = JsonUtil.removeMeasureReportFromJson(json);
} else if (ModelType.QDM_5_6.getValue().equalsIgnoreCase(model)) {
jsonFromImportRequest = JsonUtil.getTestCaseJson(json);
}
return jsonFromImportRequest;
}

private String formatErrorMessage(Exception e) {
return e.getClass().getSimpleName().equals("DuplicateTestCaseNameException")
? "The Family and Given combination on the Patient resource in the Test Case JSON"
Expand Down
Loading

0 comments on commit dd6c245

Please sign in to comment.