c8aeac8f7265d4f02869569468f829342b51de1d
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6180Test.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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.File;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.regex.Pattern;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
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
29 public class Bug6180Test {
30
31     @Test
32     public void stringTest() throws Exception {
33         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
34                 "/bugs/bug6180/string-test.yang").toURI()));
35         assertNotNull(schemaContext);
36         assertEquals(1, schemaContext.getModules().size());
37         final Module module = schemaContext.getModules().iterator().next();
38         assertEquals(Optional.of("    1. this text contains \"string enclosed in double quotes\" and"
39                 + " special characters: \\,\n,\t          2. this text contains \"string enclosed in double quotes\""
40                 + " and special characters: \\,\n,\n,                     3. this text contains \"string enclosed in"
41                 + " double quotes\" and special characters: \\,\n,\t      "), module.getDescription());
42     }
43
44     @Test
45     public void doubleQuotesTest() throws Exception {
46         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
47                 "/bugs/bug6180/double-quotes.yang").toURI()));
48         assertNotNull(schemaContext);
49         verifyDoubleQuotesExpression(schemaContext);
50     }
51
52     @Test
53     public void doubleQuotesSinbleInsideTest() throws Exception {
54         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
55                 "/bugs/bug6180/double-quotes-single-inside.yang").toURI()));
56         assertNotNull(schemaContext);
57         verifySingleQuotesExpression(schemaContext);
58     }
59
60     @Test
61     public void singleQuotesTest() throws Exception {
62         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
63                 "/bugs/bug6180/single-quotes.yang").toURI()));
64         assertNotNull(schemaContext);
65         verifyDoubleQuotesExpression(schemaContext);
66     }
67
68     private static void verifyDoubleQuotesExpression(final SchemaContext schemaContext) {
69         final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
70         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
71         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
72         assertEquals("/foo != \"bar\"", bar.getWhenCondition().orElseThrow().toString());
73
74         final Collection<? extends TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
75         assertEquals(1, typeDefinitions.size());
76         final TypeDefinition<?> type = typeDefinitions.iterator().next();
77         assertTrue(type instanceof StringTypeDefinition);
78         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
79         assertEquals(1, patternConstraints.size());
80         final PatternConstraint pattern = patternConstraints.iterator().next();
81         assertEquals("^(?:\".*\")$", pattern.getJavaPatternString());
82         assertTrue(Pattern.compile(pattern.getJavaPatternString()).matcher("\"enclosed string in quotes\"").matches());
83     }
84
85     private static void verifySingleQuotesExpression(final SchemaContext schemaContext) {
86         final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
87         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
88         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
89         assertEquals("/foo != 'bar'", bar.getWhenCondition().orElseThrow().toString());
90
91         final Collection<? extends TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
92         assertEquals(1, typeDefinitions.size());
93         final TypeDefinition<?> type = typeDefinitions.iterator().next();
94         assertTrue(type instanceof StringTypeDefinition);
95         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
96         assertEquals(1, patternConstraints.size());
97         final PatternConstraint pattern = patternConstraints.iterator().next();
98         assertEquals("^(?:'.*')$", pattern.getJavaPatternString());
99         assertTrue(Pattern.compile(pattern.getJavaPatternString()).matcher("'enclosed string in quotes'").matches());
100     }
101 }