From 51e487fa555b03d507c985bb4eb3d7501f51da98 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 30 May 2016 17:08:20 +0200 Subject: [PATCH] [TEST] remove okhttp test dependency Use sun HttpServer instead and disable forbidden-apis for test classes. It turns out to be more flexible than okhttp as it allows get & delete with body. --- client-sniffer/build.gradle | 16 ++--- .../client/sniff/HostsSnifferTests.java | 70 ++++++++----------- .../client/sniff/SnifferBuilderTests.java | 5 -- client/build.gradle | 16 ++--- .../client/RestClientBuilderTests.java | 5 -- 5 files changed, 44 insertions(+), 68 deletions(-) diff --git a/client-sniffer/build.gradle b/client-sniffer/build.gradle index d446ffc0113..40551556731 100644 --- a/client-sniffer/build.gradle +++ b/client-sniffer/build.gradle @@ -39,12 +39,6 @@ dependencies { testCompile "org.apache.lucene:lucene-test-framework:${versions.lucene}" testCompile "org.apache.lucene:lucene-core:${versions.lucene}" testCompile "org.apache.lucene:lucene-codecs:${versions.lucene}" - //mock web server - testCompile "com.squareup.okhttp3:mockwebserver:3.2.0" - testCompile "com.squareup.okhttp3:okhttp:3.2.0" - testCompile "com.squareup.okhttp3:okhttp-ws:3.2.0" - testCompile "com.squareup.okio:okio:1.6.0" - testCompile "org.bouncycastle:bcprov-jdk15on:1.54" } //TODO compiling from 1.8 with target 1.7 and source 1.7 is best effort, not enough to ensure we are java 7 compatible @@ -56,10 +50,14 @@ forbiddenApisMain { signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] } -forbiddenApisTest { +//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes +//excludes don't seem to work though and we don't want to have our own @SuppressForbidden +forbiddenApisTest.enabled=false + +//forbiddenApisTest { //client does not depend on core, so only jdk signatures should be checked - signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] -} + //signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] +//} //TODO add licenses for dependencies and take care of distribution //dependency license are currently checked in distribution diff --git a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java index 9d9c054fd06..88f0480f92a 100644 --- a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java +++ b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java @@ -24,10 +24,10 @@ import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomStrings; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; +import org.apache.http.Consts; import org.apache.http.HttpHost; import org.apache.http.client.methods.HttpGet; import org.apache.lucene.util.LuceneTestCase; @@ -38,11 +38,10 @@ import org.junit.After; import org.junit.Before; import java.io.IOException; +import java.io.OutputStream; import java.io.StringWriter; -import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; import java.net.URISyntaxException; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -52,25 +51,19 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.logging.LogManager; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; public class HostsSnifferTests extends LuceneTestCase { - static { - //prevent MockWebServer from logging to stdout and stderr - LogManager.getLogManager().reset(); - } - private int sniffRequestTimeout; private String scheme; private SniffResponse sniffResponse; - private MockWebServer server; + private HttpServer httpServer; @Before - public void startMockWebServer() throws IOException { + public void startHttpServer() throws IOException { this.sniffRequestTimeout = RandomInts.randomIntBetween(random(), 1000, 10000); this.scheme = RandomPicks.randomFrom(random(), Arrays.asList("http", "https")); if (rarely()) { @@ -78,17 +71,17 @@ public class HostsSnifferTests extends LuceneTestCase { } else { this.sniffResponse = buildSniffResponse(scheme); } - this.server = buildMockWebServer(sniffResponse, sniffRequestTimeout); - this.server.start(); + this.httpServer = createHttpServer(sniffResponse, sniffRequestTimeout); + this.httpServer.start(); } @After - public void stopMockWebServer() throws IOException { - server.shutdown(); + public void stopHttpServer() throws IOException { + httpServer.stop(0); } public void testSniffNodes() throws IOException, URISyntaxException { - HttpHost httpHost = new HttpHost(server.getHostName(), server.getPort()); + HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostName(), httpServer.getAddress().getPort()); try (RestClient restClient = RestClient.builder().setHosts(httpHost).build()) { HostsSniffer sniffer = new HostsSniffer(restClient, sniffRequestTimeout, scheme); try { @@ -104,7 +97,7 @@ public class HostsSnifferTests extends LuceneTestCase { } catch(ElasticsearchResponseException e) { ElasticsearchResponse response = e.getElasticsearchResponse(); if (sniffResponse.isFailure) { - assertThat(e.getMessage(), containsString("GET http://localhost:" + server.getPort() + + assertThat(e.getMessage(), containsString("GET http://localhost:" + httpServer.getAddress().getPort() + "/_nodes/http?timeout=" + sniffRequestTimeout)); assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode))); assertThat(response.getHost(), equalTo(httpHost)); @@ -118,29 +111,26 @@ public class HostsSnifferTests extends LuceneTestCase { } } - private static MockWebServer buildMockWebServer(final SniffResponse sniffResponse, final int sniffTimeout) - throws UnsupportedEncodingException { - MockWebServer server = new MockWebServer(); - final Dispatcher dispatcher = new Dispatcher() { + private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeout) throws IOException { + HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); + httpServer.createContext("/_nodes/http", new HttpHandler() { @Override - public MockResponse dispatch(RecordedRequest request) throws InterruptedException { - if (request.getMethod().equals(HttpGet.METHOD_NAME)) { - String decodedUrl; - try { - decodedUrl = URLDecoder.decode(request.getPath(), StandardCharsets.UTF_8.name()); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - String sniffUrl = "/_nodes/http?timeout=" + sniffTimeout + "ms"; - if (sniffUrl.equals(decodedUrl)) { - return new MockResponse().setBody(sniffResponse.nodesInfoBody).setResponseCode(sniffResponse.nodesInfoResponseCode); + public void handle(HttpExchange httpExchange) throws IOException { + if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) { + if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeout + "ms")) { + String nodesInfoBody = sniffResponse.nodesInfoBody; + httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length()); + try (OutputStream out = httpExchange.getResponseBody()) { + out.write(nodesInfoBody.getBytes(Consts.UTF_8)); + return; + } } } - return new MockResponse().setResponseCode(404); + httpExchange.sendResponseHeaders(404, 0); + httpExchange.close(); } - }; - server.setDispatcher(dispatcher); - return server; + }); + return httpServer; } private static SniffResponse buildSniffResponse(String scheme) throws IOException { diff --git a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java index 868c5788120..ff3cad34bfa 100644 --- a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java +++ b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java @@ -26,14 +26,9 @@ import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.client.RestClient; import java.util.Arrays; -import java.util.logging.LogManager; public class SnifferBuilderTests extends LuceneTestCase { - static { - LogManager.getLogManager().reset(); - } - public void testBuild() throws Exception { try { diff --git a/client/build.gradle b/client/build.gradle index 8cb0018e991..43979e84597 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -37,12 +37,6 @@ dependencies { testCompile "org.apache.lucene:lucene-test-framework:${versions.lucene}" testCompile "org.apache.lucene:lucene-core:${versions.lucene}" testCompile "org.apache.lucene:lucene-codecs:${versions.lucene}" - //mock web server - testCompile "com.squareup.okhttp3:mockwebserver:3.2.0" - testCompile "com.squareup.okhttp3:okhttp:3.2.0" - testCompile "com.squareup.okhttp3:okhttp-ws:3.2.0" - testCompile "com.squareup.okio:okio:1.6.0" - testCompile "org.bouncycastle:bcprov-jdk15on:1.54" } //TODO compiling from 1.8 with target 1.7 and source 1.7 is best effort, not enough to ensure we are java 7 compatible @@ -54,10 +48,14 @@ forbiddenApisMain { signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] } -forbiddenApisTest { +//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes +//excludes don't seem to work though and we don't want to have our own @SuppressForbidden +forbiddenApisTest.enabled=false + +//forbiddenApisTest { //client does not depend on core, so only jdk signatures should be checked - signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] -} + //signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] +//} //TODO add licenses for dependencies and take care of distribution //dependency license are currently checked in distribution diff --git a/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java b/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java index 44909336dd6..1d73db6ec37 100644 --- a/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java +++ b/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java @@ -30,14 +30,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.logging.LogManager; public class RestClientBuilderTests extends LuceneTestCase { - static { - LogManager.getLogManager().reset(); - } - public void testBuild() throws IOException { try { RestClient.builder().setMaxRetryTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));