From 17de77fd0f11f525e0747cbba18e866888bb298e Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Fri, 20 Mar 2020 16:47:21 +0000 Subject: [PATCH] Convert Wildfly tests to run in Docker (#53159) Backport of #53126. Convert the Wildfly tests to run using Docker Compose. This drastically simplifies the build setup, and will hopefully make the tests more resilient. --- build.gradle | 1 + qa/wildfly/build.gradle | 206 +++--------------- qa/wildfly/docker-compose-oss.yml | 35 +++ qa/wildfly/docker-compose.yml | 35 +++ ...java => RestHighLevelClientActivator.java} | 4 +- ... RestHighLevelClientEmployeeResource.java} | 22 +- .../RestHighLevelClientProducer.java | 45 ++++ ... => RestHighLevelJacksonJsonProvider.java} | 3 +- .../transport/TransportClientProducer.java | 69 ------ .../org/elasticsearch/wildfly/WildflyIT.java | 91 ++++---- 10 files changed, 212 insertions(+), 299 deletions(-) create mode 100644 qa/wildfly/docker-compose-oss.yml create mode 100644 qa/wildfly/docker-compose.yml rename qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/{TransportClientActivator.java => RestHighLevelClientActivator.java} (87%) rename qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/{TransportClientEmployeeResource.java => RestHighLevelClientEmployeeResource.java} (81%) create mode 100644 qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientProducer.java rename qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/{TransportJacksonJsonProvider.java => RestHighLevelJacksonJsonProvider.java} (92%) delete mode 100644 qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientProducer.java diff --git a/build.gradle b/build.gradle index a8e31ce66af..fe77ca077a7 100644 --- a/build.gradle +++ b/build.gradle @@ -116,6 +116,7 @@ subprojects { ':distribution:tools:launchers', ':distribution:tools:plugin-cli', ':qa:os', + ':qa:wildfly', ':x-pack:plugin:autoscaling', ':x-pack:plugin:enrich', ':x-pack:plugin:logstash' diff --git a/qa/wildfly/build.gradle b/qa/wildfly/build.gradle index 594fd8cc1e9..3a7156aa094 100644 --- a/qa/wildfly/build.gradle +++ b/qa/wildfly/build.gradle @@ -1,12 +1,3 @@ -import org.elasticsearch.gradle.LoggedExec -import org.elasticsearch.gradle.VersionProperties -import org.apache.tools.ant.taskdefs.condition.Os -import org.elasticsearch.gradle.testclusters.DefaultTestClustersTask - -import java.nio.charset.StandardCharsets -import java.nio.file.Files -import java.util.stream.Stream - /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with @@ -26,33 +17,14 @@ import java.util.stream.Stream * under the License. */ +import org.elasticsearch.gradle.VersionProperties + apply plugin: 'war' -apply plugin: 'elasticsearch.testclusters' apply plugin: 'elasticsearch.build' -apply plugin: 'elasticsearch.rest-test' +apply plugin: 'elasticsearch.test.fixtures' +apply plugin: 'elasticsearch.distribution-download' -final String wildflyVersion = '11.0.0.Final' -final String wildflyDir = "${buildDir}/wildfly" -final String wildflyInstall = "${buildDir}/wildfly/wildfly-${wildflyVersion}" -int managementPort - -repositories { - // the Wildfly distribution is not available via a repository, so we fake an Ivy repository on top of the download site - ivy { - name "wildfly" - url "https://download.jboss.org" - metadataSources { - artifact() - } - patternLayout { - artifact 'wildfly/[revision]/[module]-[revision].[ext]' - } - } -} - -configurations { - wildfly -} +testFixtures.useFixture() dependencies { providedCompile 'javax.enterprise:cdi-api:1.2' @@ -72,164 +44,42 @@ dependencies { compile "com.fasterxml.jackson.module:jackson-module-jaxb-annotations:${versions.jackson}" compile "org.apache.logging.log4j:log4j-api:${versions.log4j}" compile "org.apache.logging.log4j:log4j-core:${versions.log4j}" - compile project(path: ':client:transport', configuration: 'runtime') - wildfly "org.jboss:wildfly:${wildflyVersion}@zip" + compile project(path: ':client:rest-high-level') testCompile project(':test:framework') } -task unzipWildfly(type: Sync) { - into wildflyDir - from { zipTree(configurations.wildfly.singleFile) } +war { + archiveName 'example-app.war' } -task deploy(type: Copy) { - dependsOn unzipWildfly, war - from war - into "${wildflyInstall}/standalone/deployments" -} - -task writeElasticsearchProperties(type: DefaultTestClustersTask) { - onlyIf { !Os.isFamily(Os.FAMILY_WINDOWS) } - useCluster testClusters.integTest - dependsOn deploy - doLast { - final File elasticsearchProperties = file("${wildflyInstall}/standalone/configuration/elasticsearch.properties") - elasticsearchProperties.write( - [ - "transport.uri=${-> testClusters.integTest.getAllTransportPortURI().get(0)}", - "cluster.name=integTest" - ].join("\n")) +elasticsearch_distributions { + docker { + type = 'docker' + flavor = System.getProperty('tests.distribution', 'default') + version = VersionProperties.getElasticsearch() + failIfUnavailable = false // This ensures we skip this testing if Docker is unavailable } } -// the default configuration ships with IPv6 disabled but our cluster could be bound to IPv6 if the host supports it -task enableIPv6 { - dependsOn unzipWildfly - doLast { - final File standaloneConf = file("${wildflyInstall}/bin/standalone.conf") - final List lines = - Files.readAllLines(standaloneConf.toPath()) - .collect { line -> line.replace("-Djava.net.preferIPv4Stack=true", "-Djava.net.preferIPv4Stack=false") } - standaloneConf.write(lines.join("\n")) +preProcessFixture { + dependsOn war, elasticsearch_distributions.docker +} + +dockerCompose { + if ('default'.equalsIgnoreCase(System.getProperty('tests.distribution', 'default'))) { + useComposeFiles = ['docker-compose.yml'] + } else { + useComposeFiles = ['docker-compose-oss.yml'] } } -task startWildfly { - dependsOn enableIPv6, writeElasticsearchProperties - doLast { - // we skip these tests on Windows so we do no need to worry about compatibility here - final ProcessBuilder wildfly = new ProcessBuilder( - "${wildflyInstall}/bin/standalone.sh", - "-Djboss.http.port=0", - "-Djboss.https.port=0", - "-Djboss.management.http.port=0") - final Process process = wildfly.start() - new BufferedReader(new InputStreamReader(process.getInputStream())).withReader { br -> - String line - int httpPort = 0 - while ((line = br.readLine()) != null) { - logger.info(line) - if (line.matches('.*Undertow HTTP listener default listening on .*:\\d+$')) { - assert httpPort == 0 - final int index = line.lastIndexOf(":") - assert index >= 0 - httpPort = Integer.parseInt(line.substring(index + 1)) - // set this system property so the test runner knows the port Wildfly is listening for HTTP requests on - integTestRunner.systemProperty("tests.jboss.root", "http://localhost:$httpPort/wildfly-$version/transport") - } else if (line.matches('.*Http management interface listening on http://.*:\\d+/management$')) { - assert managementPort == 0 - final int colonIndex = line.lastIndexOf(":") - assert colonIndex >= 0 - final int slashIndex = line.lastIndexOf("/") - assert slashIndex >= 0 - managementPort = Integer.parseInt(line.substring(colonIndex + 1, slashIndex)) - - /* - * As soon as we know the management port, we fork a process that will ensure the Wildfly process is killed if we - * teardown abnormally. We skip these tests on Windows so we do not need to worry about CLI compatibility here. - */ - final File script = new File(project.buildDir, "wildfly/wildfly.killer.sh") - script.setText( - ["function shutdown {", - " ${wildflyInstall}/bin/jboss-cli.sh --controller=localhost:${-> managementPort} --connect command=shutdown", - "}", - "trap shutdown EXIT", - // will wait indefinitely for input, but we never pass input, and the pipe is only closed when the build dies - "read line\n"].join('\n'), 'UTF-8') - final ProcessBuilder killer = new ProcessBuilder("bash", script.absolutePath) - killer.start() - - } else if (line.matches(".*WildFly Full \\d+\\.\\d+\\.\\d+\\.Final \\(WildFly Core \\d+\\.\\d+\\.\\d+\\.Final\\) started.*")) { - break - } - } - - if (httpPort == 0 || managementPort == 0) { - String portType = httpPort == 0 ? "http" : "management" - throw new GradleException("Failed to find ${portType} port in wildfly log") - } - } - } +task integTest(type: Test) { + outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true } + maxParallelForks = '1' + include '**/*IT.class' } -task configureTransportClient(type: LoggedExec) { - dependsOn startWildfly - // we skip these tests on Windows so we do not need to worry about compatibility here - commandLine "${wildflyInstall}/bin/jboss-cli.sh", - "--controller=localhost:${-> managementPort}", - "--connect", - "--command=/system-property=elasticsearch.properties:add(value=\${jboss.server.config.dir}/elasticsearch.properties)" -} - -task stopWildfly(type: LoggedExec) { - // we skip these tests on Windows so we do not need to worry about CLI compatibility here - commandLine "${wildflyInstall}/bin/jboss-cli.sh", "--controller=localhost:${-> managementPort}", "--connect", "command=shutdown" -} - -if (!Os.isFamily(Os.FAMILY_WINDOWS)) { - integTestRunner.dependsOn(configureTransportClient) - final TaskExecutionAdapter logDumpListener = new TaskExecutionAdapter() { - @Override - void afterExecute(final Task task, final TaskState state) { - if (task != startWildfly && task != integTestRunner) { - // we might have been called from a parallel, unrelated task - return - } - if (state.failure != null) { - final File logFile = new File(wildflyInstall, "standalone/log/server.log") - println("\nWildfly server log (from ${logFile}):") - println('-----------------------------------------') - final Stream stream = Files.lines(logFile.toPath(), StandardCharsets.UTF_8) - try { - for (String line : stream) { - println(line) - } - } finally { - stream.close() - } - println('=========================================') - } - } - } - startWildfly.doFirst { - project.gradle.addListener(logDumpListener) - } - integTestRunner.doFirst { - project.gradle.addListener(logDumpListener) - } - integTestRunner.doLast { - project.gradle.removeListener(logDumpListener) - } - startWildfly.doLast { - project.gradle.removeListener(logDumpListener) - } - integTestRunner.finalizedBy(stopWildfly) -} else { - integTest.enabled = false - testingConventions.enabled = false -} - -check.dependsOn(integTest) +check.dependsOn integTest test.enabled = false diff --git a/qa/wildfly/docker-compose-oss.yml b/qa/wildfly/docker-compose-oss.yml new file mode 100644 index 00000000000..e328240a367 --- /dev/null +++ b/qa/wildfly/docker-compose-oss.yml @@ -0,0 +1,35 @@ +version: '3.7' +services: + + wildfly: + image: jboss/wildfly:18.0.1.Final + environment: + JAVA_OPTS: -Delasticsearch.uri=elasticsearch:9200 -Djboss.http.port=8080 -Djava.net.preferIPv4Stack=true + volumes: + - ./build/distributions/example-app.war:/opt/jboss/wildfly/standalone/deployments/example-app.war + ports: + - "8080" + healthcheck: + start_period: 5s + test: ["CMD", "grep", "Admin console listening on", "/opt/jboss/wildfly/standalone/log/server.log"] + interval: 2s + timeout: 1s + retries: 5 + + elasticsearch: + image: elasticsearch-oss:test + environment: + discovery.type: single-node + ulimits: + memlock: + soft: -1 + hard: -1 + nofile: + soft: 65536 + hard: 65536 + healthcheck: + start_period: 15s + test: ["CMD", "curl", "-f", "-k", "http://localhost:9200"] + interval: 10s + timeout: 2s + retries: 5 diff --git a/qa/wildfly/docker-compose.yml b/qa/wildfly/docker-compose.yml new file mode 100644 index 00000000000..005bb5f0c7b --- /dev/null +++ b/qa/wildfly/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3.7' +services: + + wildfly: + image: jboss/wildfly:18.0.1.Final + environment: + JAVA_OPTS: -Delasticsearch.uri=elasticsearch:9200 -Djboss.http.port=8080 -Djava.net.preferIPv4Stack=true + volumes: + - ./build/distributions/example-app.war:/opt/jboss/wildfly/standalone/deployments/example-app.war + ports: + - "8080" + healthcheck: + start_period: 5s + test: ["CMD", "grep", "Admin console listening on", "/opt/jboss/wildfly/standalone/log/server.log"] + interval: 2s + timeout: 1s + retries: 5 + + elasticsearch: + image: elasticsearch:test + environment: + discovery.type: single-node + ulimits: + memlock: + soft: -1 + hard: -1 + nofile: + soft: 65536 + hard: 65536 + healthcheck: + start_period: 15s + test: ["CMD", "curl", "-f", "-k", "http://localhost:9200"] + interval: 10s + timeout: 2s + retries: 5 diff --git a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientActivator.java b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientActivator.java similarity index 87% rename from qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientActivator.java rename to qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientActivator.java index 881b263f35b..c860f9e5e1b 100644 --- a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientActivator.java +++ b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientActivator.java @@ -26,11 +26,11 @@ import java.util.Collections; import java.util.Set; @ApplicationPath("/transport") -public class TransportClientActivator extends Application { +public class RestHighLevelClientActivator extends Application { @Override public Set> getClasses() { - return Collections.singleton(TransportClientEmployeeResource.class); + return Collections.singleton(RestHighLevelClientEmployeeResource.class); } } diff --git a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientEmployeeResource.java b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientEmployeeResource.java similarity index 81% rename from qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientEmployeeResource.java rename to qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientEmployeeResource.java index 4008bf8801a..b90042a10e8 100644 --- a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientEmployeeResource.java +++ b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientEmployeeResource.java @@ -19,9 +19,12 @@ package org.elasticsearch.wildfly.transport; +import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.client.transport.TransportClient; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.wildfly.model.Employee; @@ -33,7 +36,6 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; - import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -44,17 +46,17 @@ import java.util.Objects; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @Path("/employees") -public class TransportClientEmployeeResource { +public class RestHighLevelClientEmployeeResource { @Inject - private TransportClient client; + private RestHighLevelClient client; @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) - public Response getEmployeeById(final @PathParam("id") Long id) { + public Response getEmployeeById(final @PathParam("id") Long id) throws IOException { Objects.requireNonNull(id); - final GetResponse response = client.prepareGet("megacorp", "employee", Long.toString(id)).get(); + final GetResponse response = client.get(new GetRequest("megacorp", Long.toString(id)), RequestOptions.DEFAULT); if (response.isExists()) { final Map source = response.getSource(); final Employee employee = new Employee(); @@ -62,7 +64,8 @@ public class TransportClientEmployeeResource { employee.setLastName((String) source.get("last_name")); employee.setAge((Integer) source.get("age")); employee.setAbout((String) source.get("about")); - @SuppressWarnings("unchecked") final List interests = (List) source.get("interests"); + @SuppressWarnings("unchecked") + final List interests = (List) source.get("interests"); employee.setInterests(interests); return Response.ok(employee).build(); } else { @@ -94,7 +97,10 @@ public class TransportClientEmployeeResource { } } builder.endObject(); - final IndexResponse response = client.prepareIndex("megacorp", "employee", Long.toString(id)).setSource(builder).get(); + final IndexRequest request = new IndexRequest("megacorp"); + request.id(Long.toString(id)); + request.source(builder); + final IndexResponse response = client.index(request, RequestOptions.DEFAULT); if (response.status().getStatus() == 201) { return Response.created(new URI("/employees/" + id)).build(); } else { diff --git a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientProducer.java b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientProducer.java new file mode 100644 index 00000000000..66a28ba91c2 --- /dev/null +++ b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelClientProducer.java @@ -0,0 +1,45 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.wildfly.transport; + +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.SuppressForbidden; +import org.elasticsearch.common.io.PathUtils; + +import javax.enterprise.inject.Produces; +import java.nio.file.Path; + +@SuppressWarnings("unused") +public final class RestHighLevelClientProducer { + + @Produces + public RestHighLevelClient createRestHighLevelClient() { + String httpUri = System.getProperty("elasticsearch.uri"); + + return new RestHighLevelClient(RestClient.builder(HttpHost.create(httpUri))); + } + + @SuppressForbidden(reason = "get path not configured in environment") + private Path getPath(final String elasticsearchProperties) { + return PathUtils.get(elasticsearchProperties); + } +} diff --git a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportJacksonJsonProvider.java b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelJacksonJsonProvider.java similarity index 92% rename from qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportJacksonJsonProvider.java rename to qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelJacksonJsonProvider.java index 07585780c06..c75ed443760 100644 --- a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportJacksonJsonProvider.java +++ b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/RestHighLevelJacksonJsonProvider.java @@ -24,5 +24,6 @@ import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider; import javax.ws.rs.ext.Provider; @Provider -public class TransportJacksonJsonProvider extends ResteasyJackson2Provider { +public class RestHighLevelJacksonJsonProvider extends ResteasyJackson2Provider { + } diff --git a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientProducer.java b/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientProducer.java deleted file mode 100644 index 7c234bce6cd..00000000000 --- a/qa/wildfly/src/main/java/org/elasticsearch/wildfly/transport/TransportClientProducer.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.wildfly.transport; - -import org.elasticsearch.client.transport.TransportClient; -import org.elasticsearch.common.SuppressForbidden; -import org.elasticsearch.common.io.PathUtils; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.transport.client.PreBuiltTransportClient; - -import javax.enterprise.inject.Produces; - -import java.io.IOException; -import java.io.InputStream; -import java.net.InetAddress; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Collections; -import java.util.Properties; - -@SuppressWarnings("unused") -public final class TransportClientProducer { - - @Produces - public TransportClient createTransportClient() throws IOException { - final String elasticsearchProperties = System.getProperty("elasticsearch.properties"); - final Properties properties = new Properties(); - - final String transportUri; - final String clusterName; - try (InputStream is = Files.newInputStream(getPath(elasticsearchProperties))) { - properties.load(is); - transportUri = properties.getProperty("transport.uri"); - clusterName = properties.getProperty("cluster.name"); - } - - final int lastColon = transportUri.lastIndexOf(':'); - final String host = transportUri.substring(0, lastColon); - final int port = Integer.parseInt(transportUri.substring(lastColon + 1)); - final Settings settings = Settings.builder().put("cluster.name", clusterName).build(); - final TransportClient transportClient = new PreBuiltTransportClient(settings, Collections.emptyList()); - transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port)); - return transportClient; - } - - @SuppressForbidden(reason = "get path not configured in environment") - private Path getPath(final String elasticsearchProperties) { - return PathUtils.get(elasticsearchProperties); - } - -} diff --git a/qa/wildfly/src/test/java/org/elasticsearch/wildfly/WildflyIT.java b/qa/wildfly/src/test/java/org/elasticsearch/wildfly/WildflyIT.java index 995501d78f2..5b82e75be01 100644 --- a/qa/wildfly/src/test/java/org/elasticsearch/wildfly/WildflyIT.java +++ b/qa/wildfly/src/test/java/org/elasticsearch/wildfly/WildflyIT.java @@ -32,10 +32,8 @@ import org.apache.logging.log4j.Logger; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestRuleLimitSysouts; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -43,10 +41,8 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.List; -import java.util.Locale; import java.util.Map; -import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -54,50 +50,62 @@ import static org.hamcrest.Matchers.instanceOf; @TestRuleLimitSysouts.Limit(bytes = 14000) public class WildflyIT extends LuceneTestCase { - Logger logger = LogManager.getLogger(WildflyIT.class); + private Logger logger = LogManager.getLogger(WildflyIT.class); - public void testTransportClient() throws URISyntaxException, IOException { - try (CloseableHttpClient client = HttpClientBuilder.create().build()) { - final String str = String.format( - Locale.ROOT, - "%s/employees/1", - System.getProperty("tests.jboss.root") + private String buildBaseUrl() { + final String propertyName = "test.fixtures.wildfly.tcp.8080"; + final String port = System.getProperty(propertyName); + if (port == null) { + throw new IllegalStateException( + "Could not find system property " + + propertyName + + ". This test expects to run with the elasticsearch.test.fixtures Gradle plugin" ); - logger.info("Connecting to uri: " + str); - final HttpPut put = new HttpPut(new URI(str)); - final String body; - try (XContentBuilder builder = jsonBuilder()) { - builder.startObject(); - { - builder.field("first_name", "John"); - builder.field("last_name", "Smith"); - builder.field("age", 25); - builder.field("about", "I love to go rock climbing"); - builder.startArray("interests"); - { - builder.value("sports"); - builder.value("music"); - } - builder.endArray(); - } - builder.endObject(); - body = Strings.toString(builder); - } + } + return "http://localhost:" + port + "/example-app/transport"; + } + + public void testRestClient() throws URISyntaxException, IOException { + final String baseUrl = buildBaseUrl(); + + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + final String endpoint = baseUrl + "/employees/1"; + logger.info("Connecting to uri: " + baseUrl); + + final HttpPut put = new HttpPut(new URI(endpoint)); + + final String body = "{" + + " \"first_name\": \"John\"," + + " \"last_name\": \"Smith\"," + + " \"age\": 25," + + " \"about\": \"I love to go rock climbing\"," + + " \"interests\": [" + + " \"sports\"," + + " \"music\"" + + " ]" + + "}"; + put.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = client.execute(put)) { int status = response.getStatusLine().getStatusCode(); - assertThat("expected a 201 response but got: " + status + " - body: " + EntityUtils.toString(response.getEntity()), - status, equalTo(201)); + assertThat( + "expected a 201 response but got: " + status + " - body: " + EntityUtils.toString(response.getEntity()), + status, + equalTo(201) + ); } - final HttpGet get = new HttpGet(new URI(str)); + logger.info("Fetching resource at " + endpoint); + + final HttpGet get = new HttpGet(new URI(endpoint)); try ( - CloseableHttpResponse response = client.execute(get); - XContentParser parser = - JsonXContent.jsonXContent.createParser( - new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - response.getEntity().getContent())) { + CloseableHttpResponse response = client.execute(get); + XContentParser parser = JsonXContent.jsonXContent.createParser( + new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + response.getEntity().getContent() + ) + ) { final Map map = parser.map(); assertThat(map.get("first_name"), equalTo("John")); assertThat(map.get("last_name"), equalTo("Smith")); @@ -105,7 +113,8 @@ public class WildflyIT extends LuceneTestCase { assertThat(map.get("about"), equalTo("I love to go rock climbing")); final Object interests = map.get("interests"); assertThat(interests, instanceOf(List.class)); - @SuppressWarnings("unchecked") final List interestsAsList = (List) interests; + @SuppressWarnings("unchecked") + final List interestsAsList = (List) interests; assertThat(interestsAsList, containsInAnyOrder("sports", "music")); } }