Search in sources :

Example 21 with WriteRequest

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

the class WriteTest method cannot_write_security_resource.

@Test
public void cannot_write_security_resource() throws InterruptedException {
    // try to write unwritable resource like manufacturer on device
    String uri = "new.dest.server";
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(0, 0, 0, uri));
    // verify result
    assertEquals(ResponseCode.NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) Test(org.junit.Test)

Example 22 with WriteRequest

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

the class WriteTest method write_string_resource.

private void write_string_resource(ContentFormat format) throws InterruptedException {
    // write resource
    String expectedvalue = "stringvalue";
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, STRING_RESOURCE_ID, expectedvalue));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read resource to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, STRING_RESOURCE_ID));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertEquals(expectedvalue, resource.getValue());
}
Also used : 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 23 with WriteRequest

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

the class WriteTest method cannot_write_non_writable_resource.

@Test
public void cannot_write_non_writable_resource() throws InterruptedException {
    // try to write unwritable resource like manufacturer on device
    String manufacturer = "new manufacturer";
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 0, manufacturer));
    // verify result
    assertEquals(ResponseCode.METHOD_NOT_ALLOWED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) Test(org.junit.Test)

Example 24 with WriteRequest

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

the class CoapRequestBuilderTest method build_write_request.

@Test
public void build_write_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    WriteRequest request = new WriteRequest(Mode.UPDATE, 3, 0, LwM2mSingleResource.newStringResource(4, "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(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());
    assertEquals("value", TlvDecoder.decodeString(tlvs[0].getValue()));
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) 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 25 with WriteRequest

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

the class CoapRequestBuilderTest method build_write_request_replace.

@Test
public void build_write_request_replace() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    WriteRequest request = new WriteRequest(3, 0, 14, "value");
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.PUT, coapRequest.getCode());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) 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) Test(org.junit.Test)

Aggregations

WriteRequest (org.eclipse.leshan.core.request.WriteRequest)27 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)19 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)18 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)14 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)13 Test (org.junit.Test)13 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)7 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)7 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)6 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)5 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)5 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 Observation (org.eclipse.leshan.core.observation.Observation)3 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)3 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3