use of org.eclipse.leshan.core.response.ReadResponse 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.response.ReadResponse 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.response.ReadResponse 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.response.ReadResponse 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);
}
use of org.eclipse.leshan.core.response.ReadResponse in project leshan by eclipse.
the class ObserveTest method can_observe_object.
@Test
public void can_observe_object() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// write device timezone
LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
// verify result
listener.waitForNotification(2000);
assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
assertTrue(listener.receivedNotify().get());
assertTrue(listener.getResponse().getContent() instanceof LwM2mObject);
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
// try to read the object for comparing
ReadResponse readResp = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
assertEquals(readResp.getContent(), listener.getResponse().getContent());
}
Aggregations