Rename opendaylight.mdsal.binding.runtime.spi
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / StoreTreeNodesTest.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.tree.impl;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15
16 import java.util.Optional;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
25 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
28 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 class StoreTreeNodesTest extends AbstractTestModelTest {
32     private static final Short ONE_ID = 1;
33     private static final Short TWO_ID = 2;
34     private static final String TWO_ONE_NAME = "one";
35     private static final String TWO_TWO_NAME = "two";
36
37     private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(
38         TestModel.OUTER_LIST_PATH)
39             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
40             .build();
41
42     private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(
43         TestModel.OUTER_LIST_PATH)
44             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
45             .build();
46
47     private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
48             .node(TestModel.INNER_LIST_QNAME)
49             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
50             .build();
51
52     private static final MapEntryNode BAR_NODE = ImmutableNodes.newMapEntryBuilder()
53         .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID))
54         .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, TWO_ID))
55         .withChild(ImmutableNodes.newSystemMapBuilder()
56             .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
57             .withChild(ImmutableNodes.newMapEntryBuilder()
58                 .withNodeIdentifier(
59                     NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
60                 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_ONE_NAME))
61                 .build())
62             .withChild(ImmutableNodes.newMapEntryBuilder()
63                 .withNodeIdentifier(
64                     NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
65                 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_TWO_NAME))
66                 .build())
67             .build())
68         .build();
69
70     private RootApplyStrategy rootOper;
71
72     @BeforeEach
73     void prepare() throws ExcludedDataSchemaNodeException {
74         rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
75             DataTreeConfiguration.DEFAULT_OPERATIONAL));
76     }
77
78     public static ContainerNode createDocumentOne() {
79         return ImmutableNodes.newContainerBuilder()
80             .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
81             .withChild(createTestContainer())
82             .build();
83     }
84
85     @Test
86     void findNodeTestNodeFound() {
87         final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
88                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
89         final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
90         final var node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
91         assertPresentAndType(node, TreeNode.class);
92     }
93
94     @Test
95     void findNodeTestNodeNotFound() {
96         final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
97                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
98         final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
99         final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
100                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
101                 .build();
102         final var node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
103         assertFalse(node.isPresent());
104     }
105
106     @Test
107     void findNodeCheckedTestNodeFound() {
108         final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
109                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
110         final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
111         TreeNode foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
112         assertNotNull(foundNode);
113     }
114
115     @Test
116     void findNodeCheckedTestNodeNotFound() {
117         final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
118                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
119         final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
120         final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
121                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
122                 .build();
123         assertThrows(IllegalArgumentException.class,
124             () -> StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath));
125     }
126
127     @Test
128     void findClosestOrFirstMatchTestNodeExists() {
129         final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
130                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
131         final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
132         final var expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
133         assertPresentAndType(expectedNode, TreeNode.class);
134
135         final var actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
136         assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
137     }
138
139     @Test
140     void findClosestOrFirstMatchTestNodeDoesNotExist() {
141         final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
142                 TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
143         final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
144         final var outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
145                 .node(TestModel.INNER_LIST_QNAME)
146                 .build();
147         final var twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
148                 .node(TestModel.INNER_LIST_QNAME)
149                 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
150                 .build();
151         final var expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
152         assertPresentAndType(expectedNode, TreeNode.class);
153
154         final var actualNode = StoreTreeNodes.findClosest(rootNode,
155             twoTwoInvalidPath);
156         assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
157     }
158
159     private static ContainerNode createTestContainer() {
160         return ImmutableNodes.newContainerBuilder()
161             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
162             .withChild(ImmutableNodes.newSystemMapBuilder()
163                 .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
164                 .withChild(ImmutableNodes.newMapEntryBuilder()
165                     .withNodeIdentifier(
166                         NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
167                     .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, ONE_ID))
168                     .build())
169                 .withChild(BAR_NODE)
170                 .build())
171             .build();
172     }
173
174     private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
175             final Class<T> type) {
176         assertNotNull(potential);
177         assertTrue(potential.isPresent());
178         assertTrue(type.isInstance(potential.orElseThrow()));
179         return type.cast(potential.orElseThrow());
180     }
181
182     private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
183         assertEquals(expected.getIdentifier(), actual.getIdentifier());
184         assertEquals(expected.getVersion(), actual.getVersion());
185         assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
186         assertEquals(expected.getData(), actual.getData());
187         assertEquals(expected.toString(), actual.toString());
188     }
189 }