ecdfc881f0d1b101b50046dc6a2e6c36e513395c
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6870Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2021 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.CoreMatchers.startsWith;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertThrows;
16
17 import java.util.List;
18 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
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.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
31
32 public class Bug6870Test {
33     @Test
34     public void valid11Test() throws Exception {
35         final EffectiveModelContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6870/foo.yang");
36
37         assertModifier(schemaContext, ModifierKind.INVERT_MATCH, QName.create("foo", "root"),
38             QName.create("foo", "my-leaf"));
39         assertModifier(schemaContext, null, QName.create("foo", "root"), QName.create("foo", "my-leaf-2"));
40     }
41
42     private static void assertModifier(final EffectiveModelContext schemaContext,
43             final ModifierKind expectedModifierKind, final QName... qnames) {
44         final DataSchemaNode findNode = schemaContext.findDataTreeChild(qnames).orElseThrow();
45         assertThat(findNode, instanceOf(LeafSchemaNode.class));
46         final LeafSchemaNode myLeaf = (LeafSchemaNode) findNode;
47
48         final TypeDefinition<? extends TypeDefinition<?>> type = myLeaf.getType();
49         assertThat(type, instanceOf(StringTypeDefinition.class));
50         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
51         assertEquals(1, patternConstraints.size());
52         assertEquals(Optional.ofNullable(expectedModifierKind), patternConstraints.iterator().next().getModifier());
53     }
54
55     @Test
56     public void invalid11Test() {
57         final ReactorException ex = assertThrows(ReactorException.class,
58             () -> StmtTestUtils.parseYangSource("/rfc7950/bug6870/invalid11.yang"));
59         final Throwable cause = ex.getCause();
60         assertThat(cause, instanceOf(SourceException.class));
61         assertThat(cause.getMessage(), startsWith("'Invert-match' is not valid argument of modifier statement"));
62     }
63
64     @Test
65     public void invalid10Test() {
66         final ReactorException ex = assertThrows(ReactorException.class,
67             () -> StmtTestUtils.parseYangSource("/rfc7950/bug6870/invalid10.yang"));
68         final Throwable cause = ex.getCause();
69         assertThat(cause, instanceOf(SourceException.class));
70         assertThat(cause.getMessage(), startsWith("modifier is not a YANG statement or use of extension"));
71     }
72 }