097fa671f5d8a7e0fa700bc36f69851d69caa531
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / LeafRefContextTest.java
1 /*
2  * Copyright (c) 2016 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.impl.leafref.context;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.net.URISyntaxException;
16 import java.util.Arrays;
17 import java.util.Map;
18 import java.util.Set;
19 import org.junit.BeforeClass;
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.data.impl.TestUtils;
24 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
25 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31
32 public class LeafRefContextTest {
33
34     private static SchemaContext context;
35     private static Module rootMod;
36     private static QNameModule root;
37     private static LeafRefContext rootLeafRefContext;
38
39     @BeforeClass
40     public static void init() throws URISyntaxException, FileNotFoundException, ReactorException {
41
42         final File resourceFile = new File(LeafRefContextTreeBuilderTest.class.getResource(
43                 "/leafref-context-test/correct-modules/leafref-test2.yang").toURI());
44
45         final File resourceDir = resourceFile.getParentFile();
46
47         context = TestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
48
49         final Set<Module> modules = context.getModules();
50         for (final Module module : modules) {
51             if (module.getName().equals("leafref-test2")) {
52                 rootMod = module;
53             }
54         }
55
56         root = rootMod.getQNameModule();
57         rootLeafRefContext = LeafRefContext.create(context);
58     }
59
60     @Test
61     public void test() {
62
63         final QName q1 = QName.create(root, "ref1");
64         final QName q2 = QName.create(root, "leaf1");
65         final QName q3 = QName.create(root, "cont1");
66         final QName q4 = QName.create(root, "cont2");
67         final QName q5 = QName.create(root, "list1");
68         final QName q6 = QName.create(root, "name");
69
70         final DataSchemaNode leafRefNode = rootMod.getDataChildByName(q1);
71         final DataSchemaNode targetNode = rootMod.getDataChildByName(q2);
72         final DataSchemaNode cont1Node = rootMod.getDataChildByName(q3);
73         final DataSchemaNode cont2Node = rootMod.getDataChildByName(q4);
74         final DataSchemaNode name1Node = ((DataNodeContainer) ((DataNodeContainer) rootMod.getDataChildByName(q3))
75                 .getDataChildByName(q5)).getDataChildByName(q6);
76
77         assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
78         assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
79
80         assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
81         assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
82
83         assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
84         assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
85
86         assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
87         assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
88
89         Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
90                 rootLeafRefContext);
91         assertEquals(4, leafRefs.size());
92         leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
93         assertTrue(leafRefs.isEmpty());
94     }
95 }