From 80826b62fd17d32aa9a1a5c0e4e57e01ad45f9d3 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Thu, 20 Mar 2014 13:49:51 -0400 Subject: [PATCH] Fix unit tests --- hapi-fhir-base/pom.xml | 7 ++ .../ca/uhn/fhir/model/primitive/IdDt.java | 74 ++++++++++++------- .../ca/uhn/fhir/model/primitive/XhtmlDt.java | 33 ++++++++- .../java/ca/uhn/fhir/parser/JsonParser.java | 3 +- .../uhn/fhir/rest/server/RestfulServer.java | 4 +- .../ca/uhn/fhir/parser/JsonParserTest.java | 3 + pom.xml | 1 + 7 files changed, 92 insertions(+), 33 deletions(-) diff --git a/hapi-fhir-base/pom.xml b/hapi-fhir-base/pom.xml index f495672536a..a2bda0af738 100644 --- a/hapi-fhir-base/pom.xml +++ b/hapi-fhir-base/pom.xml @@ -168,6 +168,13 @@ 0.8 test + + org.hamcrest + hamcrest-all + ${hamcrest_version} + test + + diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java index b33761209af..27ccadb9ca2 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java @@ -1,30 +1,37 @@ package ca.uhn.fhir.model.primitive; +import java.math.BigDecimal; + import ca.uhn.fhir.model.api.BasePrimitive; import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.model.api.annotation.SimpleSetter; import ca.uhn.fhir.parser.DataFormatException; +/** + * Represents the FHIR ID type. This is the actual resource ID, meaning the ID that + * will be used in RESTful URLs, Resource References, etc. to represent a specific + * instance of a resource. + * + *

+ * Description: A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length + * limit of 36 characters. + *

+ *

+ * regex: [a-z0-9\-\.]{1,36} + *

+ */ @DatatypeDef(name = "id") public class IdDt extends BasePrimitive { private String myValue; /** - * Create a new ID. - * - *

- * Description: - * A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length limit of 36 characters. - *

- *

- * regex: [a-z0-9\-\.]{1,36} - *

+ * Create a new empty ID */ public IdDt() { super(); } - + /** * Create a new ID using a long */ @@ -33,21 +40,32 @@ public class IdDt extends BasePrimitive { } /** - * Create a new ID - * + * Create a new ID using a string + * *

- * Description: - * A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length limit of 36 characters. + * Description: A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length + * limit of 36 characters. *

*

- * regex: [a-z0-9\-\.]{1,36} - *

+ * regex: [a-z0-9\-\.]{1,36} + *

*/ @SimpleSetter - public IdDt(@SimpleSetter.Parameter(name="theId") String theValue) { + public IdDt(@SimpleSetter.Parameter(name = "theId") String theValue) { setValue(theValue); } + /** + * Create a new ID, using a BigDecimal input. Uses {@link BigDecimal#toPlainString()} to generate the string representation. + */ + public IdDt(BigDecimal thePid) { + if (thePid != null) { + setValue(thePid.toPlainString()); + } else { + setValue(null); + } + } + @Override public String getValue() { return myValue; @@ -60,14 +78,14 @@ public class IdDt extends BasePrimitive { /** * Set the value - * + * *

- * Description: - * A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length limit of 36 characters. + * Description: A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length + * limit of 36 characters. *

*

- * regex: [a-z0-9\-\.]{1,36} - *

+ * regex: [a-z0-9\-\.]{1,36} + *

*/ @Override public void setValue(String theValue) throws DataFormatException { @@ -77,20 +95,20 @@ public class IdDt extends BasePrimitive { /** * Set the value - * + * *

- * Description: - * A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length limit of 36 characters. + * Description: A whole number in the range 0 to 2^64-1 (optionally represented in hex), a uuid, an oid, or any other combination of lowercase letters, numerals, "-" and ".", with a length + * limit of 36 characters. *

*

- * regex: [a-z0-9\-\.]{1,36} - *

+ * regex: [a-z0-9\-\.]{1,36} + *

*/ @Override public void setValueAsString(String theValue) throws DataFormatException { setValue(theValue); } - + @Override public String toString() { return myValue; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/XhtmlDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/XhtmlDt.java index 2f976b582a3..7f126d3f7cd 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/XhtmlDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/XhtmlDt.java @@ -16,6 +16,7 @@ import javax.xml.stream.events.XMLEvent; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.model.api.BasePrimitive; import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.model.api.annotation.SimpleSetter; import ca.uhn.fhir.parser.DataFormatException; @DatatypeDef(name = "xhtml") @@ -23,13 +24,43 @@ public class XhtmlDt extends BasePrimitive> { private List myValue; + /** + * Constructor + */ + public XhtmlDt() { + // nothing + } + + /** + * Constructor which accepts a string code + * + * @see #setValueAsString(String) for a description of how this value is applied + */ + @SimpleSetter() + public XhtmlDt(@SimpleSetter.Parameter(name = "theTextDiv") String theTextDiv) { + setValueAsString(theTextDiv); + } + + /** + * Accepts a textual DIV and parses it into XHTML events which are stored internally. + *

+ * Formatting note: The text will be trimmed {@link String#trim()}. If the text does not start with + * an HTML tag (generally this would be a div tag), a div tag will be automatically + * placed surrounding the text. + *

+ */ @Override public void setValueAsString(String theValue) throws DataFormatException { if (theValue == null) { myValue = null; return; } - String val = "" + theValue + ""; + + String val = theValue.trim(); + if (!val.startsWith("<")) { + val = "
" + val + "
"; + } + try { ArrayList value = new ArrayList(); XMLEventReader er = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(val)); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index 69153e0753b..ca9ae7daf05 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -462,7 +462,8 @@ public class JsonParser extends BaseParser implements IParser { } if (!haveContent) { - theEventWriter.writeEnd(); +// theEventWriter.writeEnd(); + theEventWriter.flush(); // TODO: remove theEventWriter.writeNull(); } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java index d2df5007d55..88833a06627 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java @@ -7,7 +7,6 @@ import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.StringTokenizer; @@ -19,7 +18,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; -import org.hamcrest.core.IsNot; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; @@ -37,8 +35,8 @@ import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.MethodNotFoundException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; -import ca.uhn.fhir.rest.server.provider.ServerProfileProvider; import ca.uhn.fhir.rest.server.provider.ServerConformanceProvider; +import ca.uhn.fhir.rest.server.provider.ServerProfileProvider; public abstract class RestfulServer extends HttpServlet { 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 c7ac2e4f7b3..dbdc2cb259b 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 @@ -3,6 +3,7 @@ package ca.uhn.fhir.parser; import static org.junit.Assert.*; import java.io.IOException; +import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.List; @@ -34,6 +35,8 @@ public class JsonParserTest { UndeclaredExtension undeclaredExtension = undeclaredExtensions.get(0); assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl()); + ctx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out)); + String encoded = ctx.newJsonParser().encodeResourceToString(obs); ourLog.info(encoded); diff --git a/pom.xml b/pom.xml index 8d363d369d8..f0f9dd51877 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ UTF-8 2.9.1 1.6.6 + 1.3