Bug 5884: Augmenting a choice without a case results in no getter
[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.io.FileNotFoundException;
14 import java.net.URISyntaxException;
15 import java.text.ParseException;
16 import java.util.Date;
17 import java.util.Iterator;
18 import java.util.Set;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31
32 public class Bug5884Test {
33     private static final String NS = "urn:yang.foo";
34     private static final String REV = "2016-01-01";
35
36     @Test
37     public void testBug5884() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException, ParseException {
38         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5884");
39         assertNotNull(context);
40
41         final QName root = QName.create(NS, REV, "main-container");
42         final QName choice = QName.create(NS, REV, "test-choice");
43         final QName testContainerQname = QName.create(NS, REV, "test");
44         final Date date = SimpleDateFormatUtil.getRevisionFormat().parse("2016-01-01");
45         final Module foo = context.findModuleByName("foo", date);
46         final ContainerSchemaNode rootContainer = (ContainerSchemaNode) context.getDataChildByName(root);
47         final ContainerSchemaNode testContainer = (ContainerSchemaNode) rootContainer.getDataChildByName(testContainerQname);
48         final ChoiceSchemaNode dataChildByName = (ChoiceSchemaNode) testContainer.getDataChildByName(choice);
49         final Set<AugmentationSchema> augmentations = foo.getAugmentations();
50         final Set<AugmentationSchema> availableAugmentations = dataChildByName.getAvailableAugmentations();
51         final Iterator<AugmentationSchema> iterator = augmentations.iterator();
52         final Iterator<AugmentationSchema> availableIterator = availableAugmentations.iterator();
53
54         testIterator(iterator);
55         testIterator(availableIterator);
56     }
57
58     private void testIterator(final Iterator<AugmentationSchema> iterator) {
59         AugmentationSchema allAugments;
60         while (iterator.hasNext()) {
61             allAugments = iterator.next();
62             final DataSchemaNode currentChoice = allAugments.getChildNodes().iterator().next();
63             assertTrue(currentChoice instanceof ChoiceCaseNode);
64         }
65     }
66 }