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.assertThrows;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.UnmodifiableIterator;
16 import java.util.Collections;
17 import java.util.HashMap;
19 import java.util.NoSuchElementException;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 public class PathArgumentListTest {
29 private static final class TestClass extends PathArgumentList {
31 public UnmodifiableIterator<PathArgument> iterator() {
32 return new UnmodifiableIterator<>() {
34 public boolean hasNext() {
39 public PathArgument next() {
40 throw new NoSuchElementException();
46 public PathArgument get(final int index) {
57 public void testIsEmpty() {
58 assertFalse(new TestClass().isEmpty());
62 public void testProtections() {
63 final PathArgumentList l = new TestClass();
65 assertThrows(UnsupportedOperationException.class, () -> l.remove(null));
66 assertThrows(UnsupportedOperationException.class, () -> l.addAll(Collections.emptyList()));
67 assertThrows(UnsupportedOperationException.class, () -> l.removeAll(Collections.emptyList()));
68 assertThrows(UnsupportedOperationException.class, () -> l.retainAll(Collections.emptyList()));
69 assertThrows(UnsupportedOperationException.class, () -> l.clear());
70 assertThrows(UnsupportedOperationException.class, () -> l.addAll(0, null));
74 public void testPathArgument() {
75 final QNameModule qNameModule = QNameModule.create(XMLNamespace.of("urn:opendaylight.test2"),
76 Revision.of("2015-08-08"));
77 final QName qNameRoot = QName.create(qNameModule, "root");
78 final QName qNameList = QName.create(qNameModule, "list");
79 final QName qNameLeaf = QName.create(qNameModule, "leaf-a");
80 final Map<QName, Object> entryLeaf = new HashMap<>();
81 entryLeaf.put(qNameList, "leaf");
82 final NodeIdentifierWithPredicates nodeIdentifierWithPredicates = NodeIdentifierWithPredicates.of(qNameList,
84 final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.of(qNameRoot).node(qNameList)
85 .node(nodeIdentifierWithPredicates).node(qNameLeaf);
86 final PathArgument pathArgumentToRoot = yangInstanceIdentifier.getAncestor(1).getPathArguments().iterator()
88 final StackedPathArguments stackedPathArguments = (StackedPathArguments)yangInstanceIdentifier
90 assertTrue(yangInstanceIdentifier.pathArgumentsEqual(yangInstanceIdentifier));
91 assertEquals(pathArgumentToRoot, stackedPathArguments.get(0));
92 assertEquals(4, stackedPathArguments.size());
93 assertTrue(stackedPathArguments.contains(pathArgumentToRoot));
94 assertEquals(0, stackedPathArguments.indexOf(pathArgumentToRoot));
95 assertEquals(0, stackedPathArguments.lastIndexOf(pathArgumentToRoot));
97 final StackedReversePathArguments stackedReversePathArguments =
98 (StackedReversePathArguments)yangInstanceIdentifier.getReversePathArguments();
99 final QName rootQname = pathArgumentToRoot.getNodeType();
100 final QName leafQname = stackedReversePathArguments.get(0).getNodeType();
101 assertEquals(qNameRoot, rootQname);
102 assertEquals(qNameLeaf, leafQname);
103 assertEquals(4, stackedReversePathArguments.size());
104 assertTrue(stackedReversePathArguments.contains(pathArgumentToRoot));
105 assertEquals(3, stackedReversePathArguments.indexOf(pathArgumentToRoot));
106 assertEquals(3, stackedReversePathArguments.lastIndexOf(pathArgumentToRoot));
108 final StackedYangInstanceIdentifier stackedYangInstanceIdentifier = (StackedYangInstanceIdentifier)
109 yangInstanceIdentifier;
110 final StackedYangInstanceIdentifier stackedYangInstanceIdentifierClone = stackedYangInstanceIdentifier.clone();
111 final YangInstanceIdentifier yangInstanceIdentifier1 = stackedYangInstanceIdentifier.getAncestor(4);
112 assertEquals(stackedYangInstanceIdentifier, stackedYangInstanceIdentifierClone);
113 assertEquals(stackedReversePathArguments, yangInstanceIdentifier1.getReversePathArguments());
115 assertThrows(IllegalArgumentException.class, () -> stackedYangInstanceIdentifier.getAncestor(12));