Add unit test for #428

This commit is contained in:
James 2016-08-23 07:10:28 -04:00
parent db17342149
commit 696fa8701b
1 changed files with 92 additions and 65 deletions

View File

@ -15,7 +15,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.GenericDeclaration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -30,6 +29,7 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.ThrowsException; import org.mockito.internal.stubbing.answers.ThrowsException;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -43,6 +43,7 @@ import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.Tag; import ca.uhn.fhir.model.api.Tag;
import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.base.composite.BaseCodingDt;
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu2.composite.CodingDt; import ca.uhn.fhir.model.dstu2.composite.CodingDt;
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt; import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
@ -73,6 +74,8 @@ import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation; import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import net.sf.json.JSON; import net.sf.json.JSON;
import net.sf.json.JSONSerializer; import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig; import net.sf.json.JsonConfig;
@ -81,46 +84,6 @@ public class JsonParserDstu2Test {
private static FhirContext ourCtx = FhirContext.forDstu2(); private static FhirContext ourCtx = FhirContext.forDstu2();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class);
@Test
public void testEncodeNarrativeShouldIncludeNamespace() {
Patient p = new Patient();
p.getText().setDivAsString("<div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\": \"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
/**
* See #390
*/
@Test
public void testEncodeAndParseBundleWithNoEntries() {
ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle();
b.setId("123");
String encoded = ourCtx.newJsonParser().encodeResourceToString(b);
ourLog.info(encoded);
assertThat(encoded, containsString("123"));
assertThat(encoded, not(containsString("entry")));
b = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, encoded);
assertEquals("123", b.getId().getIdPart());
assertEquals(0, b.getEntry().size());
}
@Test
public void testEncodeNarrativeShouldIncludeNamespaceWithProcessingInstruction() {
Patient p = new Patient();
p.getText().setDivAsString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
@Test @Test
public void testContainedResourceInExtensionUndeclared() { public void testContainedResourceInExtensionUndeclared() {
Patient p = new Patient(); Patient p = new Patient();
@ -143,6 +106,8 @@ public class JsonParserDstu2Test {
assertEquals("ORG", o.getName()); assertEquals("ORG", o.getName());
} }
/** /**
* See #308 * See #308
*/ */
@ -161,6 +126,23 @@ public class JsonParserDstu2Test {
assertEquals(true, obs.getReadOnly().getValue().booleanValue()); assertEquals(true, obs.getReadOnly().getValue().booleanValue());
} }
/**
* See #390
*/
@Test
public void testEncodeAndParseBundleWithNoEntries() {
ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle();
b.setId("123");
String encoded = ourCtx.newJsonParser().encodeResourceToString(b);
ourLog.info(encoded);
assertThat(encoded, containsString("123"));
assertThat(encoded, not(containsString("entry")));
b = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, encoded);
assertEquals("123", b.getId().getIdPart());
assertEquals(0, b.getEntry().size());
}
@Test @Test
public void testEncodeAndParseExtensions() throws Exception { public void testEncodeAndParseExtensions() throws Exception {
@ -247,6 +229,22 @@ public class JsonParserDstu2Test {
} }
@Test
public void testEncodeAndParseLanguage() {
Patient p = new Patient();
p.setLanguage(new CodeDt("en_CA"));
String encoded = ourCtx.newJsonParser().encodeResourceToString(p);
ourLog.info(encoded);
assertEquals("{\"resourceType\":\"Patient\",\"language\":\"en_CA\"}", encoded);
p = (Patient) ourCtx.newJsonParser().parseResource(encoded);
assertEquals("en_CA", p.getLanguage().getValue());
p = (Patient) ourCtx.newJsonParser().parseResource("{\"resourceType\":\"Patient\",\"language\":[\"en_CA\"]}");
assertEquals("en_CA", p.getLanguage().getValue());
}
@Test @Test
public void testEncodeAndParseMetaProfileAndTags() { public void testEncodeAndParseMetaProfileAndTags() {
@ -362,6 +360,7 @@ public class JsonParserDstu2Test {
} }
@Test @Test
public void testEncodeAndParseSecurityLabels() { public void testEncodeAndParseSecurityLabels() {
Patient p = new Patient(); Patient p = new Patient();
@ -426,6 +425,7 @@ public class JsonParserDstu2Test {
assertEquals("VERSION2", label.getVersion()); assertEquals("VERSION2", label.getVersion());
} }
@Test @Test
public void testEncodeBundleNewBundleNoText() { public void testEncodeBundleNewBundleNoText() {
@ -501,7 +501,6 @@ public class JsonParserDstu2Test {
assertEquals("{\"resourceType\":\"Binary\"}", output); assertEquals("{\"resourceType\":\"Binary\"}", output);
} }
/** /**
* #158 * #158
*/ */
@ -518,24 +517,6 @@ public class JsonParserDstu2Test {
assertThat(encoded, not(containsString("tag"))); assertThat(encoded, not(containsString("tag")));
} }
@Test
public void testEncodeAndParseLanguage() {
Patient p = new Patient();
p.setLanguage(new CodeDt("en_CA"));
String encoded = ourCtx.newJsonParser().encodeResourceToString(p);
ourLog.info(encoded);
assertEquals("{\"resourceType\":\"Patient\",\"language\":\"en_CA\"}", encoded);
p = (Patient) ourCtx.newJsonParser().parseResource(encoded);
assertEquals("en_CA", p.getLanguage().getValue());
p = (Patient) ourCtx.newJsonParser().parseResource("{\"resourceType\":\"Patient\",\"language\":[\"en_CA\"]}");
assertEquals("en_CA", p.getLanguage().getValue());
}
/** /**
* #158 * #158
*/ */
@ -554,6 +535,7 @@ public class JsonParserDstu2Test {
assertThat(encoded, not(containsString("Label"))); assertThat(encoded, not(containsString("Label")));
} }
@Test @Test
public void testEncodeExtensionInPrimitiveElement() { public void testEncodeExtensionInPrimitiveElement() {
@ -583,6 +565,7 @@ public class JsonParserDstu2Test {
} }
@Test @Test
public void testEncodeExtensionUndeclaredNonModifier() { public void testEncodeExtensionUndeclaredNonModifier() {
Observation obs = new Observation(); Observation obs = new Observation();
@ -664,6 +647,28 @@ public class JsonParserDstu2Test {
assertEquals("sub_ext_value", ((StringDt)obs.getUndeclaredExtensions().get(0).getExtension().get(0).getValue()).getValue()); assertEquals("sub_ext_value", ((StringDt)obs.getUndeclaredExtensions().get(0).getExtension().get(0).getValue()).getValue());
} }
/**
* See #428
*/
@Test
public void testEncodeExtensionWithCodeableConcept() {
Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
Level initialLevel = logger.getLevel();
logger.setLevel(Level.TRACE);
try {
Patient p = new Patient();
CodeableConceptDt cc = new CodeableConceptDt();
cc.addCoding().setCode("123").setSystem("http://foo").setDisplay("AAA");
p.addUndeclaredExtension(false, "http://extension", cc);
ourLog.info(ourCtx.newJsonParser().encodeResourceToString(p));
} finally {
logger.setLevel(initialLevel);
}
}
@Test @Test
public void testEncodeForceResourceId() { public void testEncodeForceResourceId() {
Patient p = new Patient(); Patient p = new Patient();
@ -680,6 +685,28 @@ public class JsonParserDstu2Test {
assertThat(encoded, not(containsString("111"))); assertThat(encoded, not(containsString("111")));
} }
@Test
public void testEncodeNarrativeShouldIncludeNamespace() {
Patient p = new Patient();
p.getText().setDivAsString("<div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\": \"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
@Test
public void testEncodeNarrativeShouldIncludeNamespaceWithProcessingInstruction() {
Patient p = new Patient();
p.getText().setDivAsString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
@Test @Test
public void testEncodeNarrativeSuppressed() throws Exception { public void testEncodeNarrativeSuppressed() throws Exception {
Patient patient = new Patient(); Patient patient = new Patient();