use of org.eclipse.leshan.Link in project leshan by eclipse.
the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_explicit_empty_root_path.
@Test
public void encode_objectModel_to_linkObject_with_explicit_empty_root_path() {
ObjectModel locationModel = getObjectModel(6);
Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "/");
String strLinks = Link.serialize(links);
assertEquals("</6>, </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationSortObjectLinksTest method sort_link_object_on_get.
@Test
public void sort_link_object_on_get() throws UnknownHostException {
Link[] objs = new Link[3];
objs[0] = new Link("/0/1024/2");
objs[1] = new Link("/0/2");
objs[2] = null;
Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", Identity.unsecure(Inet4Address.getLocalHost(), 1), new InetSocketAddress(212)).objectLinks(objs);
Registration r = builder.build();
Link[] res = r.getSortedObjectLinks();
Assert.assertEquals(3, res.length);
Assert.assertNull(res[0]);
Assert.assertEquals("/0/2", res[1].getUrl());
Assert.assertEquals("/0/1024/2", res[2].getUrl());
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationUpdateSerDes method jSerialize.
public static JsonObject jSerialize(RegistrationUpdate u) {
JsonObject o = Json.object();
// mandatory fields
o.add("regId", u.getRegistrationId());
o.add("identity", IdentitySerDes.serialize(u.getIdentity()));
// optional fields
if (u.getLifeTimeInSec() != null)
o.add("lt", u.getLifeTimeInSec());
if (u.getSmsNumber() != null)
o.add("sms", u.getSmsNumber());
if (u.getBindingMode() != null)
o.add("bnd", u.getBindingMode().name());
if (u.getObjectLinks() != null) {
JsonArray links = new JsonArray();
for (Link l : u.getObjectLinks()) {
JsonObject ol = Json.object();
ol.add("url", l.getUrl());
JsonObject at = Json.object();
for (Map.Entry<String, Object> e : l.getAttributes().entrySet()) {
if (e.getValue() == null) {
at.add(e.getKey(), Json.NULL);
} else if (e.getValue() instanceof Integer) {
at.add(e.getKey(), (int) e.getValue());
} else {
at.add(e.getKey(), e.getValue().toString());
}
}
ol.add("at", at);
links.add(ol);
}
o.add("objLink", links);
}
if (u.getAdditionalAttributes() != null) {
JsonObject addAttr = Json.object();
for (Map.Entry<String, String> e : u.getAdditionalAttributes().entrySet()) {
addAttr.add(e.getKey(), e.getValue());
}
o.add("addAttr", addAttr);
}
return o;
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationUpdateSerDes method deserialize.
public static RegistrationUpdate deserialize(byte[] data) throws UnknownHostException {
JsonObject v = (JsonObject) Json.parse(new String(data));
// mandatory fields
String regId = v.getString("regId", null);
Identity identity = IdentitySerDes.deserialize(v.get("identity").asObject());
// optional fields
BindingMode b = null;
if (v.get("bnd") != null) {
b = BindingMode.valueOf(v.getString("bnd", null));
}
Long lifetime = null;
if (v.get("lt") != null) {
lifetime = v.getLong("lt", 0);
}
String sms = null;
if (v.get("sms") != null) {
sms = v.getString("sms", "");
}
// parse object link
JsonArray links = (JsonArray) v.get("objLink");
Link[] linkObjs = null;
if (links != null) {
linkObjs = new Link[links.size()];
for (int i = 0; i < links.size(); i++) {
JsonObject ol = (JsonObject) links.get(i);
Map<String, Object> attMap = new HashMap<>();
JsonObject att = (JsonObject) ol.get("at");
for (String k : att.names()) {
JsonValue jsonValue = att.get(k);
if (jsonValue.isNull()) {
attMap.put(k, null);
} else if (jsonValue.isNumber()) {
attMap.put(k, jsonValue.asInt());
} else {
attMap.put(k, jsonValue.asString());
}
}
Link o = new Link(ol.getString("url", null), attMap);
linkObjs[i] = o;
}
}
Map<String, String> addAttr = new HashMap<>();
JsonObject o = (JsonObject) v.get("addAttr");
for (String k : o.names()) {
addAttr.put(k, o.getString(k, ""));
}
return new RegistrationUpdate(regId, identity, lifetime, sms, b, linkObjs, addAttr);
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationUpdateSerDesTest method ser_and_des_are_equals.
@Test
public void ser_and_des_are_equals() throws Exception {
Link[] objs = new Link[2];
Map<String, Object> att = new HashMap<>();
att.put("ts", 12);
att.put("rt", "test");
att.put("hb", null);
objs[0] = new Link("/0/1024/2", att);
objs[1] = new Link("/0/2");
Map<String, String> additionalAtt = new HashMap<>();
additionalAtt.put("at", "5000");
RegistrationUpdate ru = new RegistrationUpdate("myId", Identity.unsecure(Inet4Address.getByName("127.0.0.1"), LwM2m.DEFAULT_COAP_PORT), 60000l, null, BindingMode.U, objs, additionalAtt);
byte[] ser = RegistrationUpdateSerDes.bSerialize(ru);
RegistrationUpdate ru2 = RegistrationUpdateSerDes.deserialize(ser);
assertEquals(ru, ru2);
}
Aggregations