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.model.api.type.IntegerTypeDefinition;
19 import org.opendaylight.controller.model.api.type.PatternConstraint;
20 import org.opendaylight.controller.model.api.type.RangeConstraint;
21 import org.opendaylight.controller.model.util.Decimal64;
22 import org.opendaylight.controller.model.util.Int32;
23 import org.opendaylight.controller.model.util.StringType;
24 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
25 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
28 import org.opendaylight.controller.yang.model.api.Module;
29 import org.opendaylight.controller.yang.model.api.TypeDefinition;
30 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
32 public class YangModelParserTest {
34 private final String testFile1 = "src/test/resources/model/testfile1.yang";
35 private final String testFile2 = "src/test/resources/model/testfile2.yang";
36 private YangModelParser tested;
40 tested = new YangModelParserImpl();
44 public void testAugment() {
45 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
46 assertEquals(2, modules.size());
49 for(Module m : modules) {
50 if(m.getName().equals("types2")) {
56 AugmentationSchema augment = m2.getAugmentations().iterator().next();
57 assertNotNull(augment);
61 public void testAugmentTarget() {
62 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
63 assertEquals(2, modules.size());
66 for(Module m : modules) {
67 if(m.getName().equals("types1")) {
73 ContainerSchemaNode container = (ContainerSchemaNode)m1.getDataChildByName("interfaces");
74 assertNotNull(container);
76 ListSchemaNode list = (ListSchemaNode)container.getDataChildByName("ifEntry");
78 assertEquals(1, list.getAvailableAugmentations().size());
80 LeafSchemaNode leaf = (LeafSchemaNode)list.getDataChildByName("ds0ChannelNumber");
85 public void testTypedefRangesResolving() {
86 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
87 assertEquals(2, modules.size());
90 for(Module m : modules) {
91 if(m.getName().equals("types1")) {
97 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("testleaf");
98 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
99 assertTrue(testleaf.getType().getBaseType() instanceof Int32);
100 Int32 baseTypeCast = (Int32)baseType;
101 List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
102 assertEquals(2, ranges.size());
103 RangeConstraint range = ranges.get(0);
104 assertEquals(2L, range.getMin());
105 assertEquals(20L, range.getMax());
109 public void testTypedefPatternsResolving() {
110 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
111 assertEquals(2, modules.size());
114 for(Module m : modules) {
115 if(m.getName().equals("types1")) {
121 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-string-leaf");
122 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
123 assertTrue(testleaf.getType().getBaseType() instanceof StringType);
124 StringType baseTypeCast = (StringType)baseType;
126 Set<String> expectedRegularExpressions = new HashSet<String>();
127 expectedRegularExpressions.add("[a-k]*");
128 expectedRegularExpressions.add("[b-u]*");
129 expectedRegularExpressions.add("[e-z]*");
131 Set<String> actualRegularExpressions = new HashSet<String>();
132 List<PatternConstraint> patterns = baseTypeCast.getPatterns();
133 for(PatternConstraint pc : patterns) {
134 actualRegularExpressions.add(pc.getRegularExpression());
137 assertEquals(expectedRegularExpressions, actualRegularExpressions);
141 public void testTypedefLengthsResolving() {
142 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
143 assertEquals(2, modules.size());
146 for(Module m : modules) {
147 if(m.getName().equals("types1")) {
153 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-int-leaf");
154 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
155 assertTrue(testleaf.getType().getBaseType() instanceof IntegerTypeDefinition);
156 Int32 baseTypeCast = (Int32)baseType;
158 Long[][] expectedRanges = new Long[3][2];
159 expectedRanges[0] = new Long[]{10L, 20L};
160 expectedRanges[1] = new Long[]{12L, 18L};
161 expectedRanges[2] = new Long[]{14L, 16L};
163 List<RangeConstraint> actualRanges = baseTypeCast.getRangeStatements();
164 assertEquals(3, actualRanges.size());
165 for(int i = 0; i < actualRanges.size(); i++) {
166 assertEquals(expectedRanges[i][0], actualRanges.get(i).getMin());
167 assertEquals(expectedRanges[i][1], actualRanges.get(i).getMax());
172 public void testTypeDef() {
173 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
174 assertEquals(2, modules.size());
177 for(Module m : modules) {
178 if(m.getName().equals("types2")) {
184 LeafSchemaNode testleaf = (LeafSchemaNode)m2.getDataChildByName("nested-type-leaf");
185 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
186 assertTrue(testleaf.getType().getBaseType() instanceof Int32);
187 Int32 baseTypeCast = (Int32)baseType;
188 List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
189 assertEquals(2, ranges.size());
190 RangeConstraint range = ranges.get(0);
191 assertEquals(2L, range.getMin());
192 assertEquals(20L, range.getMax());
196 public void testTypedefDecimal1() {
197 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
198 assertEquals(2, modules.size());
201 for(Module m : modules) {
202 if(m.getName().equals("types1")) {
208 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf");
209 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
210 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
211 Decimal64 baseTypeCast = (Decimal64)baseType;
212 assertEquals(4, (int)baseTypeCast.getFractionDigits());
216 public void testTypedefDecimal2() {
217 Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
218 assertEquals(2, modules.size());
221 for(Module m : modules) {
222 if(m.getName().equals("types1")) {
228 LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf2");
229 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
230 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
231 Decimal64 baseTypeCast = (Decimal64)baseType;
232 assertEquals(5, (int)baseTypeCast.getFractionDigits());