2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import java.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 public class Bug6316 {
33 public void test() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
34 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6316");
35 assertNotNull(context);
36 verifyEnumTypedefinition(context);
37 verifyBitsTypedefinition(context);
40 private void verifyEnumTypedefinition(final SchemaContext context) {
41 final DataSchemaNode dataChildByName = context.getDataChildByName(QName
42 .create("foo", "1970-01-01", "enum-leaf"));
43 assertTrue(dataChildByName instanceof LeafSchemaNode);
44 final LeafSchemaNode enumLeaf = (LeafSchemaNode) dataChildByName;
45 final TypeDefinition<? extends TypeDefinition<?>> type = enumLeaf.getType();
46 assertTrue(type instanceof EnumTypeDefinition);
47 final EnumTypeDefinition myEnumeration = (EnumTypeDefinition) type;
48 final List<EnumPair> values = myEnumeration.getValues();
49 for (final EnumPair enumPair : values) {
50 final String name = enumPair.getName();
53 assertEquals(0, enumPair.getValue());
56 assertEquals(20, enumPair.getValue());
59 assertEquals(21, enumPair.getValue());
62 assertEquals(2, enumPair.getValue());
65 assertEquals(22, enumPair.getValue());
68 fail("Unexpected enum name.");
73 private void verifyBitsTypedefinition(final SchemaContext context) {
74 final DataSchemaNode dataChildByName = context.getDataChildByName(QName
75 .create("foo", "1970-01-01", "bits-leaf"));
76 assertTrue(dataChildByName instanceof LeafSchemaNode);
77 final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
78 final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
79 assertTrue(type instanceof BitsTypeDefinition);
80 final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
81 final List<Bit> positions = myBits.getBits();
82 for (final Bit bit : positions) {
83 final String name = bit.getName();
86 assertEquals(0, bit.getPosition());
89 assertEquals(20, bit.getPosition());
92 assertEquals(21, bit.getPosition());
95 assertEquals(2, bit.getPosition());
98 assertEquals(22, bit.getPosition());
101 fail("Unexpected bit name.");