Move Bug4079/Bug5410 tests from yang-parser to yang-model-util
[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.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;
26
27 public class Bug5410Test {
28     private static final String FOO_NS = "foo";
29     private static final String FOO_REV = "1970-01-01";
30
31     @Test
32     public void testYangPattern() throws Exception {
33         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5410");
34         assertNotNull(context);
35
36         final PatternConstraint pattern = getPatternConstraintOf(context, "leaf-with-pattern");
37
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);
41
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);
45
46         final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/00gEz3QeB7joSxBU3YtbHDm6NSkS1dKTQy3BWhwKKDS8nB5S//";
47         testPattern(javaRegexFromYang, ImmutableList.of(value), ImmutableList.of());
48     }
49
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();
58     }
59
60
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));
65         }
66         for (final String value : negativeMatches) {
67             assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", value.matches(javaRegex));
68         }
69     }
70
71     private static QName foo(final String localName) {
72         return QName.create(FOO_NS, FOO_REV, localName);
73     }
74 }