Search in sources :

Example 1 with AsyncClientWithCompletionStage

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

the class AsyncMethodTest method testInvokesPostOperationWithRegisteredProvidersAsyncCompletionStage.

@Test
public void testInvokesPostOperationWithRegisteredProvidersAsyncCompletionStage() throws Exception {
    wireMockRule.stubFor(put(urlEqualTo("/echo/test")).willReturn(aResponse().withBody("this is the replaced writer input body will be removed")));
    String inputBody = "input body will be removed";
    String expectedResponseBody = TestMessageBodyReader.REPLACED_BODY;
    AsyncClientWithCompletionStage api = RestClientBuilder.newBuilder().register(TestClientRequestFilter.class).register(TestClientResponseFilter.class).register(TestMessageBodyReader.class, 3).register(TestMessageBodyWriter.class).register(TestParamConverterProvider.class).register(TestReaderInterceptor.class).register(TestWriterInterceptor.class).baseUri(getBaseUri()).build(AsyncClientWithCompletionStage.class);
    CompletionStage<Response> cs = api.put(inputBody);
    // should need <1 second, but 20s timeout in case something goes wrong
    Response response = cs.toCompletableFuture().get(20, TimeUnit.SECONDS);
    String actualResponseBody = response.readEntity(String.class);
    assertEquals(expectedResponseBody, actualResponseBody);
    assertEquals(TestClientResponseFilter.getAndResetValue(), 1);
    assertEquals(TestClientRequestFilter.getAndResetValue(), 1);
    assertEquals(TestReaderInterceptor.getAndResetValue(), 1);
}
Also used : TestReaderInterceptor(org.eclipse.microprofile.rest.client.tck.providers.TestReaderInterceptor) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Response(javax.ws.rs.core.Response) AsyncClientWithCompletionStage(org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage) TestMessageBodyWriter(org.eclipse.microprofile.rest.client.tck.providers.TestMessageBodyWriter) JsonString(javax.json.JsonString) TestClientResponseFilter(org.eclipse.microprofile.rest.client.tck.providers.TestClientResponseFilter) Test(org.junit.Test)

Example 2 with AsyncClientWithCompletionStage

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

the class AsyncMethodTest method testInvokesGetOperationWithRegisteredProvidersAsyncCompletionStage.

@Test
public void testInvokesGetOperationWithRegisteredProvidersAsyncCompletionStage() throws Exception {
    wireMockRule.stubFor(get(urlEqualTo("/echo/test2")).willReturn(aResponse().withBody("{\"name\": \"test\"}")));
    AsyncClientWithCompletionStage api = RestClientBuilder.newBuilder().register(TestClientRequestFilter.class).register(TestClientResponseFilter.class).register(TestMessageBodyReader.class, 3).register(TestMessageBodyWriter.class).register(TestParamConverterProvider.class).register(TestReaderInterceptor.class).register(TestWriterInterceptor.class).register(JsrJsonpProvider.class).baseUri(getBaseUri()).build(AsyncClientWithCompletionStage.class);
    CompletionStage<JsonStructure> cs = api.get();
    // should need <1 second, but 20s timeout in case something goes wrong
    JsonStructure response = cs.toCompletableFuture().get(20, TimeUnit.SECONDS);
    assertThat(response, instanceOf(JsonObject.class));
    final JsonObject jsonObject = (JsonObject) response;
    assertThat(jsonObject.get("name"), instanceOf(JsonString.class));
    assertThat(((JsonString) jsonObject.get("name")).getString(), equalTo("test"));
    assertEquals(TestClientResponseFilter.getAndResetValue(), 1);
    assertEquals(TestClientRequestFilter.getAndResetValue(), 1);
    assertEquals(TestReaderInterceptor.getAndResetValue(), 1);
}
Also used : TestMessageBodyReader(org.eclipse.microprofile.rest.client.tck.providers.TestMessageBodyReader) TestWriterInterceptor(org.eclipse.microprofile.rest.client.tck.providers.TestWriterInterceptor) AsyncClientWithCompletionStage(org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage) JsonObject(javax.json.JsonObject) TestClientRequestFilter(org.eclipse.microprofile.rest.client.tck.providers.TestClientRequestFilter) TestParamConverterProvider(org.eclipse.microprofile.rest.client.tck.providers.TestParamConverterProvider) JsonString(javax.json.JsonString) JsonStructure(javax.json.JsonStructure) Test(org.junit.Test)

Example 3 with AsyncClientWithCompletionStage

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

the class AsyncMethodTest method testInvokesGetAllOperationWithRegisteredProvidersAsyncCompletionStage.

@Test
public void testInvokesGetAllOperationWithRegisteredProvidersAsyncCompletionStage() throws Exception {
    wireMockRule.stubFor(get(urlEqualTo("/echo/test3")).willReturn(aResponse().withBody("[{\"name\": \"test\"}]")));
    AsyncClientWithCompletionStage api = RestClientBuilder.newBuilder().register(TestClientRequestFilter.class).register(TestClientResponseFilter.class).register(TestMessageBodyReader.class, 3).register(TestMessageBodyWriter.class).register(TestParamConverterProvider.class).register(TestReaderInterceptor.class).register(TestWriterInterceptor.class).register(JsrJsonpProvider.class).baseUri(getBaseUri()).build(AsyncClientWithCompletionStage.class);
    CompletionStage<Collection<JsonObject>> cs = api.getAll();
    // should need <1 second, but 20s timeout in case something goes wrong
    Collection<JsonObject> response = cs.toCompletableFuture().get(20, TimeUnit.SECONDS);
    assertEquals(1, response.size());
    final JsonObject jsonObject = response.iterator().next();
    assertThat(jsonObject.get("name"), instanceOf(JsonString.class));
    assertThat(((JsonString) jsonObject.get("name")).getString(), equalTo("test"));
    assertEquals(TestClientResponseFilter.getAndResetValue(), 1);
    assertEquals(TestClientRequestFilter.getAndResetValue(), 1);
    assertEquals(TestReaderInterceptor.getAndResetValue(), 1);
}
Also used : TestMessageBodyReader(org.eclipse.microprofile.rest.client.tck.providers.TestMessageBodyReader) TestWriterInterceptor(org.eclipse.microprofile.rest.client.tck.providers.TestWriterInterceptor) AsyncClientWithCompletionStage(org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage) Collection(java.util.Collection) JsonObject(javax.json.JsonObject) TestClientRequestFilter(org.eclipse.microprofile.rest.client.tck.providers.TestClientRequestFilter) TestParamConverterProvider(org.eclipse.microprofile.rest.client.tck.providers.TestParamConverterProvider) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 4 with AsyncClientWithCompletionStage

use of org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage 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

JsonString (javax.json.JsonString)4 AsyncClientWithCompletionStage (org.apache.cxf.systest.microprofile.rest.client.mock.AsyncClientWithCompletionStage)4 Test (org.junit.Test)4 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)2 JsonObject (javax.json.JsonObject)2 Response (javax.ws.rs.core.Response)2 TestClientRequestFilter (org.eclipse.microprofile.rest.client.tck.providers.TestClientRequestFilter)2 TestMessageBodyReader (org.eclipse.microprofile.rest.client.tck.providers.TestMessageBodyReader)2 TestParamConverterProvider (org.eclipse.microprofile.rest.client.tck.providers.TestParamConverterProvider)2 TestWriterInterceptor (org.eclipse.microprofile.rest.client.tck.providers.TestWriterInterceptor)2 Collection (java.util.Collection)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 JsonStructure (javax.json.JsonStructure)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 TestClientResponseFilter (org.eclipse.microprofile.rest.client.tck.providers.TestClientResponseFilter)1 TestMessageBodyWriter (org.eclipse.microprofile.rest.client.tck.providers.TestMessageBodyWriter)1 TestReaderInterceptor (org.eclipse.microprofile.rest.client.tck.providers.TestReaderInterceptor)1