2 * Copyright (c) 2013 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.controller.yang.model.parser.impl;
10 import static org.junit.Assert.*;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
19 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
22 import org.opendaylight.controller.yang.model.api.Module;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
25 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
26 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
27 import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition;
28 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
29 import org.opendaylight.controller.yang.model.util.Decimal64;
30 import org.opendaylight.controller.yang.model.util.Int32;
31 import org.opendaylight.controller.yang.model.util.StringType;
33 public class YangModelParserTest {
35 private final String testFile1 = "src/test/resources/model/testfile1.yang";
36 private final String testFile2 = "src/test/resources/model/testfile2.yang";
37 private YangModelParser tested;
41 tested = new YangModelParserImpl();
45 public void testAugment() {
46 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
47 assertEquals(2, modules.size());
50 for(Module m : modules) {
51 if(m.getName().equals("types2")) {
57 AugmentationSchema augment = m2.getAugmentations().iterator().next();
58 assertNotNull(augment);
62 public void testAugmentTarget() {
63 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
64 assertEquals(2, modules.size());
67 for(Module m : modules) {
68 if(m.getName().equals("types1")) {
74 ContainerSchemaNode container = (ContainerSchemaNode)m1.getDataChildByName("interfaces");
75 assertNotNull(container);
77 ListSchemaNode list = (ListSchemaNode)container.getDataChildByName("ifEntry");
79 assertEquals(1, list.getAvailableAugmentations().size());
81 LeafSchemaNode leaf = (LeafSchemaNode)list.getDataChildByName("ds0ChannelNumber");
86 public void testTypedefRangesResolving() {
87 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
88 assertEquals(2, modules.size());
91 for(Module m : modules) {
92 if(m.getName().equals("types1")) {
98 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("testleaf");
99 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
100 assertTrue(testleaf.getType().getBaseType() instanceof Int32);
101 Int32 baseTypeCast = (Int32)baseType;
102 List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
103 assertEquals(1, ranges.size());
104 RangeConstraint range = ranges.get(0);
105 assertEquals(11L, range.getMin());
106 assertEquals(20L, range.getMax());
110 public void testTypedefPatternsResolving() {
111 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
112 assertEquals(2, modules.size());
115 for(Module m : modules) {
116 if(m.getName().equals("types1")) {
122 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-string-leaf");
123 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
124 assertTrue(testleaf.getType().getBaseType() instanceof StringType);
125 StringType baseTypeCast = (StringType)baseType;
127 Set<String> expectedRegularExpressions = new HashSet<String>();
128 expectedRegularExpressions.add("[a-k]*");
129 expectedRegularExpressions.add("[b-u]*");
130 expectedRegularExpressions.add("[e-z]*");
132 Set<String> actualRegularExpressions = new HashSet<String>();
133 List<PatternConstraint> patterns = baseTypeCast.getPatterns();
134 for(PatternConstraint pc : patterns) {
135 actualRegularExpressions.add(pc.getRegularExpression());
138 assertEquals(expectedRegularExpressions, actualRegularExpressions);
142 public void testTypedefLengthsResolving() {
143 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
144 assertEquals(2, modules.size());
147 for(Module m : modules) {
148 if(m.getName().equals("types1")) {
154 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("leaf-with-length");
155 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
156 assertTrue(testleaf.getType().getBaseType() instanceof StringTypeDefinition);
157 StringType baseTypeCast = (StringType)baseType;
159 List<LengthConstraint> actualLengths = baseTypeCast.getLengthStatements();
160 assertEquals(1, actualLengths.size());
161 assertEquals(7L, actualLengths.get(0).getMin());
162 assertEquals(10L, actualLengths.get(0).getMax());
166 public void testTypeDef() {
167 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
168 assertEquals(2, modules.size());
171 for(Module m : modules) {
172 if(m.getName().equals("types2")) {
178 LeafSchemaNode testleaf = (LeafSchemaNode)m2.getDataChildByName("nested-type-leaf");
179 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
180 assertTrue(testleaf.getType().getBaseType() instanceof Int32);
181 Int32 baseTypeCast = (Int32)baseType;
182 List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
183 assertEquals(1, ranges.size());
184 RangeConstraint range = ranges.get(0);
185 assertEquals(11L, range.getMin());
186 assertEquals(20L, range.getMax());
190 public void testTypedefDecimal1() {
191 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
192 assertEquals(2, modules.size());
195 for(Module m : modules) {
196 if(m.getName().equals("types1")) {
202 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf");
203 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
204 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
205 Decimal64 baseTypeCast = (Decimal64)baseType;
206 assertEquals(4, (int)baseTypeCast.getFractionDigits());
210 public void testTypedefDecimal2() {
211 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
212 assertEquals(2, modules.size());
215 for(Module m : modules) {
216 if(m.getName().equals("types1")) {
222 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf2");
223 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
224 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
225 Decimal64 baseTypeCast = (Decimal64)baseType;
226 assertEquals(5, (int)baseTypeCast.getFractionDigits());