Skip to content
Permalink
Browse files
CXF-8630: Remove mockwebserver dependency, use wiremock instead (#885)
  • Loading branch information
reta committed Jan 8, 2022
1 parent c5b7db5 commit d967092c6e4c7ca62c0b23a34c07141f79519161
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
@@ -171,7 +171,6 @@
<cxf.microprofile.openapi.version>2.0</cxf.microprofile.openapi.version>
<cxf.mina.version>2.1.5</cxf.mina.version>
<cxf.mockito.version>4.2.0</cxf.mockito.version>
<cxf.mockwebserver.version>4.9.3</cxf.mockwebserver.version>
<cxf.msv.version>2013.6.1</cxf.msv.version>
<cxf.neethi.version>3.2.0</cxf.neethi.version>
<cxf.netty.version.range>[4,5)</cxf.netty.version.range>
@@ -182,16 +182,22 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jcs-jcache</artifactId>
<version>${cxf.commons-jcs-jcache.version}</version>
<scope>test</scope>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jcs-jcache</artifactId>
<version>${cxf.commons-jcs-jcache.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${cxf.mockwebserver.version}</version>
<scope>test</scope>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>${cxf.wiremock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

@@ -19,36 +19,77 @@
package org.apache.cxf.microprofile.client;

import java.net.URI;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.junit.WireMockRule;

import org.apache.cxf.microprofile.client.mock.AsyncClient;
import org.apache.cxf.microprofile.client.mock.NotFoundExceptionMapper;
import org.eclipse.microprofile.rest.client.RestClientBuilder;

import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class AsyncTest {
@SuppressWarnings("unchecked")
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig()
.extensions(SimpleTransformer.class));

public static class SimpleTransformer extends ResponseTransformer {
private final Queue<String> queue = new ArrayBlockingQueue<>(2);

public SimpleTransformer() {
queue.add("Hello");
queue.add("World");
}

@Override
public Response transform(Request request, Response response, FileSource fileSource, Parameters parameters) {
return Response.Builder
.like(response)
.but().body(queue.poll())
.build();
}

@Override
public boolean applyGlobally() {
return false;
}

@Override
public String getName() {
return "enqueue-transformer";
}
}

@Test
public void testAsyncClient() throws Exception {
MockWebServer mockWebServer = new MockWebServer();
URI uri = mockWebServer.url("/").uri();
URI uri = URI.create(wireMockRule.baseUrl());
AsyncClient client = RestClientBuilder.newBuilder()
.baseUri(uri)
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build(AsyncClient.class);
assertNotNull(client);

mockWebServer.enqueue(new MockResponse().setBody("Hello"));
mockWebServer.enqueue(new MockResponse().setBody("World"));
wireMockRule.stubFor(get("/").willReturn(ok().withTransformers("enqueue-transformer")));

String combined = client.get().thenCombine(client.get(), (a, b) -> {
return a + " " + b;
@@ -59,18 +100,16 @@ public void testAsyncClient() throws Exception {

@Test
public void testAsyncClientCanMapExceptionResponses() throws Exception {
MockWebServer mockWebServer = new MockWebServer();
URI uri = mockWebServer.url("/").uri();

URI uri = URI.create(wireMockRule.baseUrl());
AsyncClient client = RestClientBuilder.newBuilder()
.baseUri(uri)
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.register(NotFoundExceptionMapper.class)
.build(AsyncClient.class);
mockWebServer.enqueue(new MockResponse().setResponseCode(404));
wireMockRule.stubFor(get("/").willReturn(notFound()));

CompletionStage cs = client.get().exceptionally(t -> {
CompletionStage<?> cs = client.get().exceptionally(t -> {
Throwable t2 = t.getCause();
return t.getClass().getSimpleName() + ":" + (t2 == null ? "null" : t2.getClass().getSimpleName());
});

0 comments on commit d967092

Please sign in to comment.