Merge branch 'master' of ../controller
[yangtools.git] / yang / 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.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
14
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.ReactorDeclaredModel;
23
24 public class AugmentArgumentParsingTest {
25
26     private static final StatementStreamSource IMPORTED = sourceForResource(
27             "/semantic-statement-parser/augment-arg-parsing/imported.yang");
28     private static final StatementStreamSource VALID_ARGS = sourceForResource(
29             "/semantic-statement-parser/augment-arg-parsing/root-valid-aug-args.yang");
30     private static final StatementStreamSource INVALID_REL1 = sourceForResource(
31             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel1.yang");
32     private static final StatementStreamSource INVALID_REL2 = sourceForResource(
33             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel2.yang");
34     private static final StatementStreamSource INVALID_ABS = sourceForResource(
35             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs.yang");
36     private static final StatementStreamSource INVALID_ABS_PREFIXED_NO_IMP = sourceForResource(
37             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs-no-imp.yang");
38     private static final StatementStreamSource INVALID_EMPTY = sourceForResource(
39             "/semantic-statement-parser/augment-arg-parsing/root-invalid-empty.yang");
40     private static final StatementStreamSource INVALID_XPATH = sourceForResource(
41             "/semantic-statement-parser/augment-arg-parsing/root-invalid-xpath.yang");
42
43     @Test
44     public void validAugAbsTest() throws ReactorException {
45         final ReactorDeclaredModel result = RFC7950Reactors.defaultReactor().newBuild()
46                 .addSources(IMPORTED, VALID_ARGS)
47                 .build();
48         assertNotNull(result);
49     }
50
51     @Test
52     public void invalidAugRel1Test() {
53         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_REL1);
54
55         try {
56             reactor.build();
57             fail("reactor.process should fail due to invalid relative path");
58         } catch (ReactorException e) {
59             assertSourceExceptionCause(e, "Augment argument './aug1/aug11' is not valid");
60         }
61     }
62
63     @Test
64     public void invalidAugRel2Test() {
65         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_REL2);
66
67         try {
68             reactor.build();
69             fail("reactor.process should fail due to invalid relative path");
70         } catch (ReactorException e) {
71             assertSourceExceptionCause(e, "Augment argument '../aug1/aug11' is not valid");
72         }
73     }
74
75     @Test
76     public void invalidAugAbs() {
77         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_ABS);
78
79         try {
80             reactor.build();
81             fail("reactor.process should fail due to invalid absolute path");
82         } catch (ReactorException e) {
83             assertSourceExceptionCause(e, "Augment argument '//aug1/aug11/aug111' is not valid");
84         }
85     }
86
87     @Test
88     public void invalidAugAbsPrefixedNoImp() {
89         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_ABS_PREFIXED_NO_IMP);
90
91         try {
92             reactor.build();
93             fail("reactor.process should fail due to missing import from augment path");
94         } catch (ReactorException e) {
95             assertSourceExceptionCause(e, "Failed to parse node 'imp:aug1'");
96         }
97     }
98
99     @Test(expected = IllegalArgumentException.class)
100     @Ignore
101     public void invalidAugEmptyTest() throws ReactorException {
102         RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_EMPTY).build();
103         fail("reactor.process should fail due to empty path");
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     @Ignore
108     public void invalidAugXPathTest() throws ReactorException {
109         RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_XPATH).build();
110         fail("reactor.process should fail due to invalid XPath");
111     }
112
113     private static void assertSourceExceptionCause(final Throwable exception, final String start) {
114         final Throwable cause = exception.getCause();
115         assertTrue(cause instanceof SourceException);
116         assertTrue(cause.getMessage().startsWith(start));
117     }
118 }