Skip to content

Commit

Permalink
Adds test for DataPoint.fromByteArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAtticus committed Aug 4, 2015
1 parent 67a0ea3 commit 0b22d58
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/com/iobeam/api/resource/DataPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,14 @@ public static DataPoint fromByteArray(byte[] array) {
return new DataPoint(timestamp, buf.getLong());
} else if (type == TYPE_DOUBLE) {
return new DataPoint(timestamp, buf.getDouble());
} else {
} else if (type == TYPE_STRING) {
int len = buf.getInt();
byte[] bytes = new byte[len];
buf.get(bytes, 0, len);
return new DataPoint(timestamp, new String(bytes));
} else {
// TODO - Throw exception if invalid format.
return null;
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/iobeam/api/resource/DataPointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ public void testToJson() throws Exception {
assertEquals("11.2", json.getString("value"));
}

@Test
public void testFromByteArray() throws Exception {
long now = System.currentTimeMillis();

DataPoint dataPointInt = new DataPoint(now, 10);
ByteBuffer buf = ByteBuffer.wrap(dataPointInt.toByteArray());

DataPoint reverse = DataPoint.fromByteArray(buf.array());
assertEquals(dataPointInt, reverse);

DataPoint dataPointDouble = new DataPoint(now, 10.5);
buf = ByteBuffer.wrap(dataPointDouble.toByteArray());
reverse = DataPoint.fromByteArray(buf.array());
assertEquals(dataPointDouble, reverse);

DataPoint dataPointString = new DataPoint(now, "11.2");
buf = ByteBuffer.wrap(dataPointString.toByteArray());
reverse = DataPoint.fromByteArray(buf.array());
assertEquals(dataPointString, reverse);
}

@Test
public void testToByteArray() throws Exception {
long now = System.currentTimeMillis();
Expand Down

0 comments on commit 0b22d58

Please sign in to comment.