From 0d6eca70a968ff01a12ea4cf7e39a637f3fd8165 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 26 Sep 2014 17:16:37 -0400 Subject: [PATCH 1/2] Work on #30, still not working though... --- hapi-fhir-base/src/changes/changes.xml | 6 + .../main/java/ca/uhn/fhir/model/api/Tag.java | 42 +-- .../java/ca/uhn/fhir/model/api/TagList.java | 239 +++++++++++++++++- .../java/ca/uhn/fhir/parser/BaseParser.java | 2 +- .../ca/uhn/fhir/model/api/TagListTest.java | 67 +++++ .../uhn/fhir/jpa/dao/FhirResourceDaoTest.java | 26 +- 6 files changed, 356 insertions(+), 26 deletions(-) create mode 100644 hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java diff --git a/hapi-fhir-base/src/changes/changes.xml b/hapi-fhir-base/src/changes/changes.xml index 2ced661c149..e9917647dd5 100644 --- a/hapi-fhir-base/src/changes/changes.xml +++ b/hapi-fhir-base/src/changes/changes.xml @@ -7,6 +7,12 @@ + + API CHANGE:]]> The TagList class previously implemented ArrayList semantics, + but this has been replaced with LinkedHashMap semantics. This means that the list of + tags will no longer accept duplicate tags, but that tag order will still be + preserved. Thanks to Bill de Beaubien for reporting! + Documentation fixes diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java index 6a8c6904859..b38aafa2891 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java @@ -29,28 +29,25 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class Tag extends BaseElement implements IElement { - /** - * Convenience constant containing the "http://hl7.org/fhir/tag" scheme - * value - */ - public static final String HL7_ORG_FHIR_TAG = "http://hl7.org/fhir/tag"; + + public static final String ATTR_LABEL = "label"; + public static final String ATTR_SCHEME = "scheme"; + public static final String ATTR_TERM = "term"; /** - * Convenience constant containing the "http://hl7.org/fhir/tag/security" scheme - * value + * Convenience constant containing the "http://hl7.org/fhir/tag" scheme value + */ + public static final String HL7_ORG_FHIR_TAG = "http://hl7.org/fhir/tag"; + /** + * Convenience constant containing the "http://hl7.org/fhir/tag/profile" scheme value + */ + public static final String HL7_ORG_PROFILE_TAG = "http://hl7.org/fhir/tag/profile"; + /** + * Convenience constant containing the "http://hl7.org/fhir/tag/security" scheme value */ public static final String HL7_ORG_SECURITY_TAG = "http://hl7.org/fhir/tag/security"; - /** - * Convenience constant containing the "http://hl7.org/fhir/tag/profile" scheme - * value - */ - public static final String HL7_ORG_PROFILE_TAG = "http://hl7.org/fhir/tag/profile"; - - public static final String ATTR_TERM = "term"; - public static final String ATTR_LABEL = "label"; - public static final String ATTR_SCHEME = "scheme"; - + private transient Integer myHashCode; private String myLabel; private String myScheme; private String myTerm; @@ -59,7 +56,7 @@ public class Tag extends BaseElement implements IElement { } public Tag(String theTerm) { - this((String)null, theTerm, null); + this((String) null, theTerm, null); } public Tag(String theScheme, String theTerm, String theLabel) { @@ -119,11 +116,17 @@ public class Tag extends BaseElement implements IElement { @Override public int hashCode() { + if (myHashCode != null) { + System.out.println("Tag alread has hashcode " + myHashCode + " - " + myScheme + " - " + myTerm + " - " + myLabel); + return myHashCode; + } final int prime = 31; int result = 1; result = prime * result + ((myLabel == null) ? 0 : myLabel.hashCode()); result = prime * result + ((myScheme == null) ? 0 : myScheme.hashCode()); result = prime * result + ((myTerm == null) ? 0 : myTerm.hashCode()); + myHashCode = result; + System.out.println("Tag has hashcode " + myHashCode + " - " + myScheme + " - " + myTerm + " - " + myLabel); return result; } @@ -134,16 +137,19 @@ public class Tag extends BaseElement implements IElement { public Tag setLabel(String theLabel) { myLabel = theLabel; + myHashCode = null; return this; } public Tag setScheme(String theScheme) { myScheme = theScheme; + myHashCode = null; return this; } public Tag setTerm(String theTerm) { myTerm = theTerm; + myHashCode = null; return this; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java index 458540976bb..6a669990bf2 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java @@ -20,26 +20,193 @@ package ca.uhn.fhir.model.api; * #L% */ +import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; -public class TagList extends ArrayList { +/** + * A collection of tags present on a single resource. TagList is backed by a {@link LinkedHashSet}, so the order of added tags will be consistent, but duplicates will not be preserved. + * + *

+ * Thread safety: This class is not thread safe + *

+ */ +public class TagList implements Set, Serializable { - private static final long serialVersionUID = 1L; public static final String ATTR_CATEGORY = "category"; public static final String ELEMENT_NAME = "TagList"; + public static final String ELEMENT_NAME_LC = ELEMENT_NAME.toLowerCase(); + private static final long serialVersionUID = 1L; + private transient List myOrderedTags; + private LinkedHashSet myTagSet = new LinkedHashSet(); + + /** + * Constructor + */ + public TagList() { + super(); + } + + @Override + public String toString() { + StringBuilder b = new StringBuilder(); + b.append("TagList[").append(size()).append(" tag(s)]"); + for (Tag next : this) { + b.append("\n * ").append(next.toString()); + } + return b.toString(); + } + + @Override + public boolean add(Tag theE) { + myOrderedTags = null; + return myTagSet.add(theE); + } + + @Override + public boolean addAll(Collection theC) { + myOrderedTags = null; + return myTagSet.addAll(theC); + } + + public Tag addTag() { + myOrderedTags = null; + return addTag(null, null, null); + } public Tag addTag(String theScheme, String theTerm, String theLabel) { Tag retVal = new Tag(theScheme, theTerm, theLabel); add(retVal); + myOrderedTags = null; return retVal; } - public Tag addTag() { - return addTag(null, null, null); + @Override + public void clear() { + myOrderedTags = null; + myTagSet.clear(); } + @Override + public boolean contains(Object theO) { + return myTagSet.contains(theO); + } + + @Override + public boolean containsAll(Collection theC) { + return myTagSet.containsAll(theC); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TagList other = (TagList) obj; + if (myTagSet == null) { + if (other.myTagSet != null) + return false; + } else if (!myTagSet.equals(other.myTagSet)) + return false; + return true; + } + + /** + * Returns the tag at a given index - Note that the TagList is backed by a {@link LinkedHashSet}, so the order of added tags will be consistent, but duplicates will not be preserved. + */ + public Tag get(int theIndex) { + if (myOrderedTags == null) { + myOrderedTags = new ArrayList(); + for (Tag next : myTagSet) { + myOrderedTags.add(next); + } + } + return myOrderedTags.get(theIndex); + } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public Tag set(int theIndex, Tag theElement) { + // throw new UnsupportedOperationException(); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public void add(int theIndex, Tag theElement) { + // myTagSet.add(theElement); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public Tag remove(int theIndex) { + // Tag retVal = myTagSet.remove(theIndex); + // myTagSet.remove(retVal); + // return retVal; + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public int indexOf(Object theO) { + // myTagSet.remove(theO); + // return myTagSet.indexOf(theO); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public int lastIndexOf(Object theO) { + // return myTagSet.lastIndexOf(theO); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public ListIterator listIterator() { + // return myTagSet.listIterator(); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public ListIterator listIterator(int theIndex) { + // return myTagSet.listIterator(theIndex); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public List subList(int theFromIndex, int theToIndex) { + // return myTagSet.subList(theFromIndex, theToIndex); + // } + public Tag getTag(String theScheme, String theTerm) { for (Tag next : this) { if (theScheme.equals(next.getScheme()) && theTerm.equals(next.getTerm())) { @@ -59,4 +226,68 @@ public class TagList extends ArrayList { return retVal; } + @Override + public int hashCode() { + return myTagSet.hashCode(); + } + + @Override + public boolean isEmpty() { + return myTagSet.isEmpty(); + } + + @Override + public Iterator iterator() { + return myTagSet.iterator(); + } + + @Override + public boolean remove(Object theO) { + myOrderedTags = null; + return myTagSet.remove(theO); + } + + @Override + public boolean removeAll(Collection theC) { + myOrderedTags = null; + return myTagSet.removeAll(theC); + } + + @Override + public boolean retainAll(Collection theC) { + myOrderedTags = null; + return myTagSet.retainAll(theC); + } + + @Override + public int size() { + return myTagSet.size(); + } + + @Override + public Object[] toArray() { + return myTagSet.toArray(); + } + + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + // public boolean addAll(int theIndex, Collection theC) { + // myTagSet.addAll(theC); + // return myTagSet.addAll(theC); + // } + // + // /** + // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful + // */ + // @Deprecated + // @Override + + @Override + public T[] toArray(T[] theA) { + return myTagSet.toArray(theA); + } + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java index 4de489f592e..b5b8ba30b66 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java @@ -29,7 +29,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeDeclaredChildDefinition; diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java new file mode 100644 index 00000000000..f00e0bf4b2c --- /dev/null +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java @@ -0,0 +1,67 @@ +package ca.uhn.fhir.model.api; + +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.stringContainsInOrder; +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.dstu.resource.Patient; + +public class TagListTest { + + private FhirContext myCtx = new FhirContext(); + + @Test + public void testEquals() { + TagList tagList1 = new TagList(); + tagList1.addTag(null, "Dog", "Puppies"); + tagList1.addTag("http://foo", "Cat", "Kittens"); + + TagList tagList2 = new TagList(); + tagList2.addTag(null, "Dog", "Puppies"); + tagList2.addTag("http://foo", "Cat", "Kittens"); + + assertEquals(tagList1,tagList2); + } + + @Test + public void testEqualsIgnoresOrder() { + TagList tagList1 = new TagList(); + tagList1.addTag(null, "Dog", "Puppies"); + tagList1.addTag("http://foo", "Cat", "Kittens"); + + TagList tagList2 = new TagList(); + tagList2.addTag("http://foo", "Cat", "Kittens"); + tagList2.addTag(null, "Dog", "Puppies"); + + assertEquals(tagList1,tagList2); + } + + @Test + public void testPreventDuplication() { + + Patient patient = new Patient(); + patient.addIdentifier("urn:system", "testTagsWithCreateAndReadAndSearch"); + patient.addName().addFamily("Tester").addGiven("Joe"); + TagList tagList = new TagList(); + tagList.addTag(null, "Dog", "Puppies"); + // Add this twice + tagList.addTag("http://foo", "Cat", "Kittens"); + tagList.addTag("http://foo", "Cat", "Kittens"); + + patient.getResourceMetadata().put(ResourceMetadataKeyEnum.TAG_LIST, tagList); + + Bundle b = new Bundle(); + b.addResource(patient, myCtx, "http://foo"); + + String encoded = myCtx.newXmlParser().encodeBundleToString(b); + assertThat(encoded, stringContainsInOrder(Arrays.asList("Cat", "Kittens"))); + assertThat(encoded, not(stringContainsInOrder(Arrays.asList("Cat", "Kittens", "Cat", "Kittens")))); + + } + +} diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java index f3c6ae7d9fc..dce096145ac 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java @@ -1189,14 +1189,17 @@ public class FhirResourceDaoTest { patient.addName().addFamily("Tester").addGiven("Joe"); TagList tagList = new TagList(); tagList.addTag(null, "Dog", "Puppies"); + // Add this twice + tagList.addTag("http://foo", "Cat", "Kittens"); tagList.addTag("http://foo", "Cat", "Kittens"); patient.getResourceMetadata().put(ResourceMetadataKeyEnum.TAG_LIST, tagList); MethodOutcome outcome = ourPatientDao.create(patient); - assertNotNull(outcome.getId()); - assertFalse(outcome.getId().isEmpty()); + IdDt patientId = outcome.getId(); + assertNotNull(patientId); + assertFalse(patientId.isEmpty()); - Patient retrieved = ourPatientDao.read(outcome.getId()); + Patient retrieved = ourPatientDao.read(patientId); TagList published = (TagList) retrieved.getResourceMetadata().get(ResourceMetadataKeyEnum.TAG_LIST); assertEquals(2, published.size()); assertEquals("Dog", published.get(0).getTerm()); @@ -1217,6 +1220,23 @@ public class FhirResourceDaoTest { assertEquals("Kittens", published.get(1).getLabel()); assertEquals("http://foo", published.get(1).getScheme()); + ourPatientDao.addTag(patientId, "http://foo", "Cat", "Kittens"); + ourPatientDao.addTag(patientId, "http://foo", "Cow", "Calves"); + + retrieved = ourPatientDao.read(patientId); + published = (TagList) retrieved.getResourceMetadata().get(ResourceMetadataKeyEnum.TAG_LIST); + assertEquals(3, published.size()); + assertEquals("Dog", published.get(0).getTerm()); + assertEquals("Puppies", published.get(0).getLabel()); + assertEquals(null, published.get(0).getScheme()); + assertEquals("Cat", published.get(1).getTerm()); + assertEquals("Kittens", published.get(1).getLabel()); + assertEquals("http://foo", published.get(1).getScheme()); + assertEquals("Cow", published.get(2).getTerm()); + assertEquals("Calves", published.get(2).getLabel()); + assertEquals("http://foo", published.get(2).getScheme()); + + } @Test From 24caec9f44108f4a85a560db34796a0236fea47e Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 30 Sep 2014 11:57:15 -0400 Subject: [PATCH 2/2] Get tags working as a set instead of a list --- .../java/ca/uhn/fhir/model/api/Bundle.java | 8 + .../ca/uhn/fhir/model/api/BundleEntry.java | 7 + .../main/java/ca/uhn/fhir/model/api/Tag.java | 92 ++++--- .../java/ca/uhn/fhir/model/api/TagList.java | 121 +++------ .../java/ca/uhn/fhir/parser/ParserState.java | 63 +++-- .../BaseAddOrDeleteTagsMethodBinding.java | 4 - .../fhir/rest/method/BaseMethodBinding.java | 3 +- .../ca/uhn/fhir/model/api/TagListTest.java | 14 + .../ca/uhn/fhir/parser/JsonParserTest.java | 252 ++++++++---------- .../ca/uhn/fhir/parser/XmlParserTest.java | 2 +- .../fhir/rest/server/DynamicSearchTest.java | 3 +- .../uhn/fhir/rest/server/TagsServerTest.java | 73 +++-- .../ca/uhn/fhir/rest/server/UpdateTest.java | 50 ++-- 13 files changed, 328 insertions(+), 364 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java index b3bc6f7d75a..28e9415c8be 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java @@ -59,12 +59,20 @@ public class Bundle extends BaseBundle /* implements IElement */{ private IntegerDt myTotalResults; private InstantDt myUpdated; + /** + * @deprecated Tags wil become immutable in a future release of HAPI, so {@link #addCategory(String, String, String)} should be used instead + */ public Tag addCategory() { Tag retVal = new Tag(); getCategories().add(retVal); return retVal; } + public void addCategory(String theScheme, String theTerm, String theLabel) { + getCategories().add(new Tag(theScheme, theTerm, theLabel)); + } + + public void addCategory(Tag theTag) { getCategories().add(theTag); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/BundleEntry.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/BundleEntry.java index a8a880a4e72..cdc6f293776 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/BundleEntry.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/BundleEntry.java @@ -50,12 +50,19 @@ public class BundleEntry extends BaseBundle { private StringDt myTitle; private InstantDt myUpdated; + /** + * @deprecated Tags wil become immutable in a future release of HAPI, so {@link #addCategory(String, String, String)} should be used instead + */ public Tag addCategory() { Tag retVal = new Tag(); getCategories().add(retVal); return retVal; } + public void addCategory(String theScheme, String theTerm, String theLabel) { + getCategories().add(new Tag(theScheme, theTerm, theLabel)); + } + public void addCategory(Tag theTag) { getCategories().add(theTag); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java index b38aafa2891..8264b91e283 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java @@ -28,6 +28,13 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +/** + * A single tag + *

+ * Note on equality- When computing hashCode or equals values for this class, only the + * {@link #getScheme() scheme} and + *

+ */ public class Tag extends BaseElement implements IElement { public static final String ATTR_LABEL = "label"; @@ -47,18 +54,28 @@ public class Tag extends BaseElement implements IElement { */ public static final String HL7_ORG_SECURITY_TAG = "http://hl7.org/fhir/tag/security"; - private transient Integer myHashCode; private String myLabel; private String myScheme; private String myTerm; + /** + * @deprecated Tags will become immutable in a future release, so this constructor should not be used. + */ public Tag() { } + /** + * @deprecated There is no reason to create a tag with a term and not a scheme, so this constructor will be removed + */ public Tag(String theTerm) { this((String) null, theTerm, null); } + public Tag(String theScheme, String theTerm) { + myScheme = theScheme; + myTerm = theTerm; + } + public Tag(String theScheme, String theTerm, String theLabel) { myTerm = theTerm; myLabel = theLabel; @@ -75,32 +92,6 @@ public class Tag extends BaseElement implements IElement { myLabel = theLabel; } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Tag other = (Tag) obj; - if (myLabel == null) { - if (other.myLabel != null) - return false; - } else if (!myLabel.equals(other.myLabel)) - return false; - if (myScheme == null) { - if (other.myScheme != null) - return false; - } else if (!myScheme.equals(other.myScheme)) - return false; - if (myTerm == null) { - if (other.myTerm != null) - return false; - } else if (!myTerm.equals(other.myTerm)) - return false; - return true; - } public String getLabel() { return myLabel; @@ -114,19 +105,34 @@ public class Tag extends BaseElement implements IElement { return myTerm; } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Tag other = (Tag) obj; + if (myScheme == null) { + if (other.myScheme != null) + return false; + } else if (!myScheme.equals(other.myScheme)) + return false; + if (myTerm == null) { + if (other.myTerm != null) + return false; + } else if (!myTerm.equals(other.myTerm)) + return false; + return true; + } + @Override public int hashCode() { - if (myHashCode != null) { - System.out.println("Tag alread has hashcode " + myHashCode + " - " + myScheme + " - " + myTerm + " - " + myLabel); - return myHashCode; - } final int prime = 31; int result = 1; - result = prime * result + ((myLabel == null) ? 0 : myLabel.hashCode()); result = prime * result + ((myScheme == null) ? 0 : myScheme.hashCode()); result = prime * result + ((myTerm == null) ? 0 : myTerm.hashCode()); - myHashCode = result; - System.out.println("Tag has hashcode " + myHashCode + " - " + myScheme + " - " + myTerm + " - " + myLabel); return result; } @@ -135,21 +141,33 @@ public class Tag extends BaseElement implements IElement { return StringUtils.isBlank(myLabel) && StringUtils.isBlank(myScheme) && StringUtils.isBlank(myTerm); } + /** + * @deprecated Tags will become immutable in a future release of HAPI in order to facilitate + * ensuring that the TagList acts as an unordered set. Use the constructor to set this field when creating a new + * tag object + */ public Tag setLabel(String theLabel) { myLabel = theLabel; - myHashCode = null; return this; } + /** + * @deprecated Tags will become immutable in a future release of HAPI in order to facilitate + * ensuring that the TagList acts as an unordered set. Use the constructor to set this field when creating a new + * tag object + */ public Tag setScheme(String theScheme) { myScheme = theScheme; - myHashCode = null; return this; } + /** + * @deprecated Tags will become immutable in a future release of HAPI in order to facilitate + * ensuring that the TagList acts as an unordered set. Use the constructor to set this field when creating a new + * tag object + */ public Tag setTerm(String theTerm) { myTerm = theTerm; - myHashCode = null; return this; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java index 6a669990bf2..17f43996aa6 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java @@ -74,11 +74,41 @@ public class TagList implements Set, Serializable { return myTagSet.addAll(theC); } + /** + * @deprecated Tags wil become immutable in a future release of HAPI, so {@link #addTag(String, String, String)} should be used instead + */ public Tag addTag() { myOrderedTags = null; return addTag(null, null, null); } + /** + * Add a new tag instance + * + * @param theScheme + * The tag scheme + * @param theTerm + * The tag term + * @return Returns the newly created tag instance. Note that the tag is added to the list by this method, so you generally do not need to interact directly with the added tag. + */ + public Tag addTag(String theScheme, String theTerm) { + Tag retVal = new Tag(theScheme, theTerm); + add(retVal); + myOrderedTags = null; + return retVal; + } + + /** + * Add a new tag instance + * + * @param theScheme + * The tag scheme + * @param theTerm + * The tag term + * @param theLabel + * The tag label + * @return Returns the newly created tag instance. Note that the tag is added to the list by this method, so you generally do not need to interact directly with the added tag. + */ public Tag addTag(String theScheme, String theTerm, String theLabel) { Tag retVal = new Tag(theScheme, theTerm, theLabel); add(retVal); @@ -131,81 +161,6 @@ public class TagList implements Set, Serializable { } return myOrderedTags.get(theIndex); } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public Tag set(int theIndex, Tag theElement) { - // throw new UnsupportedOperationException(); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public void add(int theIndex, Tag theElement) { - // myTagSet.add(theElement); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public Tag remove(int theIndex) { - // Tag retVal = myTagSet.remove(theIndex); - // myTagSet.remove(retVal); - // return retVal; - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public int indexOf(Object theO) { - // myTagSet.remove(theO); - // return myTagSet.indexOf(theO); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public int lastIndexOf(Object theO) { - // return myTagSet.lastIndexOf(theO); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public ListIterator listIterator() { - // return myTagSet.listIterator(); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public ListIterator listIterator(int theIndex) { - // return myTagSet.listIterator(theIndex); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public List subList(int theFromIndex, int theToIndex) { - // return myTagSet.subList(theFromIndex, theToIndex); - // } public Tag getTag(String theScheme, String theTerm) { for (Tag next : this) { @@ -269,22 +224,6 @@ public class TagList implements Set, Serializable { return myTagSet.toArray(); } - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - // public boolean addAll(int theIndex, Collection theC) { - // myTagSet.addAll(theC); - // return myTagSet.addAll(theC); - // } - // - // /** - // * @deprecated TagList will not implement the {@link List} interface in a future release, as tag order is not supposed to be meaningful - // */ - // @Deprecated - // @Override - @Override public T[] toArray(T[] theA) { return myTagSet.toArray(theA); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java index 87703646578..9eb610ec6cf 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java @@ -158,8 +158,7 @@ class ParserState { } /** - * Invoked after any new XML event is individually processed, containing a copy of the XML event. This is basically - * intended for embedded XHTML content + * Invoked after any new XML event is individually processed, containing a copy of the XML event. This is basically intended for embedded XHTML content */ public void xmlEvent(XMLEvent theNextEvent) { myState.xmlEvent(theNextEvent); @@ -222,35 +221,37 @@ class ParserState { private static final int STATE_TERM = 1; private int myCatState = STATE_NONE; - private Tag myInstance; + private TagList myTagList; + private String myTerm; + private String myLabel; + private String myScheme; - public AtomCategoryState(Tag theEntry) { + public AtomCategoryState(TagList theTagList) { super(null); - myInstance = theEntry; + myTagList = theTagList; } @Override public void attributeValue(String theName, String theValue) throws DataFormatException { if ("term".equals(theName)) { - myInstance.setTerm(theValue); + myTerm = theValue; } else if ("label".equals(theName)) { - myInstance.setLabel(theValue); + myLabel = theValue; } else if ("scheme".equals(theName)) { - myInstance.setScheme(theValue); + myScheme = theValue; } else if ("value".equals(theName)) { /* - * This handles XML parsing, which is odd for this quasi-resource type, since the tag has three values - * instead of one like everything else. + * This handles XML parsing, which is odd for this quasi-resource type, since the tag has three values instead of one like everything else. */ switch (myCatState) { case STATE_LABEL: - myInstance.setLabel(theValue); + myLabel = theValue; break; case STATE_TERM: - myInstance.setTerm(theValue); + myTerm = theValue; break; case STATE_SCHEME: - myInstance.setScheme(theValue); + myScheme = theValue; break; default: super.string(theValue); @@ -265,6 +266,9 @@ class ParserState { if (myCatState != STATE_NONE) { myCatState = STATE_NONE; } else { + if (isNotEmpty(myScheme) || isNotBlank(myTerm) || isNotBlank(myLabel)) { + myTagList.addTag(myScheme, myTerm, myLabel); + } pop(); } } @@ -422,7 +426,7 @@ class ParserState { } else if ("summary".equals(theLocalPart)) { push(new XhtmlState(getPreResourceState(), myEntry.getSummary(), false)); } else if ("category".equals(theLocalPart)) { - push(new AtomCategoryState(myEntry.addCategory())); + push(new AtomCategoryState(myEntry.getCategories())); } else if ("deleted".equals(theLocalPart) && myJsonMode) { // JSON and XML deleted entries are completely different for some reason myDeleted = true; @@ -629,7 +633,7 @@ class ParserState { } else if ("author".equals(theLocalPart)) { push(new AtomAuthorState(myInstance)); } else if ("category".equals(theLocalPart)) { - push(new AtomCategoryState(myInstance.getCategories().addTag())); + push(new AtomCategoryState(myInstance.getCategories())); } else if ("deleted-entry".equals(theLocalPart) && verifyNamespace(XmlParser.TOMBSTONES_NS, theNamespaceURI)) { push(new AtomDeletedEntryState(myInstance, myResourceType)); } else { @@ -707,7 +711,8 @@ class ParserState { } /** - * @param theData The string value + * @param theData + * The string value */ public void string(String theData) { // ignore by default @@ -718,7 +723,8 @@ class ParserState { } /** - * @param theNextEvent The XML event + * @param theNextEvent + * The XML event */ public void xmlEvent(XMLEvent theNextEvent) { // ignore @@ -1275,7 +1281,8 @@ class ParserState { myContext.newTerser().visit(myInstance, new IModelVisitor() { @Override - public void acceptUndeclaredExtension(ISupportsUndeclaredExtensions theContainingElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition theDefinition, ExtensionDt theNextExt) { + public void acceptUndeclaredExtension(ISupportsUndeclaredExtensions theContainingElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition theDefinition, + ExtensionDt theNextExt) { acceptElement(theNextExt.getValue(), null, null); } @@ -1508,7 +1515,7 @@ class ParserState { @Override public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException { if (TagList.ATTR_CATEGORY.equals(theLocalPart)) { - push(new TagState(myTagList.addTag())); + push(new TagState(myTagList)); } else { throw new DataFormatException("Unexpected element: " + theLocalPart); } @@ -1524,11 +1531,14 @@ class ParserState { private static final int SCHEME = 3; private static final int TERM = 1; private int mySubState = 0; - private Tag myTag; + private TagList myTagList; + private String myTerm; + private String myLabel; + private String myScheme; - public TagState(Tag theTag) { + public TagState(TagList theTagList) { super(null); - myTag = theTag; + myTagList = theTagList; } @Override @@ -1537,13 +1547,13 @@ class ParserState { switch (mySubState) { case TERM: - myTag.setTerm(value); + myTerm = (value); break; case LABEL: - myTag.setLabel(value); + myLabel = (value); break; case SCHEME: - myTag.setScheme(value); + myScheme = (value); break; case NONE: // This handles JSON encoding, which is a bit weird @@ -1559,6 +1569,9 @@ class ParserState { if (mySubState != NONE) { mySubState = NONE; } else { + if (isNotEmpty(myScheme) || isNotBlank(myTerm) || isNotBlank(myLabel)) { + myTagList.addTag(myScheme, myTerm, myLabel); + } pop(); } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java index 50fcc6e809f..a7ad2d6dee9 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java @@ -214,10 +214,6 @@ abstract class BaseAddOrDeleteTagsMethodBinding extends BaseMethodBinding return false; } - if ((myVersionIdParamIndex != null) != (theRequest.getId() != null && theRequest.getId().hasVersionIdPart())) { - return false; - } - if (isDelete()) { if (Constants.PARAM_DELETE.equals(theRequest.getSecondaryOperation()) == false) { return false; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java index 0088f34ab6a..65a513af42c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java @@ -20,7 +20,7 @@ package ca.uhn.fhir.rest.method; * #L% */ -import static org.apache.commons.lang3.StringUtils.*; +import static org.apache.commons.lang3.StringUtils.isBlank; import java.io.IOException; import java.io.Reader; @@ -66,7 +66,6 @@ import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.IDynamicSearchResourceProvider; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.rest.server.SearchParameterMap; import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java index f00e0bf4b2c..bcce42d2a6e 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/model/api/TagListTest.java @@ -28,6 +28,20 @@ public class TagListTest { assertEquals(tagList1,tagList2); } + @Test + public void testEqualsIgnoresLabel() { + TagList tagList1 = new TagList(); + tagList1.addTag(null, "Dog", "AAAA"); + tagList1.addTag("http://foo", "Cat", "BBBB"); + + TagList tagList2 = new TagList(); + tagList2.addTag(null, "Dog", "Puppies"); + tagList2.addTag("http://foo", "Cat", "Kittens"); + + assertEquals(tagList1,tagList2); + } + + @Test public void testEqualsIgnoresOrder() { TagList tagList1 = new TagList(); diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/JsonParserTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/JsonParserTest.java index 2a23c196e43..417d15bf3c2 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/JsonParserTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/JsonParserTest.java @@ -63,18 +63,17 @@ public class JsonParserTest { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserTest.class); private static FhirContext ourCtx; - @Test public void testEncodeNarrativeBlockInBundle() { Patient p = new Patient(); p.addIdentifier("foo", "bar"); p.getText().setStatus(NarrativeStatusEnum.GENERATED); p.getText().setDiv("
hello
"); - + Bundle b = new Bundle(); b.getTotalResults().setValue(123); b.addEntry().setResource(p); - + String out = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(b); ourLog.info(out); assertThat(out, containsString("
hello
")); @@ -84,29 +83,28 @@ public class JsonParserTest { ourLog.info(out); // Backslashes need to be escaped because they are in a JSON value assertThat(out, containsString("hello")); - + } - - + @Test public void testEncodingNullExtension() { Patient p = new Patient(); ExtensionDt extension = new ExtensionDt(false, "http://foo#bar"); p.addUndeclaredExtension(extension); String str = ourCtx.newJsonParser().encodeResourceToString(p); - + assertEquals("{\"resourceType\":\"Patient\"}", str); - + extension.setValue(new StringDt()); - str = ourCtx.newJsonParser().encodeResourceToString(p); + str = ourCtx.newJsonParser().encodeResourceToString(p); assertEquals("{\"resourceType\":\"Patient\"}", str); extension.setValue(new StringDt("")); - str = ourCtx.newJsonParser().encodeResourceToString(p); + str = ourCtx.newJsonParser().encodeResourceToString(p); assertEquals("{\"resourceType\":\"Patient\"}", str); - + } @Test @@ -119,79 +117,77 @@ public class JsonParserTest { assertThat(e.getMessage(), containsString("double quote")); } } - + @Test public void testEncodeExtensionInCompositeElement() { - + Conformance c = new Conformance(); c.addRest().getSecurity().addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); - + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(c); ourLog.info(encoded); - + encoded = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(c); ourLog.info(encoded); assertEquals(encoded, "{\"resourceType\":\"Conformance\",\"rest\":[{\"security\":{\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"AAA\"}]}}]}"); - + } @Test public void testEncodeExtensionInPrimitiveElement() { - + Conformance c = new Conformance(); c.getAcceptUnknown().addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); - + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(c); ourLog.info(encoded); - + encoded = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(c); ourLog.info(encoded); assertEquals(encoded, "{\"resourceType\":\"Conformance\",\"_acceptUnknown\":[{\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"AAA\"}]}]}"); // Now with a value ourLog.info("---------------"); - + c = new Conformance(); c.getAcceptUnknown().setValue(true); c.getAcceptUnknown().addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); - + encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(c); ourLog.info(encoded); - + encoded = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(c); ourLog.info(encoded); assertEquals(encoded, "{\"resourceType\":\"Conformance\",\"acceptUnknown\":true,\"_acceptUnknown\":[{\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"AAA\"}]}]}"); } - - @Test public void testEncodeExtensionInResourceElement() { - + Conformance c = new Conformance(); -// c.addRest().getSecurity().addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); + // c.addRest().getSecurity().addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); c.addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); - + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(c); ourLog.info(encoded); - + encoded = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(c); ourLog.info(encoded); assertEquals(encoded, "{\"resourceType\":\"Conformance\",\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"AAA\"}]}"); - + } - + @Test public void testEncodeBinaryResource() { Binary patient = new Binary(); patient.setContentType("foo"); - patient.setContent(new byte[] {1,2,3,4}); - + patient.setContent(new byte[] { 1, 2, 3, 4 }); + String val = ourCtx.newJsonParser().encodeResourceToString(patient); - assertEquals("{\"resourceType\":\"Binary\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}",val); - + assertEquals("{\"resourceType\":\"Binary\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", val); + } @Test @@ -210,84 +206,69 @@ public class JsonParserTest { " }" + "}"; //@formatter:on - + Patient res = (Patient) ourCtx.newJsonParser().parseResource(text); String value = res.getText().getDiv().getValueAsString(); - + assertNull(value); } - - @Test public void testNestedContainedResources() { Observation A = new Observation(); A.getName().setText("A"); - + Observation B = new Observation(); B.getName().setText("B"); A.addRelated().setTarget(new ResourceReferenceDt(B)); - + Observation C = new Observation(); C.getName().setText("C"); B.addRelated().setTarget(new ResourceReferenceDt(C)); String str = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(A); ourLog.info(str); - + assertThat(str, stringContainsInOrder(Arrays.asList("\"text\":\"B\"", "\"text\":\"C\"", "\"text\":\"A\""))); - + // Only one (outer) contained block int idx0 = str.indexOf("\"contained\""); - int idx1 = str.indexOf("\"contained\"",idx0+1); - + int idx1 = str.indexOf("\"contained\"", idx0 + 1); + assertNotEquals(-1, idx0); assertEquals(-1, idx1); - + Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, str); - assertEquals("A",obs.getName().getText().getValue()); - + assertEquals("A", obs.getName().getText().getValue()); + Observation obsB = (Observation) obs.getRelatedFirstRep().getTarget().getResource(); - assertEquals("B",obsB.getName().getText().getValue()); + assertEquals("B", obsB.getName().getText().getValue()); Observation obsC = (Observation) obsB.getRelatedFirstRep().getTarget().getResource(); - assertEquals("C",obsC.getName().getText().getValue()); + assertEquals("C", obsC.getName().getText().getValue()); - } - + @Test public void testParseQuery() { - String msg = "{\n" + - " \"resourceType\": \"Query\",\n" + - " \"text\": {\n" + - " \"status\": \"generated\",\n" + - " \"div\": \"
[Put rendering here]
\"\n" + - " },\n" + - " \"identifier\": \"urn:uuid:42b253f5-fa17-40d0-8da5-44aeb4230376\",\n" + - " \"parameter\": [\n" + - " {\n" + - " \"url\": \"http://hl7.org/fhir/query#_query\",\n" + - " \"valueString\": \"example\"\n" + - " }\n" + - " ]\n" + - "}"; + String msg = "{\n" + " \"resourceType\": \"Query\",\n" + " \"text\": {\n" + " \"status\": \"generated\",\n" + " \"div\": \"
[Put rendering here]
\"\n" + " },\n" + + " \"identifier\": \"urn:uuid:42b253f5-fa17-40d0-8da5-44aeb4230376\",\n" + " \"parameter\": [\n" + " {\n" + " \"url\": \"http://hl7.org/fhir/query#_query\",\n" + + " \"valueString\": \"example\"\n" + " }\n" + " ]\n" + "}"; Query query = ourCtx.newJsonParser().parseResource(Query.class, msg); - + assertEquals("urn:uuid:42b253f5-fa17-40d0-8da5-44aeb4230376", query.getIdentifier().getValueAsString()); assertEquals("http://hl7.org/fhir/query#_query", query.getParameterFirstRep().getUrlAsString()); assertEquals("example", query.getParameterFirstRep().getValueAsPrimitive().getValueAsString()); - + } - + @Test public void testEncodeQuery() { Query q = new Query(); ExtensionDt parameter = q.addParameter(); parameter.setUrl("http://hl7.org/fhir/query#_query").setValue(new StringDt("example")); - - + String val = new FhirContext().newJsonParser().encodeResourceToString(q); ourLog.info(val); @@ -307,21 +288,21 @@ public class JsonParserTest { ourLog.info("Expect: {}", expected); ourLog.info("Got : {}", val); assertEquals(expected, val); - + } - + @Test public void testParseBinaryResource() { Binary val = ourCtx.newJsonParser().parseResource(Binary.class, "{\"resourceType\":\"Binary\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}"); assertEquals("foo", val.getContentType()); - assertArrayEquals(new byte[] {1,2,3,4}, val.getContent()); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, val.getContent()); } - + @Test public void testTagList() { - + //@formatter:off String tagListStr = "{\n" + " \"resourceType\" : \"TagList\", " + @@ -343,7 +324,7 @@ public class JsonParserTest { " ] " + "}"; //@formatter:on - + TagList tagList = new FhirContext().newJsonParser().parseTagList(tagListStr); assertEquals(3, tagList.size()); assertEquals("term0", tagList.get(0).getTerm()); @@ -355,7 +336,7 @@ public class JsonParserTest { assertEquals("term2", tagList.get(2).getTerm()); assertEquals("label2", tagList.get(2).getLabel()); assertEquals(null, tagList.get(2).getScheme()); - + /* * Encode */ @@ -380,28 +361,28 @@ public class JsonParserTest { "]" + "}"; //@formatter:on - + String encoded = new FhirContext().newJsonParser().encodeTagListToString(tagList); - assertEquals(expected,encoded); + assertEquals(expected, encoded); } - + @Test public void testEncodeBundleCategory() { Bundle b = new Bundle(); BundleEntry e = b.addEntry(); e.setResource(new Patient()); - b.addCategory().setLabel("label").setTerm("term").setScheme("scheme"); - + b.addCategory("scheme", "term", "label"); + String val = new FhirContext().newJsonParser().setPrettyPrint(false).encodeBundleToString(b); ourLog.info(val); assertThat(val, StringContains.containsString("\"category\":[{\"term\":\"term\",\"label\":\"label\",\"scheme\":\"scheme\"}]")); - + b = new FhirContext().newJsonParser().parseBundle(val); - assertEquals(1,b.getEntries().size()); - assertEquals(1,b.getCategories().size()); + assertEquals(1, b.getEntries().size()); + assertEquals(1, b.getCategories().size()); assertEquals("term", b.getCategories().get(0).getTerm()); assertEquals("label", b.getCategories().get(0).getLabel()); assertEquals("scheme", b.getCategories().get(0).getScheme()); @@ -415,23 +396,23 @@ public class JsonParserTest { Bundle b = new Bundle(); BundleEntry e = b.addEntry(); e.setResource(new Patient()); - e.addCategory().setLabel("label").setTerm("term").setScheme("scheme"); - - String val = new FhirContext().newJsonParser().setPrettyPrint(false).encodeBundleToString(b); + e.addCategory("scheme", "term", "label"); + + String val = ourCtx.newJsonParser().setPrettyPrint(false).encodeBundleToString(b); ourLog.info(val); assertThat(val, StringContains.containsString("\"category\":[{\"term\":\"term\",\"label\":\"label\",\"scheme\":\"scheme\"}]")); - - b = new FhirContext().newJsonParser().parseBundle(val); - assertEquals(1,b.getEntries().size()); - assertEquals(1,b.getEntries().get(0).getCategories().size()); + + b = ourCtx.newJsonParser().parseBundle(val); + assertEquals(1, b.getEntries().size()); + assertEquals(1, b.getEntries().get(0).getCategories().size()); assertEquals("term", b.getEntries().get(0).getCategories().get(0).getTerm()); assertEquals("label", b.getEntries().get(0).getCategories().get(0).getLabel()); assertEquals("scheme", b.getEntries().get(0).getCategories().get(0).getScheme()); assertNull(b.getEntries().get(0).getResource()); } - + @Test public void testEncodeContainedResources() throws IOException { @@ -450,7 +431,6 @@ public class JsonParserTest { parseAndEncode("/alert.profile.json"); } - private void parseAndEncode(String name) throws IOException { String msg = IOUtils.toString(XmlParser.class.getResourceAsStream(name)); ourLog.info(msg); @@ -465,16 +445,14 @@ public class JsonParserTest { JSON actual = JSONSerializer.toJSON(encoded.trim()); String exp = expected.toString().replace("\\r\\n", "\\n").replace("§", "ยง"); - String act = actual.toString().replace("\\r\\n","\\n"); - + String act = actual.toString().replace("\\r\\n", "\\n"); + ourLog.info("Expected: {}", exp); ourLog.info("Actual : {}", act); - + assertEquals(exp, act); } - - @Test public void testEncodeContainedResourcesMore() { @@ -517,8 +495,7 @@ public class JsonParserTest { assertEquals("line1", ref.getLineFirstRep().getValue()); } - - + @Test public void testEncodeDeclaredExtensionWithResourceContent() { IParser parser = new FhirContext().newJsonParser(); @@ -543,20 +520,18 @@ public class JsonParserTest { ValueSet valueSet = new ValueSet(); valueSet.addTelecom().addUndeclaredExtension(false, "http://foo", new StringDt("AAA")); - + String encoded = ourCtx.newJsonParser().encodeResourceToString(valueSet); assertThat(encoded, containsString("\"telecom\":[{\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"AAA\"}]}")); - + } - - @Test public void testEncodeExt() throws Exception { ValueSet valueSet = new ValueSet(); valueSet.setId("123456"); - + Define define = valueSet.getDefine(); DefineConcept code = define.addConcept(); code.setCode("someCode"); @@ -567,11 +542,12 @@ public class JsonParserTest { ourLog.info(encoded); assertThat(encoded, not(containsString("123456"))); - assertEquals("{\"resourceType\":\"ValueSet\",\"define\":{\"concept\":[{\"extension\":[{\"url\":\"urn:alt\",\"valueString\":\"alt name\"}],\"code\":\"someCode\",\"display\":\"someDisplay\"}]}}", encoded); - + assertEquals( + "{\"resourceType\":\"ValueSet\",\"define\":{\"concept\":[{\"extension\":[{\"url\":\"urn:alt\",\"valueString\":\"alt name\"}],\"code\":\"someCode\",\"display\":\"someDisplay\"}]}}", + encoded); + } - @Test public void testEncodeExtensionWithResourceContent() { IParser parser = new FhirContext().newJsonParser(); @@ -593,7 +569,6 @@ public class JsonParserTest { } - @Test public void testEncodeInvalidChildGoodException() { Observation obs = new Observation(); @@ -607,7 +582,7 @@ public class JsonParserTest { assertThat(e.getMessage(), StringContains.containsString("DecimalDt")); } } - + @Test public void testEncodeResourceRef() throws DataFormatException { @@ -662,7 +637,7 @@ public class JsonParserTest { String enc = new FhirContext().newJsonParser().encodeResourceToString(patient); ourLog.info(enc); assertEquals("{\"resourceType\":\"Patient\",\"name\":[{\"extension\":[{\"url\":\"http://examples.com#givenext\",\"valueString\":\"Hello\"}],\"family\":[\"Shmoe\"],\"given\":[\"Joe\"]}]}", enc); - + IParser newJsonParser = new FhirContext().newJsonParser(); StringReader reader = new StringReader(enc); Patient parsed = newJsonParser.parseResource(Patient.class, reader); @@ -753,7 +728,7 @@ public class JsonParserTest { assertEquals("http://scheme", bundle.getCategories().get(0).getScheme()); assertEquals("http://term", bundle.getCategories().get(0).getTerm()); assertEquals("label", bundle.getCategories().get(0).getLabel()); - + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(bundle); ourLog.info(encoded); @@ -768,9 +743,9 @@ public class JsonParserTest { DiagnosticReport res = (DiagnosticReport) entry.getResource(); assertEquals("Complete Blood Count", res.getName().getText().getValue()); - + assertThat(entry.getSummary().getValueAsString(), containsString("CBC Report for Wile")); - + } @Test @@ -790,10 +765,9 @@ public class JsonParserTest { BundleEntry deletedEntry = bundle.getEntries().get(3); assertEquals("2014-06-20T20:15:49Z", deletedEntry.getDeletedAt().getValueAsString()); - + } - /** * This sample has extra elements in that are not actually a part of the spec any more.. */ @@ -828,10 +802,10 @@ public class JsonParserTest { public static void beforeClass() { ourCtx = new FhirContext(); } - + @Test public void testParseBundleDeletedEntry() { - + //@formatter:off String bundleString = "{" + @@ -851,7 +825,7 @@ public class JsonParserTest { "]" + "}"; //@formatter:on - + Bundle bundle = ourCtx.newJsonParser().parseBundle(bundleString); BundleEntry entry = bundle.getEntries().get(0); assertEquals("2012-05-29T23:45:32+00:00", entry.getDeletedAt().getValueAsString()); @@ -859,19 +833,19 @@ public class JsonParserTest { assertEquals("1", entry.getResource().getId().getIdPart()); assertEquals("2", entry.getResource().getId().getVersionIdPart()); assertEquals(new InstantDt("2012-05-29T23:45:32+00:00"), entry.getResource().getResourceMetadata().get(ResourceMetadataKeyEnum.DELETED_AT)); - + // Now encode - + ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(bundle)); String encoded = ourCtx.newJsonParser().encodeBundleToString(bundle); - assertEquals(bundleString,encoded); - + assertEquals(bundleString, encoded); + } - + @Test public void testEncodeBundle() throws InterruptedException { - Bundle b= new Bundle(); - + Bundle b = new Bundle(); + InstantDt pub = InstantDt.withCurrentTime(); b.setPublished(pub); Thread.sleep(2); @@ -882,35 +856,34 @@ public class JsonParserTest { entry.getId().setValue("1"); entry.setResource(p1); entry.getSummary().setValueAsString("this is the summary"); - + Patient p2 = new Patient(); p2.addName().addFamily("Family2"); entry = b.addEntry(); entry.getId().setValue("2"); entry.setLinkAlternate(new StringDt("http://foo/bar")); entry.setResource(p2); - + BundleEntry deletedEntry = b.addEntry(); deletedEntry.setId(new IdDt("Patient/3")); InstantDt nowDt = InstantDt.withCurrentTime(); deletedEntry.setDeleted(nowDt); - + String bundleString = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(b); ourLog.info(bundleString); List strings = new ArrayList(); - strings.addAll(Arrays.asList("\"published\":\""+pub.getValueAsString()+"\"")); + strings.addAll(Arrays.asList("\"published\":\"" + pub.getValueAsString() + "\"")); strings.addAll(Arrays.asList("\"id\":\"1\"")); strings.addAll(Arrays.asList("this is the summary")); strings.addAll(Arrays.asList("\"id\":\"2\"", "\"rel\":\"alternate\"", "\"href\":\"http://foo/bar\"")); - strings.addAll(Arrays.asList("\"deleted\":\""+nowDt.getValueAsString()+"\"", "\"id\":\"Patient/3\"")); + strings.addAll(Arrays.asList("\"deleted\":\"" + nowDt.getValueAsString() + "\"", "\"id\":\"Patient/3\"")); assertThat(bundleString, StringContainsInOrder.stringContainsInOrder(strings)); - + b.getEntries().remove(2); bundleString = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(b); assertThat(bundleString, not(containsString("deleted"))); - - + } @Test @@ -964,7 +937,7 @@ public class JsonParserTest { // The encoded escapes quote marks using XML escaping instead of JSON escaping, which is probably nicer anyhow... String exp = expected.toString().replace("\\\"Jim\\\"", ""Jim""); String act = actual.toString(); - + ourLog.info("Expected: {}", exp); ourLog.info("Actual : {}", act); assertEquals(exp, act); @@ -978,11 +951,10 @@ public class JsonParserTest { public void testParseWithIncorrectReference() throws IOException { String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json")); jsonString = jsonString.replace("\"reference\"", "\"resource\""); - Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class,jsonString); + Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, jsonString); assertEquals("Organization/1", parsed.getManagingOrganization().getReference().getValue()); } - - + @Test public void testSimpleResourceEncodeWithCustomType() throws IOException { @@ -1011,7 +983,7 @@ public class JsonParserTest { // The encoded escapes quote marks using XML escaping instead of JSON escaping, which is probably nicer anyhow... String exp = expected.toString().replace("\\\"Jim\\\"", ""Jim""); String act = actual.toString(); - + ourLog.info("Expected: {}", exp); ourLog.info("Actual : {}", act); assertEquals(exp, act); diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java index 6ed70a4b47e..f661783f913 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java @@ -199,7 +199,7 @@ public class XmlParserTest { Bundle b = new Bundle(); BundleEntry e = b.addEntry(); e.setResource(new Patient()); - e.addCategory().setLabel("label").setTerm("term").setScheme("scheme"); + e.addCategory("scheme", "term", "label"); String val = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(b); ourLog.info(val); diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/DynamicSearchTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/DynamicSearchTest.java index 8077e824ffb..f9c7229ed69 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/DynamicSearchTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/DynamicSearchTest.java @@ -1,7 +1,6 @@ package ca.uhn.fhir.rest.server; -import static org.apache.commons.lang.StringUtils.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.List; diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/TagsServerTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/TagsServerTest.java index 4dd44808f91..1fa5a8f7739 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/TagsServerTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/TagsServerTest.java @@ -1,11 +1,12 @@ package ca.uhn.fhir.rest.server; -import static org.apache.commons.lang.StringUtils.*; -import static org.junit.Assert.*; +import static org.apache.commons.lang.StringUtils.defaultString; +import static org.junit.Assert.assertEquals; import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; @@ -33,7 +34,6 @@ import ca.uhn.fhir.rest.annotation.DeleteTags; import ca.uhn.fhir.rest.annotation.GetTags; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.TagListParam; -import ca.uhn.fhir.rest.annotation.VersionIdParam; import ca.uhn.fhir.util.PortUtil; /** @@ -94,17 +94,6 @@ public class TagsServerTest { assertEquals(tagList, ourLastTagList); } - @Test - public void testEquals() { - TagList list1 = ourProvider.getAllTagsPatient(); - TagList list2 = ourProvider.getAllTagsPatient(); - assertEquals(list1, list2); - - list1 = ourProvider.getAllTagsPatient(); - list2 = ourProvider.getAllTagsPatient(); - list2.get(0).setTerm("!!!!!"); - assertNotEquals(list1, list2); - } @Test public void testGetAllTags() throws Exception { @@ -134,8 +123,10 @@ public class TagsServerTest { assertEquals(200, status.getStatusLine().getStatusCode()); TagList actual = ourCtx.newXmlParser().parseTagList(responseContent); - TagList expected = ourProvider.getAllTags(); - expected.get(0).setTerm("Patient"); + + TagList expected = new TagList(); + expected.addTag(null, "Patient", "DogLabel"); + expected.addTag("http://cats", "AllCat", "CatLabel"); assertEquals(expected, actual); } @@ -152,8 +143,10 @@ public class TagsServerTest { assertEquals(200, status.getStatusLine().getStatusCode()); TagList actual = ourCtx.newXmlParser().parseTagList(responseContent); - TagList expected = ourProvider.getAllTags(); - expected.get(0).setTerm("Patient111"); + + TagList expected = new TagList(); + expected.addTag(null, "Patient111", "DogLabel"); + expected.addTag("http://cats", "AllCat", "CatLabel"); assertEquals(expected, actual); } @@ -170,8 +163,9 @@ public class TagsServerTest { assertEquals(200, status.getStatusLine().getStatusCode()); TagList actual = ourCtx.newXmlParser().parseTagList(responseContent); - TagList expected = ourProvider.getAllTags(); - expected.get(0).setTerm("Patient111222"); + TagList expected = new TagList(); + expected.addTag(null, "Patient111222", "DogLabel"); + expected.addTag("http://cats", "AllCat", "CatLabel"); assertEquals(expected, actual); } @@ -188,8 +182,9 @@ public class TagsServerTest { assertEquals(200, status.getStatusLine().getStatusCode()); TagList actual = ourCtx.newXmlParser().parseTagList(responseContent); - TagList expected = ourProvider.getAllTags(); - expected.get(0).setTerm("Patient111222"); + TagList expected = new TagList(); + expected.addTag(null, "Patient111222", "DogLabel"); + expected.addTag("http://cats", "AllCat", "CatLabel"); assertEquals(expected, actual); } @@ -261,15 +256,9 @@ public class TagsServerTest { public static class DummyProvider { - @AddTags(type = Patient.class) - public void addTagsPatient(@IdParam IdDt theId, @VersionIdParam IdDt theVersion, @TagListParam TagList theTagList) { - ourLastOutcome = "add" + theId.getIdPart() + theVersion.getVersionIdPart(); - ourLastTagList=theTagList; - } - @AddTags(type = Patient.class) public void addTagsPatient(@IdParam IdDt theId, @TagListParam TagList theTagList) { - ourLastOutcome = "add" + theId.getIdPart(); + ourLastOutcome = "add" + theId.getIdPart() + StringUtils.defaultString(theId.getVersionIdPart()); ourLastTagList=theTagList; } @@ -297,13 +286,13 @@ public class TagsServerTest { return tagList; } - @GetTags(type = Patient.class) - public TagList getAllTagsPatientIdVersion(@IdParam IdDt theId, @VersionIdParam IdDt theVersion) { - TagList tagList = new TagList(); - tagList.add(new Tag((String) null, "Patient" + theId.getIdPart() + theVersion.getVersionIdPart(), "DogLabel")); - tagList.add(new Tag("http://cats", "AllCat", "CatLabel")); - return tagList; - } +// @GetTags(type = Patient.class) +// public TagList getAllTagsPatientIdVersion(@IdParam IdDt theId, @VersionIdParam IdDt theVersion) { +// TagList tagList = new TagList(); +// tagList.add(new Tag((String) null, "Patient" + theId.getIdPart() + theVersion.getVersionIdPart(), "DogLabel")); +// tagList.add(new Tag("http://cats", "AllCat", "CatLabel")); +// return tagList; +// } @GetTags(type = Observation.class) public TagList getAllTagsObservationIdVersion(@IdParam IdDt theId) { @@ -313,15 +302,15 @@ public class TagsServerTest { return tagList; } - @DeleteTags(type = Patient.class) - public void RemoveTagsPatient(@IdParam IdDt theId, @VersionIdParam IdDt theVersion, @TagListParam TagList theTagList) { - ourLastOutcome = "Remove" + theId.getIdPart() + theVersion.getVersionIdPart(); - ourLastTagList=theTagList; - } +// @DeleteTags(type = Patient.class) +// public void RemoveTagsPatient(@IdParam IdDt theId, @VersionIdParam IdDt theVersion, @TagListParam TagList theTagList) { +// ourLastOutcome = "Remove" + theId.getIdPart() + theVersion.getVersionIdPart(); +// ourLastTagList=theTagList; +// } @DeleteTags(type = Patient.class) public void RemoveTagsPatient(@IdParam IdDt theId, @TagListParam TagList theTagList) { - ourLastOutcome = "Remove" + theId.getIdPart(); + ourLastOutcome = "Remove" + theId.getIdPart()+StringUtils.defaultString(theId.getVersionIdPart()); ourLastTagList=theTagList; } diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java index 02f22db4e5c..e0efbc86b14 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java @@ -141,23 +141,25 @@ public class UpdateTest { dr.addCodedDiagnosis().addCoding().setCode("AAA"); HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); - httpPost.addHeader("Category", "Dog, Cat"); + httpPost.addHeader("Category", "Dog; scheme=\"urn:animals\", Cat; scheme=\"urn:animals\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); CloseableHttpResponse status = ourClient.execute(httpPost); - assertEquals(2, ourReportProvider.getLastTags().size()); - assertEquals(new Tag("Dog"), ourReportProvider.getLastTags().get(0)); - assertEquals(new Tag("Cat"), ourReportProvider.getLastTags().get(1)); IOUtils.closeQuietly(status.getEntity().getContent()); + + assertEquals(2, ourReportProvider.getLastTags().size()); + assertEquals(new Tag("urn:animals", "Dog"), ourReportProvider.getLastTags().get(0)); + assertEquals(new Tag("urn:animals", "Cat"), ourReportProvider.getLastTags().get(1)); httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); - httpPost.addHeader("Category", "Dog; label=\"aa\", Cat; label=\"bb\""); + httpPost.addHeader("Category", "Dog; label=\"aa\"; scheme=\"urn:animals\", Cat; label=\"bb\"; scheme=\"urn:animals\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); status = ourClient.execute(httpPost); - assertEquals(2, ourReportProvider.getLastTags().size()); - assertEquals(new Tag((String) null, "Dog", "aa"), ourReportProvider.getLastTags().get(0)); - assertEquals(new Tag((String) null, "Cat", "bb"), ourReportProvider.getLastTags().get(1)); IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(2, ourReportProvider.getLastTags().size()); + assertEquals(new Tag("urn:animals", "Dog", "aa"), ourReportProvider.getLastTags().get(0)); + assertEquals(new Tag("urn:animals", "Cat", "bb"), ourReportProvider.getLastTags().get(1)); + } @Test @@ -167,13 +169,14 @@ public class UpdateTest { dr.addCodedDiagnosis().addCoding().setCode("AAA"); HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); - httpPost.addHeader("Category", "Dog"); + httpPost.addHeader("Category", "Dog; scheme=\"animals\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); CloseableHttpResponse status = ourClient.execute(httpPost); - assertEquals(1, ourReportProvider.getLastTags().size()); - assertEquals(new Tag("Dog"), ourReportProvider.getLastTags().get(0)); IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(1, ourReportProvider.getLastTags().size()); + assertEquals(new Tag("animals", "Dog"), ourReportProvider.getLastTags().get(0)); + } @Test @@ -186,17 +189,19 @@ public class UpdateTest { httpPost.addHeader("Category", "Dog; scheme=\"http://foo\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); CloseableHttpResponse status = ourClient.execute(httpPost); + IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", null), ourReportProvider.getLastTags().get(0)); - IOUtils.closeQuietly(status.getEntity().getContent()); httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog; scheme=\"http://foo\";"); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); ourClient.execute(httpPost); + IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", null), ourReportProvider.getLastTags().get(0)); - IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -218,9 +223,10 @@ public class UpdateTest { httpPost.addHeader("Category", "Dog; scheme=\"http://foo\"; label=\"aaaa\"; "); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); status=ourClient.execute(httpPost); + IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", "aaaa"), ourReportProvider.getLastTags().get(0)); - IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -235,6 +241,7 @@ public class UpdateTest { httpPut.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); HttpResponse status = ourClient.execute(httpPut); + IOUtils.closeQuietly(status.getEntity().getContent()); // String responseContent = // IOUtils.toString(status.getEntity().getContent()); @@ -242,7 +249,6 @@ public class UpdateTest { assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals("http://localhost:" + ourPort + "/DiagnosticReport/001/_history/002", status.getFirstHeader("Location").getValue()); - IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -257,10 +263,11 @@ public class UpdateTest { httpPut.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); CloseableHttpResponse status = ourClient.execute(httpPut); - assertEquals(400, status.getStatusLine().getStatusCode()); String responseContent = IOUtils.toString(status.getEntity().getContent()); - ourLog.info("Response was:\n{}", responseContent); IOUtils.closeQuietly(status.getEntity().getContent()); + + assertEquals(400, status.getStatusLine().getStatusCode()); + ourLog.info("Response was:\n{}", responseContent); } @@ -289,9 +296,11 @@ public class UpdateTest { HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Organization/001"); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - CloseableHttpResponse response = ourClient.execute(httpPost); - assertEquals(200, response.getStatusLine().getStatusCode()); - response.close(); + CloseableHttpResponse status = ourClient.execute(httpPost); + IOUtils.closeQuietly(status.getEntity().getContent()); + + assertEquals(200, status.getStatusLine().getStatusCode()); + status.close(); } @@ -376,6 +385,7 @@ public class UpdateTest { return Observation.class; } + @SuppressWarnings("unused") @Update() public MethodOutcome updateDiagnosticReportWithVersion(@IdParam IdDt theId, @ResourceParam DiagnosticOrder thePatient) { /*