diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java index 6344f6fbdb4..cf689c9ac88 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java @@ -2,7 +2,9 @@ package ca.uhn.fhir.rest.server; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import java.util.concurrent.TimeUnit; @@ -23,8 +25,8 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu2.resource.Bundle; import ca.uhn.fhir.model.dstu2.resource.Patient; -import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.rest.annotation.Search; +import ca.uhn.fhir.util.PatternMatcher; import ca.uhn.fhir.util.PortUtil; /** @@ -56,6 +58,18 @@ public class SearchDstu2Test { assertNull(status.getFirstHeader(Constants.HEADER_CONTENT_LOCATION)); } + @Test + public void testResultBundleHasUuid() throws Exception { + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchWithRef"); + HttpResponse status = ourClient.execute(httpGet); + String responseContent = IOUtils.toString(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + ourLog.info(responseContent); + + assertEquals(200, status.getStatusLine().getStatusCode()); + assertThat(responseContent, PatternMatcher.pattern("id value..[0-9a-f-]+\\\"")); + } + @Test public void testEncodeConvertsReferencesToRelativeJson() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchWithRef&_format=json"); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/PatternMatcher.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/PatternMatcher.java new file mode 100644 index 00000000000..cf126c64ee7 --- /dev/null +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/PatternMatcher.java @@ -0,0 +1,68 @@ +package ca.uhn.fhir.util; + +import java.util.regex.Pattern; + +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Tests if the argument is a {@link CharSequence} that matches a regular expression. + */ +public class PatternMatcher extends TypeSafeMatcher { + + /** + * Creates a matcher that matches if the examined {@link CharSequence} matches the specified regular expression. + *

+ * For example: + * + *

+	 * assertThat("myStringOfNote", pattern("[0-9]+"))
+	 * 
+ * + * @param regex + * the regular expression that the returned matcher will use to match any examined {@link CharSequence} + */ + @Factory + public static Matcher pattern(String regex) { + return pattern(Pattern.compile(regex)); + } + + /** + * Creates a matcher that matches if the examined {@link CharSequence} matches the specified {@link Pattern}. + *

+ * For example: + * + *

+	 * assertThat("myStringOfNote", Pattern.compile("[0-9]+"))
+	 * 
+ * + * @param pattern + * the pattern that the returned matcher will use to match any examined {@link CharSequence} + */ + @Factory + public static Matcher pattern(Pattern pattern) { + return new PatternMatcher(pattern); + } + + private final Pattern pattern; + + public PatternMatcher(Pattern pattern) { + this.pattern = pattern; + } + + @Override + public boolean matchesSafely(CharSequence item) { + return pattern.matcher(item).find(); + } + + @Override + public void describeMismatchSafely(CharSequence item, org.hamcrest.Description mismatchDescription) { + mismatchDescription.appendText("was \"").appendText(String.valueOf(item)).appendText("\""); + } + + @Override + public void describeTo(org.hamcrest.Description description) { + description.appendText("a string with pattern \"").appendText(String.valueOf(pattern)).appendText("\""); + } +}