Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integration-tests: make influxdb tests more idiomatic #1346

Merged
merged 1 commit into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,77 @@
*/
package org.apache.camel.quarkus.component.influxdb.it;

import java.util.List;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Disposes;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import io.quarkus.arc.Unremovable;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.component.influxdb.InfluxDbConstants;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Pong;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.jboss.logging.Logger;

@Path("/influxdb")
@ApplicationScoped
public class InfluxdbResource {

private static final Logger LOG = Logger.getLogger(InfluxdbResource.class);

public static final String DB_NAME = "myTestTimeSeries";

public static final String INFLUXDB_CONNECTION_PROPERTY = "quarkus.influxdb.connection.url";
public static final String INFLUXDB_VERSION = "1.7.10";

private static final String INFLUXDB_CONNECTION = "http://{{" + INFLUXDB_CONNECTION_PROPERTY + "}}/";
private static final String INFLUXDB_CONNECTION_NAME = "influxDb_connection";
private static final String INFLUXDB_ENDPOINT_URL = "influxdb:" + INFLUXDB_CONNECTION_NAME;
public static final String INFLUXDB_CONNECTION_PROPERTY = "influxdb.connection.url";
public static final String INFLUXDB_CONNECTION_NAME = "influxDbConnection";

@Inject
ProducerTemplate producerTemplate;

@Inject
CamelContext context;

private InfluxDB influxDB;
FluentProducerTemplate producerTemplate;

void onStart(@Observes org.apache.camel.quarkus.core.CamelMainEvents.BeforeConfigure ev) {
influxDB = InfluxDBFactory.connect(context.getPropertiesComponent().parseUri(INFLUXDB_CONNECTION));
@ConfigProperty(name = INFLUXDB_CONNECTION_PROPERTY)
String connectionUrl;

influxDB.query(new Query("CREATE DATABASE " + DB_NAME));
@Unremovable
@Singleton
@javax.enterprise.inject.Produces
InfluxDB createInfluxDbConnection() {
InfluxDB influxDbConnection = InfluxDBFactory.connect(connectionUrl);
influxDbConnection.query(new Query("CREATE DATABASE " + DB_NAME));

context.getRegistry().bind(INFLUXDB_CONNECTION_NAME, influxDB);
return influxDbConnection;
}

void beforeStop(@Observes org.apache.camel.quarkus.core.CamelMainEvents.BeforeStop ev) {
if (influxDB != null) {
influxDB.query(new Query("DROP DATABASE " + DB_NAME, ""));
influxDB.close();
}
void disposeInfluxDbConnection(@Disposes InfluxDB influxDbConnection) {
influxDbConnection.query(new Query("DROP DATABASE " + DB_NAME, ""));
influxDbConnection.close();
}

@Path("/ping")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String ping() {
Pong pong = producerTemplate.requestBody(INFLUXDB_ENDPOINT_URL + "?operation=ping", null, Pong.class);

return pong.getVersion();
return producerTemplate.toF(
"influxdb:%s?operation=ping", INFLUXDB_CONNECTION_NAME)
.request(Pong.class)
.getVersion();
}

@Path("/insert")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public boolean insert(Point point) {
org.influxdb.dto.Point p = point.toPoint();

org.influxdb.dto.Point result = producerTemplate.requestBody(
INFLUXDB_ENDPOINT_URL + "?databaseName=" + DB_NAME + "&operation=insert&retentionPolicy=autogen", p,
org.influxdb.dto.Point.class);
org.influxdb.dto.Point result = producerTemplate.toF(
"influxdb:%s?databaseName=%s&operation=insert&retentionPolicy=autogen", INFLUXDB_CONNECTION_NAME, DB_NAME)
.withBody(point.toPoint())
.request(org.influxdb.dto.Point.class);

return result != null;
}
Expand All @@ -106,29 +95,28 @@ public boolean insert(Point point) {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String batch(Points points) {
BatchPoints p = points.toBatchPoints();

BatchPoints result = producerTemplate.requestBody(INFLUXDB_ENDPOINT_URL + "?batch=true", p,
BatchPoints.class);

return String.valueOf(result.getPoints().size());
public int batch(Points points) {
return producerTemplate.toF(
"influxdb:%s?batch=true", INFLUXDB_CONNECTION_NAME)
.withBody(points.toBatchPoints())
.request(BatchPoints.class)
.getPoints().size();
}

@Path("/query")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String query(String query) throws Exception {
Exchange exchange = producerTemplate.request(
INFLUXDB_ENDPOINT_URL + "?databaseName=" + DB_NAME + "&operation=query&retentionPolicy=autogen",
e -> e.getIn().setHeader(InfluxDbConstants.INFLUXDB_QUERY, query));
List<QueryResult.Result> results = exchange.getMessage().getBody(QueryResult.class).getResults();
return results.stream()
.flatMap(r -> r.getSeries() != null ? r.getSeries().stream() : null)
.map(s -> s.getName())
QueryResult result = producerTemplate.toF(
"influxdb:%s?databaseName=%s&operation=query&retentionPolicy=autogen", INFLUXDB_CONNECTION_NAME, DB_NAME)
.withHeader(InfluxDbConstants.INFLUXDB_QUERY, query)
.request(QueryResult.class);

return result.getResults().stream()
.filter(r -> r.getSeries() != null)
.flatMap(r -> r.getSeries().stream())
.map(QueryResult.Series::getName)
.collect(Collectors.joining(", "));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.junit.jupiter.api.TestMethodOrder;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@QuarkusTest
@QuarkusTestResource(InfluxdbTestResource.class)
Expand All @@ -42,30 +42,27 @@ class InfluxdbTest {
@Test
@Order(1)
public void pingTest() {
RestAssured.given().get("/influxdb/ping").then().body(is(InfluxdbResource.INFLUXDB_VERSION));
RestAssured.given().get("/influxdb/ping").then().body(is(InfluxdbTestResource.INFLUXDB_VERSION));
}

@Test
@Order(2)
public void insertTest() {

Point point = createBatchPoints().getPoints().get(0);
RestAssured.given() //
RestAssured.given()
.contentType(ContentType.JSON)
.body(point)
.post("/influxdb/insert")
.then()
.statusCode(200)
.body(is("true"));

}

@Test
@Order(3)
public void batchInsertTest() {

Points points = createBatchPoints();
RestAssured.given() //
RestAssured.given()
.contentType(ContentType.JSON)
.body(points)
.post("/influxdb/batch")
Expand All @@ -78,7 +75,7 @@ public void batchInsertTest() {
@Order(4)
public void queryTest() {
// result should contain only 1 result with name 'cpu', because 'cpu' is only part of batchInsert, which was executed before
RestAssured.given() //
RestAssured.given()
.contentType(ContentType.TEXT)
.body("select * from cpu")
.post("/influxdb/query")
Expand All @@ -92,21 +89,18 @@ public void queryTest() {
public void doesNotAddCamelHeaders() {
Map<String, Object> pointInMapFormat = new HashMap<>();
pointInMapFormat.put(InfluxDbConstants.MEASUREMENT_NAME, "testCPU");
double value = 99.999999d;
pointInMapFormat.put("busy", value);
pointInMapFormat.put("busy", 99.999999d);

org.influxdb.dto.Point p = CamelInfluxDbConverters.fromMapToPoint(pointInMapFormat);
assertNotNull(p);

String line = p.lineProtocol();

assertNotNull(line);

assertTrue(!line.contains(InfluxDbConstants.MEASUREMENT_NAME));

assertFalse(line.contains(InfluxDbConstants.MEASUREMENT_NAME));
}

private Points createBatchPoints() {
private static Points createBatchPoints() {
Points points = new Points();
points.setDatabase(InfluxdbResource.DB_NAME);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;

import org.apache.camel.quarkus.testcontainers.ContainerResourceLifecycleManager;
import org.apache.camel.quarkus.testcontainers.ContainerSupport;
import org.apache.camel.util.CollectionHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,27 +29,27 @@
import org.testcontainers.utility.TestcontainersConfiguration;

public class InfluxdbTestResource implements ContainerResourceLifecycleManager {
public static final Logger LOGGER = LoggerFactory.getLogger(InfluxdbTestResource.class);
public static final int INFLUXDB_PORT = 8086;
public static final String INFLUXDB_VERSION = "1.7.10";
public static final String INFLUXDB_IMAGE = "influxdb:" + INFLUXDB_VERSION;

private static final Logger LOGGER = LoggerFactory.getLogger(InfluxdbTestResource.class);
private static final int INFLUXDB_PORT = 8086;
private static final String INFLUXDB_IMAGE = "influxdb:" + InfluxdbResource.INFLUXDB_VERSION;

private GenericContainer container;
private GenericContainer<?> container;

@Override
public Map<String, String> start() {
LOGGER.info(TestcontainersConfiguration.getInstance().toString());

try {
container = new GenericContainer(INFLUXDB_IMAGE)
container = new GenericContainer<>(INFLUXDB_IMAGE)
.withExposedPorts(INFLUXDB_PORT)
.waitingFor(Wait.forListeningPort());

container.start();

return CollectionHelper.mapOf(
InfluxdbResource.INFLUXDB_CONNECTION_PROPERTY,
container.getContainerIpAddress() + ":" + container.getMappedPort(INFLUXDB_PORT));
"http://" + ContainerSupport.getHostAndPort(container, INFLUXDB_PORT));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down