2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.collect.UnmodifiableIterator;
17 import java.util.Collections;
18 import java.util.HashMap;
20 import java.util.NoSuchElementException;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 public class PathArgumentListTest {
30 private static final class TestClass extends PathArgumentList {
32 public UnmodifiableIterator<PathArgument> iterator() {
33 return new UnmodifiableIterator<>() {
35 public boolean hasNext() {
40 public PathArgument next() {
41 throw new NoSuchElementException();
47 public PathArgument get(final int index) {
58 public void testIsEmpty() {
59 assertFalse(new TestClass().isEmpty());
63 public void testProtections() {
64 final PathArgumentList l = new TestClass();
66 assertThrows(UnsupportedOperationException.class, () -> l.remove(null));
67 assertThrows(UnsupportedOperationException.class, () -> l.addAll(Collections.emptyList()));
68 assertThrows(UnsupportedOperationException.class, () -> l.removeAll(Collections.emptyList()));
69 assertThrows(UnsupportedOperationException.class, () -> l.retainAll(Collections.emptyList()));
70 assertThrows(UnsupportedOperationException.class, () -> l.clear());
71 assertThrows(UnsupportedOperationException.class, () -> l.addAll(0, null));
75 public void testPathArgument() {
76 final QNameModule qNameModule = QNameModule.create(XMLNamespace.of("urn:opendaylight.test2"),
77 Revision.of("2015-08-08"));
78 final QName qNameRoot = QName.create(qNameModule, "root");
79 final QName qNameList = QName.create(qNameModule, "list");
80 final QName qNameLeaf = QName.create(qNameModule, "leaf-a");
81 final Map<QName, Object> entryLeaf = new HashMap<>();
82 entryLeaf.put(qNameList, "leaf");
83 final NodeIdentifierWithPredicates nodeIdentifierWithPredicates = NodeIdentifierWithPredicates.of(qNameList,
85 final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.of(qNameRoot).node(qNameList)
86 .node(nodeIdentifierWithPredicates).node(qNameLeaf);
87 final PathArgument pathArgumentToRoot = yangInstanceIdentifier.getAncestor(1).getPathArguments().iterator()
89 final StackedPathArguments stackedPathArguments = (StackedPathArguments)yangInstanceIdentifier
91 assertTrue(yangInstanceIdentifier.pathArgumentsEqual(yangInstanceIdentifier));
92 assertEquals(pathArgumentToRoot, stackedPathArguments.get(0));
93 assertEquals(4, stackedPathArguments.size());
94 assertTrue(stackedPathArguments.contains(pathArgumentToRoot));
95 assertEquals(0, stackedPathArguments.indexOf(pathArgumentToRoot));
96 assertEquals(0, stackedPathArguments.lastIndexOf(pathArgumentToRoot));
98 final StackedReversePathArguments stackedReversePathArguments =
99 (StackedReversePathArguments)yangInstanceIdentifier.getReversePathArguments();
100 final QName rootQname = pathArgumentToRoot.getNodeType();
101 final QName leafQname = stackedReversePathArguments.get(0).getNodeType();
102 assertEquals(qNameRoot, rootQname);
103 assertEquals(qNameLeaf, leafQname);
104 assertEquals(4, stackedReversePathArguments.size());
105 assertTrue(stackedReversePathArguments.contains(pathArgumentToRoot));
106 assertEquals(3, stackedReversePathArguments.indexOf(pathArgumentToRoot));
107 assertEquals(3, stackedReversePathArguments.lastIndexOf(pathArgumentToRoot));
109 final StackedYangInstanceIdentifier stackedYangInstanceIdentifier = (StackedYangInstanceIdentifier)
110 yangInstanceIdentifier;
111 final StackedYangInstanceIdentifier stackedYangInstanceIdentifierClone = stackedYangInstanceIdentifier.clone();
112 final YangInstanceIdentifier yangInstanceIdentifier1 = stackedYangInstanceIdentifier.getAncestor(4);
113 assertEquals(stackedYangInstanceIdentifier, stackedYangInstanceIdentifierClone);
114 assertEquals(stackedReversePathArguments, yangInstanceIdentifier1.getReversePathArguments());
115 assertSame(stackedYangInstanceIdentifier.getParent(), stackedYangInstanceIdentifier.getAncestor(3));
117 final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
118 () -> stackedYangInstanceIdentifier.getAncestor(12));
119 assertEquals("Depth 12 exceeds maximum depth 4", thrown.getMessage());