2 * Copyright (c) 2017 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.yangtools.yang.parser.stmt.rfc6020;
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;
15 import com.google.common.collect.ImmutableList;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
24 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
25 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27 public class Bug5410Test {
28 private static final String FOO_NS = "foo";
29 private static final String FOO_REV = "1970-01-01";
32 public void testYangPattern() throws Exception {
33 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5410");
34 assertNotNull(context);
36 final PatternConstraint pattern = getPatternConstraintOf(context, "leaf-with-pattern");
38 final String rawRegex = pattern.getRawRegularExpression();
39 final String expectedYangRegex = "$0$.*|$1$[a-zA-Z0-9./]{1,8}$[a-zA-Z0-9./]{22}|$5$(rounds=\\d+$)?[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}";
40 assertEquals(expectedYangRegex, rawRegex);
42 final String javaRegexFromYang = pattern.getRegularExpression();
43 final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$(rounds=\\d+\\$)?[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}$";
44 assertEquals(expectedJavaRegex, javaRegexFromYang);
46 final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/00gEz3QeB7joSxBU3YtbHDm6NSkS1dKTQy3BWhwKKDS8nB5S//";
47 testPattern(javaRegexFromYang, ImmutableList.of(value), ImmutableList.of());
50 private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) {
51 final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName));
52 assertTrue(dataChildByName instanceof LeafSchemaNode);
53 final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName;
54 final TypeDefinition<? extends TypeDefinition<?>> type = leaf.getType();
55 assertTrue(type instanceof StringTypeDefinition);
56 final StringTypeDefinition strType = (StringTypeDefinition) type;
57 return strType.getPatternConstraints().iterator().next();
61 private static void testPattern(final String javaRegex, final List<String> positiveMatches,
62 final List<String> negativeMatches) {
63 for (final String value : positiveMatches) {
64 assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'", value.matches(javaRegex));
66 for (final String value : negativeMatches) {
67 assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", value.matches(javaRegex));
71 private static QName foo(final String localName) {
72 return QName.create(FOO_NS, FOO_REV, localName);