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