BUG-7052: extract SimpleSchemaContext
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug2872Test.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
27
28 public class Bug2872Test {
29
30     @Test
31     public void test() throws Exception {
32         final SchemaContext schema = StmtTestUtils.parseYangSources("/bugs/bug2872");
33         assertNotNull(schema);
34
35         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-06-08");
36         final QNameModule bug2872module = QNameModule.create(new URI("bug2872"), revision);
37         final QName foo = QName.create(bug2872module, "bar");
38
39         final DataSchemaNode dataSchemaNode = schema.getDataChildByName(foo);
40         assertTrue(dataSchemaNode instanceof LeafSchemaNode);
41         final LeafSchemaNode myLeaf = (LeafSchemaNode) dataSchemaNode;
42
43         final TypeDefinition<?> type = myLeaf.getType();
44         assertTrue(type instanceof EnumTypeDefinition);
45         final EnumTypeDefinition myEnum = (EnumTypeDefinition) type;
46
47         final List<EnumTypeDefinition.EnumPair> values = myEnum.getValues();
48         assertEquals(2, values.size());
49
50         final List<String> valueNames = new ArrayList<>();
51         for (EnumTypeDefinition.EnumPair pair : values) {
52             valueNames.add(pair.getName());
53         }
54         assertTrue(valueNames.contains("value-one"));
55         assertTrue(valueNames.contains("value-two"));
56     }
57 }