Ja interceptor callback fix (#1308)

* Interceptor callback fix

* Ensure that interceptor methods are called after a transaction
This commit is contained in:
James Agnew 2019-05-14 18:47:11 -04:00 committed by GitHub
parent f5a7333c4c
commit cb8634f26f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 16 deletions

View File

@ -1,6 +1,8 @@
package ca.uhn.fhir.jpa.provider.r4; package ca.uhn.fhir.jpa.provider.r4;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test; import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test;
import ca.uhn.fhir.jpa.provider.SystemProviderDstu2Test; import ca.uhn.fhir.jpa.provider.SystemProviderDstu2Test;
import ca.uhn.fhir.jpa.rp.r4.*; import ca.uhn.fhir.jpa.rp.r4.*;
@ -49,6 +51,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -56,7 +59,7 @@ import static org.junit.Assert.*;
public class SystemProviderR4Test extends BaseJpaR4Test { public class SystemProviderR4Test extends BaseJpaR4Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderR4Test.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderR4Test.class);
private static RestfulServer myRestServer; private static RestfulServer ourRestServer;
private static IGenericClient ourClient; private static IGenericClient ourClient;
private static FhirContext ourCtx; private static FhirContext ourCtx;
private static CloseableHttpClient ourHttpClient; private static CloseableHttpClient ourHttpClient;
@ -78,7 +81,7 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
@Before @Before
public void beforeStartServer() throws Exception { public void beforeStartServer() throws Exception {
if (myRestServer == null) { if (ourRestServer == null) {
PatientResourceProvider patientRp = new PatientResourceProvider(); PatientResourceProvider patientRp = new PatientResourceProvider();
patientRp.setDao(myPatientDao); patientRp.setDao(myPatientDao);
@ -135,19 +138,19 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
ourCtx.getRestfulClientFactory().setSocketTimeout(600 * 1000); ourCtx.getRestfulClientFactory().setSocketTimeout(600 * 1000);
ourClient = ourCtx.newRestfulGenericClient(ourServerBase); ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.setLogRequestAndResponse(true); ourClient.setLogRequestAndResponse(true);
myRestServer = restServer; ourRestServer = restServer;
} }
myRestServer.setDefaultResponseEncoding(EncodingEnum.XML); ourRestServer.setDefaultResponseEncoding(EncodingEnum.XML);
myRestServer.setPagingProvider(myPagingProvider); ourRestServer.setPagingProvider(myPagingProvider);
} }
@Test @Test
public void testEverythingReturnsCorrectBundleType() throws Exception { public void testEverythingReturnsCorrectBundleType() throws Exception {
myRestServer.setDefaultResponseEncoding(EncodingEnum.JSON); ourRestServer.setDefaultResponseEncoding(EncodingEnum.JSON);
myRestServer.setPagingProvider(new FifoMemoryPagingProvider(1).setDefaultPageSize(10)); ourRestServer.setPagingProvider(new FifoMemoryPagingProvider(1).setDefaultPageSize(10));
ResponseHighlighterInterceptor interceptor = new ResponseHighlighterInterceptor(); ResponseHighlighterInterceptor interceptor = new ResponseHighlighterInterceptor();
myRestServer.registerInterceptor(interceptor); ourRestServer.registerInterceptor(interceptor);
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
Patient p = new Patient(); Patient p = new Patient();
@ -171,15 +174,15 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
http.close(); http.close();
} }
myRestServer.unregisterInterceptor(interceptor); ourRestServer.unregisterInterceptor(interceptor);
} }
@Test @Test
public void testEverythingReturnsCorrectFormatInPagingLink() throws Exception { public void testEverythingReturnsCorrectFormatInPagingLink() throws Exception {
myRestServer.setDefaultResponseEncoding(EncodingEnum.JSON); ourRestServer.setDefaultResponseEncoding(EncodingEnum.JSON);
myRestServer.setPagingProvider(new FifoMemoryPagingProvider(1).setDefaultPageSize(10)); ourRestServer.setPagingProvider(new FifoMemoryPagingProvider(1).setDefaultPageSize(10));
ResponseHighlighterInterceptor interceptor = new ResponseHighlighterInterceptor(); ResponseHighlighterInterceptor interceptor = new ResponseHighlighterInterceptor();
myRestServer.registerInterceptor(interceptor); ourRestServer.registerInterceptor(interceptor);
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
Patient p = new Patient(); Patient p = new Patient();
@ -200,7 +203,7 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
http.close(); http.close();
} }
myRestServer.unregisterInterceptor(interceptor); ourRestServer.unregisterInterceptor(interceptor);
} }
@Test @Test
@ -249,7 +252,7 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void testResponseUsesCorrectContentType() throws Exception { public void testResponseUsesCorrectContentType() throws Exception {
myRestServer.setDefaultResponseEncoding(EncodingEnum.JSON); ourRestServer.setDefaultResponseEncoding(EncodingEnum.JSON);
HttpGet get = new HttpGet(ourServerBase); HttpGet get = new HttpGet(ourServerBase);
// get.addHeader("Accept", "application/xml, text/html"); // get.addHeader("Accept", "application/xml, text/html");
@ -716,7 +719,7 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
interceptor.addValidatorModule(val); interceptor.addValidatorModule(val);
interceptor.setFailOnSeverity(ResultSeverityEnum.ERROR); interceptor.setFailOnSeverity(ResultSeverityEnum.ERROR);
interceptor.setAddResponseHeaderOnSeverity(ResultSeverityEnum.INFORMATION); interceptor.setAddResponseHeaderOnSeverity(ResultSeverityEnum.INFORMATION);
myRestServer.registerInterceptor(interceptor); ourRestServer.registerInterceptor(interceptor);
try { try {
InputStream bundleRes = SystemProviderDstu2Test.class.getResourceAsStream("/questionnaire-sdc-profile-example-ussg-fht.xml"); InputStream bundleRes = SystemProviderDstu2Test.class.getResourceAsStream("/questionnaire-sdc-profile-example-ussg-fht.xml");
@ -741,10 +744,50 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
IOUtils.closeQuietly(resp.getEntity().getContent()); IOUtils.closeQuietly(resp.getEntity().getContent());
} }
} finally { } finally {
myRestServer.unregisterInterceptor(interceptor); ourRestServer.unregisterInterceptor(interceptor);
} }
} }
@Test()
public void testEndpointInterceptorIsCalledForTransaction() {
// Just to get this out of the way
ourClient.forceConformanceCheck();
// Register an interceptor on the response
AtomicBoolean called = new AtomicBoolean(false);
Object interceptor = new Object() {
@Hook(Pointcut.SERVER_OUTGOING_RESPONSE)
public void outgoing() {
called.set(true);
}
};
ourRestServer.getInterceptorService().registerInterceptor(interceptor);
try {
Patient p = new Patient();
p.addName().setFamily("Test");
Bundle b = new Bundle();
b.setType(Bundle.BundleType.TRANSACTION);
b.addEntry()
.setResource(p)
.setFullUrl("Patient")
.getRequest()
.setMethod(Bundle.HTTPVerb.POST)
.setUrl("Patient");
ourClient.transaction().withBundle(b).execute();
assertTrue(called.get());
} finally {
ourRestServer.getInterceptorService().unregisterInterceptor(interceptor);
}
}
@AfterClass @AfterClass
public static void afterClassClearContext() throws Exception { public static void afterClassClearContext() throws Exception {
ourServer.stop(); ourServer.stop();

View File

@ -66,6 +66,8 @@ public class TestDstu2Config extends BaseJavaConfigDstu2 {
retVal.setAllowMultipleDelete(true); retVal.setAllowMultipleDelete(true);
retVal.setAllowInlineMatchUrlReferences(true); retVal.setAllowInlineMatchUrlReferences(true);
retVal.setAllowExternalReferences(true); retVal.setAllowExternalReferences(true);
retVal.getTreatBaseUrlsAsLocal().add("http://hapi.fhir.org/baseDstu2");
retVal.getTreatBaseUrlsAsLocal().add("https://hapi.fhir.org/baseDstu2");
retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseDstu2"); retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseDstu2");
retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseDstu2"); retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseDstu2");
retVal.setCountSearchResultsUpTo(TestR4Config.COUNT_SEARCH_RESULTS_UP_TO); retVal.setCountSearchResultsUpTo(TestR4Config.COUNT_SEARCH_RESULTS_UP_TO);

View File

@ -56,6 +56,8 @@ public class TestDstu3Config extends BaseJavaConfigDstu3 {
retVal.setAllowMultipleDelete(true); retVal.setAllowMultipleDelete(true);
retVal.setAllowInlineMatchUrlReferences(true); retVal.setAllowInlineMatchUrlReferences(true);
retVal.setAllowExternalReferences(true); retVal.setAllowExternalReferences(true);
retVal.getTreatBaseUrlsAsLocal().add("http://hapi.fhir.org/baseDstu3");
retVal.getTreatBaseUrlsAsLocal().add("https://hapi.fhir.org/baseDstu3");
retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseDstu3"); retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseDstu3");
retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseDstu3"); retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseDstu3");
retVal.setCountSearchResultsUpTo(TestR4Config.COUNT_SEARCH_RESULTS_UP_TO); retVal.setCountSearchResultsUpTo(TestR4Config.COUNT_SEARCH_RESULTS_UP_TO);

View File

@ -57,6 +57,8 @@ public class TestR4Config extends BaseJavaConfigR4 {
retVal.setAllowMultipleDelete(true); retVal.setAllowMultipleDelete(true);
retVal.setAllowInlineMatchUrlReferences(true); retVal.setAllowInlineMatchUrlReferences(true);
retVal.setAllowExternalReferences(true); retVal.setAllowExternalReferences(true);
retVal.getTreatBaseUrlsAsLocal().add("http://hapi.fhir.org/baseR4");
retVal.getTreatBaseUrlsAsLocal().add("https://hapi.fhir.org/baseR4");
retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseR4"); retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseR4");
retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseR4"); retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseR4");
retVal.setIndexMissingFields(DaoConfig.IndexEnabledEnum.ENABLED); retVal.setIndexMissingFields(DaoConfig.IndexEnabledEnum.ENABLED);

View File

@ -449,6 +449,7 @@ public abstract class RequestDetails {
DeferredOperationCallback deferredCallback = myDeferredInterceptorBroadcaster; DeferredOperationCallback deferredCallback = myDeferredInterceptorBroadcaster;
deferredCallback.playDeferredActions(); deferredCallback.playDeferredActions();
myInterceptorBroadcaster = deferredCallback.getWrap(); myInterceptorBroadcaster = deferredCallback.getWrap();
myDeferredInterceptorBroadcaster = null;
} }