Add test for #392

This commit is contained in:
James Agnew 2016-07-04 13:25:49 -04:00
parent a3af502ee2
commit e26e31ae02
3 changed files with 95 additions and 23 deletions

View File

@ -0,0 +1,28 @@
package ca.uhn.fhir.parser.i391;
import java.util.List;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.annotation.Block;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.util.ElementUtil;
@Block
public class CustomBlock extends Observation.Component {
@Child(name = "value", min = 0, max = 1, order = Child.REPLACE_PARENT, summary = true, type = {StringDt.class})
public StringDt ourValue;
@Override
public boolean isEmpty() {
return super.isBaseEmpty() && ElementUtil.isEmpty(ourValue);
}
@Override
public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
return ElementUtil.allPopulatedChildElements(theType, ourValue);
}
}

View File

@ -0,0 +1,15 @@
package ca.uhn.fhir.parser.i391;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
@ResourceDef(name = "OperationOutcome", profile = "http://hl7.org/fhir/profiles/custom-operation-outcome", id = "custom-operation-outcome")
public class CustomOperationOutcome extends OperationOutcome {
@Child(name = "someElement2", type = CustomBlock.class)
@Extension(definedLocally = false, isModifier = false, url = "#someElement2")
public CustomBlock element2;
}

View File

@ -2,44 +2,73 @@ package ca.uhn.fhir.parser.i391;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.primitive.BoundCodeDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.CustomTypeDstu2Test;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.util.TestUtil;
/**
* See #391
*/
public class TestOutcomeTest {
@Test
public void testParseBoundCodeDtJson() {
IParser jsonParser = FhirContext.forDstu2().newJsonParser();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(CustomTypeDstu2Test.class);
TestOutcome outcome = new TestOutcome();
outcome.setElement(new BoundCodeDt<OutcomeEnum>(new OutcomeBinder(), OutcomeEnum.ITEM1));
@Test
public void testCustomDataTypeBugN2_UnknownElement() {
CustomBlock nameDt = new CustomBlock();
nameDt.ourValue = new StringDt("testText");
String xmlResource = jsonParser.encodeResourceToString(outcome);
TestOutcome operationOutcome = jsonParser.parseResource(TestOutcome.class, xmlResource);
CustomOperationOutcome outcome = new CustomOperationOutcome();
outcome.element2 = nameDt;
assertNotNull(operationOutcome.getElement());
assertTrue(operationOutcome.getElement() instanceof BoundCodeDt);
assertEquals(outcome.getElement(), operationOutcome.getElement());
}
IParser parser = FhirContext.forDstu2().newXmlParser();
String outcomeString = parser.setPrettyPrint(true).encodeResourceToString(outcome);
CustomOperationOutcome parsedOutcome = parser.parseResource(CustomOperationOutcome.class, outcomeString);
ourLog.info(outcomeString);
@Test
public void testParseBoundCodeDtXml() {
IParser xmlParser = FhirContext.forDstu2().newXmlParser();
// assertNotNull(parsedOutcome.element2);
// assertNotNull(parsedOutcome.element2.ourValue);
}
TestOutcome outcome = new TestOutcome();
outcome.setElement(new BoundCodeDt<OutcomeEnum>(new OutcomeBinder(), OutcomeEnum.ITEM1));
@Test
public void testParseBoundCodeDtJson() {
IParser jsonParser = FhirContext.forDstu2().newJsonParser();
String xmlResource = xmlParser.encodeResourceToString(outcome);
TestOutcome operationOutcome = xmlParser.parseResource(TestOutcome.class, xmlResource);
TestOutcome outcome = new TestOutcome();
outcome.setElement(new BoundCodeDt<OutcomeEnum>(new OutcomeBinder(), OutcomeEnum.ITEM1));
assertNotNull(operationOutcome.getElement());
assertTrue(operationOutcome.getElement() instanceof BoundCodeDt);
assertEquals(outcome.getElement(), operationOutcome.getElement());
}
}
String xmlResource = jsonParser.encodeResourceToString(outcome);
TestOutcome operationOutcome = jsonParser.parseResource(TestOutcome.class, xmlResource);
assertNotNull(operationOutcome.getElement());
assertTrue(operationOutcome.getElement() instanceof BoundCodeDt);
assertEquals(outcome.getElement(), operationOutcome.getElement());
}
@Test
public void testParseBoundCodeDtXml() {
IParser xmlParser = FhirContext.forDstu2().newXmlParser();
TestOutcome outcome = new TestOutcome();
outcome.setElement(new BoundCodeDt<OutcomeEnum>(new OutcomeBinder(), OutcomeEnum.ITEM1));
String xmlResource = xmlParser.encodeResourceToString(outcome);
TestOutcome operationOutcome = xmlParser.parseResource(TestOutcome.class, xmlResource);
assertNotNull(operationOutcome.getElement());
assertTrue(operationOutcome.getElement() instanceof BoundCodeDt);
assertEquals(outcome.getElement(), operationOutcome.getElement());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}