Search in sources :

Example 6 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class CoapRequestBuilderTest method build_create_request__with_instance_id.

@Test
public void build_create_request__with_instance_id() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    CreateRequest request = new CreateRequest(12, new LwM2mObjectInstance(26, LwM2mSingleResource.newStringResource(0, "value")));
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, 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/12", coapRequest.getURI());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of instance TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.OBJECT_INSTANCE, tlvs[0].getType());
    assertEquals(26, tlvs[0].getIdentifier());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Registration(org.eclipse.leshan.server.registration.Registration) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) 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) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 7 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class CoapRequestBuilderTest method build_observe_request.

@Test
public void build_observe_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    ObserveRequest request = new ObserveRequest(12, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals(0, coapRequest.getOptions().getObserve().intValue());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12/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) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) Test(org.junit.Test)

Example 8 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class CoapRequestBuilderTest method build_delete_request.

@Test
public void build_delete_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    DeleteRequest request = new DeleteRequest(12, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.DELETE, 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/12/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) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) Test(org.junit.Test)

Example 9 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class CoapRequestBuilderTest method build_create_request__without_instance_id.

@Test
public void build_create_request__without_instance_id() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    CreateRequest request = new CreateRequest(12, LwM2mSingleResource.newStringResource(0, "value"));
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, 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/12", coapRequest.getURI());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of resources TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.RESOURCE_VALUE, tlvs[0].getType());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) 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) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 10 with Request

use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.

the class RegistrationTest method register_with_invalid_request.

@Test
public void register_with_invalid_request() throws InterruptedException, IOException {
    // Check registration
    helper.assertClientNotRegisterered();
    // create a register request without the list of supported object
    Request coapRequest = new Request(Code.POST);
    coapRequest.setDestinationContext(new AddressEndpointContext(helper.server.getUnsecuredAddress()));
    coapRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode());
    coapRequest.getOptions().addUriPath("rd");
    coapRequest.getOptions().addUriQuery("ep=" + helper.currentEndpointIdentifier);
    // send request
    CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
    builder.setInetSocketAddress(new InetSocketAddress(0));
    CoapEndpoint coapEndpoint = builder.build();
    coapEndpoint.start();
    coapEndpoint.sendRequest(coapRequest);
    // check response
    Response response = coapRequest.waitForResponse(1000);
    assertEquals(response.getCode(), org.eclipse.californium.core.coap.CoAP.ResponseCode.BAD_REQUEST);
    coapEndpoint.stop();
}
Also used : Response(org.eclipse.californium.core.coap.Response) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse) InetSocketAddress(java.net.InetSocketAddress) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) Request(org.eclipse.californium.core.coap.Request) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Test(org.junit.Test)

Aggregations

Request (org.eclipse.californium.core.coap.Request)26 Registration (org.eclipse.leshan.server.registration.Registration)14 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)13 Test (org.junit.Test)13 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)12 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)12 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)12 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)12 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)12 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)12 Response (org.eclipse.californium.core.coap.Response)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)6 DownlinkRequest (org.eclipse.leshan.core.request.DownlinkRequest)5 MessageObserver (org.eclipse.californium.core.coap.MessageObserver)3 AsyncRequestObserver (org.eclipse.leshan.core.californium.AsyncRequestObserver)3 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)3 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)3 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)3 UplinkRequest (org.eclipse.leshan.core.request.UplinkRequest)3