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.assertTrue;
13 import static org.junit.Assert.fail;
15 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.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<PathArgument>() {
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();
68 } catch (UnsupportedOperationException e) {
73 l.addAll(Collections.emptyList());
75 } catch (UnsupportedOperationException e) {
80 l.removeAll(Collections.emptyList());
82 } catch (UnsupportedOperationException e) {
87 l.retainAll(Collections.emptyList());
89 } catch (UnsupportedOperationException e) {
96 } catch (UnsupportedOperationException e) {
103 } catch (UnsupportedOperationException e) {
109 public void testPathArgument() {
110 final QNameModule qNameModule = QNameModule.create(URI.create("urn:opendaylight.test2"),
111 Revision.of("2015-08-08"));
112 final QName qNameRoot = QName.create(qNameModule, "root");
113 final QName qNameList = QName.create(qNameModule, "list");
114 final QName qNameLeaf = QName.create(qNameModule, "leaf-a");
115 final Map<QName, Object> entryLeaf = new HashMap<>();
116 entryLeaf.put(qNameList, "leaf");
117 final NodeIdentifierWithPredicates nodeIdentifierWithPredicates = NodeIdentifierWithPredicates.of(qNameList,
119 final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.of(qNameRoot).node(qNameList)
120 .node(nodeIdentifierWithPredicates).node(qNameLeaf);
121 final PathArgument pathArgumentToRoot = yangInstanceIdentifier.getAncestor(1).getPathArguments().iterator()
123 final StackedPathArguments stackedPathArguments = (StackedPathArguments)yangInstanceIdentifier
125 assertTrue(yangInstanceIdentifier.pathArgumentsEqual(yangInstanceIdentifier));
126 assertEquals(pathArgumentToRoot, stackedPathArguments.get(0));
127 assertEquals(4, stackedPathArguments.size());
128 assertTrue(stackedPathArguments.contains(pathArgumentToRoot));
129 assertEquals(0, stackedPathArguments.indexOf(pathArgumentToRoot));
130 assertEquals(0, stackedPathArguments.lastIndexOf(pathArgumentToRoot));
132 final StackedReversePathArguments stackedReversePathArguments =
133 (StackedReversePathArguments)yangInstanceIdentifier.getReversePathArguments();
134 final QName rootQname = pathArgumentToRoot.getNodeType();
135 final QName leafQname = stackedReversePathArguments.get(0).getNodeType();
136 assertEquals(qNameRoot, rootQname);
137 assertEquals(qNameLeaf, leafQname);
138 assertEquals(4, stackedReversePathArguments.size());
139 assertTrue(stackedReversePathArguments.contains(pathArgumentToRoot));
140 assertEquals(3, stackedReversePathArguments.indexOf(pathArgumentToRoot));
141 assertEquals(3, stackedReversePathArguments.lastIndexOf(pathArgumentToRoot));
143 final StackedYangInstanceIdentifier stackedYangInstanceIdentifier = (StackedYangInstanceIdentifier)
144 yangInstanceIdentifier;
145 final StackedYangInstanceIdentifier stackedYangInstanceIdentifierClone = stackedYangInstanceIdentifier.clone();
146 final YangInstanceIdentifier yangInstanceIdentifier1 = stackedYangInstanceIdentifier.getAncestor(4);
147 assertEquals(stackedYangInstanceIdentifier, stackedYangInstanceIdentifierClone);
148 assertEquals(stackedReversePathArguments, yangInstanceIdentifier1.getReversePathArguments());
151 stackedYangInstanceIdentifier.getAncestor(12);
153 } catch (IllegalArgumentException e) {