Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
15
16 import org.junit.Ignore;
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.EffectiveModelContext;
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 EffectiveModelContext result = RFC7950Reactors.defaultReactor().newBuild()
47                 .addSources(IMPORTED, VALID_ARGS)
48                 .build();
49         assertNotNull(result);
50     }
51
52     @Test
53     public void invalidAugRel1Test() {
54         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_REL1);
55
56         try {
57             reactor.build();
58             fail("reactor.process should fail due to invalid relative path");
59         } catch (ReactorException e) {
60             assertSourceExceptionCause(e, "Augment argument './aug1/aug11' is not valid");
61         }
62     }
63
64     @Test
65     public void invalidAugRel2Test() {
66         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_REL2);
67
68         try {
69             reactor.build();
70             fail("reactor.process should fail due to invalid relative path");
71         } catch (ReactorException e) {
72             assertSourceExceptionCause(e, "Augment argument '../aug1/aug11' is not valid");
73         }
74     }
75
76     @Test
77     public void invalidAugAbs() {
78         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_ABS);
79
80         try {
81             reactor.build();
82             fail("reactor.process should fail due to invalid absolute path");
83         } catch (ReactorException e) {
84             assertSourceExceptionCause(e, "Augment argument '//aug1/aug11/aug111' is not valid");
85         }
86     }
87
88     @Test
89     public void invalidAugAbsPrefixedNoImp() {
90         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_ABS_PREFIXED_NO_IMP);
91
92         try {
93             reactor.build();
94             fail("reactor.process should fail due to missing import from augment path");
95         } catch (ReactorException e) {
96             assertSourceExceptionCause(e, "Failed to parse node 'imp:aug1'");
97         }
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     @Ignore
102     public void invalidAugEmptyTest() throws ReactorException {
103         RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_EMPTY).build();
104         fail("reactor.process should fail due to empty path");
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     @Ignore
109     public void invalidAugXPathTest() throws ReactorException {
110         RFC7950Reactors.defaultReactor().newBuild().addSources(INVALID_XPATH).build();
111         fail("reactor.process should fail due to invalid XPath");
112     }
113
114     private static void assertSourceExceptionCause(final Throwable exception, final String start) {
115         final Throwable cause = exception.getCause();
116         assertTrue(cause instanceof SourceException);
117         assertTrue(cause.getMessage().startsWith(start));
118     }
119 }