Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
perf(InfluxDBManager,InfluxDBTest,TestUtility) : Improuve Tests
Browse files Browse the repository at this point in the history
Add TestUtility to manage InfluxDB server URL.
Refactor InfluxDBUnitTest to InfluxDBTest.
Improuve InfluxDBManger.
  • Loading branch information
aoudiamoncef committed Oct 9, 2017
1 parent ac9d8f2 commit f9643ac
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 90 deletions.
58 changes: 31 additions & 27 deletions app/src/main/java/science/apolline/InfluxDBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,62 @@ public class InfluxDBManager {
public final String DATE = "date";


public InfluxDB configInfluxDB(String dbName, String dbUrlPort, String dbUser, String dbPassword) {
public InfluxDB setup(String dbUrlPort, String dbUser, String dbPassword) {

InfluxDB influxDB = InfluxDBFactory.connect(dbUrlPort, dbUser, dbPassword);
influxDB.setLogLevel(InfluxDB.LogLevel.NONE);
influxDB.setConsistency(InfluxDB.ConsistencyLevel.QUORUM);
// influxDB.createDatabase(dbName);
// Flush every 2000 Points, at least every 100ms
influxDB.enableBatch(2000, 100, TimeUnit.NANOSECONDS);

return influxDB;
}


public void write(InfluxDB influxDB, String dbName, String jsonArg) {
public boolean write(InfluxDB influxDB, String dbName, String jsonArg) {

BatchPoints batchPoints = BatchPoints
.database(dbName)
.build();
try {
BatchPoints batchPoints = BatchPoints
.database(dbName)
.build();

JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(jsonArg);
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(jsonArg);

if (jsonElement.isJsonObject()) {
if (jsonElement.isJsonObject()) {

JsonObject json = jsonElement.getAsJsonObject();
JsonObject json = jsonElement.getAsJsonObject();

if (json.has(DATA)) {
JsonObject tmp = json.get(DATA).getAsJsonObject();
if (json.has(DATA)) {
JsonObject tmp = json.get(DATA).getAsJsonObject();

Set<Map.Entry<String, JsonElement>> entries = tmp.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {
Set<Map.Entry<String, JsonElement>> entries = tmp.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {

Point.Builder pointBuilder = Point.measurement(entry.getKey())
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField(VALUE, entry.getValue().toString())
.addField(DATE, json.get(DATE).getAsString())
.tag(DEVICE, json.get(DEVICE).getAsString())
.tag(SENSOR, json.get(SENSOR).getAsString())
.tag(UNIT, json.get(UNIT).getAsString());
Point.Builder pointBuilder = Point.measurement(entry.getKey())
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField(VALUE, entry.getValue().toString())
.addField(DATE, json.get(DATE).getAsString())
.tag(DEVICE, json.get(DEVICE).getAsString())
.tag(SENSOR, json.get(SENSOR).getAsString())
.tag(UNIT, json.get(UNIT).getAsString());

Point point = pointBuilder.build();
batchPoints.point(point);
Point point = pointBuilder.build();
batchPoints.point(point);

}
}

}
}

influxDB.write(batchPoints);
influxDB.write(batchPoints);

}
} catch (Exception e) {
e.printStackTrace();
return false;
}


return true;
}


Expand Down
70 changes: 70 additions & 0 deletions app/src/test/java/science/apolline/InfluxDBTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package science.apolline;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.influxdb.InfluxDB;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;


/**
* Created by sparow on 10/1/17.
*/

public class InfluxDBTest {

public static String JSONTOSEND = "{\"device\":\"Arduino\",\"sensorId\":\"MQ135\",\"gasId\":{\"CO2\":1,\"SMOKE\":2,\"CH4\":3,\"O3\":4},\"unit\":\"PPM\",\"time\":\"WedSep2614:23:28EST2017\"}";
JsonParser parser = new JsonParser();
private InfluxDBManager influxDBManager = new InfluxDBManager();

@Rule
public final ExpectedException exception = ExpectedException.none();



/**
* Test for a ping.
*/
@Test
public void setupInfluxDBTest() throws Exception {
InfluxDB influxDB = this.influxDBManager.setup("http://" + TestUtility.getInfluxDBIP() + ":" + TestUtility.getInfluxDBPORT(true),"toto", "root");
Assert.assertNotNull(influxDB);
}

/**
* Test for input JSON parsing.
*/
@Test
public void testJsonParsing() throws Exception {

JsonElement jsonElement = this.parser.parse(JSONTOSEND);
Assert.assertNotNull(jsonElement);
Assert.assertEquals(this.parser.parse(JSONTOSEND).toString(),JSONTOSEND);

}

/**
* Test for writing points to remote InfluxDB server.
*/
@Test
public void testWriteToInfluxDB() throws Exception {
InfluxDB influxDB = this.influxDBManager.setup("http://" + TestUtility.getInfluxDBIP() + ":" + TestUtility.getInfluxDBPORT(true), "toto", "root");
Assert.assertNotNull(influxDB);
Assert.assertTrue(this.influxDBManager.write(influxDB, "test0", JSONTOSEND));
}


}








63 changes: 0 additions & 63 deletions app/src/test/java/science/apolline/InfluxDBUnitTest.java

This file was deleted.

35 changes: 35 additions & 0 deletions app/src/test/java/science/apolline/TestUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package science.apolline;

/**
* Created by sparow on 10/9/17.
*/

import java.util.Map;

public class TestUtility {


public static String getInfluxDBIP() {
String ip = "127.0.0.1";

Map<String, String> getenv = System.getenv();
if (getenv.containsKey("INFLUXDB_IP")) {
ip = getenv.get("INFLUXDB_IP");
}

return ip;
}



public static String getInfluxDBPORT(boolean apiPort) {
String port = "8086";

Map<String, String> getenv = System.getenv();
if(apiPort) {
if (getenv.containsKey("INFLUXDB_PORT_API"))
port = getenv.get("INFLUXDB_PORT_API");
}
return port;
}
}

0 comments on commit f9643ac

Please sign in to comment.