use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class WriteTest method can_write_single_instance_objlnk_resource_in_text.
@Test
public void can_write_single_instance_objlnk_resource_in_text() throws InterruptedException {
// Write objlnk resource in TEXT format
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(ContentFormat.TEXT, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_SINGLE_INSTANCE_RESOURCE_ID, new ObjectLink(10245, 0)));
// Verify Write result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// Reading back the written OBJLNK value
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(ContentFormat.TEXT, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_SINGLE_INSTANCE_RESOURCE_ID));
LwM2mSingleResource resource = (LwM2mSingleResource) readResponse.getContent();
// verify read value
assertEquals(((ObjectLink) resource.getValue()).getObjectId(), 10245);
assertEquals(((ObjectLink) resource.getValue()).getObjectInstanceId(), 0);
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class WriteTest method can_write_object_instance.
public void can_write_object_instance(ContentFormat format) throws InterruptedException {
// write device timezone and offset
LwM2mResource utcOffset = LwM2mSingleResource.newStringResource(14, "+02");
LwM2mResource timeZone = LwM2mSingleResource.newStringResource(15, "Europe/Paris");
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, format, 3, 0, utcOffset, timeZone));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read the timezone to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0));
LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
assertEquals(utcOffset, instance.getResource(14));
assertEquals(timeZone, instance.getResource(15));
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_read_request.
@Test
public void build_read_request() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
ReadRequest request = new ReadRequest(3, 0);
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.GET, coapRequest.getCode());
assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class FailingTest method sync_send_with_acknowleged_request_without_response.
@Test
public void sync_send_with_acknowleged_request_without_response() throws Exception {
// Register client
LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress());
client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, Link.parse("</1>,</2>,</3>".getBytes()), null));
client.expectResponse().go();
helper.waitForRegistration(1);
// Send read
Future<ReadResponse> future = Executors.newSingleThreadExecutor().submit(new Callable<ReadResponse>() {
@Override
public ReadResponse call() throws Exception {
// send a request with 3 seconds timeout
return helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000);
}
});
// Acknowledge the response
client.expectRequest().storeMID("R").go();
client.sendEmpty(Type.ACK).loadMID("R").go();
// Request should timedout in ~3s as we send the ACK
Thread.sleep(1500);
Assert.assertFalse("we should still wait for response", future.isDone());
ReadResponse response = future.get(2000, TimeUnit.MILLISECONDS);
Assert.assertNull("we should timeout", response);
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class FailingTest method async_send_with_acknowleged_request_without_response.
@Test
public void async_send_with_acknowleged_request_without_response() throws Exception {
// register client
LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress());
client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, Link.parse("</1>,</2>,</3>".getBytes()), null));
client.expectResponse().go();
helper.waitForRegistration(1);
// send read
Callback<ReadResponse> callback = new Callback<ReadResponse>();
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000l, callback, callback);
// Acknowledge the response
client.expectRequest().storeMID("R").go();
client.sendEmpty(Type.ACK).loadMID("R").go();
// Request should timedout in ~3s as we send a ack
Thread.sleep(1500);
Assert.assertTrue("we should still wait for response", callback.getException() == null);
callback.waitForResponse(2000);
Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException);
}
Aggregations