Search in sources :

Example 56 with LwM2mPath

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());
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Example 57 with LwM2mPath

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());
}
Also used : LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Example 58 with LwM2mPath

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());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 59 with LwM2mPath

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());
}
Also used : LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 60 with LwM2mPath

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());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Aggregations

LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)70 Test (org.junit.Test)51 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 Observation (org.eclipse.leshan.core.observation.Observation)11 ArrayList (java.util.ArrayList)10 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)9 Tlv (org.eclipse.leshan.tlv.Tlv)8 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)7 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)6 CodecException (org.eclipse.leshan.core.node.codec.CodecException)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 HashMap (java.util.HashMap)4 Response (org.eclipse.californium.core.coap.Response)4 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4