BUG-8043: correct LengthConstraint definition
[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.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16
17 import org.junit.Ignore;
18 import org.junit.Test;
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 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25
26 public class AugmentArgumentParsingTest {
27
28     private static final StatementStreamSource IMPORTED = sourceForResource(
29             "/semantic-statement-parser/augment-arg-parsing/imported.yang");
30     private static final StatementStreamSource VALID_ARGS = sourceForResource(
31             "/semantic-statement-parser/augment-arg-parsing/root-valid-aug-args.yang");
32     private static final StatementStreamSource INVALID_REL1 = sourceForResource(
33             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel1.yang");
34     private static final StatementStreamSource INVALID_REL2 = sourceForResource(
35             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel2.yang");
36     private static final StatementStreamSource INVALID_ABS = sourceForResource(
37             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs.yang");
38     private static final StatementStreamSource INVALID_ABS_PREFIXED_NO_IMP = sourceForResource(
39             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs-no-imp.yang");
40     private static final StatementStreamSource INVALID_EMPTY = sourceForResource(
41             "/semantic-statement-parser/augment-arg-parsing/root-invalid-empty.yang");
42     private static final StatementStreamSource INVALID_XPATH = sourceForResource(
43             "/semantic-statement-parser/augment-arg-parsing/root-invalid-xpath.yang");
44
45     @Test
46     public void validAugAbsTest() throws ReactorException {
47
48         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49         reactor.addSources(IMPORTED, VALID_ARGS);
50
51         final EffectiveModelContext result = reactor.build();
52         assertNotNull(result);
53     }
54
55     @Test
56     public void invalidAugRel1Test() {
57
58         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
59         reactor.addSources(INVALID_REL1);
60
61         try {
62             reactor.build();
63             fail("reactor.process should fail due to invalid relative path");
64         } catch (ReactorException e) {
65             assertSourceExceptionCause(e, "Augment argument './aug1/aug11' is not valid");
66         }
67     }
68
69     @Test
70     public void invalidAugRel2Test() {
71
72         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
73         reactor.addSources(INVALID_REL2);
74
75         try {
76             reactor.build();
77             fail("reactor.process should fail due to invalid relative path");
78         } catch (ReactorException e) {
79             assertSourceExceptionCause(e, "Augment argument '../aug1/aug11' is not valid");
80         }
81     }
82
83     @Test
84     public void invalidAugAbs() {
85
86         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
87         reactor.addSources(INVALID_ABS);
88
89         try {
90             reactor.build();
91             fail("reactor.process should fail due to invalid absolute path");
92         } catch (ReactorException e) {
93             assertSourceExceptionCause(e, "Augment argument '//aug1/aug11/aug111' is not valid");
94         }
95     }
96
97     @Test
98     public void invalidAugAbsPrefixedNoImp() {
99
100         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
101         reactor.addSources(INVALID_ABS_PREFIXED_NO_IMP);
102
103         try {
104             reactor.build();
105             fail("reactor.process should fail due to missing import from augment path");
106         } catch (ReactorException e) {
107             assertSourceExceptionCause(e, "Failed to parse node 'imp:aug1'");
108         }
109     }
110
111     @Test
112     @Ignore
113     public void invalidAugEmptyTest() throws SourceException {
114
115         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
116         reactor.addSources(INVALID_EMPTY);
117
118         try {
119             reactor.build();
120             fail("reactor.process should fail due to empty path");
121         } catch (Exception e) {
122             assertEquals(IllegalArgumentException.class, e.getClass());
123         }
124     }
125
126     @Test
127     @Ignore
128     public void invalidAugXPathTest() throws SourceException {
129
130         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
131         reactor.addSources(INVALID_XPATH);
132
133         try {
134             reactor.build();
135             fail("reactor.process should fail due to invalid XPath");
136         } catch (Exception e) {
137             assertEquals(IllegalArgumentException.class, e.getClass());
138         }
139     }
140
141     private static void assertSourceExceptionCause(final Throwable e, final String start) {
142         final Throwable cause = e.getCause();
143         assertTrue(cause instanceof SourceException);
144         assertTrue(cause.getMessage().startsWith(start));
145     }
146 }