2 * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.stmt;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
21 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
22 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
24 public class Bug4623Test {
27 public void testStringTypeWithUnknownSchemaNodeAtTheEndOfTypeDefinition() throws Exception {
29 final File extdef = new File(getClass().getResource("/bugs/bug4623/extension-def.yang").toURI());
30 final File stringWithExt = new File(getClass().getResource("/bugs/bug4623/string-with-ext.yang").toURI());
33 final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);
35 final LeafSchemaNode leaf = (LeafSchemaNode) schemaContext.findModuleByName("types", null).getDataChildByName(
36 QName.create("urn:custom.types.demo", "1970-01-01", "leaf-length-pattern-unknown"));
39 Assert.assertNotNull(leaf);
41 final TypeDefinition<?> type = leaf.getType();
42 Assert.assertNotNull(type);
43 final List<UnknownSchemaNode> unknownSchemaNodes = type.getUnknownSchemaNodes();
44 Assert.assertNotNull(unknownSchemaNodes);
45 Assert.assertFalse(unknownSchemaNodes.size() == 0);
47 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0);
48 Assert.assertEquals(unknownSchemaNode.getNodeParameter(), "unknown");
49 Assert.assertEquals(unknownSchemaNode.getNodeType().getModule().getNamespace().toString(), "urn:simple.extension.typedefs");
51 final List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) type).getLengthConstraints();
52 final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
54 Assert.assertNotNull(lengthConstraints);
55 Assert.assertNotNull(patternConstraints);
56 Assert.assertFalse(lengthConstraints.size() == 0);
57 Assert.assertFalse(patternConstraints.size() == 0);
59 final LengthConstraint lengthConstraint = lengthConstraints.get(0);
60 Assert.assertEquals(lengthConstraint.getMin(), Integer.valueOf(2));
61 Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10));
63 final PatternConstraint patternConstraint = patternConstraints.get(0);
64 Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$");
68 public void testStringTypeWithUnknownSchemaNodeBetweenStringRestrictionStatements() throws Exception {
70 final File extdef = new File(getClass().getResource("/bugs/bug4623/extension-def.yang").toURI());
71 final File stringWithExt = new File(getClass().getResource("/bugs/bug4623/string-with-ext.yang").toURI());
74 final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);
76 final LeafSchemaNode leaf = (LeafSchemaNode) schemaContext.findModuleByName("types", null).getDataChildByName(
77 QName.create("urn:custom.types.demo", "1970-01-01", "leaf-length-unknown-pattern"));
80 Assert.assertNotNull(leaf);
82 final TypeDefinition<?> type = leaf.getType();
83 Assert.assertNotNull(type);
84 final List<UnknownSchemaNode> unknownSchemaNodes = type.getUnknownSchemaNodes();
85 Assert.assertNotNull(unknownSchemaNodes);
86 Assert.assertFalse(unknownSchemaNodes.size() == 0);
88 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0);
89 Assert.assertEquals(unknownSchemaNode.getNodeParameter(), "unknown");
90 Assert.assertEquals(unknownSchemaNode.getNodeType().getModule().getNamespace().toString(), "urn:simple.extension.typedefs");
92 final List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) type).getLengthConstraints();
93 final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
95 Assert.assertNotNull(lengthConstraints);
96 Assert.assertNotNull(patternConstraints);
97 Assert.assertFalse(lengthConstraints.size() == 0);
98 Assert.assertFalse(patternConstraints.size() == 0);
100 final LengthConstraint lengthConstraint = lengthConstraints.get(0);
101 Assert.assertEquals(lengthConstraint.getMin(), Integer.valueOf(2));
102 Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10));
104 final PatternConstraint patternConstraint = patternConstraints.get(0);
105 Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$");
109 public void testStringTypeWithUnknownSchemaNodeOnTheStartOfTypeDefinition() throws Exception {
111 final File extdef = new File(getClass().getResource("/bugs/bug4623/extension-def.yang").toURI());
112 final File stringWithExt = new File(getClass().getResource("/bugs/bug4623/string-with-ext.yang").toURI());
115 final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);
117 final LeafSchemaNode leaf = (LeafSchemaNode) schemaContext.findModuleByName("types", null).getDataChildByName(
118 QName.create("urn:custom.types.demo", "1970-01-01", "leaf-unknown-length-pattern"));
121 Assert.assertNotNull(leaf);
123 final TypeDefinition<?> type = leaf.getType();
124 Assert.assertNotNull(type);
125 final List<UnknownSchemaNode> unknownSchemaNodes = type.getUnknownSchemaNodes();
126 Assert.assertNotNull(unknownSchemaNodes);
127 Assert.assertFalse(unknownSchemaNodes.size() == 0);
129 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0);
130 Assert.assertEquals(unknownSchemaNode.getNodeParameter(), "unknown");
131 Assert.assertEquals(unknownSchemaNode.getNodeType().getModule().getNamespace().toString(), "urn:simple.extension.typedefs");
133 final List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) type).getLengthConstraints();
134 final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
136 Assert.assertNotNull(lengthConstraints);
137 Assert.assertNotNull(patternConstraints);
138 Assert.assertFalse(lengthConstraints.size() == 0);
139 Assert.assertFalse(patternConstraints.size() == 0);
141 final LengthConstraint lengthConstraint = lengthConstraints.get(0);
142 Assert.assertEquals(lengthConstraint.getMin(), Integer.valueOf(2));
143 Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10));
145 final PatternConstraint patternConstraint = patternConstraints.get(0);
146 Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$");