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));
}
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());
}
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();
}
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)));
}
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)));
}
Aggregations