LinkFormatHelper { public static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectEnablers, String rootPath) { List<Link> links = new ArrayList<>(); String root = rootPath == null ? "" : rootPath; String rootURL = getPath("/", root); Map<String, String> attributes = new HashMap<>(); attributes.put("rt", "\"oma.lwm2m\""); links.add(new Link(rootURL, attributes)); List<LwM2mObjectEnabler> objEnablerList = new ArrayList<>(objectEnablers); Collections.sort(objEnablerList, new Comparator<LwM2mObjectEnabler>() { @Override public int compare(LwM2mObjectEnabler o1, LwM2mObjectEnabler o2) { return o1.getId() - o2.getId(); } }); for (LwM2mObjectEnabler objectEnabler : objEnablerList) { if (objectEnabler.getId() == LwM2mId.SECURITY) continue; List<Integer> availableInstance = objectEnabler.getAvailableInstanceIds(); Map<String, String> objectAttributes = getObjectAttributes(objectEnabler.getObjectModel()); if (availableInstance.isEmpty() || (objectAttributes != null)) { String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId())); links.add(new Link(objectInstanceUrl, objectAttributes)); } for (Integer instanceId : objectEnabler.getAvailableInstanceIds()) { String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()), instanceId.toString()); links.add(new Link(objectInstanceUrl)); } } return links.toArray(new Link[] {}); } private  LinkFormatHelper(); static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectEnablers, String rootPath); static Link[] getBootstrapClientDescription(Collection<LwM2mObjectEnabler> objectEnablers); static Link[] getObjectDescription(LwM2mObjectEnabler objectEnabler, String root); static Link[] getBootstrapObjectDescription(LwM2mObjectEnabler objectEnabler); static Link[] getInstanceDescription(LwM2mObjectEnabler objectEnabler, int instanceId, String root); static Link getResourceDescription(LwM2mObjectEnabler objectEnabler, int instanceId, int resourceId,
            String root);  }
@Test public void encode_client_description_with_version_1_0() { List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>(); Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>(); instancesMap.put(0, new BaseInstanceEnabler()); objectEnablers.add(new ObjectEnabler(6, getObjectModel(6), instancesMap, null, ContentFormat.DEFAULT)); Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null); String strLinks = Link.serialize(links); assertEquals("</>;rt=\"oma.lwm2m\",</6/0>", strLinks); }
@Test public void encode_client_description_with_version_2_0() { List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>(); Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>(); instancesMap.put(0, new BaseInstanceEnabler()); instancesMap.put(1, new BaseInstanceEnabler()); objectEnablers.add( new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null, ContentFormat.DEFAULT)); Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null); String strLinks = Link.serialize(links); assertEquals("</>;rt=\"oma.lwm2m\",</6>;ver=2.0,</6/0>,</6/1>", strLinks); }
@Test public void encode_client_description_with_version_2_0_no_instances() { List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>(); Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>(); objectEnablers.add( new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null, ContentFormat.DEFAULT)); Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null); String strLinks = Link.serialize(links); assertEquals("</>;rt=\"oma.lwm2m\",</6>;ver=2.0", strLinks); }