Skip to content

Commit

Permalink
Fix UriReference issue in MixedLwM2mLink when rootpath is not null.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 3, 2022
1 parent 9fadb35 commit 13dea84
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,27 @@ public MixedLwM2mLink(String rootPath, LwM2mPath path, Collection<? extends Attr
}

public MixedLwM2mLink(String rootPath, LwM2mPath path, MixedLwM2mAttributeSet attributes) {
super(rootPath == null || rootPath.equals("/") ? path.toString() : rootPath + path.toString(), attributes);
super(getUriReference(rootPath, path), attributes);

Validate.notNull(path);
Validate.notNull(attributes);

this.path = path;
this.rootPath = rootPath == null ? "/" : rootPath;
}

private static String getUriReference(String rootPath, LwM2mPath path) {
Validate.notNull(path);

if (rootPath == null || rootPath.equals("/")) {
return path.toString();
} else if (path.isRoot()) {
// rootpath can not be null because we check this before
return rootPath;
} else {
return rootPath + path.toString();
}
}

public LwM2mPath getPath() {
return path;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2022 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core.link;

import static org.junit.Assert.assertEquals;

import org.eclipse.leshan.core.link.attributes.ResourceTypeAttribute;
import org.eclipse.leshan.core.link.lwm2m.MixedLwM2mLink;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.junit.Test;

public class MixedLwM2mLinkTest {

@Test
public void check_uri_reference() {
Link link = new MixedLwM2mLink("/root", LwM2mPath.ROOTPATH, new ResourceTypeAttribute("oma.lwm2m"));
assertEquals("/root", link.getUriReference());

link = new MixedLwM2mLink("/root", new LwM2mPath(2));
assertEquals("/root/2", link.getUriReference());

link = new MixedLwM2mLink("/root/", new LwM2mPath(2));
assertEquals("/root//2", link.getUriReference());

link = new MixedLwM2mLink("/", new LwM2mPath(2));
assertEquals("/2", link.getUriReference());

link = new MixedLwM2mLink(null, new LwM2mPath(2));
assertEquals("/2", link.getUriReference());
}
}

0 comments on commit 13dea84

Please sign in to comment.