Skip to content

Commit cf7a2da

Browse files
committed
add unittest for timecolumn,add readme & changelog
1 parent 8d4a853 commit cf7a2da

File tree

5 files changed

+123
-9
lines changed

5 files changed

+123
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.16
4+
5+
### Fixes
6+
7+
- Add new annotation called TimeColumn for timestamp field in POJO bean, this can set Point time and precision field correctly, also avoid UnableToParseException when flush Point to influx.
8+
39
## 2.15 [2019-02-22]
410

511
### Fixes

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ public class Cpu {
231231
}
232232
```
233233

234-
2. Add @Measurement and @Column annotations:
234+
2. Add @Measurement,@TimeColumn and @Column annotations:
235235

236236
```Java
237237
@Measurement(name = "cpu")
238238
public class Cpu {
239+
@TimeColumn
239240
@Column(name = "time")
240241
private Instant time;
241242
@Column(name = "host", tag = true)
@@ -270,7 +271,7 @@ Having the same POJO class Cpu
270271
```java
271272
String dbName = "myTimeseries";
272273
String rpName = "aRetentionPolicy";
273-
// Cpu has annotations @Measurement and @Column
274+
// Cpu has annotations @Measurement,@TimeColumn and @Column
274275
Cpu cpu = new Cpu();
275276
// ... setting data
276277

src/main/java/org/influxdb/dto/Point.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Map.Entry;
1212
import java.util.Objects;
1313
import java.util.TreeMap;
14+
import java.util.Optional;
1415
import java.util.concurrent.TimeUnit;
1516
import org.influxdb.BuilderException;
1617
import org.influxdb.annotation.Column;
@@ -263,10 +264,11 @@ private void addFieldByAttribute(final Object pojo, final Field field, final Col
263264

264265
TimeColumn tc = field.getAnnotation(TimeColumn.class);
265266
if (tc != null && Instant.class.isAssignableFrom(field.getType())) {
266-
Instant instant = (Instant) fieldValue;
267-
TimeUnit timeUint = tc.timeUnit();
268-
this.time = TimeUnit.MILLISECONDS.convert(instant.toEpochMilli(), timeUint);
269-
this.precision = timeUint;
267+
Optional.ofNullable((Instant) fieldValue).ifPresent(instant -> {
268+
TimeUnit timeUint = tc.timeUnit();
269+
this.time = TimeUnit.MILLISECONDS.convert(instant.toEpochMilli(), timeUint);
270+
this.precision = timeUint;
271+
});
270272
return;
271273
}
272274

src/test/java/org/influxdb/dto/PointTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.influxdb.dto;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.lang.reflect.Field;
46
import java.math.BigDecimal;
57
import java.math.BigInteger;
68
import java.time.Instant;
@@ -14,6 +16,7 @@
1416
import org.influxdb.BuilderException;
1517
import org.influxdb.annotation.Column;
1618
import org.influxdb.annotation.Measurement;
19+
import org.influxdb.annotation.TimeColumn;
1720
import org.junit.jupiter.api.Assertions;
1821
import org.junit.jupiter.api.Test;
1922
import org.junit.platform.runner.JUnitPlatform;
@@ -511,7 +514,42 @@ public void testAddFieldsFromPOJOWithoutData() {
511514
}
512515

513516
@Test
514-
public void testAddFieldsFromPOJOWithData() {
517+
public void testAddFieldsFromPOJOWithTimeColumn() throws NoSuchFieldException, IllegalAccessException {
518+
TimeColumnPojo pojo = new TimeColumnPojo();
519+
pojo.time = Instant.now();
520+
pojo.booleanPrimitive = true;
521+
522+
Point p = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();
523+
Field timeField = p.getClass().getDeclaredField("time");
524+
Field precisionField = p.getClass().getDeclaredField("precision");
525+
timeField.setAccessible(true);
526+
precisionField.setAccessible(true);
527+
528+
Assertions.assertEquals(pojo.booleanPrimitive, p.getFields().get("booleanPrimitive"));
529+
Assertions.assertEquals(TimeUnit.MILLISECONDS, precisionField.get(p));
530+
Assertions.assertEquals(TimeUnit.MILLISECONDS.convert(pojo.time.toEpochMilli(),TimeUnit.MILLISECONDS), timeField.get(p));
531+
532+
pojo.time = null;
533+
}
534+
535+
@Test
536+
public void testAddFieldsFromPOJOWithTimeColumnNull() throws NoSuchFieldException, IllegalAccessException {
537+
TimeColumnPojo pojo = new TimeColumnPojo();
538+
pojo.booleanPrimitive = true;
539+
540+
Point p = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();
541+
Field timeField = p.getClass().getDeclaredField("time");
542+
Field precisionField = p.getClass().getDeclaredField("precision");
543+
timeField.setAccessible(true);
544+
precisionField.setAccessible(true);
545+
546+
Assertions.assertEquals(pojo.booleanPrimitive, p.getFields().get("booleanPrimitive"));
547+
548+
pojo.time = null;
549+
}
550+
551+
@Test
552+
public void testAddFieldsFromPOJOWithData() throws NoSuchFieldException, IllegalAccessException {
515553
Pojo pojo = new Pojo();
516554
pojo.booleanObject = true;
517555
pojo.booleanPrimitive = false;
@@ -594,6 +632,15 @@ public void setId(String id) {
594632
}
595633
}
596634

635+
@Measurement(name = "tcmeasurement")
636+
static class TimeColumnPojo {
637+
@Column(name = "booleanPrimitive")
638+
private boolean booleanPrimitive;
639+
640+
@TimeColumn
641+
@Column(name = "time")
642+
private Instant time;
643+
}
597644

598645
@Measurement(name = "mymeasurement")
599646
static class Pojo {

src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.influxdb.InfluxDBMapperException;
3535
import org.influxdb.annotation.Column;
3636
import org.influxdb.annotation.Measurement;
37+
import org.influxdb.annotation.TimeColumn;
3738
import org.influxdb.dto.QueryResult;
3839
import org.junit.jupiter.api.Assertions;
3940
import org.junit.jupiter.api.Test;
@@ -441,6 +442,38 @@ void testToPOJOInheritance() {
441442
Assertions.assertEquals(subValue, result.get(0).subValue);
442443
}
443444

445+
@Test
446+
public void testToPOJO_HasTimeColumn() {
447+
// Given...
448+
mapper.cacheMeasurementClass(HasTimeColumnMeasurement.class);
449+
450+
List<String> columnList = Arrays.asList("time");
451+
List<List<Object>> valuesList = Arrays.asList(
452+
Arrays.asList("2015-08-17T19:00:00-05:00"), // Chicago (UTC-5)
453+
Arrays.asList("2015-08-17T19:00:00.000000001-05:00"), // Chicago (UTC-5)
454+
Arrays.asList("2000-01-01T00:00:00-00:00"),
455+
Arrays.asList("2000-01-02T00:00:00+00:00")
456+
);
457+
458+
QueryResult.Series series = new QueryResult.Series();
459+
series.setColumns(columnList);
460+
series.setValues(valuesList);
461+
462+
// When...
463+
List<HasTimeColumnMeasurement> result = new LinkedList<>();
464+
mapper.parseSeriesAs(series, HasTimeColumnMeasurement.class, result);
465+
466+
// Then...
467+
Assertions.assertEquals(4, result.size(), "incorrect number of elemets");
468+
// Note: RFC3339 timestamp with TZ from InfluxDB are parsed into an Instant (UTC)
469+
Assertions.assertTrue(result.get(0).time.equals(Instant.parse("2015-08-18T00:00:00Z")));
470+
Assertions.assertTrue(result.get(1).time.equals(Instant.parse("2015-08-18T00:00:00.000000001Z")));
471+
// RFC3339 section 4.3 https://tools.ietf.org/html/rfc3339#section-4.3
472+
Assertions.assertTrue(result.get(2).time.equals(Instant.parse("2000-01-01T00:00:00Z")));
473+
Assertions.assertTrue(result.get(3).time.equals(Instant.parse("2000-01-02T00:00:00Z")));
474+
475+
}
476+
444477
@Test
445478
public void testToPOJO_ticket573() {
446479
// Given...
@@ -472,9 +505,35 @@ public void testToPOJO_ticket573() {
472505
Assertions.assertTrue(result.get(3).time.equals(Instant.parse("2000-01-02T00:00:00Z")));
473506
}
474507

508+
@Measurement(name = "HasTimeColumnMeasurement")
509+
static class HasTimeColumnMeasurement {
510+
@TimeColumn
511+
@Column(name = "time")
512+
private Instant time;
513+
514+
@Column(name = "value")
515+
private Integer value;
516+
517+
public Instant getTime() {
518+
return time;
519+
}
520+
521+
public void setTime(Instant time) {
522+
this.time = time;
523+
}
524+
525+
public Integer getValue() {
526+
return value;
527+
}
528+
529+
public void setValue(Integer value) {
530+
this.value = value;
531+
}
532+
}
533+
534+
475535
@Measurement(name = "CustomMeasurement")
476536
static class MyCustomMeasurement {
477-
478537
@Column(name = "time")
479538
private Instant time;
480539

@@ -555,7 +614,6 @@ static class MyPojoWithUnsupportedField {
555614
*/
556615
@Measurement(name = "tb_network")
557616
static class GroupByCarrierDeviceOS {
558-
559617
@Column(name = "time")
560618
private Instant time;
561619

0 commit comments

Comments
 (0)