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