Remove ImportResolutionMode.SEMVER_LATEST
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentArgumentParsingTest.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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.ReactorDeclaredModel;
24
25 public class AugmentArgumentParsingTest {
26
27     private static final StatementStreamSource IMPORTED = sourceForResource(
28             "/semantic-statement-parser/augment-arg-parsing/imported.yang");
29     private static final StatementStreamSource VALID_ARGS = sourceForResource(
30             "/semantic-statement-parser/augment-arg-parsing/root-valid-aug-args.yang");
31     private static final StatementStreamSource INVALID_REL1 = sourceForResource(
32             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel1.yang");
33     private static final StatementStreamSource INVALID_REL2 = sourceForResource(
34             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel2.yang");
35     private static final StatementStreamSource INVALID_ABS = sourceForResource(
36             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs.yang");
37     private static final StatementStreamSource INVALID_ABS_PREFIXED_NO_IMP = sourceForResource(
38             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs-no-imp.yang");
39     private static final StatementStreamSource INVALID_EMPTY = sourceForResource(
40             "/semantic-statement-parser/augment-arg-parsing/root-invalid-empty.yang");
41     private static final StatementStreamSource INVALID_XPATH = sourceForResource(
42             "/semantic-statement-parser/augment-arg-parsing/root-invalid-xpath.yang");
43
44     @Test
45     public void validAugAbsTest() throws ReactorException {
46         final ReactorDeclaredModel result = RFC7950Reactors.defaultReactor().newBuild()
47                 .addSources(IMPORTED, VALID_ARGS)
48                 .build();
49         assertNotNull(result);
50     }
51
52     @Test
53     public void invalidAugRel1Test() {
54         assertSourceExceptionCause(assertReactorThrows(INVALID_REL1), "Augment argument './aug1/aug11' is not valid");
55     }
56
57     @Test
58     public void invalidAugRel2Test() {
59         assertSourceExceptionCause(assertReactorThrows(INVALID_REL2), "Augment argument '../aug1/aug11' is not valid");
60     }
61
62     @Test
63     public void invalidAugAbs() {
64         assertSourceExceptionCause(assertReactorThrows(INVALID_ABS),
65             "Augment argument '//aug1/aug11/aug111' is not valid");
66     }
67
68     @Test
69     public void invalidAugAbsPrefixedNoImp() {
70         assertSourceExceptionCause(assertReactorThrows(INVALID_ABS_PREFIXED_NO_IMP), "Failed to parse node 'imp:aug1'");
71     }
72
73     @Test
74     public void invalidAugEmptyTest() throws ReactorException {
75         final ReactorException ex = assertReactorThrows(INVALID_EMPTY);
76         final Throwable cause = ex.getCause();
77         assertThat(cause, instanceOf(SourceException.class));
78         assertThat(cause.getMessage(), startsWith("Schema node identifier must not be empty"));
79     }
80
81     @Test
82     public void invalidAugXPathTest() throws ReactorException {
83         final ReactorException ex = assertReactorThrows(INVALID_XPATH);
84         final Throwable cause = ex.getCause();
85         assertThat(cause, instanceOf(SourceException.class));
86         assertThat(cause.getMessage(), startsWith("Failed to parse node '-' in path '/aug1/-'"));
87
88         final Throwable nested = cause.getCause();
89         assertThat(nested, instanceOf(SourceException.class));
90         assertThat(nested.getMessage(), startsWith("Invalid identifier '-'"));
91     }
92
93     private static ReactorException assertReactorThrows(final StatementStreamSource source) {
94         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(source);
95         return assertThrows(ReactorException.class, () -> reactor.build());
96     }
97
98     private static void assertSourceExceptionCause(final Throwable exception, final String start) {
99         final Throwable cause = exception.getCause();
100         assertThat(cause, instanceOf(SourceException.class));
101         assertThat(cause.getMessage(), startsWith(start));
102     }
103 }