Skip to content

Commit 511ab70

Browse files
committed
improve unit test
1 parent fd8f830 commit 511ab70

11 files changed

+307
-308
lines changed

compile-and-test.sh

+41-85
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22
#
33
# script to start influxdb and compile influxdb-java with all tests.
44
#
5-
# Note for Windows users:
6-
# In case your docker still uses VirtualBox as a VM, you will probably have to
7-
# inform the containers about the location of your repository.
8-
# Please not that Docker for windows, enables you to mount everything
9-
# from your Users (C:\Users in most cases) directory, so to keep it easy,
10-
# it's better to keep your repository somewhere there.
11-
# If you will decide to put your sources somewhere else, please visit your
12-
# VirtualBox settings and check out the "Shared folder configuration".
13-
# This script uses environment variable BUILD_HOME which should point to this
14-
# project directory (i.e. //c/Users/MyWindowsUserName/Projects/influxdb-java)
15-
#
16-
# Of course you still need bash to launch this script. But this should be no
17-
# problem either to install it (this script was tested with GitExtensions package).
18-
195
set -e
206

217
DEFAULT_INFLUXDB_VERSION="1.5"
@@ -24,75 +10,45 @@ DEFAULT_MAVEN_JAVA_VERSION="3-jdk-10-slim"
2410
INFLUXDB_VERSION="${INFLUXDB_VERSION:-$DEFAULT_INFLUXDB_VERSION}"
2511
MAVEN_JAVA_VERSION="${MAVEN_JAVA_VERSION:-$DEFAULT_MAVEN_JAVA_VERSION}"
2612

27-
WORKDIR=/usr/src/mymaven
28-
29-
if [ -z "$BUILD_HOME" ] ; then
30-
BUILD_HOME=$PWD
31-
if [ -x /c/Windows/System32/ ] ; then
32-
BUILD_HOME=/$PWD
33-
fi
34-
fi
35-
36-
if [ -x /c/Windows/System32/ ] ; then
37-
echo "Detected Windows as a host system"
38-
WORKDIR=//usr/src/mymaven
39-
fi
40-
41-
echo Using build home: $BUILD_HOME
42-
43-
function run_test {
44-
USE_PROXY=$1
45-
46-
INFLUXDB_API_URL=http://influxdb:8086
47-
if [ "$USE_PROXY" == "nginx" ] ; then
48-
echo Test with Nginx as proxy
49-
INFLUXDB_API_URL=http://nginx:8080/influx-api/
50-
fi
51-
52-
53-
echo "Run tests with maven:${MAVEN_JAVA_VERSION} on onfluxdb-${INFLUXDB_VERSION}"
54-
docker kill influxdb || true
55-
docker rm influxdb || true
56-
docker pull influxdb:${version}-alpine || true
57-
docker run \
13+
echo "Run tests with maven:${MAVEN_JAVA_VERSION} on onfluxdb-${INFLUXDB_VERSION}"
14+
docker kill influxdb || true
15+
docker rm influxdb || true
16+
docker pull influxdb:${version}-alpine || true
17+
docker run \
18+
--detach \
19+
--name influxdb \
20+
--publish 8086:8086 \
21+
--publish 8089:8089/udp \
22+
--volume ${PWD}/influxdb.conf:/etc/influxdb/influxdb.conf \
23+
influxdb:${INFLUXDB_VERSION}-alpine
24+
25+
echo Starting Nginx
26+
docker kill nginx || true
27+
docker rm nginx || true
28+
29+
echo ----- STARTING NGINX CONTAINER -----
30+
docker run \
5831
--detach \
59-
--name influxdb \
60-
--publish 8086:8086 \
61-
--publish 8089:8089/udp \
62-
--volume ${BUILD_HOME}/influxdb.conf:/etc/influxdb/influxdb.conf \
63-
influxdb:${INFLUXDB_VERSION}-alpine
64-
65-
NGINX_LINK=
66-
SKIP_TESTS=
67-
if [ "$USE_PROXY" == "nginx" ] ; then
68-
echo Starting Nginx
69-
docker kill nginx || true
70-
docker rm nginx || true
71-
echo ----- STARTING NGINX CONTAINER -----
72-
docker run \
73-
--detach \
74-
--name nginx \
75-
--publish 8888:8080 \
76-
--volume ${BUILD_HOME}/src/test/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro \
77-
--link influxdb:influxdb \
78-
nginx nginx '-g' 'daemon off;'
79-
NGINX_LINK=--link=nginx
80-
SKIP_TESTS=-DsomeModule.test.excludes="**/*UDPInfluxDBTest*"
81-
fi
82-
83-
docker run -it --rm \
84-
--volume $BUILD_HOME:/usr/src/mymaven \
85-
--volume $BUILD_HOME/.m2:/root/.m2 \
86-
--workdir $WORKDIR \
87-
--link=influxdb $NGINX_LINK \
88-
--env INFLUXDB_API_URL=$INFLUXDB_API_URL \
89-
maven:${MAVEN_JAVA_VERSION} mvn clean install $SKIP_TESTS
90-
91-
docker kill influxdb || true
92-
if [ "$USE_PROXY" == "nginx" ] ; then
93-
docker kill nginx || true
94-
fi
95-
}
96-
97-
run_test nginx
98-
run_test
32+
--name nginx \
33+
--publish 8080:8080 \
34+
--publish 8080:8080/udp \
35+
--volume ${PWD}/src/test/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
36+
--link influxdb:influxdb \
37+
nginx:stable nginx '-g' 'daemon off;'
38+
39+
PROXY_API_URL=http://nginx:8080/influx-api/
40+
PROXY_UDP_PORT=8080
41+
42+
docker run -it --rm \
43+
--volume $PWD:/usr/src/mymaven \
44+
--volume $PWD/.m2:/root/.m2 \
45+
--workdir /usr/src/mymaven \
46+
--link=influxdb \
47+
--link=nginx \
48+
--env INFLUXDB_IP=influxdb \
49+
--env PROXY_API_URL=$PROXY_API_URL \
50+
--env PROXY_UDP_PORT=$PROXY_UDP_PORT \
51+
maven:${MAVEN_JAVA_VERSION} mvn clean install
52+
53+
docker kill influxdb || true
54+
docker kill nginx || true

pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@
8989
<groupId>org.apache.maven.plugins</groupId>
9090
<artifactId>maven-surefire-plugin</artifactId>
9191
<version>2.21.0</version>
92-
<configuration>
93-
<excludes>
94-
<exclude>${someModule.test.excludes}</exclude>
95-
</excludes>
96-
</configuration>
9792
</plugin>
9893
<plugin>
9994
<groupId>org.apache.maven.plugins</groupId>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.influxdb;
22

3-
import okhttp3.OkHttpClient;
43
import org.influxdb.dto.Pong;
54
import org.junit.jupiter.api.Assertions;
65
import org.junit.jupiter.api.Test;
76
import org.junit.platform.runner.JUnitPlatform;
87
import org.junit.runner.RunWith;
98

9+
import okhttp3.OkHttpClient;
10+
1011
/**
1112
* Test the InfluxDB Factory API.
1213
*
@@ -16,35 +17,35 @@
1617
@RunWith(JUnitPlatform.class)
1718
public class InfluxDBFactoryTest {
1819

19-
/**
20-
* Test for a {@link InfluxDBFactory #connect(String)}.
21-
*/
22-
@Test
23-
public void testCreateInfluxDBInstanceWithoutUserNameAndPassword() {
24-
InfluxDB influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL());
25-
verifyInfluxDBInstance(influxDB);
26-
}
20+
/**
21+
* Test for a {@link InfluxDBFactory #connect(String)}.
22+
*/
23+
@Test
24+
public void testCreateInfluxDBInstanceWithoutUserNameAndPassword() {
25+
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true));
26+
verifyInfluxDBInstance(influxDB);
27+
}
2728

28-
private void verifyInfluxDBInstance(InfluxDB influxDB) {
29-
Assertions.assertNotNull(influxDB);
30-
Pong pong = influxDB.ping();
31-
Assertions.assertNotNull(pong);
32-
Assertions.assertNotEquals(pong.getVersion(), "unknown");
33-
}
29+
private void verifyInfluxDBInstance(InfluxDB influxDB) {
30+
Assertions.assertNotNull(influxDB);
31+
Pong pong = influxDB.ping();
32+
Assertions.assertNotNull(pong);
33+
Assertions.assertNotEquals(pong.getVersion(), "unknown");
34+
}
3435

35-
/**
36-
* Test for a {@link InfluxDBFactory #connect(String, okhttp3.OkHttpClient.Builder)}.
37-
*/
38-
@Test
39-
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
40-
InfluxDB influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL(), new OkHttpClient.Builder());
41-
verifyInfluxDBInstance(influxDB);
42-
}
36+
/**
37+
* Test for a {@link InfluxDBFactory #connect(String, okhttp3.OkHttpClient.Builder)}.
38+
*/
39+
@Test
40+
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
41+
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), new OkHttpClient.Builder());
42+
verifyInfluxDBInstance(influxDB);
43+
}
4344

44-
@Test
45-
public void testShouldThrowIllegalArgumentWithInvalidUrl() {
46-
Assertions.assertThrows(IllegalArgumentException.class,() -> {
47-
InfluxDBFactory.connect("invalidUrl");
48-
});
49-
}
45+
@Test
46+
public void testShouldThrowIllegalArgumentWithInvalidUrl() {
47+
Assertions.assertThrows(IllegalArgumentException.class,() -> {
48+
InfluxDBFactory.connect("invalidUrl");
49+
});
50+
}
5051
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.influxdb;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.influxdb.dto.Point;
7+
import org.influxdb.dto.Query;
8+
import org.influxdb.dto.QueryResult;
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.platform.runner.JUnitPlatform;
14+
import org.junit.runner.RunWith;
15+
/**
16+
* Test the InfluxDB API.
17+
*
18+
* @author hoan.le [at] bonitoo.io
19+
*
20+
*/
21+
@RunWith(JUnitPlatform.class)
22+
public class InfluxDBProxyTest {
23+
private InfluxDB influxDB;
24+
private String db = "udp";
25+
26+
@BeforeEach
27+
public void setUp() throws InterruptedException, IOException {
28+
this.influxDB = TestUtils.connectToInfluxDB(TestUtils.getProxyApiUrl());
29+
this.influxDB.createDatabase(db);
30+
influxDB.setDatabase(db);
31+
}
32+
33+
/**
34+
* delete database after all tests end.
35+
*/
36+
@AfterEach
37+
public void cleanup(){
38+
this.influxDB.deleteDatabase(db);
39+
}
40+
41+
@Test
42+
public void testWriteSomePointThroughTcpProxy() {
43+
for(int i = 0; i < 20; i++) {
44+
Point point = Point.measurement("weather")
45+
.time(i,TimeUnit.HOURS)
46+
.addField("temperature", (double) i)
47+
.addField("humidity", (double) (i) * 1.1)
48+
.addField("uv_index", "moderate").build();
49+
influxDB.write(point);
50+
}
51+
52+
QueryResult result = influxDB.query(new Query("select * from weather", db));
53+
//check points written already to DB
54+
Assertions.assertEquals(20, result.getResults().get(0).getSeries().get(0).getValues().size());
55+
56+
}
57+
58+
@Test
59+
public void testWriteSomePointThroughUdpProxy() throws InterruptedException {
60+
int proxyUdpPort = Integer.parseInt(TestUtils.getProxyUdpPort());
61+
for(int i = 0; i < 20; i++) {
62+
Point point = Point.measurement("weather")
63+
.time(i,TimeUnit.HOURS)
64+
.addField("temperature", (double) i)
65+
.addField("humidity", (double) (i) * 1.1)
66+
.addField("uv_index", "moderate").build();
67+
influxDB.write(proxyUdpPort, point);
68+
}
69+
70+
Thread.sleep(2000);
71+
QueryResult result = influxDB.query(new Query("select * from weather", db));
72+
//check points written already to DB
73+
Assertions.assertEquals(20, result.getResults().get(0).getSeries().get(0).getValues().size());
74+
75+
}
76+
77+
}

src/test/java/org/influxdb/InfluxDBTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ public void testBatchEnabledTwice() {
658658
*/
659659
@Test
660660
public void testCloseInfluxDBClient() {
661-
InfluxDB influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "admin", "admin");
661+
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", "admin");
662662
influxDB.enableBatch(1, 1, TimeUnit.SECONDS);
663663
Assertions.assertTrue(influxDB.isBatchEnabled());
664664
influxDB.close();
@@ -670,7 +670,7 @@ public void testCloseInfluxDBClient() {
670670
*/
671671
@Test
672672
public void testWriteEnableGzip() {
673-
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "admin", "admin");
673+
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", "admin");
674674
String dbName = "write_unittest_" + System.currentTimeMillis();
675675
try {
676676
influxDBForTestGzip.setLogLevel(LogLevel.NONE);
@@ -702,7 +702,7 @@ public void testWriteEnableGzip() {
702702
*/
703703
@Test
704704
public void testWriteEnableGzipAndDisableGzip() {
705-
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "admin", "admin");
705+
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", "admin");
706706
try {
707707
//test default: gzip is disable
708708
Assertions.assertFalse(influxDBForTestGzip.isGzipEnabled());

src/test/java/org/influxdb/PerformanceTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class PerformanceTests {
3333

3434
@BeforeEach
3535
public void setUp() {
36-
this.influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "root", "root");
36+
this.influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "root", "root");
3737
this.influxDB.setLogLevel(LogLevel.NONE);
3838
this.influxDB.createDatabase(UDP_DATABASE);
3939
}

0 commit comments

Comments
 (0)