Add mode to parser to force it to not encode certain elements when serializing

This commit is contained in:
jamesagnew 2016-03-16 07:53:21 -04:00
parent 2ae2cbba23
commit 9aa7eea274
11 changed files with 456 additions and 52 deletions

View File

@ -91,6 +91,10 @@ public abstract class BaseParser implements IParser {
private boolean mySummaryMode; private boolean mySummaryMode;
private boolean mySuppressNarratives; private boolean mySuppressNarratives;
private Set<String> myDontEncodeElements;
private boolean myDontEncodeElementsIncludesStars;
/** /**
* Constructor * Constructor
* *
@ -605,6 +609,21 @@ public abstract class BaseParser implements IParser {
return theValues; return theValues;
} }
@Override
public void setDontEncodeElements(Set<String> theDontEncodeElements) {
myDontEncodeElementsIncludesStars = false;
if (theDontEncodeElements == null || theDontEncodeElements.isEmpty()) {
myDontEncodeElements = null;
} else {
myDontEncodeElements = theDontEncodeElements;
for (String next : theDontEncodeElements) {
if (next.startsWith("*.")) {
myDontEncodeElementsIncludesStars = true;
}
}
}
}
@Override @Override
public void setEncodeElements(Set<String> theEncodeElements) { public void setEncodeElements(Set<String> theEncodeElements) {
myEncodeElementsIncludesStars = false; myEncodeElementsIncludesStars = false;
@ -676,6 +695,38 @@ public abstract class BaseParser implements IParser {
return isSummaryMode() || isSuppressNarratives(); return isSummaryMode() || isSuppressNarratives();
} }
protected boolean shouldEncodeResourceId(IBaseResource theResource) {
boolean retVal = true;
if (isOmitResourceId()) {
retVal = false;
} else {
if (myDontEncodeElements != null) {
String resourceName = myContext.getResourceDefinition(theResource).getName();
if (myDontEncodeElements.contains(resourceName + ".id")) {
retVal = false;
} else if (myDontEncodeElements.contains("*.id")) {
retVal = false;
}
}
}
return retVal;
}
/**
* Used for DSTU2 only
*/
protected boolean shouldEncodeResourceMeta(IResource theResource) {
if (myDontEncodeElements != null) {
String resourceName = myContext.getResourceDefinition(theResource).getName();
if (myDontEncodeElements.contains(resourceName + ".meta")) {
return false;
} else if (myDontEncodeElements.contains("*.meta")) {
return false;
}
}
return true;
}
private String subsetDescription() { private String subsetDescription() {
return "Resource encoded in summary mode"; return "Resource encoded in summary mode";
} }
@ -770,32 +821,46 @@ public abstract class BaseParser implements IParser {
} }
} }
private boolean checkIfParentShouldBeEncodedAndBuildPath(StringBuilder theB, boolean theStarPass) { private boolean checkIfParentShouldBeEncodedAndBuildPath(StringBuilder thePathBuilder, boolean theStarPass) {
return checkIfPathMatches(thePathBuilder, theStarPass, myEncodeElementsAppliesToResourceTypes, myEncodeElements, true);
}
private boolean checkIfParentShouldNotBeEncodedAndBuildPath(StringBuilder thePathBuilder, boolean theStarPass) {
return checkIfPathMatches(thePathBuilder, theStarPass, null, myDontEncodeElements, false);
}
private boolean checkIfPathMatches(StringBuilder thePathBuilder, boolean theStarPass, Set<String> theResourceTypes, Set<String> theElements, boolean theCheckingForWhitelist) {
if (myResDef != null) { if (myResDef != null) {
if (myEncodeElementsAppliesToResourceTypes != null) { if (theResourceTypes != null) {
if (!myEncodeElementsAppliesToResourceTypes.contains(myResDef.getName())) { if (!theResourceTypes.contains(myResDef.getName())) {
return true; return true;
} }
} }
if (theStarPass) { if (theStarPass) {
theB.append('*'); thePathBuilder.append('*');
} else { } else {
theB.append(myResDef.getName()); thePathBuilder.append(myResDef.getName());
} }
if (myEncodeElements.contains(theB.toString())) { if (theElements.contains(thePathBuilder.toString())) {
return true; return true;
} else { } else {
return false; return false;
} }
} else if (myParent != null) { } else if (myParent != null) {
if (myParent.checkIfParentShouldBeEncodedAndBuildPath(theB, theStarPass)) { boolean parentCheck;
if (theCheckingForWhitelist) {
parentCheck = myParent.checkIfParentShouldBeEncodedAndBuildPath(thePathBuilder, theStarPass);
} else {
parentCheck = myParent.checkIfParentShouldNotBeEncodedAndBuildPath(thePathBuilder, theStarPass);
}
if (parentCheck) {
return true; return true;
} }
if (myDef != null) { if (myDef != null) {
theB.append('.'); thePathBuilder.append('.');
theB.append(myDef.getElementName()); thePathBuilder.append(myDef.getElementName());
return myEncodeElements.contains(theB.toString()); return theElements.contains(thePathBuilder.toString());
} }
} }
@ -815,13 +880,19 @@ public abstract class BaseParser implements IParser {
} }
public boolean shouldBeEncoded() { public boolean shouldBeEncoded() {
if (myEncodeElements == null) { boolean retVal = true;
return true; if (myEncodeElements != null) {
} retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), false);
boolean retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), false);
if (retVal == false && myEncodeElementsIncludesStars) { if (retVal == false && myEncodeElementsIncludesStars) {
retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), true); retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), true);
} }
}
if (retVal && myDontEncodeElements != null) {
retVal = !checkIfParentShouldNotBeEncodedAndBuildPath(new StringBuilder(), false);
if (retVal && myDontEncodeElementsIncludesStars) {
retVal = !checkIfParentShouldNotBeEncodedAndBuildPath(new StringBuilder(), true);
}
}
return retVal; return retVal;
} }
} }

View File

@ -215,18 +215,41 @@ public interface IParser {
*/ */
TagList parseTagList(String theString); TagList parseTagList(String theString);
/**
* If provided, specifies the elements which should NOT be encoded. Valid values for this
* field would include:
* <ul>
* <li><b>Patient</b> - Don't encode patient and all its children</li>
* <li><b>Patient.name</b> - Don't encode the patient's name</li>
* <li><b>Patient.name.family</b> - Don't encode the patient's family name</li>
* <li><b>*.text</b> - Don't encode the text element on any resource (only the very first position may contain a wildcard)</li>
* </ul>
* <p>
* DSTU2 note: Note that values including meta, such as <code>Patient.meta</code>
* will work for DSTU2 parsers, but values with subelements on meta such
* as <code>Patient.meta.lastUpdated</code> will only work in
* DSTU3+ mode.
* </p>
*
* @param theDontEncodeElements
* The elements to encode
* @see #setEncodeElements(Set)
*/
void setDontEncodeElements(Set<String> theDontEncodeElements);
/** /**
* If provided, specifies the elements which should be encoded, to the exclusion of all others. Valid values for this * If provided, specifies the elements which should be encoded, to the exclusion of all others. Valid values for this
* field would include: * field would include:
* <ul> * <ul>
* <li><b>Patient</b> - Encode patient and all its children</li> * <li><b>Patient</b> - Encode patient and all its children</li>
* <li><b>Patient.name</b> - Encoding only the patient's name</li> * <li><b>Patient.name</b> - Encode only the patient's name</li>
* <li><b>Patient.name.family</b> - Encode only the patient's family name</li> * <li><b>Patient.name.family</b> - Encode only the patient's family name</li>
* <li><b>*.text</b> - Encode the text element on any resource (only the very first position may contain a wildcard)</li> * <li><b>*.text</b> - Encode the text element on any resource (only the very first position may contain a wildcard)</li>
* </ul> * </ul>
* *
* @param theEncodeElements * @param theEncodeElements
* The elements to encode * The elements to encode
* @see #setDontEncodeElements(Set)
*/ */
void setEncodeElements(Set<String> theEncodeElements); void setEncodeElements(Set<String> theEncodeElements);

View File

@ -800,7 +800,7 @@ public class JsonParser extends BaseParser implements IParser {
} }
if (!theContainedResource) { if (!theContainedResource) {
if (isOmitResourceId()) { if (super.shouldEncodeResourceId(theResource) == false) {
resourceId = null; resourceId = null;
} else if (getEncodeForceResourceId() != null) { } else if (getEncodeForceResourceId() != null) {
resourceId = getEncodeForceResourceId(); resourceId = getEncodeForceResourceId();
@ -847,7 +847,7 @@ public class JsonParser extends BaseParser implements IParser {
versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource); versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
} }
if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) { if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
theEventWriter.writeStartObject("meta"); theEventWriter.writeStartObject("meta");
writeOptionalTagWithTextNode(theEventWriter, "versionId", versionIdPart); writeOptionalTagWithTextNode(theEventWriter, "versionId", versionIdPart);
writeOptionalTagWithTextNode(theEventWriter, "lastUpdated", updated); writeOptionalTagWithTextNode(theEventWriter, "lastUpdated", updated);

View File

@ -787,7 +787,7 @@ public class XmlParser extends BaseParser implements IParser {
} }
if (!theIncludedResource) { if (!theIncludedResource) {
if (isOmitResourceId()) { if (super.shouldEncodeResourceId(theResource) == false) {
resourceId = null; resourceId = null;
} else if (getEncodeForceResourceId() != null) { } else if (getEncodeForceResourceId() != null) {
resourceId = getEncodeForceResourceId(); resourceId = getEncodeForceResourceId();
@ -844,7 +844,7 @@ public class XmlParser extends BaseParser implements IParser {
List<IdDt> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); List<IdDt> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES);
TagList tags = getMetaTagsForEncoding((resource)); TagList tags = getMetaTagsForEncoding((resource));
if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) { if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
theEventWriter.writeStartElement("meta"); theEventWriter.writeStartElement("meta");
writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart); writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart);
if (updated != null) { if (updated != null) {

View File

@ -68,6 +68,7 @@ import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import com.google.common.base.Charsets;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
@ -720,18 +721,14 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {
ResourceEncodingEnum encoding = myConfig.getResourceEncoding(); ResourceEncodingEnum encoding = myConfig.getResourceEncoding();
theEntity.setEncoding(encoding); theEntity.setEncoding(encoding);
theEntity.setFhirVersion(myContext.getVersion().getVersion()); theEntity.setFhirVersion(myContext.getVersion().getVersion());
try {
switch (encoding) { switch (encoding) {
case JSON: case JSON:
theEntity.setResource(encoded.getBytes("UTF-8")); theEntity.setResource(encoded.getBytes(Charsets.UTF_8));
break; break;
case JSONC: case JSONC:
theEntity.setResource(GZipUtil.compress(encoded)); theEntity.setResource(GZipUtil.compress(encoded));
break; break;
} }
} catch (UnsupportedEncodingException e) {
throw new InternalErrorException(e);
}
Set<TagDefinition> allDefs = new HashSet<TagDefinition>(); Set<TagDefinition> allDefs = new HashSet<TagDefinition>();
@ -1119,16 +1116,6 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {
theEntity.setPublished(theUpdateTime); theEntity.setPublished(theUpdateTime);
} }
// if (theUpdateHistory) {
// Long existingId = theEntity.getId();
// ResourceHistoryTable existingHistory = existingId != null ? myResourceHistoryTableDao.findForIdAndVersion(existingId, theEntity.getVersion()) : null;
// final ResourceHistoryTable historyEntry = theEntity.toHistory(existingHistory);
//
// ourLog.info("Saving history entry for update {}", historyEntry.getIdDt());
// myResourceHistoryTableDao.save(historyEntry);
//
// }
if (theUpdateVersion) { if (theUpdateVersion) {
theEntity.setVersion(theEntity.getVersion() + 1); theEntity.setVersion(theEntity.getVersion() + 1);
} }

View File

@ -31,6 +31,7 @@ import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.dstu3.model.UriType;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
@ -277,6 +278,25 @@ public class FhirResourceDaoDstu3UpdateTest extends BaseJpaDstu3Test {
} }
@Test
@Ignore
public void testUpdateIgnoresIdenticalVersions() throws InterruptedException {
String methodName = "testUpdateIgnoresIdenticalVersions";
Patient p1 = new Patient();
p1.addIdentifier().setSystem("urn:system").setValue(methodName);
p1.addName().addFamily("Tester").addGiven(methodName);
IIdType p1id = myPatientDao.create(p1, new ServletRequestDetails()).getId();
IIdType p1id2 = myPatientDao.update(p1, mySrd).getId();
assertEquals(p1id.getValue(), p1id2.getValue());
p1.addName().addGiven("NewGiven");
IIdType p1id3 = myPatientDao.update(p1, mySrd).getId();
assertNotEquals(p1id.getValue(), p1id3.getValue());
}
@Test @Test
public void testDuplicateProfilesIgnored() { public void testDuplicateProfilesIgnored() {
String name = "testDuplicateProfilesIgnored"; String name = "testDuplicateProfilesIgnored";

View File

@ -16,6 +16,8 @@ import static org.mockito.Mockito.verify;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -27,6 +29,8 @@ 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 com.google.common.collect.Sets;
import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Bundle;
@ -71,6 +75,7 @@ public class JsonParserDstu2Test {
private static final FhirContext ourCtx = FhirContext.forDstu2(); private static final 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);
/** /**
* See #308 * See #308
*/ */
@ -510,6 +515,77 @@ public class JsonParserDstu2Test {
assertThat(json, not(containsString("\"id\""))); assertThat(json, not(containsString("\"id\"")));
} }
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
ArrayList<IdDt> list = new ArrayList<IdDt>();
list.add(new IdDt("http://profile"));
ResourceMetadataKeyEnum.PROFILES.put(patient, list);
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test @Test
public void testEncodingNullExtension() { public void testEncodingNullExtension() {
Patient p = new Patient(); Patient p = new Patient();

View File

@ -38,6 +38,8 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.internal.stubbing.answers.ThrowsException; import org.mockito.internal.stubbing.answers.ThrowsException;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.ExtensionDt; import ca.uhn.fhir.model.api.ExtensionDt;
@ -1159,6 +1161,78 @@ public class XmlParserDstu2Test {
} }
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
ArrayList<IdDt> list = new ArrayList<IdDt>();
list.add(new IdDt("http://profile"));
ResourceMetadataKeyEnum.PROFILES.put(patient, list);
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test @Test
public void testMoreExtensions() throws Exception { public void testMoreExtensions() throws Exception {

View File

@ -14,6 +14,8 @@ import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -54,6 +56,8 @@ import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
@ -70,15 +74,6 @@ public class JsonParserDstu3Test {
ourCtx.setNarrativeGenerator(null); ourCtx.setNarrativeGenerator(null);
} }
@Test
public void testLinkage() {
Linkage l = new Linkage();
l.addItem().getResource().setDisplay("FOO");
String out = ourCtx.newXmlParser().encodeResourceToString(l);
ourLog.info(out);
assertEquals("<Linkage xmlns=\"http://hl7.org/fhir\"><item><resource><display value=\"FOO\"/></resource></item></Linkage>", out);
}
@Test @Test
public void testEncodeAndParseExtensions() throws Exception { public void testEncodeAndParseExtensions() throws Exception {
@ -241,6 +236,7 @@ public class JsonParserDstu3Test {
assertEquals("sec_label2", tagList.get(1).getDisplay()); assertEquals("sec_label2", tagList.get(1).getDisplay());
} }
@Test @Test
public void testEncodeAndParseSecurityLabels() { public void testEncodeAndParseSecurityLabels() {
Patient p = new Patient(); Patient p = new Patient();
@ -474,6 +470,75 @@ public class JsonParserDstu3Test {
assertThat(json, not(containsString("\"id\""))); assertThat(json, not(containsString("\"id\"")));
} }
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
patient.getMeta().addProfile(("http://profile"));
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test @Test
public void testEncodeWithNarrative() { public void testEncodeWithNarrative() {
Patient p = new Patient(); Patient p = new Patient();
@ -547,6 +612,15 @@ public class JsonParserDstu3Test {
} }
@Test
public void testLinkage() {
Linkage l = new Linkage();
l.addItem().getResource().setDisplay("FOO");
String out = ourCtx.newXmlParser().encodeResourceToString(l);
ourLog.info(out);
assertEquals("<Linkage xmlns=\"http://hl7.org/fhir\"><item><resource><display value=\"FOO\"/></resource></item></Linkage>", out);
}
// FIXME: this should pass // FIXME: this should pass
@Test @Test
@Ignore @Ignore

View File

@ -85,6 +85,8 @@ import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation; import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
@ -101,6 +103,8 @@ public class XmlParserDstu3Test {
} }
@Test @Test
public void testBundleWithBinary() { public void testBundleWithBinary() {
//@formatter:off //@formatter:off
@ -1018,9 +1022,10 @@ public class XmlParserDstu3Test {
assertThat(encoded, not(containsString("maritalStatus"))); assertThat(encoded, not(containsString("maritalStatus")));
} }
@Test @Ignore @Test
public void testEncodeWithEncodeElements() throws Exception { public void testEncodeWithEncodeElements() throws Exception {
Patient patient = new Patient(); Patient patient = new Patient();
patient.getMeta().addProfile("http://profile");
patient.addName().addFamily("FAMILY"); patient.addName().addFamily("FAMILY");
patient.addAddress().addLine("LINE1"); patient.addAddress().addLine("LINE1");
@ -1066,6 +1071,73 @@ public class XmlParserDstu3Test {
} }
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
patient.getMeta().addProfile("http://profile");
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test @Test
public void testEncodeWithNarrative() { public void testEncodeWithNarrative() {

View File

@ -241,6 +241,13 @@
first query. This should improve performance when searching against large first query. This should improve performance when searching against large
datasets. datasets.
</action> </action>
<action type="add">
Parsers have new method
<![CDATA[<code>setDontEncodeElements</code>]]>
which can be used to force the parser to not encode certain elements
in a resource when serializing. For example this can be used to omit
sensitive data or skip the resource metadata.
</action>
</release> </release>
<release version="1.4" date="2016-02-04"> <release version="1.4" date="2016-02-04">
<action type="add"> <action type="add">