BUG-7267: catch RuntimeExceptions when processing sources
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / KeyTest.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
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
22
23 public class KeyTest {
24
25     private static final YangStatementSourceImpl KEY_SIMPLE_AND_COMP = new YangStatementSourceImpl(
26             "/semantic-statement-parser/key-arg-parsing/key-simple-and-comp.yang", false);
27     private static final YangStatementSourceImpl KEY_COMP_DUPLICATE = new YangStatementSourceImpl(
28             "/semantic-statement-parser/key-arg-parsing/key-comp-duplicate.yang", false);
29
30     @Test
31     public void keySimpleTest() throws ReactorException {
32
33         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
34         addSources(reactor, KEY_SIMPLE_AND_COMP);
35
36         EffectiveModelContext result = reactor.build();
37         assertNotNull(result);
38     }
39
40     @Test
41     public void keyCompositeInvalid() {
42
43         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
44         addSources(reactor, KEY_COMP_DUPLICATE);
45
46         try {
47             reactor.build();
48             fail("reactor.process should fail due to duplicate name in key");
49         } catch (ReactorException e) {
50             final Throwable cause = e.getCause();
51             assertTrue(cause instanceof SourceException);
52             assertTrue(cause.getMessage().startsWith("Key argument 'key1 key2 key2' contains duplicates"));
53         }
54     }
55
56     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
57         for (YangStatementSourceImpl source : sources) {
58             reactor.addSource(source);
59         }
60     }
61
62 }