1 package org.opendaylight.controller.cluster.datastore.node.utils;
4 import org.opendaylight.controller.cluster.datastore.util.TestModel;
5 import org.opendaylight.yangtools.yang.common.QName;
6 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
8 import java.util.HashMap;
9 import java.util.HashSet;
13 import static junit.framework.TestCase.assertEquals;
15 public class PathUtilsTest {
18 public void getParentPath(){
19 assertEquals("", PathUtils.getParentPath("foobar"));
20 assertEquals("", PathUtils.getParentPath("/a"));
21 assertEquals("/a", PathUtils.getParentPath("/a/b"));
22 assertEquals("/a/b", PathUtils.getParentPath("/a/b/c"));
23 assertEquals("/a/b", PathUtils.getParentPath("a/b/c"));
27 public void toStringNodeIdentifier(){
28 YangInstanceIdentifier.PathArgument pathArgument = nodeIdentifier();
30 String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test";
32 assertEquals(expected , PathUtils.toString(pathArgument));
36 public void toStringAugmentationIdentifier(){
37 String expected = "AugmentationIdentifier{childNames=[(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics]}";
39 YangInstanceIdentifier.PathArgument pathArgument = augmentationIdentifier();
41 assertEquals(expected, PathUtils.toString(pathArgument));
45 public void toStringNodeWithValue(){
47 YangInstanceIdentifier.PathArgument pathArgument = nodeWithValue();
49 String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[100]";
51 assertEquals(expected, PathUtils.toString(pathArgument));
56 public void toStringNodeIdentifierWithPredicates(){
58 YangInstanceIdentifier.PathArgument pathArgument = nodeIdentifierWithPredicates();
60 String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=100}]";
62 assertEquals(expected, PathUtils.toString(pathArgument));
66 public void toStringYangInstanceIdentifier(){
68 YangInstanceIdentifier path =
69 YangInstanceIdentifier.create(nodeIdentifier())
70 .node(nodeIdentifierWithPredicates())
71 .node(augmentationIdentifier()).node(nodeWithValue());
74 String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test/" +
75 "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=100}]/" +
76 "AugmentationIdentifier{childNames=[(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics]}/" +
77 "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[100]";
79 assertEquals(expected, PathUtils.toString(path));
84 public void toYangInstanceIdentifier(){
85 String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test/" +
86 "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=100}]/" +
87 "AugmentationIdentifier{childNames=[(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics]}/" +
88 "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[100]";
90 YangInstanceIdentifier yangInstanceIdentifier =
91 PathUtils.toYangInstanceIdentifier(expected);
93 String actual = PathUtils.toString(yangInstanceIdentifier);
95 assertEquals(expected, actual);
99 private YangInstanceIdentifier.NodeIdentifier nodeIdentifier(){
100 return new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME);
103 private YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier(){
104 Set<QName> childNames = new HashSet();
105 childNames.add(QNameFactory.create("(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics"));
107 return new YangInstanceIdentifier.AugmentationIdentifier(childNames);
110 private YangInstanceIdentifier.NodeWithValue nodeWithValue(){
111 return new YangInstanceIdentifier.NodeWithValue(TestModel.TEST_QNAME, Integer.valueOf(100));
114 private YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates(){
115 Map<QName, Object> keys = new HashMap<>();
117 keys.put(TestModel.ID_QNAME, Integer.valueOf(100));
119 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.TEST_QNAME, keys);