Fix a bunch of tests - Still trying to get memory down so Cobertura passes

This commit is contained in:
jamesagnew 2016-04-17 19:41:28 -04:00
parent f3a41570d3
commit 64f84849d7
33 changed files with 1172 additions and 1290 deletions

View File

@ -43,11 +43,16 @@ import ch.qos.logback.core.Appender;
*/
public class LoggingInterceptorTest {
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private Appender<ILoggingEvent> myMockAppender;
private Logger myLoggerRoot;
private Appender<ILoggingEvent> myMockAppender;
@After
public void after() {
myLoggerRoot.detachAppender(myMockAppender);
}
@SuppressWarnings("unchecked")
@Before
@ -65,11 +70,6 @@ public class LoggingInterceptorTest {
myLoggerRoot.addAppender(myMockAppender);
}
@After
public void after() {
myLoggerRoot.detachAppender(myMockAppender);
}
@Test
public void testLogger() throws Exception {
System.out.println("Starting testLogger");
@ -89,8 +89,9 @@ public class LoggingInterceptorTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -120,6 +121,7 @@ public class LoggingInterceptorTest {
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -140,10 +142,4 @@ public class LoggingInterceptorTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -20,7 +20,7 @@ import ca.uhn.fhir.util.TestUtil;
public class DateRangeParamTest {
private static SimpleDateFormat ourFmt;
private static final SimpleDateFormat ourFmt;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DateRangeParamTest.class);
static {

View File

@ -32,9 +32,9 @@ import ca.uhn.fhir.util.TestUtil;
public class AcceptHeaderTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static FhirContext ourCtx = FhirContext.forDstu1();
@Test
public void testReadNoHeader() throws Exception {
@ -55,16 +55,6 @@ public class AcceptHeaderTest {
assertEquals(Constants.CT_FHIR_XML + Constants.CHARSET_UTF8_CTSUFFIX.replace(" ", "").toLowerCase(), status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue().replace(" ", "").replace("UTF", "utf"));
}
@Test
public void testReadXmlHeaderHigherPriorityWithStar() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader(Constants.HEADER_ACCEPT, "*/*; q=1.0, " + Constants.CT_FHIR_XML + "; q=1.0, " + Constants.CT_FHIR_JSON + "; q=0.9");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(Constants.CT_FHIR_XML + Constants.CHARSET_UTF8_CTSUFFIX.replace(" ", "").toLowerCase(), status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue().replace(" ", "").replace("UTF", "utf"));
}
@Test
public void testReadXmlHeaderHigherPriorityBackwards() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
@ -83,9 +73,20 @@ public class AcceptHeaderTest {
assertEquals(Constants.CT_FHIR_XML + Constants.CHARSET_UTF8_CTSUFFIX.replace(" ", "").toLowerCase(), status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue().replace(" ", "").replace("UTF", "utf"));
}
@Test
public void testReadXmlHeaderHigherPriorityWithStar() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader(Constants.HEADER_ACCEPT, "*/*; q=1.0, " + Constants.CT_FHIR_XML + "; q=1.0, " + Constants.CT_FHIR_JSON + "; q=0.9");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(Constants.CT_FHIR_XML + Constants.CHARSET_UTF8_CTSUFFIX.replace(" ", "").toLowerCase(), status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue().replace(" ", "").replace("UTF", "utf"));
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -109,11 +110,17 @@ public class AcceptHeaderTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class PatientProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Read(version = true)
public Patient read(@IdParam IdDt theId) {
Patient patient = new Patient();
@ -122,17 +129,6 @@ public class AcceptHeaderTest {
return patient;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -56,53 +56,6 @@ public class BinaryTest {
ourLast=null;
}
@Test
public void testRead() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo");
HttpResponse status = ourClient.execute(httpGet);
byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("foo", status.getFirstHeader("content-type").getValue());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, responseContent);
}
@Test
public void testReadWithExplicitTypeXml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=xml");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8");
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_XML + ";"));
Binary bin = ourCtx.newXmlParser().parseResource(Binary.class, responseContent);
assertEquals("foo", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
}
@Test
public void testReadWithExplicitTypeJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=json");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8");
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_JSON + ";"));
Binary bin = ourCtx.newJsonParser().parseResource(Binary.class, responseContent);
assertEquals("foo", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
}
@Test
public void testCreate() throws Exception {
HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary");
@ -133,6 +86,53 @@ public class BinaryTest {
}
@Test
public void testRead() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo");
HttpResponse status = ourClient.execute(httpGet);
byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("foo", status.getFirstHeader("content-type").getValue());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, responseContent);
}
@Test
public void testReadWithExplicitTypeJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=json");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8");
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_JSON + ";"));
Binary bin = ourCtx.newJsonParser().parseResource(Binary.class, responseContent);
assertEquals("foo", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
}
@Test
public void testReadWithExplicitTypeXml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=xml");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8");
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_XML + ";"));
Binary bin = ourCtx.newXmlParser().parseResource(Binary.class, responseContent);
assertEquals("foo", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
}
@Test
public void testSearch() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary?");
@ -151,11 +151,13 @@ public class BinaryTest {
assertEquals("text/plain", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -177,7 +179,8 @@ public class BinaryTest {
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -216,10 +219,4 @@ public class BinaryTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -95,8 +95,9 @@ public class CompositeParameterTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -120,6 +121,7 @@ public class CompositeParameterTest {
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -173,10 +175,4 @@ public class CompositeParameterTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -39,21 +39,11 @@ import ca.uhn.fhir.util.TestUtil;
public class CompressionTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(CompressionTest.class);
private static int ourPort;
private static Server ourServer;
private static final FhirContext ourCtx = FhirContext.forDstu1();
public static String decompress(byte[] theResource) {
GZIPInputStream is;
try {
is = new GZIPInputStream(new ByteArrayInputStream(theResource));
return IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
throw new DataFormatException("Failed to decompress contents", e);
}
}
@Test
public void testRead() throws Exception {
{
@ -80,7 +70,7 @@ public class CompressionTest {
}
}
@Test
public void testVRead() throws Exception {
{
@ -96,8 +86,9 @@ public class CompressionTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -128,6 +119,17 @@ public class CompressionTest {
}
public static String decompress(byte[] theResource) {
GZIPInputStream is;
try {
is = new GZIPInputStream(new ByteArrayInputStream(theResource));
return IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
throw new DataFormatException("Failed to decompress contents", e);
}
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -148,10 +150,4 @@ public class CompressionTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -91,50 +91,6 @@ public class CreateTest {
assertEquals(EncodingEnum.XML, ourLastEncoding);
}
@Test
public void testCreateWithWrongContentTypeXml() throws Exception {
Patient patient = new Patient();
patient.addIdentifier().setValue("001");
patient.addIdentifier().setValue("002");
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient");
String inputString = ourCtx.newJsonParser().encodeResourceToString(patient);
ContentType inputCt = ContentType.create(Constants.CT_FHIR_XML, "UTF-8");
httpPost.setEntity(new StringEntity(inputString, inputCt));
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(500, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("Unexpected character"));
}
@Test
public void testCreateWithWrongContentTypeJson() throws Exception {
Patient patient = new Patient();
patient.addIdentifier().setValue("001");
patient.addIdentifier().setValue("002");
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient");
String inputString = ourCtx.newXmlParser().encodeResourceToString(patient);
ContentType inputCt = ContentType.create(Constants.CT_FHIR_JSON, "UTF-8");
httpPost.setEntity(new StringEntity(inputString, inputCt));
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(500, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("Unexpected char"));
}
@Test
public void testCreateById() throws Exception {
@ -250,11 +206,56 @@ public class CreateTest {
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
@Test
public void testCreateWithWrongContentTypeJson() throws Exception {
Patient patient = new Patient();
patient.addIdentifier().setValue("001");
patient.addIdentifier().setValue("002");
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient");
String inputString = ourCtx.newXmlParser().encodeResourceToString(patient);
ContentType inputCt = ContentType.create(Constants.CT_FHIR_JSON, "UTF-8");
httpPost.setEntity(new StringEntity(inputString, inputCt));
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(500, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("Unexpected char"));
}
@Test
public void testCreateWithWrongContentTypeXml() throws Exception {
Patient patient = new Patient();
patient.addIdentifier().setValue("001");
patient.addIdentifier().setValue("002");
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient");
String inputString = ourCtx.newJsonParser().encodeResourceToString(patient);
ContentType inputCt = ContentType.create(Constants.CT_FHIR_XML, "UTF-8");
httpPost.setEntity(new StringEntity(inputString, inputCt));
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(500, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("Unexpected character"));
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -280,6 +281,7 @@ public class CreateTest {
ourClient = builder.build();
}
@ResourceDef(name = "Observation")
public static class CustomObservation extends Observation {
@ -316,7 +318,7 @@ public class CreateTest {
}
}
public static class DiagnosticReportProvider implements IResourceProvider {
private TagList myLastTags;
@ -393,6 +395,7 @@ public class CreateTest {
}
public static class PatientProvider implements IResourceProvider {
@Create()
@ -415,10 +418,4 @@ public class CreateTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -45,9 +45,68 @@ public class CustomTypeTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = new FhirContext(ExtendedPatient.class);
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(CustomTypeTest.class);
private static int ourPort;
private static boolean ourReturnExtended = false;
private static Server ourServer;
private static RestfulServer ourServlet;
@Test
public void testFindProfileItself() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.ONLY_FOR_CUSTOM);
ourReturnExtended=true;
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Profile/prof2?_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(responseContent);
Profile bundle = ourCtx.newXmlParser().parseResource(Profile.class, responseContent);
}
@Test
public void testSearchReturnsNoProfileForExtendedType() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.NEVER);
ourReturnExtended=true;
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
BundleEntry entry = bundle.getEntries().get(0);
List<Tag> profileTags = entry.getCategories().getTagsWithScheme(Tag.HL7_ORG_PROFILE_TAG);
assertEquals(0, profileTags.size());
}
@Test
public void testSearchReturnsNoProfileForNormalType() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.ONLY_FOR_CUSTOM);
ourReturnExtended=false;
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
BundleEntry entry = bundle.getEntries().get(0);
List<Tag> profileTags = entry.getCategories().getTagsWithScheme(Tag.HL7_ORG_PROFILE_TAG);
assertEquals(0, profileTags.size());
}
@Test
@ -72,64 +131,7 @@ public class CustomTypeTest {
assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
}
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(CustomTypeTest.class);
@Test
public void testFindProfileItself() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.ONLY_FOR_CUSTOM);
ourReturnExtended=true;
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Profile/prof2?_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(responseContent);
Profile bundle = ourCtx.newXmlParser().parseResource(Profile.class, responseContent);
}
@Test
public void testSearchReturnsNoProfileForNormalType() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.ONLY_FOR_CUSTOM);
ourReturnExtended=false;
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
BundleEntry entry = bundle.getEntries().get(0);
List<Tag> profileTags = entry.getCategories().getTagsWithScheme(Tag.HL7_ORG_PROFILE_TAG);
assertEquals(0, profileTags.size());
}
@Test
public void testSearchReturnsNoProfileForExtendedType() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.NEVER);
ourReturnExtended=true;
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
BundleEntry entry = bundle.getEntries().get(0);
List<Tag> profileTags = entry.getCategories().getTagsWithScheme(Tag.HL7_ORG_PROFILE_TAG);
assertEquals(0, profileTags.size());
}
@Test
public void testSearchReturnsProfileForNormalType() throws Exception {
ourServlet.setAddProfileTag(AddProfileTagEnum.ALWAYS);
@ -153,11 +155,13 @@ public class CustomTypeTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -184,8 +188,31 @@ public class CustomTypeTest {
fhirContext.getResourceDefinition(ExtendedPatient.class);
}
public static class DummyPatientResourceProvider implements IResourceProvider {
@Search
public List<Patient> findPatient(@OptionalParam(name = "_id") StringParam theParam) {
ArrayList<Patient> retVal = new ArrayList<Patient>();
Patient patient = ourReturnExtended ? new ExtendedPatient() : new Patient();
patient.setId("1");
patient.addIdentifier("system", "identifier123");
if (theParam != null) {
patient.addName().addFamily("id" + theParam.getValue());
}
retVal.add(patient);
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
@ResourceDef(name="Patient", profile="http://foo/profiles/Profile", id="prof2")
public static class ExtendedPatient extends Patient {
@ -212,36 +239,5 @@ public class CustomTypeTest {
}
}
private static boolean ourReturnExtended = false;
public static class DummyPatientResourceProvider implements IResourceProvider {
@Search
public List<Patient> findPatient(@OptionalParam(name = "_id") StringParam theParam) {
ArrayList<Patient> retVal = new ArrayList<Patient>();
Patient patient = ourReturnExtended ? new ExtendedPatient() : new Patient();
patient.setId("1");
patient.addIdentifier("system", "identifier123");
if (theParam != null) {
patient.addName().addFamily("id" + theParam.getValue());
}
retVal.add(patient);
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -37,9 +37,28 @@ import ca.uhn.fhir.util.TestUtil;
public class DateRangeParamSearchTest {
private static CloseableHttpClient ourClient;
private static int ourPort;
private static Server ourServer;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static DateRangeParam ourLastDateRange;
private static int ourPort;
private static Server ourServer;
@Before
public void before() {
ourLastDateRange = null;
}
@Test
public void testSearchForMultipleUnqualifiedDate() throws Exception {
String baseUrl = "http://localhost:" + ourPort + "/Patient?" + Patient.SP_BIRTHDATE + "=";
HttpGet httpGet = new HttpGet(baseUrl + "2012-01-01&" + Patient.SP_BIRTHDATE + "=2012-02-03");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(400, status.getStatusLine().getStatusCode());
}
@Test
public void testSearchForOneUnqualifiedDate() throws Exception {
@ -57,27 +76,12 @@ public class DateRangeParamSearchTest {
assertEquals(DateRangeParamTest.parseM1("2012-01-02 00:00:00.0000"), ourLastDateRange.getUpperBoundAsInstant());
}
@Test
public void testSearchForMultipleUnqualifiedDate() throws Exception {
String baseUrl = "http://localhost:" + ourPort + "/Patient?" + Patient.SP_BIRTHDATE + "=";
HttpGet httpGet = new HttpGet(baseUrl + "2012-01-01&" + Patient.SP_BIRTHDATE + "=2012-02-03");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(400, status.getStatusLine().getStatusCode());
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
}
@Before
public void before() {
ourLastDateRange = null;
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -104,13 +108,18 @@ public class DateRangeParamSearchTest {
}
private static DateRangeParam ourLastDateRange;
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Search()
public List<Patient> search(@RequiredParam(name=Patient.SP_BIRTHDATE) DateRangeParam theDateRange) {
ourLastDateRange = theDateRange;
@ -124,18 +133,6 @@ public class DateRangeParamSearchTest {
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -40,10 +40,10 @@ import ca.uhn.fhir.util.TestUtil;
public class DefaultEncodingTest {
private static CloseableHttpClient ourClient;
private static int ourPort;
private static Server ourServer;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static RestfulServer ourRestfulServer;
private static Server ourServer;
@Test
public void testHonoursAcceptHeader() throws Exception {
@ -175,8 +175,9 @@ public class DefaultEncodingTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -199,11 +200,17 @@ public class DefaultEncodingTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class PatientProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Read(version = true)
public Patient read(@IdParam IdDt theId) {
Patient patient = new Patient();
@ -212,17 +219,6 @@ public class DefaultEncodingTest {
return patient;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -42,18 +42,31 @@ public class DynamicSearchTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static SearchParameterMap ourLastSearchParams;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DynamicSearchTest.class);
private static int ourPort;
private static Server ourServer;
private static SearchParameterMap ourLastSearchParams;
@Before
public void before() {
ourLastSearchParams = null;
}
@Test
public void testConformance() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/metadata?_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Conformance conf = ourCtx.newXmlParser().parseResource(Conformance.class,responseContent);
ourLog.info(responseContent);
}
@Test
public void testSearchOneStringParam() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?param1=param1value");
@ -73,28 +86,6 @@ public class DynamicSearchTest {
assertEquals("param1value", param1.getValue());
}
@Test
public void testSearchOneStringParamWithOr() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?param1=param1value,param1value2");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
assertEquals(1, ourLastSearchParams.size());
StringAndListParam andList =(StringAndListParam) ourLastSearchParams.get("param1");
assertEquals(1,andList.getValuesAsQueryTokens().size());
StringOrListParam orList = andList.getValuesAsQueryTokens().get(0);
assertEquals(2,orList.getValuesAsQueryTokens().size());
StringParam param1 = orList.getValuesAsQueryTokens().get(0);
assertEquals("param1value", param1.getValue());
StringParam param1b = orList.getValuesAsQueryTokens().get(1);
assertEquals("param1value2", param1b.getValue());
}
@Test
public void testSearchOneStringParamWithAnd() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?param1=param1value&param1=param1value2");
@ -120,21 +111,31 @@ public class DynamicSearchTest {
}
@Test
public void testConformance() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/metadata?_pretty=true");
public void testSearchOneStringParamWithOr() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?param1=param1value,param1value2");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Conformance conf = ourCtx.newXmlParser().parseResource(Conformance.class,responseContent);
ourLog.info(responseContent);
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
assertEquals(1, ourLastSearchParams.size());
StringAndListParam andList =(StringAndListParam) ourLastSearchParams.get("param1");
assertEquals(1,andList.getValuesAsQueryTokens().size());
StringOrListParam orList = andList.getValuesAsQueryTokens().get(0);
assertEquals(2,orList.getValuesAsQueryTokens().size());
StringParam param1 = orList.getValuesAsQueryTokens().get(0);
assertEquals("param1value", param1.getValue());
StringParam param1b = orList.getValuesAsQueryTokens().get(1);
assertEquals("param1value2", param1b.getValue());
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -194,10 +195,5 @@ public class DynamicSearchTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -54,13 +54,13 @@ public class ExceptionTest {
private static final String OPERATION_OUTCOME_DETAILS = "OperationOutcomeDetails";
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static Class<? extends Exception> ourExceptionType;
private static boolean ourGenerateOperationOutcome;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExceptionTest.class);
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private static final FhirContext ourCtx = FhirContext.forDstu1();
@Before
public void before() {
@ -68,21 +68,6 @@ public class ExceptionTest {
ourExceptionType = null;
}
@Test
public void testInternalError() throws Exception {
{
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?throwInternalError=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(500, status.getStatusLine().getStatusCode());
OperationOutcome oo = (OperationOutcome) servlet.getFhirContext().newXmlParser().parseResource(responseContent);
assertThat(oo.getIssueFirstRep().getDetails().getValue(), StringContains.containsString("Exception Text"));
assertThat(oo.getIssueFirstRep().getDetails().getValue(), not(StringContains.containsString("InternalErrorException")));
}
}
@Test
public void testAuthorizationFailureInPreProcessInterceptor() throws Exception {
IServerInterceptor interceptor = new InterceptorAdapter() {
@ -107,18 +92,17 @@ public class ExceptionTest {
}
@Test
public void testResourceNotFound() throws Exception {
ourExceptionType = ResourceNotFoundException.class;
ourGenerateOperationOutcome = false;
public void testInternalError() throws Exception {
{
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123");
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?throwInternalError=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(404, status.getStatusLine().getStatusCode());
assertEquals(500, status.getStatusLine().getStatusCode());
OperationOutcome oo = (OperationOutcome) servlet.getFhirContext().newXmlParser().parseResource(responseContent);
assertThat(oo.getIssueFirstRep().getDetails().getValue(), StringContains.containsString("Resource Patient/123 is not known"));
assertThat(oo.getIssueFirstRep().getDetails().getValue(), StringContains.containsString("Exception Text"));
assertThat(oo.getIssueFirstRep().getDetails().getValue(), not(StringContains.containsString("InternalErrorException")));
}
}
@ -150,6 +134,36 @@ public class ExceptionTest {
assertThat(oo.getIssueFirstRep().getDetails().getValue(), not(StringContains.containsString("InternalErrorException")));
}
@Test
public void testMethodNotAllowed() throws Exception {
{
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?throwMethodNotAllowed=aaa&_format=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
ourLog.info(status.toString());
assertEquals(MethodNotAllowedException.STATUS_CODE, status.getStatusLine().getStatusCode());
assertEquals("POST,PUT", status.getFirstHeader(Constants.HEADER_ALLOW).getValue());
}
}
@Test
public void testResourceNotFound() throws Exception {
ourExceptionType = ResourceNotFoundException.class;
ourGenerateOperationOutcome = false;
{
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(404, status.getStatusLine().getStatusCode());
OperationOutcome oo = (OperationOutcome) servlet.getFhirContext().newXmlParser().parseResource(responseContent);
assertThat(oo.getIssueFirstRep().getDetails().getValue(), StringContains.containsString("Resource Patient/123 is not known"));
}
}
@Test
public void testResourceReturning() throws Exception {
// No OO
@ -210,23 +224,11 @@ public class ExceptionTest {
}
}
@Test
public void testMethodNotAllowed() throws Exception {
{
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?throwMethodNotAllowed=aaa&_format=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
ourLog.info(status.toString());
assertEquals(MethodNotAllowedException.STATUS_CODE, status.getStatusLine().getStatusCode());
assertEquals("POST,PUT", status.getFirstHeader(Constants.HEADER_ALLOW).getValue());
}
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -251,6 +253,7 @@ public class ExceptionTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -283,13 +286,13 @@ public class ExceptionTest {
}
@Search()
public List<Patient> throwUnprocessableEntity(@RequiredParam(name = "throwUnprocessableEntity") StringParam theParam) {
throw new UnprocessableEntityException("Exception Text");
public List<Patient> throwMethodNotAllowed(@RequiredParam(name = "throwMethodNotAllowed") StringParam theParam) {
throw new MethodNotAllowedException("Exception Text", RequestTypeEnum.POST, RequestTypeEnum.PUT);
}
@Search()
public List<Patient> throwMethodNotAllowed(@RequiredParam(name = "throwMethodNotAllowed") StringParam theParam) {
throw new MethodNotAllowedException("Exception Text", RequestTypeEnum.POST, RequestTypeEnum.PUT);
public List<Patient> throwUnprocessableEntity(@RequiredParam(name = "throwUnprocessableEntity") StringParam theParam) {
throw new UnprocessableEntityException("Exception Text");
}
@Search
@ -299,10 +302,4 @@ public class ExceptionTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -116,8 +116,9 @@ public class HistoryTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -143,7 +144,8 @@ public class HistoryTest {
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -170,8 +172,7 @@ public class HistoryTest {
}
public static class DummyResourceProvider implements IResourceProvider {
@Override
@ -224,10 +225,5 @@ public class HistoryTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -273,9 +273,11 @@ public class IncludeTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -479,6 +481,7 @@ public class IncludeTest {
}
@ResourceDef(name = "Patient")
public static class ExtPatient extends Patient {
@Child(name = "secondOrg")
@ -503,10 +506,4 @@ public class IncludeTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -49,12 +49,20 @@ import ca.uhn.fhir.util.TestUtil;
public class InterceptorTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private IServerInterceptor myInterceptor1;
private IServerInterceptor myInterceptor2;
private static final FhirContext ourCtx = FhirContext.forDstu1();
@Before
public void before() {
myInterceptor1 = mock(IServerInterceptor.class);
myInterceptor2 = mock(IServerInterceptor.class);
servlet.setInterceptors(myInterceptor1, myInterceptor2);
}
@Test
public void testInterceptorFires() throws Exception {
@ -83,20 +91,13 @@ public class InterceptorTest {
verifyNoMoreInteractions(myInterceptor2);
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@Before
public void before() {
myInterceptor1 = mock(IServerInterceptor.class);
myInterceptor2 = mock(IServerInterceptor.class);
servlet.setInterceptors(myInterceptor1, myInterceptor2);
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -119,11 +120,26 @@ public class InterceptorTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
}
public Map<String, Patient> getIdToPatient() {
Map<String, Patient> idToPatient = new HashMap<String, Patient>();
{
@ -146,6 +162,7 @@ public class InterceptorTest {
return idToPatient;
}
/**
* Retrieve the resource by its identifier
*
@ -159,15 +176,6 @@ public class InterceptorTest {
Patient retVal = getIdToPatient().get(key);
return retVal;
}
@Search(queryName="searchWithWildcardRetVal")
public List<? extends IResource> searchWithWildcardRetVal() {
Patient p = new Patient();
p.setId("1234");
p.addName().addFamily("searchWithWildcardRetVal");
return Collections.singletonList(p);
}
/**
* Retrieve the resource by its identifier
@ -192,26 +200,14 @@ public class InterceptorTest {
return Patient.class;
}
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
@Search(queryName="searchWithWildcardRetVal")
public List<? extends IResource> searchWithWildcardRetVal() {
Patient p = new Patient();
p.setId("1234");
p.addName().addFamily("searchWithWildcardRetVal");
return Collections.singletonList(p);
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -45,81 +45,19 @@ import ca.uhn.fhir.util.TestUtil;
*/
public class PagingTest {
private static CloseableHttpClient ourClient;
private static int ourPort;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static Server ourServer;
private static FhirContext ourContext;
private static RestfulServer myRestfulServer;
private static SimpleBundleProvider ourBundleProvider;
private static CloseableHttpClient ourClient;
private static FhirContext ourContext;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private IPagingProvider myPagingProvider;
@Test
public void testSearchExactMatch() throws Exception {
when(myPagingProvider.getDefaultPageSize()).thenReturn(5);
when(myPagingProvider.getMaximumPageSize()).thenReturn(9);
when(myPagingProvider.storeResultList(any(IBundleProvider.class))).thenReturn("ABCD");
when(myPagingProvider.retrieveResultList(eq("ABCD"))).thenReturn(ourBundleProvider);
String link;
String base = "http://localhost:" + ourPort;
{
HttpGet httpGet = new HttpGet(base + "/Patient?_format=xml&_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourContext.newXmlParser().parseBundle(responseContent);
assertEquals(5, bundle.getEntries().size());
assertEquals("0", bundle.getEntries().get(0).getResource().getId().getIdPart());
assertEquals("4", bundle.getEntries().get(4).getResource().getId().getIdPart());
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=5&" + Constants.PARAM_COUNT + "=5&_format=xml&_pretty=true&_bundletype=searchset", bundle.getLinkNext()
.getValue());
assertNull(bundle.getLinkPrevious().getValue());
link = bundle.getLinkNext().getValue();
}
{
HttpGet httpGet = new HttpGet(link.replace("=xml", "=json"));
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourContext.newJsonParser().parseBundle(responseContent);
assertEquals(5, bundle.getEntries().size());
assertEquals("5", bundle.getEntries().get(0).getResource().getId().getIdPart());
assertEquals("9", bundle.getEntries().get(4).getResource().getId().getIdPart());
assertNull(bundle.getLinkNext().getValue());
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=0&" + Constants.PARAM_COUNT + "=5&_format=json&_pretty=true&_bundletype=searchset", bundle.getLinkPrevious()
.getValue());
}
}
@Test
public void testSearchInexactOffset() throws Exception {
when(myPagingProvider.getDefaultPageSize()).thenReturn(5);
when(myPagingProvider.getMaximumPageSize()).thenReturn(9);
when(myPagingProvider.storeResultList(any(IBundleProvider.class))).thenReturn("ABCD");
when(myPagingProvider.retrieveResultList(eq("ABCD"))).thenReturn(ourBundleProvider);
String base = "http://localhost:" + ourPort;
{
HttpGet httpGet = new HttpGet(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=8&" + Constants.PARAM_COUNT + "=5&_format=xml&_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourContext.newXmlParser().parseBundle(responseContent);
assertEquals(2, bundle.getEntries().size());
assertEquals("8", bundle.getEntries().get(0).getResource().getId().getIdPart());
assertEquals("9", bundle.getEntries().get(1).getResource().getId().getIdPart());
assertNull(bundle.getLinkNext().getValue());
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=3&" + Constants.PARAM_COUNT + "=5&_format=xml&_pretty=true", bundle.getLinkPrevious()
.getValue());
}
@Before
public void before() {
myPagingProvider = mock(IPagingProvider.class);
myRestfulServer.setPagingProvider(myPagingProvider);
}
@Test()
@ -162,7 +100,7 @@ public class PagingTest {
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=0&" + Constants.PARAM_COUNT + "=2&_format=xml&_bundletype=searchset", bundle.getLinkPrevious().getValue());
}
}
/**
* See #116
*/
@ -217,6 +155,74 @@ public class PagingTest {
}
}
@Test
public void testSearchExactMatch() throws Exception {
when(myPagingProvider.getDefaultPageSize()).thenReturn(5);
when(myPagingProvider.getMaximumPageSize()).thenReturn(9);
when(myPagingProvider.storeResultList(any(IBundleProvider.class))).thenReturn("ABCD");
when(myPagingProvider.retrieveResultList(eq("ABCD"))).thenReturn(ourBundleProvider);
String link;
String base = "http://localhost:" + ourPort;
{
HttpGet httpGet = new HttpGet(base + "/Patient?_format=xml&_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourContext.newXmlParser().parseBundle(responseContent);
assertEquals(5, bundle.getEntries().size());
assertEquals("0", bundle.getEntries().get(0).getResource().getId().getIdPart());
assertEquals("4", bundle.getEntries().get(4).getResource().getId().getIdPart());
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=5&" + Constants.PARAM_COUNT + "=5&_format=xml&_pretty=true&_bundletype=searchset", bundle.getLinkNext()
.getValue());
assertNull(bundle.getLinkPrevious().getValue());
link = bundle.getLinkNext().getValue();
}
{
HttpGet httpGet = new HttpGet(link.replace("=xml", "=json"));
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourContext.newJsonParser().parseBundle(responseContent);
assertEquals(5, bundle.getEntries().size());
assertEquals("5", bundle.getEntries().get(0).getResource().getId().getIdPart());
assertEquals("9", bundle.getEntries().get(4).getResource().getId().getIdPart());
assertNull(bundle.getLinkNext().getValue());
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=0&" + Constants.PARAM_COUNT + "=5&_format=json&_pretty=true&_bundletype=searchset", bundle.getLinkPrevious()
.getValue());
}
}
@Test
public void testSearchInexactOffset() throws Exception {
when(myPagingProvider.getDefaultPageSize()).thenReturn(5);
when(myPagingProvider.getMaximumPageSize()).thenReturn(9);
when(myPagingProvider.storeResultList(any(IBundleProvider.class))).thenReturn("ABCD");
when(myPagingProvider.retrieveResultList(eq("ABCD"))).thenReturn(ourBundleProvider);
String base = "http://localhost:" + ourPort;
{
HttpGet httpGet = new HttpGet(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=8&" + Constants.PARAM_COUNT + "=5&_format=xml&_pretty=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourContext.newXmlParser().parseBundle(responseContent);
assertEquals(2, bundle.getEntries().size());
assertEquals("8", bundle.getEntries().get(0).getResource().getId().getIdPart());
assertEquals("9", bundle.getEntries().get(1).getResource().getId().getIdPart());
assertNull(bundle.getLinkNext().getValue());
assertEquals(base + '?' + Constants.PARAM_PAGINGACTION + "=ABCD&" + Constants.PARAM_PAGINGOFFSET + "=3&" + Constants.PARAM_COUNT + "=5&_format=xml&_pretty=true", bundle.getLinkPrevious()
.getValue());
}
}
@Test
public void testSearchSmallPages() throws Exception {
when(myPagingProvider.getDefaultPageSize()).thenReturn(5);
@ -260,14 +266,9 @@ public class PagingTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
}
@Before
public void before() {
myPagingProvider = mock(IPagingProvider.class);
myRestfulServer.setPagingProvider(myPagingProvider);
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -302,6 +303,7 @@ public class PagingTest {
ourBundleProvider = new SimpleBundleProvider(retVal);
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -319,10 +321,4 @@ public class PagingTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -190,8 +190,9 @@ public class ReadDstu1Test {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -258,6 +259,7 @@ public class ReadDstu1Test {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -279,10 +281,4 @@ public class ReadDstu1Test {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -55,10 +55,10 @@ public class ReferenceParameterTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static ReferenceParam ourLastRefParam;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ReferenceParameterTest.class);
private static int ourPort;
private static Server ourServer;
private static ReferenceParam ourLastRefParam;
@Before
public void before() {
@ -103,6 +103,19 @@ public class ReferenceParameterTest {
assertEquals(null, p.getManagingOrganization().getReference().getVersionIdPart());
}
@Test
public void testReferenceParamViewToken() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?provider.name=" + URLEncoder.encode("foo|bar", "UTF-8"));
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("foo|bar", ourLastRefParam.getValue());
assertEquals("foo", ourLastRefParam.toTokenParam(ourCtx).getSystem());
assertEquals("bar", ourLastRefParam.toTokenParam(ourCtx).getValue());
}
@Test
public void testSearchReturnVersionedReferenceInResponse() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=findPatientWithVersion");
@ -220,19 +233,6 @@ public class ReferenceParameterTest {
assertEquals("2", p.getName().get(2).getFamilyFirstRep().getValue());
}
@Test
public void testReferenceParamViewToken() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?provider.name=" + URLEncoder.encode("foo|bar", "UTF-8"));
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("foo|bar", ourLastRefParam.getValue());
assertEquals("foo", ourLastRefParam.toTokenParam(ourCtx).getSystem());
assertEquals("bar", ourLastRefParam.toTokenParam(ourCtx).getValue());
}
@Test
@ -287,8 +287,9 @@ public class ReferenceParameterTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -404,6 +405,7 @@ public class ReferenceParameterTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -459,10 +461,4 @@ public class ReferenceParameterTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -92,8 +92,32 @@ public class RestfulServerMethodTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RestfulServerMethodTest.class);
private static int ourPort;
private static DummyDiagnosticReportResourceProvider ourReportProvider;
private static Server ourServer;
private static RestfulServer ourRestfulServer;
private static Server ourServer;
private HttpServletRequest createHttpServletRequest() {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/FhirStorm/fhir/Patient/_search");
when(req.getServletPath()).thenReturn("/fhir");
when(req.getRequestURL()).thenReturn(new StringBuffer().append("http://fhirstorm.dyndns.org:8080/FhirStorm/fhir/Patient/_search"));
when(req.getContextPath()).thenReturn("/FhirStorm");
return req;
}
@Test
public void test404IsPropagatedCorrectly() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/DiagnosticReport?throw404=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(404, status.getStatusLine().getStatusCode());
assertThat(responseContent, StringContains.containsString("AAAABBBB"));
}
@Test
public void testCreateBundleDoesntCreateDoubleEntries() {
@ -114,35 +138,6 @@ public class RestfulServerMethodTest {
factory.initializeBundleFromResourceList("", resources, "http://foo", "http://foo", 2, null);
assertEquals(2, factory.getDstu1Bundle().getEntries().size());
}
@Test
public void test404IsPropagatedCorrectly() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/DiagnosticReport?throw404=true");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(404, status.getStatusLine().getStatusCode());
assertThat(responseContent, StringContains.containsString("AAAABBBB"));
}
@Test
public void testInvalidResourceTriggers400() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/FooResource?blah=bar");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(400, status.getStatusLine().getStatusCode());
}
@Test
public void testDateRangeParam() throws Exception {
@ -531,6 +526,20 @@ public class RestfulServerMethodTest {
}
@Test
public void testInvalidResourceTriggers400() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/FooResource?blah=bar");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(400, status.getStatusLine().getStatusCode());
}
@Test
public void testReadOnTypeThatDoesntSupportRead() throws Exception {
@ -897,7 +906,7 @@ public class RestfulServerMethodTest {
// Make sure there is no crash
assertEquals(200, status.getStatusLine().getStatusCode());
}
@Test
public void testSearchWithOptionalParam() throws Exception {
@ -940,7 +949,15 @@ public class RestfulServerMethodTest {
assertEquals("BBB", patient.getName().get(0).getGiven().get(0).getValue());
}
@Test
public void testServerProfileProviderFindsProfiles() {
ServerProfileProvider profileProvider = (ServerProfileProvider)ourRestfulServer.getServerProfilesProvider();
IdDt id = new IdDt("Profile", "observation");
Profile profile = profileProvider.getProfileById(createHttpServletRequest(), id);
assertNotNull(profile);
}
@Test
public void testValidate() throws Exception {
@ -1036,26 +1053,10 @@ public class RestfulServerMethodTest {
}
@Test
public void testServerProfileProviderFindsProfiles() {
ServerProfileProvider profileProvider = (ServerProfileProvider)ourRestfulServer.getServerProfilesProvider();
IdDt id = new IdDt("Profile", "observation");
Profile profile = profileProvider.getProfileById(createHttpServletRequest(), id);
assertNotNull(profile);
}
private HttpServletRequest createHttpServletRequest() {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/FhirStorm/fhir/Patient/_search");
when(req.getServletPath()).thenReturn("/fhir");
when(req.getRequestURL()).thenReturn(new StringBuffer().append("http://fhirstorm.dyndns.org:8080/FhirStorm/fhir/Patient/_search"));
when(req.getContextPath()).thenReturn("/FhirStorm");
return req;
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -1148,6 +1149,20 @@ public class RestfulServerMethodTest {
public static class DummyPatientResourceProvider implements IResourceProvider {
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
}
@Delete()
public MethodOutcome deletePatient(@IdParam IdDt theId) {
MethodOutcome retVal = new MethodOutcome();
@ -1350,6 +1365,11 @@ public class RestfulServerMethodTest {
return retVal;
}
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
/**
* Retrieve the resource by its identifier
*
@ -1362,25 +1382,6 @@ public class RestfulServerMethodTest {
return getIdToPatient().get(theId.getIdPart());
}
@Read(version=true)
public Patient vread(@IdParam IdDt theId) {
Patient retVal = getIdToPatient().get(theId.getIdPart());
if (retVal == null) {
throw new ResourceNotFoundException("Couldn't find ID " + theId.getIdPart() + " - Valid IDs are: " + getIdToPatient().keySet());
}
List<HumanNameDt> name = retVal.getName();
HumanNameDt nameDt = name.get(0);
String value = theId.getVersionIdPart();
nameDt.setText(value);
return retVal;
}
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
@ -1402,22 +1403,23 @@ public class RestfulServerMethodTest {
return new MethodOutcome();
}
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
@Read(version=true)
public Patient vread(@IdParam IdDt theId) {
Patient retVal = getIdToPatient().get(theId.getIdPart());
if (retVal == null) {
throw new ResourceNotFoundException("Couldn't find ID " + theId.getIdPart() + " - Valid IDs are: " + getIdToPatient().keySet());
}
List<HumanNameDt> name = retVal.getName();
HumanNameDt nameDt = name.get(0);
String value = theId.getVersionIdPart();
nameDt.setText(value);
return retVal;
}
}
public static class DummyRestfulServer extends RestfulServer {
private static final long serialVersionUID = 1L;
@ -1440,10 +1442,4 @@ public class RestfulServerMethodTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -74,14 +74,14 @@ public class SearchSearchServerDstu1Test {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static IServerAddressStrategy ourDefaultAddressStrategy;
private static StringAndListParam ourLastAndList;
private static Set<Include> ourLastIncludes;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SearchSearchServerDstu1Test.class);
private static int ourPort;
private static Server ourServer;
private static RestfulServer ourServlet;
private static IServerAddressStrategy ourDefaultAddressStrategy;
private static Set<Include> ourLastIncludes;
private static StringAndListParam ourLastAndList;
@Before
public void before() {
@ -90,6 +90,52 @@ public class SearchSearchServerDstu1Test {
ourLastAndList = null;
}
@Test
public void testEncodeConvertsReferencesToRelative() 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());
Patient patient = (Patient) ourCtx.newXmlParser().parseBundle(responseContent).getEntries().get(0).getResource();
String ref = patient.getManagingOrganization().getReference().getValue();
assertEquals("Organization/555", ref);
}
/**
* Try loading the page as a POST just to make sure we get the right error
*/
@Test
public void testGetPagesWithPost() throws Exception {
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort);
List<? extends NameValuePair> parameters = Collections.singletonList(new BasicNameValuePair("_getpages", "AAA"));
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
CloseableHttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(400, status.getStatusLine().getStatusCode());
// assertThat(responseContent, containsString("Requests for _getpages must use HTTP GET"));
}
@Test
public void testOmitEmptyOptionalParam() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals(null, p.getNameFirstRep().getFamilyFirstRep().getValue());
}
@Test
public void testParseEscapedValues() throws Exception {
@ -121,34 +167,6 @@ public class SearchSearchServerDstu1Test {
assertEquals("E$E", ourLastAndList.getValuesAsQueryTokens().get(2).getValuesAsQueryTokens().get(0).getValue());
assertEquals("E|E", ourLastAndList.getValuesAsQueryTokens().get(3).getValuesAsQueryTokens().get(0).getValue());
}
@Test
public void testEncodeConvertsReferencesToRelative() 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());
Patient patient = (Patient) ourCtx.newXmlParser().parseBundle(responseContent).getEntries().get(0).getResource();
String ref = patient.getManagingOrganization().getReference().getValue();
assertEquals("Organization/555", ref);
}
@Test
public void testOmitEmptyOptionalParam() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals(null, p.getNameFirstRep().getFamilyFirstRep().getValue());
}
@Test
public void testReturnLinks() throws Exception {
@ -171,44 +189,6 @@ public class SearchSearchServerDstu1Test {
}
@Test
public void testSearchIncludesParametersNone() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchIncludes");
CloseableHttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(ourLastIncludes, empty());
}
@Test
public void testSearchIncludesParametersIncludes() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchIncludes&_include=foo&_include:recurse=bar");
CloseableHttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(2, ourLastIncludes.size());
assertThat(ourLastIncludes, containsInAnyOrder(new Include("foo", false), new Include("bar", true)));
}
@Test
public void testSearchIncludesParametersIncludesList() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchIncludesList&_include=foo&_include:recurse=bar");
CloseableHttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(2, ourLastIncludes.size());
assertThat(ourLastIncludes, containsInAnyOrder(new Include("foo", false), new Include("bar", true)));
}
/**
* #149
*/
@ -269,24 +249,6 @@ public class SearchSearchServerDstu1Test {
}
/**
* Try loading the page as a POST just to make sure we get the right error
*/
@Test
public void testGetPagesWithPost() throws Exception {
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort);
List<? extends NameValuePair> parameters = Collections.singletonList(new BasicNameValuePair("_getpages", "AAA"));
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
CloseableHttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(400, status.getStatusLine().getStatusCode());
// assertThat(responseContent, containsString("Requests for _getpages must use HTTP GET"));
}
@Test
public void testSearchById() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
@ -312,37 +274,6 @@ public class SearchSearchServerDstu1Test {
assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
}
@Test
public void testSearchWithOrList() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?findPatientWithOrList=aaa,bbb");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("aaa", p.getIdentifier().get(0).getValue().getValue());
assertEquals("bbb", p.getIdentifier().get(1).getValue().getValue());
}
@Test
public void testSearchWithTokenParameter() throws Exception {
String token = UrlEscapers.urlFragmentEscaper().asFunction().apply("http://www.dmix.gov/vista/2957|301");
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?tokenParam=" + token);
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("http://www.dmix.gov/vista/2957", p.getNameFirstRep().getFamilyAsSingleString());
assertEquals("301", p.getNameFirstRep().getGivenAsSingleString());
}
@Test
public void testSearchByPost() throws Exception {
HttpPost filePost = new HttpPost("http://localhost:" + ourPort + "/Patient/_search");
@ -365,35 +296,6 @@ public class SearchSearchServerDstu1Test {
assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
}
/**
* See #164
*/
@Test
public void testSearchByPostWithParamsInBodyAndUrl() throws Exception {
HttpPost filePost = new HttpPost("http://localhost:" + ourPort + "/Patient/_search?name=Central");
// add parameters to the post method
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("_id", "aaa"));
UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, "UTF-8");
filePost.setEntity(sendentity);
HttpResponse status = ourClient.execute(filePost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("idaaa", p.getName().get(0).getFamilyAsSingleString());
assertEquals("nameCentral", p.getName().get(1).getFamilyAsSingleString());
}
/**
* See #164
*/
@ -436,6 +338,35 @@ public class SearchSearchServerDstu1Test {
assertThat(responseContent, containsString("<details value=\"No Content-Type header was provided in the request. This is required for &quot;CREATE&quot; operation\"/>"));
}
/**
* See #164
*/
@Test
public void testSearchByPostWithParamsInBodyAndUrl() throws Exception {
HttpPost filePost = new HttpPost("http://localhost:" + ourPort + "/Patient/_search?name=Central");
// add parameters to the post method
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("_id", "aaa"));
UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, "UTF-8");
filePost.setEntity(sendentity);
HttpResponse status = ourClient.execute(filePost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("idaaa", p.getName().get(0).getFamilyAsSingleString());
assertEquals("nameCentral", p.getName().get(1).getFamilyAsSingleString());
}
@Test
public void testSearchCompartment() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/fooCompartment");
@ -472,6 +403,75 @@ public class SearchSearchServerDstu1Test {
}
@Test
public void testSearchIncludesParametersIncludes() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchIncludes&_include=foo&_include:recurse=bar");
CloseableHttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(2, ourLastIncludes.size());
assertThat(ourLastIncludes, containsInAnyOrder(new Include("foo", false), new Include("bar", true)));
}
@Test
public void testSearchIncludesParametersIncludesList() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchIncludesList&_include=foo&_include:recurse=bar");
CloseableHttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(2, ourLastIncludes.size());
assertThat(ourLastIncludes, containsInAnyOrder(new Include("foo", false), new Include("bar", true)));
}
@Test
public void testSearchIncludesParametersNone() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=searchIncludes");
CloseableHttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(ourLastIncludes, empty());
}
@Test
public void testSearchWithOrList() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?findPatientWithOrList=aaa,bbb");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("aaa", p.getIdentifier().get(0).getValue().getValue());
assertEquals("bbb", p.getIdentifier().get(1).getValue().getValue());
}
@Test
public void testSearchWithTokenParameter() throws Exception {
String token = UrlEscapers.urlFragmentEscaper().asFunction().apply("http://www.dmix.gov/vista/2957|301");
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?tokenParam=" + token);
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("http://www.dmix.gov/vista/2957", p.getNameFirstRep().getFamilyAsSingleString());
assertEquals("301", p.getNameFirstRep().getGivenAsSingleString());
}
@Test
public void testSpecificallyNamedQueryGetsPrecedence() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?AAA=123");
@ -501,9 +501,11 @@ public class SearchSearchServerDstu1Test {
assertEquals("AAANamed", p.getIdentifierFirstRep().getValue().getValue());
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -554,15 +556,8 @@ public class SearchSearchServerDstu1Test {
}
public static class DummyPatientResourceProvider implements IResourceProvider {
/**
* Only needed for #164
*/
@Create
public MethodOutcome create(@ResourceParam Patient thePatient) {
throw new IllegalArgumentException();
}
public static class DummyPatientResourceProvider implements IResourceProvider {
@Search(compartmentName = "fooCompartment")
public List<Patient> compartment(@IdParam IdDt theId) {
@ -575,12 +570,12 @@ public class SearchSearchServerDstu1Test {
return retVal;
}
@Search(queryName = "searchWithRef")
public Patient searchWithRef() {
Patient patient = new Patient();
patient.setId("Patient/1/_history/1");
patient.getManagingOrganization().setReference("http://localhost:" + ourPort + "/Organization/555/_history/666");
return patient;
/**
* Only needed for #164
*/
@Create
public MethodOutcome create(@ResourceParam Patient thePatient) {
throw new IllegalArgumentException();
}
@Search
@ -622,6 +617,13 @@ public class SearchSearchServerDstu1Test {
return retVal;
}
@Search()
public List<Patient> findPatientWithAndList(@RequiredParam(name = "findPatientWithAndList") StringAndListParam theParam) {
ourLastAndList = theParam;
ArrayList<Patient> retVal = new ArrayList<Patient>();
return retVal;
}
@Search()
public List<Patient> findPatientWithOrList(@RequiredParam(name = "findPatientWithOrList") StringOrListParam theParam) {
ArrayList<Patient> retVal = new ArrayList<Patient>();
@ -635,13 +637,6 @@ public class SearchSearchServerDstu1Test {
return retVal;
}
@Search()
public List<Patient> findPatientWithAndList(@RequiredParam(name = "findPatientWithAndList") StringAndListParam theParam) {
ourLastAndList = theParam;
ArrayList<Patient> retVal = new ArrayList<Patient>();
return retVal;
}
@Search()
public List<Patient> findPatientWithToken(@RequiredParam(name = "tokenParam") TokenParam theParam) {
ArrayList<Patient> retVal = new ArrayList<Patient>();
@ -669,6 +664,11 @@ public class SearchSearchServerDstu1Test {
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Search(queryName = "searchIncludes")
public List<Patient> searchIncludes(@IncludeParam Set<Include> theIncludes) {
ourLastIncludes = theIncludes;
@ -687,17 +687,14 @@ public class SearchSearchServerDstu1Test {
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
@Search(queryName = "searchWithRef")
public Patient searchWithRef() {
Patient patient = new Patient();
patient.setId("Patient/1/_history/1");
patient.getManagingOrganization().setReference("http://localhost:" + ourPort + "/Organization/555/_history/666");
return patient;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -51,58 +51,14 @@ import ca.uhn.fhir.util.TestUtil;
public class ServerFeaturesTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private static final FhirContext ourCtx = FhirContext.forDstu1();
@Test
public void testPrettyPrint() throws Exception {
/*
* Not specified
*/
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
/*
* Disabled
*/
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=false");
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
/*
* Enabled
*/
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=true");
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, IsNot.not(StringContains.containsString("<identifier><use")));
}
@Test
public void testAcceptHeaderXml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", Constants.CT_FHIR_XML);
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
@Before
public void before() {
servlet.setServerAddressStrategy(new IncomingRequestAddressStrategy());
}
@Test
@ -131,38 +87,6 @@ public class ServerFeaturesTest {
}
@Test
public void testAcceptHeaderWithMultiple() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/plain, " + Constants.CT_FHIR_XML);
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/plain, " + Constants.CT_ATOM_XML);
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
}
@Test
public void testAcceptHeaderWithMultipleJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/plain, " + Constants.CT_FHIR_JSON);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("\"identifier\":"));
}
/**
* Header value should be application/xml+fhir or application/json+fhir but
* we should also accept application/xml and application/json
@ -214,16 +138,34 @@ public class ServerFeaturesTest {
}
@Test
public void testHardcodedAddressStrategy() throws Exception {
public void testAcceptHeaderWithMultiple() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/plain, " + Constants.CT_FHIR_XML);
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
servlet.setServerAddressStrategy(new HardcodedServerAddressStrategy("http://foo/bar"));
assertThat(responseContent, StringContains.containsString("<identifier><use"));
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=1");
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/plain, " + Constants.CT_ATOM_XML);
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
}
@Test
public void testAcceptHeaderWithMultipleJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/plain, " + Constants.CT_FHIR_JSON);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<id>http://foo/bar/Patient/1"));
assertThat(responseContent, StringContains.containsString("\"identifier\":"));
}
@ -275,6 +217,32 @@ public class ServerFeaturesTest {
assertThat(responseContent, (containsString(",\n")));
}
@Test
public void testAcceptHeaderXml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", Constants.CT_FHIR_XML);
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
}
@Test
public void testHardcodedAddressStrategy() throws Exception {
servlet.setServerAddressStrategy(new HardcodedServerAddressStrategy("http://foo/bar"));
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=1");
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<id>http://foo/bar/Patient/1"));
}
@Test
public void testInternalErrorIfNoId() throws Exception {
@ -289,6 +257,43 @@ public class ServerFeaturesTest {
}
@Test
public void testPrettyPrint() throws Exception {
/*
* Not specified
*/
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
/*
* Disabled
*/
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=false");
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, StringContains.containsString("<identifier><use"));
/*
* Enabled
*/
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=true");
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertThat(responseContent, IsNot.not(StringContains.containsString("<identifier><use")));
}
@Test
public void testSearchReturnWithAbsoluteIdSpecified() throws Exception {
@ -325,13 +330,9 @@ public class ServerFeaturesTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
}
@Before
public void before() {
servlet.setServerAddressStrategy(new IncomingRequestAddressStrategy());
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -357,11 +358,46 @@ public class ServerFeaturesTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
}
@Search(queryName = "findPatientsWithAbsoluteIdSpecified")
public List<Patient> findPatientsWithAbsoluteIdSpecified() {
Patient p = new Patient();
p.addIdentifier().setSystem("foo");
p.setId("http://absolute.com/Patient/123/_history/22");
Organization o = new Organization();
o.setId("http://foo.com/Organization/222/_history/333");
p.getManagingOrganization().setResource(o);
return Collections.singletonList(p);
}
@Search(queryName = "findPatientsWithNoIdSpecified")
public List<Patient> findPatientsWithNoIdSpecified() {
Patient p = new Patient();
p.addIdentifier().setSystem("foo");
return Collections.singletonList(p);
}
public Map<String, Patient> getIdToPatient() {
Map<String, Patient> idToPatient = new HashMap<String, Patient>();
{
@ -398,14 +434,6 @@ public class ServerFeaturesTest {
return retVal;
}
@Search(queryName = "searchWithWildcardRetVal")
public List<? extends IResource> searchWithWildcardRetVal() {
Patient p = new Patient();
p.setId("1234");
p.addName().addFamily("searchWithWildcardRetVal");
return Collections.singletonList(p);
}
/**
* Retrieve the resource by its identifier
*
@ -423,51 +451,19 @@ public class ServerFeaturesTest {
}
}
@Search(queryName = "findPatientsWithNoIdSpecified")
public List<Patient> findPatientsWithNoIdSpecified() {
Patient p = new Patient();
p.addIdentifier().setSystem("foo");
return Collections.singletonList(p);
}
@Search(queryName = "findPatientsWithAbsoluteIdSpecified")
public List<Patient> findPatientsWithAbsoluteIdSpecified() {
Patient p = new Patient();
p.addIdentifier().setSystem("foo");
p.setId("http://absolute.com/Patient/123/_history/22");
Organization o = new Organization();
o.setId("http://foo.com/Organization/222/_history/333");
p.getManagingOrganization().setResource(o);
return Collections.singletonList(p);
}
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
@Search(queryName = "searchWithWildcardRetVal")
public List<? extends IResource> searchWithWildcardRetVal() {
Patient p = new Patient();
p.setId("1234");
p.addName().addFamily("searchWithWildcardRetVal");
return Collections.singletonList(p);
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -37,9 +37,9 @@ import ca.uhn.fhir.util.TestUtil;
public class SortTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static FhirContext ourCtx = FhirContext.forDstu1();
@Test
public void testNoSort() throws Exception {
@ -125,8 +125,9 @@ public class SortTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -151,6 +152,7 @@ public class SortTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -181,10 +183,4 @@ public class SortTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -162,8 +162,9 @@ public class StringParameterTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -189,6 +190,7 @@ public class StringParameterTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -250,10 +252,4 @@ public class StringParameterTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -111,6 +111,25 @@ public class TagsServerTest {
assertEquals(ourProvider.getAllTags(), tagList);
}
@Test
public void testGetAllTagsObservationIdVersion() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Observation/111/_history/222/_tags");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
TagList actual = ourCtx.newXmlParser().parseTagList(responseContent);
TagList expected = new TagList();
expected.addTag(null, "Patient111222", "DogLabel");
expected.addTag("http://cats", "AllCat", "CatLabel");
assertEquals(expected, actual);
}
@Test
public void testGetAllTagsPatient() throws Exception {
@ -169,25 +188,6 @@ public class TagsServerTest {
expected.addTag("http://cats", "AllCat", "CatLabel");
assertEquals(expected, actual);
}
@Test
public void testGetAllTagsObservationIdVersion() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Observation/111/_history/222/_tags");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
TagList actual = ourCtx.newXmlParser().parseTagList(responseContent);
TagList expected = new TagList();
expected.addTag(null, "Patient111222", "DogLabel");
expected.addTag("http://cats", "AllCat", "CatLabel");
assertEquals(expected, actual);
}
@Test
public void testRemoveTagsById() throws Exception {
@ -227,9 +227,11 @@ public class TagsServerTest {
assertEquals(tagList, ourLastTagList);
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -255,6 +257,7 @@ public class TagsServerTest {
}
public static class DummyProvider {
@AddTags(type = Patient.class)
@ -271,18 +274,18 @@ public class TagsServerTest {
return tagList;
}
@GetTags(type = Patient.class)
public TagList getAllTagsPatient() {
@GetTags(type = Observation.class)
public TagList getAllTagsObservationIdVersion(@IdParam IdDt theId) {
TagList tagList = new TagList();
tagList.add(new Tag((String) null, "Patient", "DogLabel"));
tagList.add(new Tag((String) null, "Patient" + theId.getIdPart() + theId.getVersionIdPart(), "DogLabel"));
tagList.add(new Tag("http://cats", "AllCat", "CatLabel"));
return tagList;
}
@GetTags(type = Patient.class)
public TagList getAllTagsPatientId(@IdParam IdDt theId) {
public TagList getAllTagsPatient() {
TagList tagList = new TagList();
tagList.add(new Tag((String) null, "Patient" + theId.getIdPart() + defaultString(theId.getVersionIdPart()), "DogLabel"));
tagList.add(new Tag((String) null, "Patient", "DogLabel"));
tagList.add(new Tag("http://cats", "AllCat", "CatLabel"));
return tagList;
}
@ -295,10 +298,10 @@ public class TagsServerTest {
// return tagList;
// }
@GetTags(type = Observation.class)
public TagList getAllTagsObservationIdVersion(@IdParam IdDt theId) {
@GetTags(type = Patient.class)
public TagList getAllTagsPatientId(@IdParam IdDt theId) {
TagList tagList = new TagList();
tagList.add(new Tag((String) null, "Patient" + theId.getIdPart() + theId.getVersionIdPart(), "DogLabel"));
tagList.add(new Tag((String) null, "Patient" + theId.getIdPart() + defaultString(theId.getVersionIdPart()), "DogLabel"));
tagList.add(new Tag("http://cats", "AllCat", "CatLabel"));
return tagList;
}
@ -317,10 +320,4 @@ public class TagsServerTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -37,16 +37,46 @@ public class TokenParameterTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static TokenOrListParam ourLastOrList;
private static int ourPort;
private static Server ourServer;
@Before
public void before() {
ourLastOrList = null;
}
@Test
public void testGetModifiersNone() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?identifier=a%7Cb");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(1, ourLastOrList.getListAsCodings().size());
assertEquals(null, ourLastOrList.getValuesAsQueryTokens().get(0).getModifier());
assertEquals("a", ourLastOrList.getListAsCodings().get(0).getSystemElement().getValue());
assertEquals("b", ourLastOrList.getListAsCodings().get(0).getCodeElement().getValue());
assertEquals("a", ourLastOrList.getValuesAsQueryTokens().get(0).getSystem());
assertEquals("b", ourLastOrList.getValuesAsQueryTokens().get(0).getValue());
}
@Test
public void testGetModifiersText() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?identifier:text=a%7Cb");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(1, ourLastOrList.getListAsCodings().size());
assertEquals(TokenParamModifier.TEXT, ourLastOrList.getValuesAsQueryTokens().get(0).getModifier());
assertEquals(null, ourLastOrList.getValuesAsQueryTokens().get(0).getSystem());
assertEquals("a|b", ourLastOrList.getValuesAsQueryTokens().get(0).getValue());
}
/**
* Test #192
*/
@ -78,7 +108,7 @@ public class TokenParameterTest {
assertEquals("system", ourLastOrList.getListAsCodings().get(0).getSystemElement().getValue());
assertEquals("code-include-end-with-comma,", ourLastOrList.getListAsCodings().get(0).getCodeElement().getValue());
}
/**
* Test #192
*/
@ -116,7 +146,7 @@ public class TokenParameterTest {
assertEquals(",", ourLastOrList.getListAsCodings().get(2).getCodeElement().getValue());
assertEquals(3, ourLastOrList.getListAsCodings().size());
}
/**
* Test #192
*/
@ -137,9 +167,11 @@ public class TokenParameterTest {
assertEquals(3, ourLastOrList.getListAsCodings().size());
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -164,6 +196,7 @@ public class TokenParameterTest {
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
@ -220,41 +253,4 @@ public class TokenParameterTest {
}
@Test
public void testGetModifiersNone() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?identifier=a%7Cb");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(1, ourLastOrList.getListAsCodings().size());
assertEquals(null, ourLastOrList.getValuesAsQueryTokens().get(0).getModifier());
assertEquals("a", ourLastOrList.getListAsCodings().get(0).getSystemElement().getValue());
assertEquals("b", ourLastOrList.getListAsCodings().get(0).getCodeElement().getValue());
assertEquals("a", ourLastOrList.getValuesAsQueryTokens().get(0).getSystem());
assertEquals("b", ourLastOrList.getValuesAsQueryTokens().get(0).getValue());
}
@Test
public void testGetModifiersText() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?identifier:text=a%7Cb");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(1, ourLastOrList.getListAsCodings().size());
assertEquals(TokenParamModifier.TEXT, ourLastOrList.getValuesAsQueryTokens().get(0).getModifier());
assertEquals(null, ourLastOrList.getValuesAsQueryTokens().get(0).getSystem());
assertEquals("a|b", ourLastOrList.getValuesAsQueryTokens().get(0).getValue());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -219,8 +219,9 @@ public class TransactionTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -249,6 +250,7 @@ public class TransactionTest {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -283,10 +285,4 @@ public class TransactionTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -168,10 +168,13 @@ public class TransactionWithBundleParamTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -197,9 +200,8 @@ public class TransactionWithBundleParamTest {
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -232,10 +234,4 @@ public class TransactionWithBundleParamTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -44,11 +44,11 @@ import ca.uhn.fhir.util.TestUtil;
*/
public class UpdateTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(UpdateTest.class);
private static int ourPort;
private static DiagnosticReportProvider ourReportProvider;
private static Server ourServer;
private static FhirContext ourCtx = FhirContext.forDstu1();
@Test
public void testUpdate() throws Exception {
@ -75,30 +75,6 @@ public class UpdateTest {
}
@Test
public void testUpdateWhichReturnsCreate() throws Exception {
Patient patient = new Patient();
patient.addIdentifier().setValue("002");
HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Patient/001CREATE");
httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
OperationOutcome oo = ourCtx.newXmlParser().parseResource(OperationOutcome.class, responseContent);
assertEquals("OODETAILS", oo.getIssueFirstRep().getDetails().getValue());
assertEquals(201, status.getStatusLine().getStatusCode());
assertEquals("http://localhost:" + ourPort + "/Patient/001CREATE/_history/002", status.getFirstHeader("location").getValue());
}
@Test
public void testUpdateMethodReturnsInvalidId() throws Exception {
@ -142,6 +118,47 @@ public class UpdateTest {
}
}
@Test
public void testUpdateWhichReturnsCreate() throws Exception {
Patient patient = new Patient();
patient.addIdentifier().setValue("002");
HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Patient/001CREATE");
httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Response was:\n{}", responseContent);
OperationOutcome oo = ourCtx.newXmlParser().parseResource(OperationOutcome.class, responseContent);
assertEquals("OODETAILS", oo.getIssueFirstRep().getDetails().getValue());
assertEquals(201, status.getStatusLine().getStatusCode());
assertEquals("http://localhost:" + ourPort + "/Patient/001CREATE/_history/002", status.getFirstHeader("location").getValue());
}
@Test
public void testUpdateWithNoReturn() throws Exception {
Organization dr = new Organization();
dr.setId("001");
dr.addIdentifier().setValue("002");
HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Organization/001");
httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
CloseableHttpResponse status = ourClient.execute(httpPost);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
status.close();
}
@Test
public void testUpdateWithTagMultiple() throws Exception {
@ -301,26 +318,10 @@ public class UpdateTest {
fail();
}
@Test
public void testUpdateWithNoReturn() throws Exception {
Organization dr = new Organization();
dr.setId("001");
dr.addIdentifier().setValue("002");
HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Organization/001");
httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
CloseableHttpResponse status = ourClient.execute(httpPost);
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
status.close();
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -347,21 +348,6 @@ public class UpdateTest {
}
public static class OrganizationResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Organization.class;
}
@SuppressWarnings("unused")
@Update
public MethodOutcome update(@IdParam IdDt theId, @ResourceParam Organization theOrganization) {
return new MethodOutcome();
}
}
public static class DiagnosticReportProvider implements IResourceProvider {
private TagList myLastTags;
@ -408,6 +394,22 @@ public class UpdateTest {
}
}
public static class OrganizationResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Organization.class;
}
@SuppressWarnings("unused")
@Update
public MethodOutcome update(@IdParam IdDt theId, @ResourceParam Organization theOrganization) {
return new MethodOutcome();
}
}
public static class PatientProvider implements IResourceProvider {
@Override
@ -429,10 +431,4 @@ public class UpdateTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -38,10 +38,10 @@ import ca.uhn.fhir.util.TestUtil;
*/
public class ValidateDstu1Test {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static EncodingEnum ourLastEncoding;
private static String ourLastResourceBody;
private static int ourPort;
private static FhirContext ourCtx = FhirContext.forDstu1();
private static Server ourServer;
@Before()
@ -88,8 +88,9 @@ public class ValidateDstu1Test {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -114,8 +115,31 @@ public class ValidateDstu1Test {
}
public static class OrganizationProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Organization.class;
}
@Validate()
public MethodOutcome validate(@ResourceParam String theResourceBody, @ResourceParam EncodingEnum theEncoding) {
ourLastResourceBody = theResourceBody;
ourLastEncoding = theEncoding;
return new MethodOutcome(new IdDt("001"));
}
}
public static class PatientProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Validate()
public MethodOutcome validatePatient(@ResourceParam Patient thePatient, @ResourceParam String theResourceBody, @ResourceParam EncodingEnum theEncoding) {
IdDt id = new IdDt(thePatient.getIdentifier().get(0).getValue().getValue());
@ -129,34 +153,6 @@ public class ValidateDstu1Test {
return new MethodOutcome(id.withVersion("002"));
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
public static class OrganizationProvider implements IResourceProvider {
@Validate()
public MethodOutcome validate(@ResourceParam String theResourceBody, @ResourceParam EncodingEnum theEncoding) {
ourLastResourceBody = theResourceBody;
ourLastEncoding = theEncoding;
return new MethodOutcome(new IdDt("001"));
}
@Override
public Class<? extends IResource> getResourceType() {
return Organization.class;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -62,63 +62,16 @@ import ca.uhn.fhir.util.TestUtil;
public class AuditingInterceptorTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private IServerInterceptor myInterceptor;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private class MockDataStore implements IAuditDataStore {
@Override
public void store(BaseSecurityEvent auditEvent) throws Exception {
//do nothing
}
}
@Test
public void testSinglePatient() throws Exception {
AuditingInterceptor interceptor = new AuditingInterceptor("HAPITEST", false);
Map<String, Class<? extends IResourceAuditor<? extends IResource>>> auditors = new HashMap<String, Class<? extends IResourceAuditor<? extends IResource>>>();
auditors.put("Patient", PatientAuditor.class);
interceptor.setAuditableResources(auditors );
servlet.setInterceptors(Collections.singletonList((IServerInterceptor)interceptor));
MockDataStore mockDataStore = mock(MockDataStore.class);
interceptor.setDataStore(mockDataStore);
String requestURL = "http://localhost:" + ourPort + "/Patient/1";
HttpGet httpGet = new HttpGet(requestURL);
httpGet.addHeader(UserInfoInterceptor.HEADER_USER_ID, "hapi-fhir-junit-user");
httpGet.addHeader(UserInfoInterceptor.HEADER_USER_NAME, "HAPI FHIR Junit Test Cases");
httpGet.addHeader(UserInfoInterceptor.HEADER_APPLICATION_NAME, "hapi-fhir-junit");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
ArgumentCaptor<SecurityEvent> captor = ArgumentCaptor.forClass(SecurityEvent.class);
verify(mockDataStore, times(1)).store(captor.capture());
SecurityEvent auditEvent = captor.getValue();
assertEquals(SecurityEventOutcomeEnum.SUCCESS.getCode(), auditEvent.getEvent().getOutcome().getValue());
assertEquals("HAPI FHIR Junit Test Cases", auditEvent.getParticipantFirstRep().getName().getValue());
assertEquals("hapi-fhir-junit-user", auditEvent.getParticipantFirstRep().getUserId().getValue());
assertEquals("hapi-fhir-junit", auditEvent.getSource().getIdentifier().getValue());
assertEquals("HAPITEST", auditEvent.getSource().getSite().getValue());
assertEquals(SecurityEventSourceTypeEnum.USER_DEVICE.getCode(), auditEvent.getSource().getTypeFirstRep().getCode().getValue());
List<ObjectElement> objects = auditEvent.getObject();
assertEquals(1, objects.size());
ObjectElement object = objects.get(0);
assertEquals("00001", object.getIdentifier().getValue().getValue());
assertEquals("Patient: PatientOne Test", object.getName().getValue());
assertEquals(SecurityEventObjectLifecycleEnum.ACCESS_OR_USE, object.getLifecycle().getValueAsEnum());
assertEquals(SecurityEventObjectTypeEnum.PERSON, object.getType().getValueAsEnum());
assertEquals(requestURL, new String(Base64.decodeBase64(object.getQuery().getValueAsString())));
@Before
public void before() {
myInterceptor = mock(IServerInterceptor.class);
servlet.setInterceptors(Collections.singletonList(myInterceptor));
}
@Test
@ -170,18 +123,57 @@ public class AuditingInterceptorTest {
}
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
@Test
public void testSinglePatient() throws Exception {
AuditingInterceptor interceptor = new AuditingInterceptor("HAPITEST", false);
Map<String, Class<? extends IResourceAuditor<? extends IResource>>> auditors = new HashMap<String, Class<? extends IResourceAuditor<? extends IResource>>>();
auditors.put("Patient", PatientAuditor.class);
interceptor.setAuditableResources(auditors );
servlet.setInterceptors(Collections.singletonList((IServerInterceptor)interceptor));
MockDataStore mockDataStore = mock(MockDataStore.class);
interceptor.setDataStore(mockDataStore);
String requestURL = "http://localhost:" + ourPort + "/Patient/1";
HttpGet httpGet = new HttpGet(requestURL);
httpGet.addHeader(UserInfoInterceptor.HEADER_USER_ID, "hapi-fhir-junit-user");
httpGet.addHeader(UserInfoInterceptor.HEADER_USER_NAME, "HAPI FHIR Junit Test Cases");
httpGet.addHeader(UserInfoInterceptor.HEADER_APPLICATION_NAME, "hapi-fhir-junit");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.closeQuietly(status.getEntity().getContent());
ArgumentCaptor<SecurityEvent> captor = ArgumentCaptor.forClass(SecurityEvent.class);
verify(mockDataStore, times(1)).store(captor.capture());
SecurityEvent auditEvent = captor.getValue();
assertEquals(SecurityEventOutcomeEnum.SUCCESS.getCode(), auditEvent.getEvent().getOutcome().getValue());
assertEquals("HAPI FHIR Junit Test Cases", auditEvent.getParticipantFirstRep().getName().getValue());
assertEquals("hapi-fhir-junit-user", auditEvent.getParticipantFirstRep().getUserId().getValue());
assertEquals("hapi-fhir-junit", auditEvent.getSource().getIdentifier().getValue());
assertEquals("HAPITEST", auditEvent.getSource().getSite().getValue());
assertEquals(SecurityEventSourceTypeEnum.USER_DEVICE.getCode(), auditEvent.getSource().getTypeFirstRep().getCode().getValue());
List<ObjectElement> objects = auditEvent.getObject();
assertEquals(1, objects.size());
ObjectElement object = objects.get(0);
assertEquals("00001", object.getIdentifier().getValue().getValue());
assertEquals("Patient: PatientOne Test", object.getName().getValue());
assertEquals(SecurityEventObjectLifecycleEnum.ACCESS_OR_USE, object.getLifecycle().getValueAsEnum());
assertEquals(SecurityEventObjectTypeEnum.PERSON, object.getType().getValueAsEnum());
assertEquals(requestURL, new String(Base64.decodeBase64(object.getQuery().getValueAsString())));
}
@Before
public void before() {
myInterceptor = mock(IServerInterceptor.class);
servlet.setInterceptors(Collections.singletonList(myInterceptor));
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@ -210,6 +202,20 @@ public class AuditingInterceptorTest {
public static class DummyPatientResourceProvider implements IResourceProvider {
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
}
public Map<String, Patient> getIdToPatient() {
Map<String, Patient> idToPatient = new HashMap<String, Patient>();
{
@ -237,6 +243,7 @@ public class AuditingInterceptorTest {
return idToPatient;
}
/**
* Retrieve the resource by its identifier
*
@ -251,7 +258,7 @@ public class AuditingInterceptorTest {
return retVal;
}
/**
* Retrieve the resource by its identifier
*
@ -271,31 +278,20 @@ public class AuditingInterceptorTest {
return patients;
}
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getGender().setText("M");
patient.getId().setValue("1");
return patient;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
private class MockDataStore implements IAuditDataStore {
@Override
public void store(BaseSecurityEvent auditEvent) throws Exception {
//do nothing
}
}

View File

@ -45,16 +45,16 @@ import ca.uhn.fhir.util.TestUtil;
public class ExceptionHandlingInterceptorTest {
private static ExceptionHandlingInterceptor myInterceptor;
private static final String OPERATION_OUTCOME_DETAILS = "OperationOutcomeDetails";
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static Class<? extends Exception> ourExceptionType;
private static boolean ourGenerateOperationOutcome;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExceptionTest.class);
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private static ExceptionHandlingInterceptor myInterceptor;
private static final FhirContext ourCtx = FhirContext.forDstu1();
@Before
public void before() {
@ -97,13 +97,12 @@ public class ExceptionHandlingInterceptorTest {
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -127,6 +126,8 @@ public class ExceptionHandlingInterceptorTest {
myInterceptor = new ExceptionHandlingInterceptor();
servlet.registerInterceptor(myInterceptor);
}
/**
* Created by dsotnikov on 2/25/2014.
*/
@ -171,10 +172,4 @@ public class ExceptionHandlingInterceptorTest {
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -41,12 +41,19 @@ import ca.uhn.fhir.util.TestUtil;
public class ExceptionInterceptorMethodTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExceptionInterceptorMethodTest.class);
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private IServerInterceptor myInterceptor;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExceptionInterceptorMethodTest.class);
private static final FhirContext ourCtx = FhirContext.forDstu1();
@Before
public void before() {
myInterceptor = mock(IServerInterceptor.class);
servlet.setInterceptors(Collections.singletonList(myInterceptor));
}
@Test
public void testThrowUnprocessableEntityException() throws Exception {
@ -66,8 +73,7 @@ public class ExceptionInterceptorMethodTest {
assertEquals(UnprocessableEntityException.class, captor.getValue().getClass());
}
@Test
public void testThrowUnprocessableEntityExceptionAndOverrideResponse() throws Exception {
@ -98,14 +104,9 @@ public class ExceptionInterceptorMethodTest {
}
@AfterClass
public static void afterClass() throws Exception {
public static void afterClassClearContext() throws Exception {
ourServer.stop();
}
@Before
public void before() {
myInterceptor = mock(IServerInterceptor.class);
servlet.setInterceptors(Collections.singletonList(myInterceptor));
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
@ -135,6 +136,11 @@ public class ExceptionInterceptorMethodTest {
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
/**
* Retrieve the resource by its identifier
*
@ -147,16 +153,6 @@ public class ExceptionInterceptorMethodTest {
throw new UnprocessableEntityException("Unprocessable!");
}
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}

View File

@ -167,7 +167,7 @@ public class ValueSetGenerator {
private ValueSetTm parseValueSet(ca.uhn.fhir.model.dstu2.resource.ValueSet nextVs) {
myConceptCount += nextVs.getCodeSystem().getConcept().size();
ourLog.info("Parsing ValueSetTm #{} - {} - {} concepts total", myValueSetCount++, nextVs.getName(), myConceptCount);
ourLog.debug("Parsing ValueSetTm #{} - {} - {} concepts total", myValueSetCount++, nextVs.getName(), myConceptCount);
// output.addConcept(next.getCode().getValue(),
// next.getDisplay().getValue(), next.getDefinition());
@ -216,7 +216,7 @@ public class ValueSetGenerator {
private ValueSetTm parseValueSet(ValueSet nextVs) {
myConceptCount += nextVs.getDefine().getConcept().size();
ourLog.info("Parsing ValueSetTm #{} - {} - {} concepts total", myValueSetCount++, nextVs.getName().getValue(), myConceptCount);
ourLog.debug("Parsing ValueSetTm #{} - {} - {} concepts total", myValueSetCount++, nextVs.getName().getValue(), myConceptCount);
// output.addConcept(next.getCode().getValue(),
// next.getDisplay().getValue(), next.getDefinition());