Add tests for operations

This commit is contained in:
James Agnew 2015-03-19 06:03:23 -04:00
parent 75877c18eb
commit c3877c1da9
3 changed files with 86 additions and 4 deletions

View File

@ -94,7 +94,7 @@
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven_assembly_plugin_version}</version>
<version>2.5.3</version>
<executions>
<execution>
<phase>package</phase>
@ -104,8 +104,8 @@
<configuration>
<attach>true</attach>
<descriptors>
<descriptor>${project.basedir}/src/assembly/android-sources.xml</descriptor>
<descriptor>${project.basedir}/src/assembly/android-javadoc.xml</descriptor>
<descriptor>/Users/t3903uhn/git/hapi-fhir/hapi-fhir-android/src/assembly/android-sources.xml</descriptor>
<descriptor>/Users/t3903uhn/git/hapi-fhir/hapi-fhir-android/src/assembly/android-javadoc.xml</descriptor>
</descriptors>
</configuration>
</execution>

View File

@ -41,6 +41,7 @@ import ca.uhn.fhir.model.valueset.BundleTypeEnum;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.RestfulServerUtils;
@ -128,6 +129,10 @@ public class OperationMethodBinding extends BaseResourceReturningMethodBinding {
IParser parser = encoding.newParser(getContext());
BufferedReader requestReader = theRequest.getServletRequest().getReader();
if (theRequest.getRequestType() == RequestType.GET) {
return null;
}
Class<? extends IBaseResource> wantedResourceType = getContext().getResourceDefinition("Parameters").getImplementingClass();
return parser.parseResource(wantedResourceType, requestReader);
}

View File

@ -8,6 +8,8 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@ -33,6 +35,7 @@ import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.OperationParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.util.PortUtil;
/**
@ -106,7 +109,6 @@ public class OperationServerTest {
assertEquals("100", resp.getEntryFirstRep().getTransactionResponse().getStatus());
}
@Test
public void testOperationOnServer() throws Exception {
Parameters p = new Parameters();
@ -202,6 +204,63 @@ public class OperationServerTest {
assertThat(response, containsString("Request has parameter PARAM1 of type IntegerDt but method expects type StringDt"));
}
@Test
public void testReadWithOperations() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123");
HttpResponse status = ourClient.execute(httpGet);
assertEquals(200, status.getStatusLine().getStatusCode());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals("read", ourLastMethod);
}
@Test
public void testInstanceEverythingPost() throws Exception {
String inParamsStr = ourCtx.newXmlParser().encodeResourceToString(new Parameters());
// Try with a POST
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient/123/$everything");
httpPost.setEntity(new StringEntity(inParamsStr, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
HttpResponse status = ourClient.execute(httpPost);
assertEquals(200, status.getStatusLine().getStatusCode());
String response = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals("instance $everything", ourLastMethod);
assertThat(response, startsWith("<Bundle"));
assertEquals("Patient/123", ourLastId.toUnqualifiedVersionless().getValue());
}
@Test
public void testInstanceEverythingHapiClient() throws Exception {
Parameters p = ourCtx.newRestfulGenericClient("http://localhost:" + ourPort).operation().onInstance(new IdDt("Patient/123")).named("$everything").withParameters(new Parameters()).execute();
Bundle b = (Bundle) p.getParameterFirstRep().getResource();
assertEquals("instance $everything", ourLastMethod);
assertEquals("Patient/123", ourLastId.toUnqualifiedVersionless().getValue());
}
@Test
public void testInstanceEverythingGet() throws Exception {
// Try with a GET
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/$everything");
CloseableHttpResponse status = ourClient.execute(httpGet);
assertEquals(200, status.getStatusLine().getStatusCode());
String response = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals("instance $everything", ourLastMethod);
assertThat(response, startsWith("<Bundle"));
assertEquals("Patient/123", ourLastId.toUnqualifiedVersionless().getValue());
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
@ -275,6 +334,24 @@ public class OperationServerTest {
return Patient.class;
}
/**
* Just to make sure this method doesn't "steal" calls
*/
@Read
public Patient read(@IdParam IdDt theId) {
ourLastMethod = "read";
Patient retVal = new Patient();
retVal.setId(theId);
return retVal;
}
@Operation(name = "$everything")
public Bundle patientEverything(@IdParam IdDt thePatientId) {
ourLastMethod = "instance $everything";
ourLastId = thePatientId;
return new Bundle();
}
//@formatter:off
@Operation(name="$OP_TYPE")
public Parameters opType(