Search in sources :

Example 6 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.

the class ResponseSerDes method jSerialize.

public static JsonObject jSerialize(LwM2mResponse r) {
    final JsonObject o = Json.object();
    o.add("code", r.getCode().toString());
    if (r.isFailure()) {
        o.add("errorMessage", r.getErrorMessage());
        return o;
    }
    if (r instanceof ReadResponse) {
        o.add("kind", "read");
        o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
    } else if (r instanceof ObserveResponse) {
        o.add("kind", "observe");
        o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
    } else if (r instanceof DiscoverResponse) {
        o.add("kind", "discover");
        o.add("objectLinks", Link.serialize(((DiscoverResponse) r).getObjectLinks()));
    } else if (r instanceof DeleteResponse) {
        o.add("kind", "delete");
    } else if (r instanceof ExecuteResponse) {
        o.add("kind", "execute");
    } else if (r instanceof WriteResponse) {
        o.add("kind", "write");
    } else if (r instanceof WriteAttributesResponse) {
        o.add("kind", "writeAttributes");
    } else if (r instanceof CreateResponse) {
        o.add("kind", "create");
        o.add("location", ((CreateResponse) r).getLocation());
    }
    return o;
}
Also used : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Example 7 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.

the class ResponseSerDesTest method ser_and_des_discover_response.

@Test
public void ser_and_des_discover_response() throws Exception {
    Link[] objs = new Link[2];
    Map<String, Object> att = new HashMap<>();
    att.put("ts", 12);
    att.put("rt", "test");
    objs[0] = new Link("/0/1024/2", att);
    objs[1] = new Link("/0/2");
    DiscoverResponse dr = DiscoverResponse.success(objs);
    JsonObject obj = ResponseSerDes.jSerialize(dr);
    LwM2mResponse dr2 = ResponseSerDes.deserialize(obj);
    assertEquals(dr.toString(), dr2.toString());
}
Also used : HashMap(java.util.HashMap) JsonObject(com.eclipsesource.json.JsonObject) JsonObject(com.eclipsesource.json.JsonObject) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Test(org.junit.Test)

Example 8 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.

the class DiscoverTest method can_discover_object.

@Test
public void can_discover_object() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(2));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    Link[] payload = response.getObjectLinks();
    assertArrayEquals(Link.parse("</2>, </2/0/0>, </2/0/1>, </2/0/2>, </2/0/3>".getBytes()), payload);
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 9 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.

the class DiscoverTest method can_discover_resource.

@Test
public void can_discover_resource() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 0, 0));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    Link[] payload = response.getObjectLinks();
    assertArrayEquals(Link.parse("</3/0/0>".getBytes()), payload);
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 10 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.

the class DiscoverTest method cant_discover_resource_of_non_existent_object.

@Test
public void cant_discover_resource_of_non_existent_object() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(4, 0, 0));
    // verify result
    assertEquals(NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Test(org.junit.Test)

Aggregations

DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)15 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)11 Test (org.junit.Test)10 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 Link (org.eclipse.leshan.Link)4 JsonObject (com.eclipsesource.json.JsonObject)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)2 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)2 DeleteResponse (org.eclipse.leshan.core.response.DeleteResponse)2 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)2 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)2 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ResponseCode (org.eclipse.leshan.ResponseCode)1 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)1