8f75d76d9eab9275f699a0324814b640eddd136a
[yangtools.git] / parser / yang-parser-rfc7950 / 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertSame;
12
13 import com.google.common.collect.Iterables;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Revision;
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     @Test
25     public void testDeviateReplaceWithUserDefinedTypes() throws Exception {
26         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug9242/");
27         assertNotNull(schemaContext);
28
29         final Revision revision = Revision.of("2017-10-13");
30         final Module rootModule = schemaContext.findModule("root-module", revision).get();
31         final Module impModule = schemaContext.findModule("imp-module", revision).get();
32
33         TypeDefinition<?> deviatedMyLeafType = null;
34         TypeDefinition<?> deviatedMyLeaf2Type = null;
35
36         for (final Deviation deviation : rootModule.getDeviations()) {
37             final QName last = Iterables.getLast(deviation.getTargetPath().getNodeIdentifiers());
38             if (last.equals(QName.create(impModule.getQNameModule(), "my-leaf"))) {
39                 deviatedMyLeafType = deviation.getDeviates().iterator().next().getDeviatedType();
40             }
41
42             if (last.equals(QName.create(impModule.getQNameModule(), "my-leaf-2"))) {
43                 deviatedMyLeaf2Type = deviation.getDeviates().iterator().next().getDeviatedType();
44             }
45         }
46
47         assertNotNull(deviatedMyLeafType);
48         assertNotNull(deviatedMyLeaf2Type);
49
50         final LeafSchemaNode myLeaf = (LeafSchemaNode) impModule.getDataChildByName(QName.create(
51                 impModule.getQNameModule(), "my-leaf"));
52         assertSame(deviatedMyLeafType, myLeaf.getType());
53
54         final LeafSchemaNode myLeaf2 = (LeafSchemaNode) impModule.getDataChildByName(QName.create(
55                 impModule.getQNameModule(), "my-leaf-2"));
56         assertSame(deviatedMyLeaf2Type, myLeaf2.getType());
57     }
58 }