You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2022/01/08 15:23:26 UTC

[cxf] branch master updated: CXF-8630: Remove mockwebserver dependency, use wiremock instead (#885)

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new d967092  CXF-8630: Remove mockwebserver dependency, use wiremock instead (#885)
d967092 is described below

commit d967092c6e4c7ca62c0b23a34c07141f79519161
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Sat Jan 8 10:23:05 2022 -0500

    CXF-8630: Remove mockwebserver dependency, use wiremock instead (#885)
---
 parent/pom.xml                                     |  1 -
 rt/rs/microprofile-client/pom.xml                  | 22 +++++---
 .../apache/cxf/microprofile/client/AsyncTest.java  | 61 ++++++++++++++++++----
 3 files changed, 64 insertions(+), 20 deletions(-)

diff --git a/parent/pom.xml b/parent/pom.xml
index 479a955..3f64499 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -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>
diff --git a/rt/rs/microprofile-client/pom.xml b/rt/rs/microprofile-client/pom.xml
index d62692a..0cafeb5 100644
--- a/rt/rs/microprofile-client/pom.xml
+++ b/rt/rs/microprofile-client/pom.xml
@@ -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>
 
diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java
index fc4905c..64dfa2f 100644
--- a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java
+++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java
@@ -19,27 +19,69 @@
 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)
@@ -47,8 +89,7 @@ public class AsyncTest {
                                               .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 class AsyncTest {
 
     @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());
         });