Skip to content

Commit 45f6cf4

Browse files
authored
fix: consistency between addField from POJO and regular addField (#654)
1 parent 37b93e9 commit 45f6cf4

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ public Builder addField(final String field, final double value) {
178178
return this;
179179
}
180180

181+
public Builder addField(final String field, final int value) {
182+
fields.put(field, value);
183+
return this;
184+
}
185+
186+
public Builder addField(final String field, final float value) {
187+
fields.put(field, value);
188+
return this;
189+
}
190+
191+
public Builder addField(final String field, final short value) {
192+
fields.put(field, value);
193+
return this;
194+
}
195+
181196
public Builder addField(final String field, final Number value) {
182197
fields.put(field, value);
183198
return this;

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.influxdb.dto;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
45

56
import java.lang.reflect.Field;
67
import java.math.BigDecimal;
@@ -15,14 +16,17 @@
1516
import java.util.concurrent.atomic.AtomicLong;
1617

1718
import org.influxdb.BuilderException;
19+
import org.influxdb.InfluxDB;
1820
import org.influxdb.annotation.Column;
1921
import org.influxdb.annotation.Measurement;
2022
import org.influxdb.annotation.TimeColumn;
23+
import org.influxdb.impl.InfluxDBImpl;
2124
import org.junit.Assert;
2225
import org.junit.jupiter.api.Assertions;
2326
import org.junit.jupiter.api.Test;
2427
import org.junit.platform.runner.JUnitPlatform;
2528
import org.junit.runner.RunWith;
29+
import org.mockito.Mockito;
2630

2731
/**
2832
* Test for the Point DTO.
@@ -615,6 +619,32 @@ public void testAddFieldsFromPOJOWithData() throws NoSuchFieldException, Illegal
615619
Assertions.assertEquals(pojo.uuid, p.getTags().get("uuid"));
616620
}
617621

622+
@Test
623+
public void testAddFieldsFromPOJOConsistentWithAddField() {
624+
PojoNumberPrimitiveTypes pojo = new PojoNumberPrimitiveTypes();
625+
pojo.shortType = 128;
626+
pojo.intType = 1_048_576;
627+
pojo.longType = 1_073_741_824;
628+
pojo.floatType = 246.8f;
629+
pojo.doubleType = 123.4;
630+
631+
Point pojo_point = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();
632+
633+
InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
634+
mockInfluxDB.write(pojo_point);
635+
636+
Point expected = Point.measurement("PojoNumberPrimitiveTypes")
637+
.addField("shortType", pojo.shortType)
638+
.addField("intType", pojo.intType)
639+
.addField("longType", pojo.longType)
640+
.addField("floatType", pojo.floatType)
641+
.addField("doubleType", pojo.doubleType)
642+
.build();
643+
644+
Assert.assertEquals(pojo_point.lineProtocol(), expected.lineProtocol());
645+
Mockito.verify(mockInfluxDB).write(expected);
646+
}
647+
618648
@Test
619649
public void testAddFieldsFromPOJOWithPublicAttributes() {
620650

@@ -858,4 +888,23 @@ static class SubClassMeasurement extends SuperMeasurement {
858888
@Column(name = "subClassField")
859889
String subValue;
860890
}
891+
892+
@Measurement(name = "PojoNumberPrimitiveTypes")
893+
static class PojoNumberPrimitiveTypes
894+
{
895+
@Column(name = "shortType")
896+
public short shortType;
897+
898+
@Column(name = "intType")
899+
public int intType;
900+
901+
@Column(name = "longType")
902+
public long longType;
903+
904+
@Column(name = "floatType")
905+
public float floatType;
906+
907+
@Column(name = "doubleType")
908+
public double doubleType;
909+
}
861910
}

0 commit comments

Comments
 (0)