Encapsulate regexes in a non-capturing group
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug4623Test.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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import java.io.File;
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;
23
24 public class Bug4623Test {
25
26     @Test
27     public void testStringTypeWithUnknownSchemaNodeAtTheEndOfTypeDefinition() throws Exception {
28         // given
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());
31
32         // when
33         final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);
34
35         final LeafSchemaNode leaf = (LeafSchemaNode) schemaContext.findModuleByName("types", null).getDataChildByName(
36                 QName.create("urn:custom.types.demo", "1970-01-01", "leaf-length-pattern-unknown"));
37
38         // then
39         Assert.assertNotNull(leaf);
40
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);
46
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");
50
51         final List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) type).getLengthConstraints();
52         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
53
54         Assert.assertNotNull(lengthConstraints);
55         Assert.assertNotNull(patternConstraints);
56         Assert.assertFalse(lengthConstraints.size() == 0);
57         Assert.assertFalse(patternConstraints.size() == 0);
58
59         final LengthConstraint lengthConstraint = lengthConstraints.get(0);
60         Assert.assertEquals(lengthConstraint.getMin(), Integer.valueOf(2));
61         Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10));
62
63         final PatternConstraint patternConstraint = patternConstraints.get(0);
64         Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$");
65     }
66
67     @Test
68     public void testStringTypeWithUnknownSchemaNodeBetweenStringRestrictionStatements() throws Exception {
69         // given
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());
72
73         // when
74         final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);
75
76         final LeafSchemaNode leaf = (LeafSchemaNode) schemaContext.findModuleByName("types", null).getDataChildByName(
77                 QName.create("urn:custom.types.demo", "1970-01-01", "leaf-length-unknown-pattern"));
78
79         // then
80         Assert.assertNotNull(leaf);
81
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);
87
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");
91
92         final List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) type).getLengthConstraints();
93         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
94
95         Assert.assertNotNull(lengthConstraints);
96         Assert.assertNotNull(patternConstraints);
97         Assert.assertFalse(lengthConstraints.size() == 0);
98         Assert.assertFalse(patternConstraints.size() == 0);
99
100         final LengthConstraint lengthConstraint = lengthConstraints.get(0);
101         Assert.assertEquals(lengthConstraint.getMin(), Integer.valueOf(2));
102         Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10));
103
104         final PatternConstraint patternConstraint = patternConstraints.get(0);
105         Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$");
106     }
107
108     @Test
109     public void testStringTypeWithUnknownSchemaNodeOnTheStartOfTypeDefinition() throws Exception {
110         // given
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());
113
114         // when
115         final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);
116
117         final LeafSchemaNode leaf = (LeafSchemaNode) schemaContext.findModuleByName("types", null).getDataChildByName(
118                 QName.create("urn:custom.types.demo", "1970-01-01", "leaf-unknown-length-pattern"));
119
120         // then
121         Assert.assertNotNull(leaf);
122
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);
128
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");
132
133         final List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) type).getLengthConstraints();
134         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
135
136         Assert.assertNotNull(lengthConstraints);
137         Assert.assertNotNull(patternConstraints);
138         Assert.assertFalse(lengthConstraints.size() == 0);
139         Assert.assertFalse(patternConstraints.size() == 0);
140
141         final LengthConstraint lengthConstraint = lengthConstraints.get(0);
142         Assert.assertEquals(lengthConstraint.getMin(), Integer.valueOf(2));
143         Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10));
144
145         final PatternConstraint patternConstraint = patternConstraints.get(0);
146         Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$");
147     }
148 }