Bug 2366 - new parser API - implementation of declared statements
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / key / KeyTest.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
9 package org.opendaylight.yangtools.yang.stmt.test.key;
10
11 import org.junit.Test;
12 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
13 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
14 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
15 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
16 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
17
18 import static org.junit.Assert.*;
19
20 public class KeyTest {
21
22     private static final TestKeySource KEY_SIMPLE = new TestKeySource("root", "key");
23     private static final TestKeySource KEY_COMP = new TestKeySource("root", "key1 key2 key3");
24     private static final TestKeySource KEY_COMP_DUPLICATE = new TestKeySource("root", "key1 key1 key2");
25
26     @Test
27     public void keySimpleTest() throws SourceException, ReactorException {
28
29         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
30         addSources(reactor, KEY_SIMPLE);
31
32         EffectiveModelContext result = reactor.build();
33         assertNotNull(result);
34     }
35
36     @Test
37     public void keyCompositeTest() throws SourceException, ReactorException {
38
39         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
40         addSources(reactor, KEY_COMP);
41
42         EffectiveModelContext result = reactor.build();
43         assertNotNull(result);
44     }
45
46     @Test
47     public void keyCompositeInvalid() throws SourceException, ReactorException {
48
49         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
50         addSources(reactor, KEY_COMP_DUPLICATE);
51
52         try {
53             reactor.build();
54             fail("reactor.process should fail due to duplicate name in key");
55         } catch (Exception e) {
56             assertEquals(IllegalArgumentException.class, e.getClass());
57         }
58     }
59
60     private void addSources(BuildAction reactor, TestKeySource... sources) {
61         for (TestKeySource source : sources) {
62             reactor.addSource(source);
63         }
64     }
65
66 }