BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug5884Test.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.stmt;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.Iterator;
14 import java.util.Set;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 public class Bug5884Test {
26     private static final String NS = "urn:yang.foo";
27     private static final String REV = "2016-01-01";
28
29     @Test
30     public void testBug5884() throws Exception {
31         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5884");
32         assertNotNull(context);
33
34         final QName root = QName.create(NS, REV, "main-container");
35         final QName choice = QName.create(NS, REV, "test-choice");
36         final QName testContainerQname = QName.create(NS, REV, "test");
37         final Module foo = context.findModule("foo", QName.parseRevision("2016-01-01")).get();
38         final ContainerSchemaNode rootContainer = (ContainerSchemaNode) context.getDataChildByName(root);
39         final ContainerSchemaNode testContainer = (ContainerSchemaNode) rootContainer.getDataChildByName(
40             testContainerQname);
41         final ChoiceSchemaNode dataChildByName = (ChoiceSchemaNode) testContainer.getDataChildByName(choice);
42         final Set<AugmentationSchema> augmentations = foo.getAugmentations();
43         final Set<AugmentationSchema> availableAugmentations = dataChildByName.getAvailableAugmentations();
44         final Iterator<AugmentationSchema> iterator = augmentations.iterator();
45         final Iterator<AugmentationSchema> availableIterator = availableAugmentations.iterator();
46
47         testIterator(iterator);
48         testIterator(availableIterator);
49     }
50
51     private static void testIterator(final Iterator<AugmentationSchema> iterator) {
52         while (iterator.hasNext()) {
53             AugmentationSchema allAugments = iterator.next();
54             final DataSchemaNode currentChoice = allAugments.getChildNodes().iterator().next();
55             assertTrue(currentChoice instanceof ChoiceCaseNode);
56         }
57     }
58 }