Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates tests for converter idempotency #1205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -16,6 +16,7 @@
import org.apache.avro.Schema.Parser;
import org.apache.avro.generic.IndexedRecord;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Base;
import org.hl7.fhir.r4.model.Patient;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -116,9 +117,9 @@ public void validateR4UsCoreResourceWithExtension() throws ProfileException, IOE
loadResource(fhirContext, "/r4-us-core-resources/patient_us_core.json", Patient.class);
IndexedRecord avroRecord = patientConverter.resourceToAvro(patient);
Patient patientDecoded = (Patient) patientConverter.avroToResource(avroRecord);
// For why this is needed, see: https://github.com/google/fhir-data-pipes/issues/1003
patientDecoded.setId(patient.getIdElement());
Assert.assertTrue(patient.equalsDeep(patientDecoded));
Assert.assertTrue(
patient.equalsDeep(
(Base) TestUtil.encodeThenParse(patientDecoded, Patient.class, fhirContext)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.avro.compiler.specific.SpecificCompiler;
import org.apache.avro.generic.GenericData.Record;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.model.Base;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Condition;
Expand All @@ -43,7 +44,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

// TODO refactor the shared code with AvroConverterTest (STU3).
// TODO refactor the shared code with `Stu3AvroConverterUsCoreTest`.
public class R4AvroConverterUsCoreTest {

private static final Observation testObservation = TestData.newObservation();
Expand Down Expand Up @@ -263,8 +264,8 @@ public void testChoice() throws FHIRException {
public void testIdInNestedElement() throws FHIRException {

// Ensure that nested elements do not have id as property.
Assert.assertNotNull(testPatient.getAddress().get(0).getId());
Assert.assertNull(testPatientDecoded.getAddress().get(0).getId());
Assert.assertNull(testPatientDecoded.getName().get(0).getId());
}

/**
Expand Down Expand Up @@ -587,11 +588,8 @@ public void testCompile() throws IOException {
@Test
public void testMetaElement() {

String id = testPatient.getId();
Meta meta = testPatient.getMeta();

Assert.assertEquals(id, testPatientDecoded.getId());

Assert.assertEquals(meta.getTag().size(), testPatientDecoded.getMeta().getTag().size());
Assert.assertEquals(
meta.getTag().get(0).getCode(), testPatientDecoded.getMeta().getTag().get(0).getCode());
Expand All @@ -610,4 +608,14 @@ public void testEncounterConversions() {
public void setTestQuestionnaireResponseConversions() {
Assert.assertTrue(testQuestionnaireResponse.equalsDeep(testQuestionnaireResponseDecoded));
}

@Test
public void identicalConvertedResource() {
// We need to remove id of inner fields as they are intentionally removed in Avro conversion.
Patient tempPatient = testPatient.copy();
tempPatient.getAddress().get(0).setId(null);
Assert.assertTrue(
tempPatient.equalsDeep(
(Base) TestUtil.encodeThenParse(testPatientDecoded, Patient.class, fhirContext)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.avro.Schema;
import org.apache.avro.compiler.specific.SpecificCompiler;
import org.apache.avro.generic.GenericData.Record;
import org.hl7.fhir.dstu3.model.Base;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Condition;
Expand Down Expand Up @@ -203,8 +204,8 @@ public void testChoice() throws FHIRException {
public void testIdInNestedElement() throws FHIRException {

// Ensure that nested elements do not have id as property.
Assert.assertNotNull(testPatient.getAddress().get(0).getId());
Assert.assertNull(testPatientDecoded.getAddress().get(0).getId());
Assert.assertNull(testPatientDecoded.getName().get(0).getId());
}

/**
Expand Down Expand Up @@ -530,15 +531,22 @@ public void testCompile() throws IOException {
@Test
public void testMetaElement() {

String id = testPatient.getId();
Meta meta = testPatient.getMeta();

Assert.assertEquals(id, testPatientDecoded.getId());

Assert.assertEquals(meta.getTag().size(), testPatientDecoded.getMeta().getTag().size());
Assert.assertEquals(
meta.getTag().get(0).getCode(), testPatientDecoded.getMeta().getTag().get(0).getCode());
Assert.assertEquals(
meta.getTag().get(0).getSystem(), testPatientDecoded.getMeta().getTag().get(0).getSystem());
}

@Test
public void identicalConvertedResource() {
// We need to remove id of inner fields as they are intentionally removed in Avro conversion.
Patient tempPatient = testPatient.copy();
tempPatient.getAddress().get(0).setId(null);
Assert.assertTrue(
tempPatient.equalsDeep(
(Base) TestUtil.encodeThenParse(testPatientDecoded, Patient.class, fhirContext)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cerner.bunsen.avro;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import org.hl7.fhir.instance.model.api.IBaseResource;

public class TestUtil {

/**
* This is used when we want to verify that no information is lost in the process of converting to
* Avro records. The idea is to convert the resource to the JSON representation then parse it back
* to a HAPI object and verify we get the original HAPI object. This is needed because HAPI adds
* some information, like `resourceType` in `id` fields, when parsing from JSON but, we don't have
* that in the `id` field of converted Avro fields. See:
* https://github.com/google/fhir-data-pipes/issues/1003
*
* @param resource the resource which is created from an Avro record.
* @param resourceClass the Class of the resource.
* @param fhirContext the FHIR context.
* @return a HAPI object created from parsing the encoded version of `resource`.
*/
public static IBaseResource encodeThenParse(
IBaseResource resource,
Class<? extends IBaseResource> resourceClass,
FhirContext fhirContext) {
IParser jsonParser = fhirContext.newJsonParser();
return jsonParser.parseResource(resourceClass, jsonParser.encodeResourceToString(resource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;
import org.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.BooleanType;
import org.hl7.fhir.r4.model.CodeType;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Condition;
Expand Down Expand Up @@ -224,15 +223,14 @@ public static Patient newPatient() {

Patient patient = new Patient();

patient.setId("test-patient");
patient.setId("Patient/test-patient");
patient.setGender(AdministrativeGender.MALE);
patient.setActive(true);
patient.setMultipleBirth(new IntegerType(1));

// This is to introduce conflict with managingOrganization identifier
// and make sure conflict is resolved.
Identifier patientIdentifier = new Identifier();
patientIdentifier.setId("patient123");
patientIdentifier.getAssigner().setReference("Organization/123");
patient.setIdentifier(List.of(patientIdentifier));

Expand All @@ -241,29 +239,28 @@ public static Patient newPatient() {
patient.addGeneralPractitioner().setReference("Practitioner/12345");

Identifier practitionerIdentifier = new Identifier();
practitionerIdentifier.setId("P123456");
practitionerIdentifier.getAssigner().setReference("Organization/123456");
patient.getGeneralPractitionerFirstRep().setIdentifier(practitionerIdentifier);

Identifier managingOrganisationIdentifier = new Identifier();
managingOrganisationIdentifier.setId("O123456");
managingOrganisationIdentifier.getAssigner().setReference("Organization/234");
patient.getManagingOrganization().setIdentifier(managingOrganisationIdentifier);

Address address = patient.addAddress();
address.addLine("123 Fake Street");
address.setId("address123");
address.setCity("Chicago");
address.setState("IL");
address.setDistrict("12345");

Extension birthSex = patient.addExtension();
birthSex.setUrl(US_CORE_BIRTHSEX);
birthSex.setValue(new CodeType("M"));

Extension ethnicity = patient.addExtension();
ethnicity.setUrl(US_CORE_ETHNICITY);
ethnicity.setValue(null);

Extension birthSex = patient.addExtension();
birthSex.setUrl(US_CORE_BIRTHSEX);
birthSex.setValue(new StringType("M"));

Coding ombCoding = new Coding();

ombCoding.setSystem("urn:oid:2.16.840.1.113883.6.238");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import org.hl7.fhir.dstu3.model.Address;
import org.hl7.fhir.dstu3.model.BooleanType;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Condition;
Expand Down Expand Up @@ -185,15 +184,14 @@ public static Patient newPatient() {

Patient patient = new Patient();

patient.setId("test-patient");
patient.setId("Patient/test-patient");
patient.setGender(AdministrativeGender.MALE);
patient.setActive(true);
patient.setMultipleBirth(new IntegerType(1));

// This is to introduce conflict with managingOrganization identifier
// and make sure conflict is resolved.
Identifier patientIdentifier = new Identifier();
patientIdentifier.setId("patient123");
patientIdentifier.getAssigner().setReference("Organization/123");
patient.setIdentifier(List.of(patientIdentifier));

Expand All @@ -202,12 +200,10 @@ public static Patient newPatient() {
patient.addGeneralPractitioner().setReference("Practitioner/12345");

Identifier practitionerIdentifier = new Identifier();
practitionerIdentifier.setId("P123456");
practitionerIdentifier.getAssigner().setReference("Organization/123456");
patient.getGeneralPractitionerFirstRep().setIdentifier(practitionerIdentifier);

Identifier managingOrganisationIdentifier = new Identifier();
managingOrganisationIdentifier.setId("O123456");
managingOrganisationIdentifier.getAssigner().setReference("Organization/234");
patient.getManagingOrganization().setIdentifier(managingOrganisationIdentifier);

Expand All @@ -218,15 +214,14 @@ public static Patient newPatient() {
address.setState("IL");
address.setDistrict("12345");

Extension birthSex = patient.addExtension();

birthSex.setUrl(US_CORE_BIRTHSEX);
birthSex.setValue(new CodeType("M"));

Extension ethnicity = patient.addExtension();
ethnicity.setUrl(US_CORE_ETHNICITY);
ethnicity.setValue(null);

Extension birthSex = patient.addExtension();
birthSex.setUrl(US_CORE_BIRTHSEX);
birthSex.setValue(new StringType("M"));

Coding ombCoding = new Coding();

ombCoding.setSystem("urn:oid:2.16.840.1.113883.6.238");
Expand Down Expand Up @@ -268,7 +263,6 @@ public static Patient newPatient() {
humanName.setFamily("family_name");
humanName.addGiven("given_name");
humanName.addGiven("middle_name");
humanName.setId("name123");
patient.addName(humanName);

Meta meta = new Meta();
Expand Down
Loading