Search in sources :

Example 6 with ReadResponse

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);
}
Also used : LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) ObjectLink(org.eclipse.leshan.core.node.ObjectLink) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 7 with ReadResponse

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));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 8 with ReadResponse

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);
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 9 with ReadResponse

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);
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) Test(org.junit.Test)

Example 10 with ReadResponse

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

Aggregations

ReadResponse (org.eclipse.leshan.core.response.ReadResponse)37 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)34 Test (org.junit.Test)25 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)14 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)14 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)11 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)7 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)5 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)5 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)4 TimeoutException (org.eclipse.leshan.core.request.exception.TimeoutException)4 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)4 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)3 JsonObject (com.eclipsesource.json.JsonObject)2 ArrayList (java.util.ArrayList)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 LwM2mSingleResource (org.eclipse.leshan.core.node.LwM2mSingleResource)2 Observation (org.eclipse.leshan.core.observation.Observation)2