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