Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / YangTypes2StmtTest.java
1 /*
2  * Copyright (c) 2015 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.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import java.net.URI;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
28
29 public class YangTypes2StmtTest {
30
31     private static final YangStatementSourceImpl TYPEFILE1 = new YangStatementSourceImpl(
32             "/semantic-statement-parser/types2.yang", false);
33     private static final YangStatementSourceImpl TYPEFILE2 = new YangStatementSourceImpl(
34             "/semantic-statement-parser/types.yang", false);
35     private static final YangStatementSourceImpl TYPEFILE3 = new YangStatementSourceImpl(
36             "/semantic-statement-parser/simple-types.yang", false);
37     private static final YangStatementSourceImpl TYPEFILE4 = new YangStatementSourceImpl(
38             "/semantic-statement-parser/identityreftest.yang", false);
39
40     private static final QNameModule types2Module = QNameModule.create(URI.create("types2"),
41             SimpleDateFormatUtil.DEFAULT_DATE_REV);
42
43     private static final QName lfDecimal = QName.create(types2Module, "lf-decimal");
44     private static final QName lfMyString = QName.create(types2Module, "lf-my-string");
45     private static final QName lfInt8 = QName.create(types2Module, "lf-int8");
46     private static final QName lfInt16 = QName.create(types2Module, "lf-int16");
47     private static final QName lfInt32 = QName.create(types2Module, "lf-int32");
48     private static final QName lfInt64 = QName.create(types2Module, "lf-int64");
49     private static final QName lfUInt8 = QName.create(types2Module, "lf-uint8");
50     private static final QName lfUInt16 = QName.create(types2Module, "lf-uint16");
51     private static final QName lfUInt32 = QName.create(types2Module, "lf-uint32");
52     private static final QName lfUInt64 = QName.create(types2Module, "lf-uint64");
53     private static final QName lfBool = QName.create(types2Module, "lf-bool");
54
55     @Test
56     public void readAndParseYangFileTest() throws SourceException, ReactorException {
57         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58         addSources(reactor, TYPEFILE1, TYPEFILE2, TYPEFILE3, TYPEFILE4);
59         EffectiveSchemaContext result = reactor.buildEffective();
60         assertNotNull(result);
61
62         final LeafSchemaNode lfDecimalNode = (LeafSchemaNode) result.getDataChildByName(lfDecimal);
63         assertNotNull(lfDecimalNode);
64
65         assertTrue(lfDecimalNode.getType() instanceof DecimalTypeDefinition);
66         final DecimalTypeDefinition lfDecimalNodeType = (DecimalTypeDefinition) lfDecimalNode.getType();
67         assertEquals(2, lfDecimalNodeType.getFractionDigits().intValue());
68
69         final LeafSchemaNode lfInt8Node = (LeafSchemaNode) result.getDataChildByName(lfInt8);
70         assertNotNull(lfInt8Node);
71         assertEquals(BaseTypes.int8Type().getClass(), lfInt8Node.getType().getClass());
72
73         final LeafSchemaNode lfInt16Node = (LeafSchemaNode) result.getDataChildByName(lfInt16);
74         assertNotNull(lfInt16Node);
75         assertEquals(BaseTypes.int16Type().getClass(), lfInt16Node.getType().getClass());
76
77         final LeafSchemaNode lfInt32Node = (LeafSchemaNode) result.getDataChildByName(lfInt32);
78         assertNotNull(lfInt32Node);
79         assertEquals(BaseTypes.int32Type().getClass(), lfInt32Node.getType().getClass());
80
81         final LeafSchemaNode lfInt64Node = (LeafSchemaNode) result.getDataChildByName(lfInt64);
82         assertNotNull(lfInt64Node);
83         assertEquals(BaseTypes.int64Type().getClass(), lfInt64Node.getType().getClass());
84
85         final LeafSchemaNode lfUInt8Node = (LeafSchemaNode) result.getDataChildByName(lfUInt8);
86         assertNotNull(lfUInt8Node);
87         assertEquals(BaseTypes.uint8Type().getClass(), lfUInt8Node.getType().getClass());
88
89         final LeafSchemaNode lfUInt16Node = (LeafSchemaNode) result.getDataChildByName(lfUInt16);
90         assertNotNull(lfUInt16Node);
91         assertEquals(BaseTypes.uint16Type().getClass(), lfUInt16Node.getType().getClass());
92
93         final LeafSchemaNode lfUInt32Node = (LeafSchemaNode) result.getDataChildByName(lfUInt32);
94         assertNotNull(lfUInt32Node);
95         assertEquals(BaseTypes.uint32Type().getClass(), lfUInt32Node.getType().getClass());
96
97         final LeafSchemaNode lfUInt64Node = (LeafSchemaNode) result.getDataChildByName(lfUInt64);
98         assertNotNull(lfUInt64Node);
99         assertEquals(BaseTypes.uint64Type().getClass(), lfUInt64Node.getType().getClass());
100
101         final LeafSchemaNode lfBoolNode = (LeafSchemaNode) result.getDataChildByName(lfBool);
102         assertNotNull(lfBoolNode);
103         assertEquals(BaseTypes.booleanType().getClass(), lfBoolNode.getType().getClass());
104     }
105
106     private static  void addSources(final CrossSourceStatementReactor.BuildAction reactor, final StatementStreamSource... sources) {
107         for (StatementStreamSource source : sources) {
108             reactor.addSource(source);
109         }
110     }
111 }