2 * Copyright (c) 2017 Opendaylight. 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
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.model.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;
24 public class Bug6885Test {
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);
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);
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);
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);
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);
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);
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);
76 private static void testForIfFeature(final String yangSrcPath, final String exMsg) throws URISyntaxException,
77 SourceException, IOException, YangSyntaxErrorException {
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));
88 private static void testForWhen(final String yangSrcPath, final String exMsg) throws URISyntaxException,
89 SourceException, IOException, YangSyntaxErrorException {
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));