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