Further yang-parser-impl checkstyle fixes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / Bug5410Test.java
1 /*
2  * Copyright (c) 2017 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.parser.stmt.rfc6020;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.ImmutableList;
16 import java.io.ByteArrayOutputStream;
17 import java.io.PrintStream;
18 import java.io.UnsupportedEncodingException;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
27 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.util.RegexUtils;
29 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30
31 public class Bug5410Test {
32     private static final String FOO_NS = "foo";
33     private static final String FOO_REV = "1970-01-01";
34
35     @Test
36     public void testJavaRegexFromXSD() {
37         testPattern("^[^:]+$", "^\\^[^:]+\\$$", ImmutableList.of("^a$", "^abc$"),
38                 ImmutableList.of("abc$", "^abc", "^a:bc$"));
39         testPattern("^[$^]$", "^\\^[$^]\\$$", ImmutableList.of("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$"));
40         testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^"));
41         testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'"));
42
43         testPattern("[a-z&&[^m-p]]+", "^[a-z&&[^m-p]]+$", ImmutableList.of("a", "z", "az"),
44                 ImmutableList.of("m", "anz", "o"));
45         testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
46                 ImmutableList.of("^a$", "^^$", "^_$"));
47
48         testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"),
49                 ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
50         testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$",
51                 ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
52                 ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
53         testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$",
54                 ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
55                 ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
56         testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"),
57                 ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
58     }
59
60     @Test
61     public void testInvalidXSDRegexes() throws UnsupportedEncodingException {
62         testInvalidPattern("$^a^[$^\\]", "Unclosed character class");
63         testInvalidPattern("$(\\)", "Unclosed group");
64     }
65
66     @Test
67     public void testJavaPattern() {
68         testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a"));
69         testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\"));
70     }
71
72     @Test
73     public void testYangPattern() throws Exception {
74         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5410");
75         assertNotNull(context);
76
77         final PatternConstraint pattern = getPatternConstraintOf(context, "leaf-with-pattern");
78
79         final String rawRegex = pattern.getRawRegularExpression();
80         final String expectedYangRegex = "$0$.*|$1$[a-zA-Z0-9./]{1,8}$[a-zA-Z0-9./]{22}|$5$(rounds=\\d+$)?"
81                 + "[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{43}|$6$(rounds=\\d+$)?[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{86}";
82         assertEquals(expectedYangRegex, rawRegex);
83
84         final String javaRegexFromYang = pattern.getRegularExpression();
85         final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$"
86                 + "(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43}|\\$6\\$(rounds=\\d+\\$)?"
87                 + "[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86}$";
88         assertEquals(expectedJavaRegex, javaRegexFromYang);
89
90         final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/"
91                 + "00gEz3QeB7joSxBU3YtbHDm6NSkS1dKTQy3BWhwKKDS8nB5S//";
92         testPattern(javaRegexFromYang, ImmutableList.of(value), ImmutableList.of());
93     }
94
95     @Test
96     public void testCaret() {
97         testPattern("^", "\\^");
98     }
99
100     @Test
101     public void testTextCaret() {
102         testPattern("abc^", "abc\\^");
103     }
104
105     @Test
106     public void testTextDollar() {
107         testPattern("abc$", "abc\\$");
108     }
109
110     @Test
111     public void testCaretCaret() {
112         testPattern("^^", "\\^\\^");
113     }
114
115     @Test
116     public void testCaretDollar() {
117         testPattern("^$", "\\^\\$");
118     }
119
120     @Test
121     public void testDot() {
122         testPattern(".", ".");
123     }
124
125     @Test
126     public void testNotColon() {
127         testPattern("[^:]+", "[^:]+");
128     }
129
130     @Test
131     public void testDollar() {
132         testPattern("$", "\\$");
133     }
134
135     @Test
136     public void testDollarOneDollar() {
137         testPattern("$1$", "\\$1\\$");
138     }
139
140     @Test
141     public void testDollarPercentRange() {
142         testPattern("[$-%]+", "[$-%]+");
143     }
144
145     @Test
146     public void testDollarRange() {
147         testPattern("[$$]+", "[$$]+");
148     }
149
150     @Test
151     public void testDollarCaretRange() {
152         testPattern("[$^]+", "[$^]+");
153     }
154
155     @Test
156     public void testSimple() {
157         testPattern("abc", "abc");
158     }
159
160     @Test
161     public void testDotPlus() {
162         testPattern(".+", ".+");
163     }
164
165     @Test
166     public void testDotStar() {
167         testPattern(".*", ".*");
168     }
169
170     @Test
171     public void testSimpleOptional() {
172         testPattern("a?", "a?");
173     }
174
175     @Test
176     public void testRangeOptional() {
177         testPattern("[a-z]?", "[a-z]?");
178     }
179
180     private static void testPattern(final String xsdRegex, final String expectedJavaRegex,
181             final List<String> positiveMatches, final List<String> negativeMatches) {
182         final String javaRegexFromXSD = javaRegexFromXSD(xsdRegex);
183         assertEquals(expectedJavaRegex, javaRegexFromXSD);
184
185         for (final String value : positiveMatches) {
186             assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
187                     testMatch(javaRegexFromXSD, value));
188         }
189         for (final String value : negativeMatches) {
190             assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
191                     testMatch(javaRegexFromXSD, value));
192         }
193     }
194
195     private static void testPattern(final String javaRegex, final List<String> positiveMatches,
196             final List<String> negativeMatches) {
197         for (final String value : positiveMatches) {
198             assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'",
199                     testMatch(javaRegex, value));
200         }
201         for (final String value : negativeMatches) {
202             assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
203         }
204     }
205
206     private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
207         testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of());
208     }
209
210     private static boolean testMatch(final String javaRegex, final String value) {
211         return value.matches(javaRegex);
212     }
213
214     private static String javaRegexFromXSD(final String xsdRegex) {
215         return RegexUtils.getJavaRegexFromXSD(xsdRegex);
216     }
217
218     private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) {
219         final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName));
220         assertTrue(dataChildByName instanceof LeafSchemaNode);
221         final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName;
222         final TypeDefinition<? extends TypeDefinition<?>> type = leaf.getType();
223         assertTrue(type instanceof StringTypeDefinition);
224         final StringTypeDefinition strType = (StringTypeDefinition) type;
225         return strType.getPatternConstraints().iterator().next();
226     }
227
228     private static QName foo(final String localName) {
229         return QName.create(FOO_NS, FOO_REV, localName);
230     }
231
232     @SuppressWarnings("checkstyle:regexpSinglelineJava")
233     private static void testInvalidPattern(final String xsdRegex, final String expectedMessage)
234             throws UnsupportedEncodingException {
235         final PrintStream stdout = System.out;
236         final ByteArrayOutputStream output = new ByteArrayOutputStream();
237         System.setOut(new PrintStream(output, true, "UTF-8"));
238
239         javaRegexFromXSD(xsdRegex);
240
241         final String testLog = output.toString();
242         assertTrue(testLog.contains(expectedMessage));
243         System.setOut(stdout);
244     }
245 }