2 * Copyright (c) 2016 Cisco Systems, Inc. and others. 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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17 import org.junit.jupiter.api.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;
25 class AugmentArgumentParsingTest {
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");
45 void validAugAbsTest() throws ReactorException {
46 final ReactorDeclaredModel result = RFC7950Reactors.defaultReactor().newBuild()
47 .addSources(IMPORTED, VALID_ARGS)
49 assertNotNull(result);
53 void invalidAugRel1Test() {
54 assertSourceExceptionCause(assertReactorThrows(INVALID_REL1), "Augment argument './aug1/aug11' is not valid");
58 void invalidAugRel2Test() {
59 assertSourceExceptionCause(assertReactorThrows(INVALID_REL2), "Augment argument '../aug1/aug11' is not valid");
63 void invalidAugAbs() {
64 assertSourceExceptionCause(assertReactorThrows(INVALID_ABS),
65 "Augment argument '//aug1/aug11/aug111' is not valid");
69 void invalidAugAbsPrefixedNoImp() {
70 assertSourceExceptionCause(assertReactorThrows(INVALID_ABS_PREFIXED_NO_IMP), "Failed to parse node 'imp:aug1'");
74 void invalidAugEmptyTest() throws ReactorException {
75 final ReactorException ex = assertReactorThrows(INVALID_EMPTY);
76 final Throwable cause = ex.getCause();
77 assertInstanceOf(SourceException.class, cause);
78 assertThat(cause.getMessage(), startsWith("Schema node identifier must not be empty"));
82 void invalidAugXPathTest() throws ReactorException {
83 final ReactorException ex = assertReactorThrows(INVALID_XPATH);
84 final Throwable cause = ex.getCause();
85 assertInstanceOf(SourceException.class, cause);
86 assertThat(cause.getMessage(), startsWith("Failed to parse node '-' in path '/aug1/-'"));
88 final Throwable nested = cause.getCause();
89 assertInstanceOf(SourceException.class, nested);
90 assertThat(nested.getMessage(), startsWith("Invalid identifier '-'"));
93 private static ReactorException assertReactorThrows(final StatementStreamSource source) {
94 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(source);
95 return assertThrows(ReactorException.class, () -> reactor.build());
98 private static void assertSourceExceptionCause(final Throwable exception, final String start) {
99 final Throwable cause = exception.getCause();
100 assertInstanceOf(SourceException.class, cause);
101 assertThat(cause.getMessage(), startsWith(start));