Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / ListKeysTest.java
1 /*
2  * Copyright (c) 2015 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.retest;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
24
25 public class ListKeysTest {
26
27     @Test
28     public void correctListKeysTest() throws ReactorException {
29
30         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
31                 "/list-keys-test/correct-list-keys-test.yang", false);
32
33         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
34         addSources(reactor, yangFile);
35
36         final EffectiveSchemaContext result = reactor.buildEffective();
37         assertNotNull(result);
38     }
39
40     @Test
41     public void incorrectListKeysTest1() throws IOException, YangSyntaxErrorException, URISyntaxException {
42
43         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
44                 "/list-keys-test/incorrect-list-keys-test.yang", false);
45
46         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
47         addSources(reactor, yangFile);
48
49         try {
50             reactor.buildEffective();
51             fail("effective build should fail due to list instead of leaf referenced in list key");
52         } catch (Exception e) {
53             assertEquals(InferenceException.class, e.getClass());
54             assertTrue(e.getMessage().startsWith("Key 'test1_key1 test1_key2' misses node 'test1_key2'"));
55         }
56     }
57
58     @Test
59     public void incorrectListKeysTest2() throws IOException, YangSyntaxErrorException, URISyntaxException {
60
61         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
62                 "/list-keys-test/incorrect-list-keys-test2.yang", false);
63
64         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
65         addSources(reactor, yangFile);
66
67         try {
68             reactor.buildEffective();
69             fail("effective build should fail due to missing leaf referenced in list key");
70         } catch (Exception e) {
71             assertEquals(InferenceException.class, e.getClass());
72             assertTrue(e.getMessage().startsWith("Key 'test1_key1 test1_key2' misses node 'test1_key2'"));
73         }
74     }
75
76     @Test
77     public void incorrectListKeysTest3() throws IOException, YangSyntaxErrorException, URISyntaxException {
78
79         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
80                 "/list-keys-test/incorrect-list-keys-test3.yang", false);
81
82         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
83         addSources(reactor, yangFile);
84
85         try {
86             reactor.buildEffective();
87             fail("effective build should fail due to list instead of leaf in grouping referenced in list key");
88         } catch (Exception e) {
89             assertEquals(InferenceException.class, e.getClass());
90             assertTrue(e.getMessage().startsWith("Key 'grp_list' misses node 'grp_list'"));
91         }
92     }
93
94     @Test
95     public void incorrectListKeysTest4() throws IOException, YangSyntaxErrorException, URISyntaxException {
96
97         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
98                 "/list-keys-test/incorrect-list-keys-test4.yang", false);
99
100         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
101         addSources(reactor, yangFile);
102
103         try {
104             reactor.buildEffective();
105             fail("effective build should fail due to list instead of leaf in grouping augmented to list referenced " +
106                     "in list key");
107         } catch (Exception e) {
108             assertEquals(InferenceException.class, e.getClass());
109             assertTrue(e.getMessage().startsWith("Key 'grp_leaf' misses node 'grp_leaf'"));
110         }
111     }
112
113     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
114                                    final YangStatementSourceImpl... sources) {
115         for (YangStatementSourceImpl source : sources) {
116             reactor.addSource(source);
117         }
118     }
119 }