Skip to content

Commit

Permalink
fix bug: mapping from msgpack response to int, Integer, long, Long ty…
Browse files Browse the repository at this point in the history
…pes works fine
  • Loading branch information
chabapok committed Apr 10, 2020
1 parent b3367fd commit 059089b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/influxdb/impl/InfluxDBResultMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,11 @@ <T> boolean fieldValueForPrimitivesModified(final Class<?> fieldType, final Fiel
return true;
}
if (long.class.isAssignableFrom(fieldType)) {
field.setLong(object, ((Double) value).longValue());
field.setLong(object, ((Number) value).longValue());
return true;
}
if (int.class.isAssignableFrom(fieldType)) {
field.setInt(object, ((Double) value).intValue());
field.setInt(object, ((Number) value).intValue());
return true;
}
if (boolean.class.isAssignableFrom(fieldType)) {
Expand All @@ -382,11 +382,11 @@ <T> boolean fieldValueForPrimitiveWrappersModified(final Class<?> fieldType, fin
return true;
}
if (Long.class.isAssignableFrom(fieldType)) {
field.set(object, Long.valueOf(((Double) value).longValue()));
field.set(object, ((Number) value).longValue());
return true;
}
if (Integer.class.isAssignableFrom(fieldType)) {
field.set(object, Integer.valueOf(((Double) value).intValue()));
field.set(object, ((Number) value).intValue());
return true;
}
if (Boolean.class.isAssignableFrom(fieldType)) {
Expand Down
60 changes: 58 additions & 2 deletions src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void testParseSeriesAs_testNonNullAndValidValues() {
Double now = Long.valueOf(System.currentTimeMillis()).doubleValue();
String uuidAsString = UUID.randomUUID().toString();

// InfluxDB client returns any number as Double.
// InfluxDB client returns any number as Double. (with JSON response format, but not with msgpack)
// See https://github.com/influxdata/influxdb-java/issues/153#issuecomment-259681987
// for more information.
List<Object> seriesResult = Arrays.asList(now, uuidAsString,
Expand Down Expand Up @@ -201,6 +201,62 @@ Double asDouble(Object obj) {
return (Double) obj;
}

@Test
public void testParseSeriesAs_testNonNullAndValidValues_msgpack() {
// Given...
mapper.cacheMeasurementClass(MyCustomMeasurement.class);

List<String> columnList = Arrays.asList("time", "uuid",
"doubleObject", "longObject", "integerObject",
"doublePrimitive", "longPrimitive", "integerPrimitive",
"booleanObject", "booleanPrimitive");

// InfluxDB client returns the time representation as Double.
Double now = Long.valueOf(System.currentTimeMillis()).doubleValue();
String uuidAsString = UUID.randomUUID().toString();

// InfluxDB client returns any number as Double. (with JSON response format, but not with msgpack)
// See https://github.com/influxdata/influxdb-java/issues/153#issuecomment-259681987
// for more information.
List<Object> seriesResult = Arrays.asList(now, uuidAsString,
new Double("1.01"), Long.valueOf(2), Integer.valueOf(3),
new Double("1.01"), Long.valueOf(4), Integer.valueOf(5),
Boolean.FALSE, Boolean.TRUE);

QueryResult.Series series = new QueryResult.Series();
series.setColumns(columnList);
series.setValues(Arrays.asList(seriesResult));

//When...
List<MyCustomMeasurement> result = new LinkedList<>();
mapper.parseSeriesAs(series, MyCustomMeasurement.class, result);

//Then...
MyCustomMeasurement myObject = result.get(0);
Assertions.assertEquals(now.longValue(), myObject.time.toEpochMilli(), "field 'time' does not match");
Assertions.assertEquals(uuidAsString, myObject.uuid, "field 'uuid' does not match");

Assertions.assertEquals(asDouble(seriesResult.get(2)), myObject.doubleObject, "field 'doubleObject' does not match");
Assertions.assertEquals(Long.valueOf(2), myObject.longObject, "field 'longObject' does not match");
Assertions.assertEquals(Integer.valueOf(3), myObject.integerObject, "field 'integerObject' does not match");

Assertions.assertTrue(
Double.compare(asDouble(seriesResult.get(5)).doubleValue(), myObject.doublePrimitive) == 0,
"field 'doublePrimitive' does not match");

Assertions.assertEquals( myObject.longPrimitive, 4, "field 'longPrimitive' does not match");
Assertions.assertEquals( myObject.integerPrimitive, 5, "field 'integerPrimitive' does not match");

Assertions.assertEquals(
Boolean.valueOf(String.valueOf(seriesResult.get(8))), myObject.booleanObject,
"field 'booleanObject' does not match");

Assertions.assertEquals(
Boolean.valueOf(String.valueOf(seriesResult.get(9))).booleanValue(), myObject.booleanPrimitive,
"field 'booleanPrimitive' does not match");
}


@Test
public void testFieldValueModified_DateAsISO8601() {
// Given...
Expand Down Expand Up @@ -638,4 +694,4 @@ public String toString() {
+ ", median=" + median + ", min=" + min + ", max=" + max + "]";
}
}
}
}

0 comments on commit 059089b

Please sign in to comment.