Skip to content

Commit

Permalink
Do not use hard coded ports in integration-tests apache#508
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Dec 4, 2019
1 parent 2db85bc commit 526640f
Show file tree
Hide file tree
Showing 37 changed files with 551 additions and 186 deletions.
7 changes: 7 additions & 0 deletions integration-tests/hystrix/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<!-- test dependencies - camel-quarkus -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public void configure() throws Exception {
.circuitBreaker()
.throwException(new IllegalStateException("Forced exception"))
.onFallbackViaNetwork()
.to("netty-http:http://localhost:8999/network/fallback")
.to("netty-http:http://localhost:{{camel.netty.test-port}}/network/fallback")
.end();

from("direct:delay")
.delay(simple("${header.delayMilliseconds}"))
.setBody(constant("Hello Camel Quarkus Hystrix"));

from("netty-http:http://0.0.0.0:8999/network/fallback")
from("netty-http:http://0.0.0.0:{{camel.netty.test-port}}/network/fallback")
.setBody(constant("Fallback via network response"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package org.apache.camel.quarkus.component.hystrix.it;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;

import org.junit.jupiter.api.Test;
import static org.hamcrest.core.Is.is;

@QuarkusTest
@QuarkusTestResource(HystrixTestResource.class)
class HystrixTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.camel.quarkus.component.hystrix.it;

import java.util.Map;
import java.util.Objects;

import org.apache.camel.quarkus.test.AvailablePortFinder;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;

public class HystrixTestResource implements QuarkusTestResourceLifecycleManager {
@Override
public Map<String, String> start() {
return AvailablePortFinder.reserveNetworkPorts(Objects::toString, "camel.netty.test-port");
}

@Override
public void stop() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.quarkus.component.jdbc;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand All @@ -24,6 +26,7 @@
import static org.hamcrest.Matchers.is;

@QuarkusTest
@QuarkusTestResource(H2DatabaseTestResource.class)
public class CamelJdbcTest {

@Test
Expand Down
19 changes: 19 additions & 0 deletions integration-tests/kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<name>Camel Quarkus :: Integration Tests :: Kafka</name>
<description>Integration tests for Camel Quarkus Kafka extension</description>

<properties>
<rerun.failing.test.count>2</rerun.failing.test.count>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
Expand Down Expand Up @@ -68,6 +72,13 @@
<scope>test</scope>
</dependency>

<!-- test dependencies - camel-quarkus -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support</artifactId>
<scope>test</scope>
</dependency>

<!-- test dependencies - kafka -->
<dependency>
<groupId>io.debezium</groupId>
Expand Down Expand Up @@ -100,6 +111,13 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<rerunFailingTestsCount>${rerun.failing.test.count}</rerunFailingTestsCount>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -123,6 +141,7 @@
<goal>verify</goal>
</goals>
<configuration>
<rerunFailingTestsCount>${rerun.failing.test.count}</rerunFailingTestsCount>
<systemProperties>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemProperties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.ws.rs.core.MediaType;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

Expand All @@ -39,32 +41,30 @@ public class CamelKafkaResource {
@POST
@Produces(MediaType.APPLICATION_JSON)
public JsonObject post(@PathParam("topicName") String topicName, String message) throws Exception {
RecordMetadata meta = CamelKafkaSupport.createProducer()
.send(new ProducerRecord<>(topicName, 1, message))
.get();
try (Producer<Integer, String> producer = CamelKafkaSupport.createProducer()) {
RecordMetadata meta = producer.send(new ProducerRecord<>(topicName, 1, message)).get();

return Json.createObjectBuilder()
.add("topicName", meta.topic())
.add("partition", meta.partition())
.add("offset", meta.offset())
.build();
return Json.createObjectBuilder()
.add("topicName", meta.topic())
.add("partition", meta.partition())
.add("offset", meta.offset())
.build();
}
}

@Path("/kafka/{topicName}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject get(@PathParam("topicName") String topicName) {
ConsumerRecord<Integer, String> record = CamelKafkaSupport.createConsumer(topicName)
.poll(Duration.ofSeconds(60))
.iterator()
.next();

return Json.createObjectBuilder()
.add("topicName", record.topic())
.add("partition", record.partition())
.add("offset", record.offset())
.add("key", record.key())
.add("body", record.value())
.build();
try (KafkaConsumer<Integer, String> consumer = CamelKafkaSupport.createConsumer(topicName)) {
ConsumerRecord<Integer, String> record = consumer.poll(Duration.ofSeconds(60)).iterator().next();
return Json.createObjectBuilder()
.add("topicName", record.topic())
.add("partition", record.partition())
.add("offset", record.offset())
.add("key", record.key())
.add("body", record.value())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private CamelKafkaSupport() {

public static KafkaConsumer<Integer, String> createConsumer(String topicName) {
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, System.getProperty("camel.component.kafka.brokers"));
props.put(ConsumerConfig.GROUP_ID_CONFIG, "test");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
Expand All @@ -50,7 +50,7 @@ public static KafkaConsumer<Integer, String> createConsumer(String topicName) {

public static Producer<Integer, String> createProducer() {
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, System.getProperty("camel.component.kafka.brokers"));
props.put(ProducerConfig.CLIENT_ID_CONFIG, "test-consumer");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
## ---------------------------------------------------------------------------

#
# Quarkus
# Quarkus - Log
#
quarkus.log.file.enable = false
quarkus.log.category."org.apache.camel.quarkus.core.deployment".level = INFO
quarkus.log.category."org.apache.camel.quarkus.component.kafka".level = DEBUG
quarkus.log.category."org.apache.zookeeper".level = WARNING
quarkus.log.category."org.apache.kafka".level = WARNING

#
# Camel
#
camel.component.kafka.brokers = localhost:19092
%quiet.quarkus.log.category."kafka".level = WARNING
%quiet.quarkus.log.category."kafka.log".level = FATAL
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,67 @@
package org.apache.camel.quarkus.component.kafka.it;

import java.io.File;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import io.debezium.kafka.KafkaCluster;
import io.debezium.util.Testing;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.apache.camel.quarkus.core.CamelMain;
import org.apache.camel.quarkus.test.AvailablePortFinder;

public class CamelKafkaTestResource implements QuarkusTestResourceLifecycleManager {
private KafkaCluster kafka;
private CamelMain main;

@Override
public void inject(Object testInstance) {
if (testInstance instanceof CamelKafkaTest) {
this.main = ((CamelKafkaTest) testInstance).main;
}
}

@Override
public Map<String, String> start() {
try {
final int zkPort = AvailablePortFinder.getNextAvailable();
final int kafkaPort = AvailablePortFinder.getNextAvailable();
final File directory = Files.createTempDirectory("kafka-data-").toFile();

Properties props = new Properties();
props.setProperty("zookeeper.connection.timeout.ms", "45000");

File directory = Testing.Files.createTestingDirectory("kafka-data", true);

kafka = new KafkaCluster()
.withPorts(2182, 19092)
.withPorts(zkPort, kafkaPort)
.addBrokers(1)
.usingDirectory(directory)
.deleteDataUponShutdown(true)
.withKafkaConfiguration(props)
.deleteDataPriorToStartup(true)
.startup();

return Collections.singletonMap("camel.component.kafka.brokers", "localhost:" + kafkaPort);
} catch (Exception e) {
throw new RuntimeException(e);
}
return Collections.emptyMap();
}

@Override
public void stop() {
if (main != null) {
try {
main.stop();
} catch (Exception e) {
// ignored
}
}
if (kafka != null) {
kafka.shutdown();
try {
kafka.shutdown();
} catch (Exception e) {
// ignored
}
}
}
}
9 changes: 9 additions & 0 deletions integration-tests/mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<!-- test dependencies - camel-quarkus -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support</artifactId>
<scope>test</scope>
</dependency>

<!-- test dependencies - mongodb -->
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import java.net.URI;
import java.net.URISyntaxException;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
Expand All @@ -34,16 +34,15 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoIterable;

import org.apache.camel.ProducerTemplate;
import org.bson.Document;

@Path("/mongodb")
@ApplicationScoped
public class MongoDbResource {

@Inject
ProducerTemplate producerTemplate;

Expand All @@ -65,6 +64,7 @@ public Response writeToCollection(@PathParam("collectionName") String collection
@GET
@Path("/collection/{collectionName}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public JsonArray getCollection(@PathParam("collectionName") String collectionName) {
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();

Expand All @@ -83,4 +83,10 @@ public JsonArray getCollection(@PathParam("collectionName") String collectionNam

return arrayBuilder.build();
}

@javax.enterprise.inject.Produces
@Named("camelMongoClient")
public MongoClient camelMongoClient() {
return new MongoClient("localhost", Integer.getInteger("camel.mongodb.test-port"));
}
}

0 comments on commit 526640f

Please sign in to comment.