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