|
1 | 1 | package org.influxdb;
|
2 | 2 |
|
| 3 | +import java.util.Collections; |
3 | 4 | import org.influxdb.InfluxDB.LogLevel;
|
4 | 5 | import org.influxdb.InfluxDB.ResponseFormat;
|
5 | 6 | import org.influxdb.dto.BatchPoints;
|
@@ -285,6 +286,162 @@ public void testWriteNoDatabase() {
|
285 | 286 | this.influxDB.query(new Query("DROP DATABASE " + dbName));
|
286 | 287 | }
|
287 | 288 |
|
| 289 | + /** |
| 290 | + * Tests that database information is used from {@link InfluxDB} when database information |
| 291 | + * is not present in query. |
| 292 | + */ |
| 293 | + @Test |
| 294 | + public void testQueryWithNoDatabase() { |
| 295 | + String dbName = "write_unittest_" + System.currentTimeMillis(); |
| 296 | + this.influxDB.query(new Query("CREATE DATABASE " + dbName)); |
| 297 | + this.influxDB.setDatabase(dbName); // Set db here, then after write query should pass. |
| 298 | + this.influxDB.write(Point |
| 299 | + .measurement("cpu") |
| 300 | + .tag("atag", "test") |
| 301 | + .addField("idle", 90L) |
| 302 | + .addField("usertime", 9L) |
| 303 | + .addField("system", 1L) |
| 304 | + .build()); |
| 305 | + |
| 306 | + Query query = new Query("SELECT * FROM cpu GROUP BY *"); |
| 307 | + QueryResult result = this.influxDB.query(query); |
| 308 | + Assertions.assertEquals( |
| 309 | + result.getResults().get(0).getSeries().get(0).getTags(), |
| 310 | + Collections.singletonMap("atag", "test") |
| 311 | + ); |
| 312 | + this.influxDB.query(new Query("DROP DATABASE " + dbName)); |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * Tests that database information is used from {@link InfluxDB} when database information |
| 317 | + * is not present in query. |
| 318 | + */ |
| 319 | + @Test |
| 320 | + public void testQueryWithNoDatabaseWithConsumer() { |
| 321 | + String dbName = "write_unittest_" + System.currentTimeMillis(); |
| 322 | + this.influxDB.query(new Query("CREATE DATABASE " + dbName)); |
| 323 | + this.influxDB.setDatabase(dbName); // Set db here, then after write query should pass. |
| 324 | + this.influxDB.write(Point |
| 325 | + .measurement("cpu") |
| 326 | + .tag("atag", "test") |
| 327 | + .addField("idle", 90L) |
| 328 | + .addField("usertime", 9L) |
| 329 | + .addField("system", 1L) |
| 330 | + .build()); |
| 331 | + |
| 332 | + Query query = new Query("SELECT * FROM cpu GROUP BY *"); |
| 333 | + this.influxDB.query(query, |
| 334 | + queryResult -> |
| 335 | + Assertions.assertEquals( |
| 336 | + queryResult.getResults().get(0).getSeries().get(0).getTags(), |
| 337 | + Collections.singletonMap("atag", "test") |
| 338 | + ) |
| 339 | + , |
| 340 | + throwable -> Assertions.fail() |
| 341 | + ); |
| 342 | + this.influxDB.query(new Query("DROP DATABASE " + dbName)); |
| 343 | + } |
| 344 | + |
| 345 | + |
| 346 | + /** |
| 347 | + * Tests that database information is used from {@link InfluxDB} when database information |
| 348 | + * is not present in query. |
| 349 | + */ |
| 350 | + // Note: this test is copied from InfluxDBTest#testChunking but changed so that database |
| 351 | + // information is present in client not query. Combined both tests test situations with |
| 352 | + // and without database information present in query, hence no need for additional test |
| 353 | + // for situation where database info is not set in the client. |
| 354 | + public void testQueryNoDatabaseWithChunking() throws Exception { |
| 355 | + if (this.influxDB.version().startsWith("0.") || this.influxDB.version().startsWith("1.0")) { |
| 356 | + // do not test version 0.13 and 1.0 |
| 357 | + return; |
| 358 | + } |
| 359 | + String dbName = "write_unittest_" + System.currentTimeMillis(); |
| 360 | + this.influxDB.query(new Query("CREATE DATABASE " + dbName)); |
| 361 | + this.influxDB.setDatabase(dbName); // Set database -> no need to use it as query parameter. |
| 362 | + String rp = TestUtils.defaultRetentionPolicy(this.influxDB.version()); |
| 363 | + BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy(rp).build(); |
| 364 | + Point point1 = Point.measurement("disk").tag("atag", "a").addField("used", 60L).addField("free", 1L).build(); |
| 365 | + Point point2 = Point.measurement("disk").tag("atag", "b").addField("used", 70L).addField("free", 2L).build(); |
| 366 | + Point point3 = Point.measurement("disk").tag("atag", "c").addField("used", 80L).addField("free", 3L).build(); |
| 367 | + batchPoints.point(point1); |
| 368 | + batchPoints.point(point2); |
| 369 | + batchPoints.point(point3); |
| 370 | + this.influxDB.write(batchPoints); |
| 371 | + |
| 372 | + Thread.sleep(2000); |
| 373 | + final BlockingQueue<QueryResult> queue = new LinkedBlockingQueue<>(); |
| 374 | + Query query = new Query("SELECT * FROM disk"); |
| 375 | + this.influxDB.query(query, 2, queue::add); |
| 376 | + |
| 377 | + Thread.sleep(2000); |
| 378 | + this.influxDB.query(new Query("DROP DATABASE " + dbName)); |
| 379 | + |
| 380 | + QueryResult result = queue.poll(20, TimeUnit.SECONDS); |
| 381 | + Assertions.assertNotNull(result); |
| 382 | + System.out.println(result); |
| 383 | + Assertions.assertEquals(2, result.getResults().get(0).getSeries().get(0).getValues().size()); |
| 384 | + |
| 385 | + result = queue.poll(20, TimeUnit.SECONDS); |
| 386 | + Assertions.assertNotNull(result); |
| 387 | + System.out.println(result); |
| 388 | + Assertions.assertEquals(1, result.getResults().get(0).getSeries().get(0).getValues().size()); |
| 389 | + |
| 390 | + result = queue.poll(20, TimeUnit.SECONDS); |
| 391 | + Assertions.assertNotNull(result); |
| 392 | + System.out.println(result); |
| 393 | + Assertions.assertEquals("DONE", result.getError()); |
| 394 | + } |
| 395 | + |
| 396 | + /** |
| 397 | + * Tests that database information is used from {@link InfluxDB} when database information |
| 398 | + * is not present in query and when different time format is requested from db. |
| 399 | + */ |
| 400 | + @Test |
| 401 | + public void testQueryNoDatabaseWithTimeFormat() { |
| 402 | + String dbName = "write_unittest_" + System.currentTimeMillis(); |
| 403 | + long time = 1559027876L; |
| 404 | + this.influxDB.query(new Query("CREATE DATABASE " + dbName)); |
| 405 | + this.influxDB.setDatabase(dbName); // Set db here, then after write query should pass. |
| 406 | + this.influxDB.write(Point |
| 407 | + .measurement("cpu") |
| 408 | + .tag("atag", "test") |
| 409 | + .addField("idle", 90L) |
| 410 | + .addField("usertime", 9L) |
| 411 | + .addField("system", 1L) |
| 412 | + .time(time, TimeUnit.MILLISECONDS) // Set time. |
| 413 | + .build()); |
| 414 | + |
| 415 | + Query query = new Query("SELECT * FROM cpu GROUP BY *"); |
| 416 | + |
| 417 | + // Test milliseconds |
| 418 | + QueryResult result = this.influxDB.query(query, TimeUnit.MILLISECONDS); |
| 419 | + Series series = result.getResults().get(0).getSeries().get(0); |
| 420 | + Assertions.assertEquals( |
| 421 | + ((Number)series.getValues().get(0).get(series.getColumns().indexOf("time"))).longValue() , |
| 422 | + time |
| 423 | + ); |
| 424 | + |
| 425 | + // Test nanoseconds |
| 426 | + result = this.influxDB.query(query, TimeUnit.NANOSECONDS); |
| 427 | + series = result.getResults().get(0).getSeries().get(0); |
| 428 | + Assertions.assertEquals( |
| 429 | + ((Number)series.getValues().get(0).get(series.getColumns().indexOf("time"))).longValue(), |
| 430 | + TimeUnit.NANOSECONDS.convert(time, TimeUnit.MILLISECONDS) |
| 431 | + ); |
| 432 | + |
| 433 | + // Test seconds |
| 434 | + result = this.influxDB.query(query, TimeUnit.SECONDS); |
| 435 | + series = result.getResults().get(0).getSeries().get(0); |
| 436 | + Assertions.assertEquals( |
| 437 | + ((Number)series.getValues().get(0).get(series.getColumns().indexOf("time"))).longValue(), |
| 438 | + TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS) |
| 439 | + |
| 440 | + ); |
| 441 | + |
| 442 | + this.influxDB.query(new Query("DROP DATABASE " + dbName)); |
| 443 | + } |
| 444 | + |
288 | 445 | /**
|
289 | 446 | * Test the implementation of {@link InfluxDB#write(int, Point)}'s async support.
|
290 | 447 | */
|
|
0 commit comments