BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug9242Test.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13
14 import java.util.Date;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Deviation;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22
23 public class Bug9242Test {
24
25     @Test
26     public void testDeviateReplaceWithUserDefinedTypes() throws Exception {
27         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug9242/");
28         assertNotNull(schemaContext);
29
30         final Date revision = QName.parseRevision("2017-10-13");
31
32         final Module rootModule = schemaContext.findModule("root-module", revision).get();
33         final Module impModule = schemaContext.findModule("imp-module", revision).get();
34
35         TypeDefinition<?> deviatedMyLeafType = null;
36         TypeDefinition<?> deviatedMyLeaf2Type = null;
37
38         for (final Deviation deviation : rootModule.getDeviations()) {
39             if (deviation.getTargetPath().getLastComponent().equals(QName.create(
40                     impModule.getQNameModule(), "my-leaf"))) {
41                 deviatedMyLeafType = deviation.getDeviates().iterator().next().getDeviatedType();
42             }
43
44             if (deviation.getTargetPath().getLastComponent().equals(QName.create(
45                     impModule.getQNameModule(), "my-leaf-2"))) {
46                 deviatedMyLeaf2Type = deviation.getDeviates().iterator().next().getDeviatedType();
47             }
48         }
49
50         assertNotNull(deviatedMyLeafType);
51         assertNotNull(deviatedMyLeaf2Type);
52
53         final LeafSchemaNode myLeaf = (LeafSchemaNode) impModule.getDataChildByName(QName.create(
54                 impModule.getQNameModule(), "my-leaf"));
55         assertSame(deviatedMyLeafType, myLeaf.getType());
56
57         final LeafSchemaNode myLeaf2 = (LeafSchemaNode) impModule.getDataChildByName(QName.create(
58                 impModule.getQNameModule(), "my-leaf-2"));
59         assertSame(deviatedMyLeaf2Type, myLeaf2.getType());
60     }
61 }