e2298784bbd6e16a4eefda9ccd770b584ad80a8c
[yangtools.git] / data / yang-data-api / src / test / java / org / opendaylight / yangtools / yang / data / api / PathArgumentListTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;
9
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;
14
15 import com.google.common.collect.UnmodifiableIterator;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
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;
27
28 public class PathArgumentListTest {
29     private static final class TestClass extends PathArgumentList {
30         @Override
31         public UnmodifiableIterator<PathArgument> iterator() {
32             return new UnmodifiableIterator<>() {
33                 @Override
34                 public boolean hasNext() {
35                     return false;
36                 }
37
38                 @Override
39                 public PathArgument next() {
40                     throw new NoSuchElementException();
41                 }
42             };
43         }
44
45         @Override
46         public PathArgument get(final int index) {
47             return null;
48         }
49
50         @Override
51         public int size() {
52             return 0;
53         }
54     }
55
56     @Test
57     public void testIsEmpty() {
58         assertFalse(new TestClass().isEmpty());
59     }
60
61     @Test
62     public void testProtections() {
63         final PathArgumentList l = new TestClass();
64
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));
71     }
72
73     @Test
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,
83             entryLeaf);
84         final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.of(qNameRoot).node(qNameList)
85                 .node(nodeIdentifierWithPredicates).node(qNameLeaf);
86         final PathArgument pathArgumentToRoot = yangInstanceIdentifier.getAncestor(1).getPathArguments().iterator()
87                 .next();
88         final StackedPathArguments stackedPathArguments = (StackedPathArguments)yangInstanceIdentifier
89             .getPathArguments();
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));
96
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));
107
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());
114
115         assertThrows(IllegalArgumentException.class, () -> stackedYangInstanceIdentifier.getAncestor(12));
116     }
117 }