use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeDecoderTest method json_timestamped_Object.
@Test
public void json_timestamped_Object() throws CodecException {
// json content for instance 0 of device object
StringBuilder b = new StringBuilder();
b.append("{\"e\":[");
b.append("{\"n\":\"0/1\",\"v\":22.9,\"t\":-30},");
b.append("{\"n\":\"0/1\",\"v\":22.4,\"t\":-5},");
b.append("{\"n\":\"0/0\",\"sv\":\"a string\",\"t\":-5},");
b.append("{\"n\":\"1/1\",\"v\":23,\"t\":-5},");
b.append("{\"n\":\"0/1\",\"v\":24.1,\"t\":-50}],");
b.append("\"bt\":25462634}");
List<TimestampedLwM2mNode> timestampedResources = decoder.decodeTimestampedData(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(1024), model);
assertEquals(3, timestampedResources.size());
assertEquals(Long.valueOf(25462634L - 5), timestampedResources.get(0).getTimestamp());
assertEquals(22.4d, ((LwM2mObject) timestampedResources.get(0).getNode()).getInstance(0).getResource(1).getValue());
assertEquals("a string", ((LwM2mObject) timestampedResources.get(0).getNode()).getInstance(0).getResource(0).getValue());
assertEquals(23.0d, ((LwM2mObject) timestampedResources.get(0).getNode()).getInstance(1).getResource(1).getValue());
assertEquals(Long.valueOf(25462634L - 30), timestampedResources.get(1).getTimestamp());
assertEquals(22.9d, ((LwM2mObject) timestampedResources.get(1).getNode()).getInstance(0).getResource(1).getValue());
assertEquals(Long.valueOf(25462634 - 50), timestampedResources.get(2).getTimestamp());
assertEquals(24.1d, ((LwM2mObject) timestampedResources.get(2).getNode()).getInstance(0).getResource(1).getValue());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeDecoderTest method text_battery_resource.
@Test
public void text_battery_resource() throws CodecException {
LwM2mSingleResource resource = (LwM2mSingleResource) decoder.decode("100".getBytes(StandardCharsets.UTF_8), ContentFormat.TEXT, new LwM2mPath(3, 0, 9), model);
assertEquals(9, resource.getId());
assertFalse(resource.isMultiInstances());
assertEquals(Type.INTEGER, resource.getType());
assertEquals(100, ((Number) resource.getValue()).intValue());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeDecoderTest method tlv_empty_instance.
@Test
public void tlv_empty_instance() {
byte[] content = TlvEncoder.encode(new Tlv[] {}).array();
LwM2mObjectInstance instance = (LwM2mObjectInstance) decoder.decode(content, ContentFormat.TLV, new LwM2mPath(2, 0), model);
assertNotNull(instance);
assertEquals(0, instance.getId());
assertTrue(instance.getResources().isEmpty());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeDecoderTest method tlv_empty_multi_resource.
@Test
public void tlv_empty_multi_resource() {
byte[] content = TlvEncoder.encode(new Tlv[] {}).array();
LwM2mResource resource = (LwM2mResource) decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 6), model);
assertNotNull(resource);
assertTrue(resource instanceof LwM2mMultipleResource);
assertEquals(6, resource.getId());
assertTrue(resource.getValues().isEmpty());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeDecoderTest method json_empty_instance.
@Test
public void json_empty_instance() {
// Completely empty
StringBuilder b = new StringBuilder();
b.append("{}");
LwM2mObjectInstance instance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(2, 0), model);
assertNotNull(instance);
assertEquals(0, instance.getId());
assertTrue(instance.getResources().isEmpty());
// with empty resource list
b = new StringBuilder();
b.append("{\"e\":[]}");
instance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(2, 0), model);
assertNotNull(instance);
assertEquals(0, instance.getId());
assertTrue(instance.getResources().isEmpty());
// with empty resources list and base name
b = new StringBuilder();
b.append("{\"bn\":\"2/0\", \"e\":[]}");
instance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(2, 0), model);
assertNotNull(instance);
assertEquals(0, instance.getId());
assertTrue(instance.getResources().isEmpty());
}
Aggregations