Search in sources :

Example 16 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class LwM2mNodeDecoderTest method tlv_power_source__array_values.

@Test
public void tlv_power_source__array_values() throws CodecException {
    byte[] content = new byte[] { 65, 0, 1, 65, 1, 5 };
    LwM2mResource resource = (LwM2mResource) decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 6), model);
    assertEquals(6, resource.getId());
    assertEquals(2, resource.getValues().size());
    assertEquals(1L, resource.getValue(0));
    assertEquals(5L, resource.getValue(1));
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Test(org.junit.Test)

Example 17 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class LwM2mNodeDecoderTest method json_empty_multi_resource.

@Test
public void json_empty_multi_resource() {
    // Completely empty
    StringBuilder b = new StringBuilder();
    b.append("{}");
    LwM2mResource resource = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(3, 0, 6), model);
    assertNotNull(resource);
    assertTrue(resource instanceof LwM2mMultipleResource);
    assertEquals(6, resource.getId());
    assertTrue(resource.getValues().isEmpty());
    // with empty resource list
    b = new StringBuilder();
    b.append("{\"e\":[]}");
    resource = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(3, 0, 6), model);
    assertNotNull(resource);
    assertTrue(resource instanceof LwM2mMultipleResource);
    assertEquals(6, resource.getId());
    assertTrue(resource.getValues().isEmpty());
    // with empty resources list and base name
    b = new StringBuilder();
    b.append("{\"bn\":\"3/0/6\", \"e\":[]}");
    resource = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, 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) Test(org.junit.Test)

Example 18 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class ObjectEnabler method doWrite.

@Override
protected BootstrapWriteResponse doWrite(ServerIdentity identity, BootstrapWriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Object case
    if (path.isObject()) {
        for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) {
            LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId());
            if (instanceEnabler == null) {
                doCreate(new CreateRequest(path.getObjectId(), instanceNode));
            } else {
                doWrite(identity, new WriteRequest(Mode.REPLACE, path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
            }
        }
        return BootstrapWriteResponse.success();
    }
    // Manage Instance case
    if (path.isObjectInstance()) {
        LwM2mObjectInstance instanceNode = (LwM2mObjectInstance) request.getNode();
        LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
        if (instanceEnabler == null) {
            doCreate(new CreateRequest(path.getObjectId(), instanceNode));
        } else {
            doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
        }
        return BootstrapWriteResponse.success();
    }
    // Manage resource case
    LwM2mResource resource = (LwM2mResource) request.getNode();
    LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
    if (instanceEnabler == null) {
        doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource)));
    } else {
        instanceEnabler.write(path.getResourceId(), resource);
    }
    return BootstrapWriteResponse.success();
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 19 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class CreateTest method cannot_create_mandatory_single_object.

@Test
public void cannot_create_mandatory_single_object() throws InterruptedException {
    // try to create another instance of device object
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(3, new LwM2mResource[] { LwM2mSingleResource.newIntegerResource(3, 123) }));
    // verify result
    assertEquals(ResponseCode.METHOD_NOT_ALLOWED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Test(org.junit.Test)

Example 20 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class CreateTest method cannot_create_instance_without_all_required_resources.

// TODO not sure all the writable mandatory resource should be present
// E.g. for softwareUpdate (object 9) packageURI and package are writable resource mandatory
// but you will not make a create with this two resource.
@Ignore
@Test
public void cannot_create_instance_without_all_required_resources() throws InterruptedException {
    // create ACL instance
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, new LwM2mResource[0]));
    // verify result
    assertEquals(ResponseCode.BAD_REQUEST, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // try to read to check if the instance is not created
    // client registration
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(2, 0));
    assertEquals(ResponseCode.NOT_FOUND, readResponse.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)33 Test (org.junit.Test)15 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)14 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)10 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)9 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 JsonObject (com.eclipsesource.json.JsonObject)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 JsonArray (com.eclipsesource.json.JsonArray)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonValue (com.eclipsesource.json.JsonValue)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2