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