Search in sources :

Example 6 with ReadRequest

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

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

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());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 9 with ReadRequest

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);
}
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 10 with ReadRequest

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

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