Add DataSchemaContextTreeTest
[yangtools.git] / yang / yang-data-util / src / test / java / org / opendaylight / yangtools / yang / data / util / codec / DataSchemaContextTreeTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.util.codec;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.URI;
16 import java.util.Optional;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class DataSchemaContextTreeTest {
27     private static final QNameModule MODULE = QNameModule.create(URI.create("dataschemacontext"));
28     private static final QName FOO = QName.create(MODULE, "foo");
29     private static final QName BAR = QName.create(MODULE, "bar");
30     private static final QName BAZ = QName.create(MODULE, "baz");
31     private static DataSchemaContextTree CONTEXT;
32
33     @BeforeClass
34     public static void init() {
35         CONTEXT = DataSchemaContextTree.from(YangParserTestUtils.parseYangResource("/dataschemacontext.yang"));
36     }
37
38     @AfterClass
39     public static void cleanup() {
40         CONTEXT = null;
41     }
42
43     @Test
44     public void testCorrectInput() {
45         assertTrue(CONTEXT.findChild(YangInstanceIdentifier.of(FOO)).isPresent());
46         assertTrue(CONTEXT.findChild(YangInstanceIdentifier.of(FOO).node(BAR)).isPresent());
47         assertTrue(CONTEXT.findChild(YangInstanceIdentifier.of(FOO).node(BAR).node(BAZ)).isPresent());
48
49         assertNotNull(CONTEXT.getChild(YangInstanceIdentifier.of(FOO)));
50         assertNotNull(CONTEXT.getChild(YangInstanceIdentifier.of(FOO).node(BAR)));
51         assertNotNull(CONTEXT.getChild(YangInstanceIdentifier.of(FOO).node(BAR).node(BAZ)));
52     }
53
54     @Test
55     public void testSimpleBad() {
56         assertEquals(Optional.empty(), CONTEXT.findChild(YangInstanceIdentifier.of(BAR)));
57         assertNull(CONTEXT.getChild(YangInstanceIdentifier.of(BAR)));
58     }
59
60     @Test
61     public void testNestedBad() {
62         assertEquals(Optional.empty(), CONTEXT.findChild(YangInstanceIdentifier.of(BAR).node(BAZ)));
63         assertNull(CONTEXT.getChild(YangInstanceIdentifier.of(BAR).node(BAZ)));
64     }
65 }