Search in sources :

Example 11 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest 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());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 12 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.

the class QueueModeTest method sleeping_if_timeout.

@Test
public void sleeping_if_timeout() throws InterruptedException {
    // Check client is not registered
    queueModeHelper.assertClientNotRegisterered();
    // Start it and wait for registration
    queueModeHelper.client.start();
    queueModeHelper.waitForRegistration(1);
    // Check client is well registered and awake
    queueModeHelper.assertClientRegisterered();
    queueModeHelper.ensureClientAwake();
    // Stop the client to ensure that TimeOut exception is thrown
    queueModeHelper.client.stop(false);
    // Send a response with very short timeout
    ReadResponse response = queueModeHelper.server.send(queueModeHelper.getCurrentRegistration(), new ReadRequest(3, 0, 1), 1);
    // Check that a timeout occurs
    queueModeHelper.ensureTimeoutException(response);
    // Check that the client is sleeping
    queueModeHelper.ensureClientSleeping();
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 13 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.

the class ReadTest method can_read_object.

@Test
public void can_read_object() throws InterruptedException {
    // read device object
    ReadResponse response = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    LwM2mObject object = (LwM2mObject) response.getContent();
    assertEquals(3, object.getId());
    LwM2mObjectInstance instance = object.getInstance(0);
    assertEquals(0, instance.getId());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 14 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.

the class ReadTest method cannot_read_non_existent_object.

@Test
public void cannot_read_non_existent_object() throws InterruptedException {
    // read object "50"
    ReadResponse response = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(50));
    // verify result
    assertEquals(NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 15 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.

the class ReadTest method cannot_read_non_existent_resource.

@Test
public void cannot_read_non_existent_resource() throws InterruptedException {
    // read device 50 resource
    ReadResponse response = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 50));
    // verify result
    assertEquals(NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

ReadRequest (org.eclipse.leshan.core.request.ReadRequest)43 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)34 Test (org.junit.Test)30 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)19 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)14 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)12 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)7 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)7 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)5 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)5 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)5 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)4 TimeoutException (org.eclipse.leshan.core.request.exception.TimeoutException)4 JsonObject (com.eclipsesource.json.JsonObject)3 Request (org.eclipse.californium.core.coap.Request)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3