Search in sources :

Example 1 with AsyncInvocationInterceptorFactoryTestImpl2

use of org.apache.cxf.systest.microprofile.rest.client.mock.AsyncInvocationInterceptorFactoryTestImpl2 in project cxf by apache.

the class AsyncMethodTest method testInvokesPostOperationWithRegisteredProvidersAsyncCompletionStageWithExecutor.

@Test
public void testInvokesPostOperationWithRegisteredProvidersAsyncCompletionStageWithExecutor() throws Exception {
    final String inputBody = "input body will be ignored";
    wireMockRule.stubFor(put(urlEqualTo("/echo/test")).willReturn(aResponse().withBody(inputBody)));
    AsyncInvocationInterceptorFactoryTestImpl.INBOUND.remove();
    AsyncInvocationInterceptorFactoryTestImpl.OUTBOUND.remove();
    try {
        final String asyncThreadName = "CXF-MPRestClientThread-2";
        AsyncClientWithCompletionStage api = RestClientBuilder.newBuilder().register(AsyncInvocationInterceptorFactoryTestImpl.class).register(AsyncInvocationInterceptorFactoryTestImpl2.class).register(ThreadLocalClientFilter.class).baseUri(getBaseUri()).executorService(Executors.newSingleThreadExecutor(new ThreadFactory() {

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, asyncThreadName);
            }
        })).build(AsyncClientWithCompletionStage.class);
        CompletionStage<Response> cs = api.put(inputBody);
        List<String> outboundList = AsyncInvocationInterceptorFactoryTestImpl.OUTBOUND.get();
        assertEquals(4, outboundList.size());
        // ensure filters and asyncInvocationInterceptors are executed in the correct order and the correct thread
        // outbound:
        assertEquals(ThreadLocalClientFilter.class.getSimpleName(), outboundList.get(0));
        assertEquals(AsyncInvocationInterceptorFactoryTestImpl.class.getSimpleName(), outboundList.get(1));
        assertEquals(AsyncInvocationInterceptorFactoryTestImpl2.class.getSimpleName(), outboundList.get(2));
        assertEquals(Thread.currentThread().getName(), outboundList.get(3));
        // inbound:
        // should need <1 second, but 20s timeout in case something goes wrong
        Response response = cs.toCompletableFuture().get(20, TimeUnit.SECONDS);
        List<String> responseList = response.getStringHeaders().get("CXFTestResponse");
        assertEquals(4, responseList.size());
        assertEquals(asyncThreadName, responseList.get(0));
        assertEquals(AsyncInvocationInterceptorFactoryTestImpl2.class.getSimpleName(), responseList.get(1));
        assertEquals(AsyncInvocationInterceptorFactoryTestImpl.class.getSimpleName(), responseList.get(2));
        assertEquals(ThreadLocalClientFilter.class.getSimpleName(), responseList.get(3));
    } finally {
        AsyncInvocationInterceptorFactoryTestImpl.INBOUND.remove();
        AsyncInvocationInterceptorFactoryTestImpl.OUTBOUND.remove();
    }
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Response(javax.ws.rs.core.Response) ThreadFactory(java.util.concurrent.ThreadFactory) AsyncClientWithCompletionStage(org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage) AsyncInvocationInterceptorFactoryTestImpl(org.apache.cxf.systest.microprofile.rest.client.mock.AsyncInvocationInterceptorFactoryTestImpl) ThreadLocalClientFilter(org.apache.cxf.systest.microprofile.rest.client.mock.ThreadLocalClientFilter) JsonString(javax.json.JsonString) AsyncInvocationInterceptorFactoryTestImpl2(org.apache.cxf.systest.microprofile.rest.client.mock.AsyncInvocationInterceptorFactoryTestImpl2) Test(org.junit.Test)

Aggregations

WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 JsonString (javax.json.JsonString)1 Response (javax.ws.rs.core.Response)1 AsyncClientWithCompletionStage (org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage)1 AsyncInvocationInterceptorFactoryTestImpl (org.apache.cxf.systest.microprofile.rest.client.mock.AsyncInvocationInterceptorFactoryTestImpl)1 AsyncInvocationInterceptorFactoryTestImpl2 (org.apache.cxf.systest.microprofile.rest.client.mock.AsyncInvocationInterceptorFactoryTestImpl2)1 ThreadLocalClientFilter (org.apache.cxf.systest.microprofile.rest.client.mock.ThreadLocalClientFilter)1 Test (org.junit.Test)1