Fix checkstyle in yang-parser-impl and enable enforcement
[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.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.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
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
47         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
48         reactor.addSources(IMPORTED, VALID_ARGS);
49
50         final EffectiveModelContext result = reactor.build();
51         assertNotNull(result);
52     }
53
54     @Test
55     public void invalidAugRel1Test() {
56
57         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58         reactor.addSources(INVALID_REL1);
59
60         try {
61             reactor.build();
62             fail("reactor.process should fail due to invalid relative path");
63         } catch (ReactorException e) {
64             assertSourceExceptionCause(e, "Augment argument './aug1/aug11' is not valid");
65         }
66     }
67
68     @Test
69     public void invalidAugRel2Test() {
70
71         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
72         reactor.addSources(INVALID_REL2);
73
74         try {
75             reactor.build();
76             fail("reactor.process should fail due to invalid relative path");
77         } catch (ReactorException e) {
78             assertSourceExceptionCause(e, "Augment argument '../aug1/aug11' is not valid");
79         }
80     }
81
82     @Test
83     public void invalidAugAbs() {
84
85         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
86         reactor.addSources(INVALID_ABS);
87
88         try {
89             reactor.build();
90             fail("reactor.process should fail due to invalid absolute path");
91         } catch (ReactorException e) {
92             assertSourceExceptionCause(e, "Augment argument '//aug1/aug11/aug111' is not valid");
93         }
94     }
95
96     @Test
97     public void invalidAugAbsPrefixedNoImp() {
98
99         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
100         reactor.addSources(INVALID_ABS_PREFIXED_NO_IMP);
101
102         try {
103             reactor.build();
104             fail("reactor.process should fail due to missing import from augment path");
105         } catch (ReactorException e) {
106             assertSourceExceptionCause(e, "Failed to parse node 'imp:aug1'");
107         }
108     }
109
110     @Test(expected = IllegalArgumentException.class)
111     @Ignore
112     public void invalidAugEmptyTest() throws ReactorException {
113
114         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
115         reactor.addSources(INVALID_EMPTY);
116
117         reactor.build();
118         fail("reactor.process should fail due to empty path");
119     }
120
121     @Test(expected = IllegalArgumentException.class)
122     @Ignore
123     public void invalidAugXPathTest() throws ReactorException {
124
125         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
126         reactor.addSources(INVALID_XPATH);
127         reactor.build();
128         fail("reactor.process should fail due to invalid XPath");
129     }
130
131     private static void assertSourceExceptionCause(final Throwable exception, final String start) {
132         final Throwable cause = exception.getCause();
133         assertTrue(cause instanceof SourceException);
134         assertTrue(cause.getMessage().startsWith(start));
135     }
136 }