4c7c5cd5c385eaf23580c70a4a77d4f0f36ec352
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6885Test.java
1 /*
2  * Copyright (c) 2017 Opendaylight.  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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
23
24 public class Bug6885Test {
25
26     @Test
27     public void validYang10Test() throws Exception {
28         // Yang 1.0 allows "if-feature" and "when" on list keys
29         final SchemaContext schemaContext =
30                 StmtTestUtils.parseYangSource("/rfc7950/list-keys-test/correct-list-keys-test.yang");
31         assertNotNull(schemaContext);
32     }
33
34     @Test
35     public void invalidListLeafKeyTest1() throws Exception {
36         final String exceptionMessage = "(urn:ietf:params:xml:ns:yang:yin:1)when statement is not allowed in "
37                 + "(incorrect-list-keys-test?revision=2017-02-06)a2 leaf statement which is specified as a list key.";
38         testForWhen("/rfc7950/list-keys-test/incorrect-list-keys-test.yang", exceptionMessage);
39     }
40
41     @Test
42     public void invalidListLeafKeyTest2() throws Exception {
43         final String exceptionMessage = "(urn:ietf:params:xml:ns:yang:yin:1)if-feature statement is not allowed in "
44                 + "(incorrect-list-keys-test1?revision=2017-02-06)b leaf statement which is specified as a list key.";
45         testForIfFeature("/rfc7950/list-keys-test/incorrect-list-keys-test1.yang", exceptionMessage);
46     }
47
48     @Test
49     public void invalidListUsesLeafKeyTest() throws Exception {
50         final String exceptionMessage = "(urn:ietf:params:xml:ns:yang:yin:1)if-feature statement is not allowed in "
51                 + "(incorrect-list-keys-test2?revision=2017-02-06)a1 leaf statement which is specified as a list key.";
52         testForIfFeature("/rfc7950/list-keys-test/incorrect-list-keys-test2.yang", exceptionMessage);
53     }
54
55     @Test
56     public void invalidListUsesLeafKeyTest1() throws Exception {
57         final String exceptionMessage = "(urn:ietf:params:xml:ns:yang:yin:1)when statement is not allowed in "
58                 + "(incorrect-list-keys-test3?revision=2017-02-06)a2 leaf statement which is specified as a list key.";
59         testForWhen("/rfc7950/list-keys-test/incorrect-list-keys-test3.yang", exceptionMessage);
60     }
61
62     @Test
63     public void invalidListUsesLeafKeyTest2() throws Exception {
64         final String exceptionMessage = "(urn:ietf:params:xml:ns:yang:yin:1)if-feature statement is not allowed in "
65                 + "(incorrect-list-keys-test4?revision=2017-02-06)a1 leaf statement which is specified as a list key.";
66         testForIfFeature("/rfc7950/list-keys-test/incorrect-list-keys-test4.yang", exceptionMessage);
67     }
68
69     @Test
70     public void invalidListUsesRefineLeafKeyTest() throws Exception {
71         final String exceptionMessage = "(urn:ietf:params:xml:ns:yang:yin:1)if-feature statement is not allowed in "
72                 + "(incorrect-list-keys-test5?revision=2017-02-06)a1 leaf statement which is specified as a list key.";
73         testForIfFeature("/rfc7950/list-keys-test/incorrect-list-keys-test5.yang", exceptionMessage);
74     }
75
76     private static void testForIfFeature(final String yangSrcPath, final String exMsg) throws URISyntaxException,
77             SourceException, IOException, YangSyntaxErrorException {
78         try {
79             StmtTestUtils.parseYangSource(yangSrcPath);
80             fail("Test must fail: IF-FEATURE substatement is not allowed in LIST keys");
81         } catch (final ReactorException e) {
82             final Throwable cause = e.getCause();
83             assertTrue(cause instanceof SourceException);
84             assertTrue(cause.getMessage().startsWith(exMsg));
85         }
86     }
87
88     private static void testForWhen(final String yangSrcPath, final String exMsg) throws URISyntaxException,
89             SourceException, IOException, YangSyntaxErrorException {
90         try {
91             StmtTestUtils.parseYangSource(yangSrcPath);
92             fail("Test must fail: WHEN substatement is not allowed in LIST keys");
93         } catch (final ReactorException e) {
94             final Throwable cause = e.getCause();
95             assertTrue(cause instanceof SourceException);
96             assertTrue(cause.getMessage().startsWith(exMsg));
97         }
98     }
99 }