[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.
This commit is contained in:
javanna 2016-05-30 17:08:20 +02:00 committed by Luca Cavanna
parent 3745305ffb
commit 51e487fa55
5 changed files with 44 additions and 68 deletions

View File

@ -39,12 +39,6 @@ dependencies {
testCompile "org.apache.lucene:lucene-test-framework:${versions.lucene}" testCompile "org.apache.lucene:lucene-test-framework:${versions.lucene}"
testCompile "org.apache.lucene:lucene-core:${versions.lucene}" testCompile "org.apache.lucene:lucene-core:${versions.lucene}"
testCompile "org.apache.lucene:lucene-codecs:${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 //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')] 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 //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 //TODO add licenses for dependencies and take care of distribution
//dependency license are currently checked in distribution //dependency license are currently checked in distribution

View File

@ -24,10 +24,10 @@ import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import com.carrotsearch.randomizedtesting.generators.RandomStrings; import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import okhttp3.mockwebserver.Dispatcher; import com.sun.net.httpserver.HttpExchange;
import okhttp3.mockwebserver.MockResponse; import com.sun.net.httpserver.HttpHandler;
import okhttp3.mockwebserver.MockWebServer; import com.sun.net.httpserver.HttpServer;
import okhttp3.mockwebserver.RecordedRequest; import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
@ -38,11 +38,10 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.UnsupportedEncodingException; import java.net.InetSocketAddress;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -52,25 +51,19 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.LogManager;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
public class HostsSnifferTests extends LuceneTestCase { public class HostsSnifferTests extends LuceneTestCase {
static {
//prevent MockWebServer from logging to stdout and stderr
LogManager.getLogManager().reset();
}
private int sniffRequestTimeout; private int sniffRequestTimeout;
private String scheme; private String scheme;
private SniffResponse sniffResponse; private SniffResponse sniffResponse;
private MockWebServer server; private HttpServer httpServer;
@Before @Before
public void startMockWebServer() throws IOException { public void startHttpServer() throws IOException {
this.sniffRequestTimeout = RandomInts.randomIntBetween(random(), 1000, 10000); this.sniffRequestTimeout = RandomInts.randomIntBetween(random(), 1000, 10000);
this.scheme = RandomPicks.randomFrom(random(), Arrays.asList("http", "https")); this.scheme = RandomPicks.randomFrom(random(), Arrays.asList("http", "https"));
if (rarely()) { if (rarely()) {
@ -78,17 +71,17 @@ public class HostsSnifferTests extends LuceneTestCase {
} else { } else {
this.sniffResponse = buildSniffResponse(scheme); this.sniffResponse = buildSniffResponse(scheme);
} }
this.server = buildMockWebServer(sniffResponse, sniffRequestTimeout); this.httpServer = createHttpServer(sniffResponse, sniffRequestTimeout);
this.server.start(); this.httpServer.start();
} }
@After @After
public void stopMockWebServer() throws IOException { public void stopHttpServer() throws IOException {
server.shutdown(); httpServer.stop(0);
} }
public void testSniffNodes() throws IOException, URISyntaxException { 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()) { try (RestClient restClient = RestClient.builder().setHosts(httpHost).build()) {
HostsSniffer sniffer = new HostsSniffer(restClient, sniffRequestTimeout, scheme); HostsSniffer sniffer = new HostsSniffer(restClient, sniffRequestTimeout, scheme);
try { try {
@ -104,7 +97,7 @@ public class HostsSnifferTests extends LuceneTestCase {
} catch(ElasticsearchResponseException e) { } catch(ElasticsearchResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); ElasticsearchResponse response = e.getElasticsearchResponse();
if (sniffResponse.isFailure) { 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)); "/_nodes/http?timeout=" + sniffRequestTimeout));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode))); assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
assertThat(response.getHost(), equalTo(httpHost)); assertThat(response.getHost(), equalTo(httpHost));
@ -118,29 +111,26 @@ public class HostsSnifferTests extends LuceneTestCase {
} }
} }
private static MockWebServer buildMockWebServer(final SniffResponse sniffResponse, final int sniffTimeout) private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeout) throws IOException {
throws UnsupportedEncodingException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
MockWebServer server = new MockWebServer(); httpServer.createContext("/_nodes/http", new HttpHandler() {
final Dispatcher dispatcher = new Dispatcher() {
@Override @Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException { public void handle(HttpExchange httpExchange) throws IOException {
if (request.getMethod().equals(HttpGet.METHOD_NAME)) { if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) {
String decodedUrl; if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeout + "ms")) {
try { String nodesInfoBody = sniffResponse.nodesInfoBody;
decodedUrl = URLDecoder.decode(request.getPath(), StandardCharsets.UTF_8.name()); httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length());
} catch (UnsupportedEncodingException e) { try (OutputStream out = httpExchange.getResponseBody()) {
throw new RuntimeException(e); out.write(nodesInfoBody.getBytes(Consts.UTF_8));
} return;
String sniffUrl = "/_nodes/http?timeout=" + sniffTimeout + "ms"; }
if (sniffUrl.equals(decodedUrl)) {
return new MockResponse().setBody(sniffResponse.nodesInfoBody).setResponseCode(sniffResponse.nodesInfoResponseCode);
} }
} }
return new MockResponse().setResponseCode(404); httpExchange.sendResponseHeaders(404, 0);
httpExchange.close();
} }
}; });
server.setDispatcher(dispatcher); return httpServer;
return server;
} }
private static SniffResponse buildSniffResponse(String scheme) throws IOException { private static SniffResponse buildSniffResponse(String scheme) throws IOException {

View File

@ -26,14 +26,9 @@ import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import java.util.Arrays; import java.util.Arrays;
import java.util.logging.LogManager;
public class SnifferBuilderTests extends LuceneTestCase { public class SnifferBuilderTests extends LuceneTestCase {
static {
LogManager.getLogManager().reset();
}
public void testBuild() throws Exception { public void testBuild() throws Exception {
try { try {

View File

@ -37,12 +37,6 @@ dependencies {
testCompile "org.apache.lucene:lucene-test-framework:${versions.lucene}" testCompile "org.apache.lucene:lucene-test-framework:${versions.lucene}"
testCompile "org.apache.lucene:lucene-core:${versions.lucene}" testCompile "org.apache.lucene:lucene-core:${versions.lucene}"
testCompile "org.apache.lucene:lucene-codecs:${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 //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')] 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 //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 //TODO add licenses for dependencies and take care of distribution
//dependency license are currently checked in distribution //dependency license are currently checked in distribution

View File

@ -30,14 +30,9 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.logging.LogManager;
public class RestClientBuilderTests extends LuceneTestCase { public class RestClientBuilderTests extends LuceneTestCase {
static {
LogManager.getLogManager().reset();
}
public void testBuild() throws IOException { public void testBuild() throws IOException {
try { try {
RestClient.builder().setMaxRetryTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); RestClient.builder().setMaxRetryTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));