Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
14
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.ReactorDeclaredModel;
22
23 public class KeyTest {
24
25     private static final StatementStreamSource KEY_SIMPLE_AND_COMP = sourceForResource(
26             "/semantic-statement-parser/key-arg-parsing/key-simple-and-comp.yang");
27     private static final StatementStreamSource KEY_COMP_DUPLICATE = sourceForResource(
28             "/semantic-statement-parser/key-arg-parsing/key-comp-duplicate.yang");
29
30     @Test
31     public void keySimpleTest() throws ReactorException {
32         ReactorDeclaredModel result = RFC7950Reactors.defaultReactor().newBuild()
33                 .addSource(KEY_SIMPLE_AND_COMP)
34                 .build();
35         assertNotNull(result);
36     }
37
38     @Test
39     public void keyCompositeInvalid() {
40         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(KEY_COMP_DUPLICATE);
41         try {
42             reactor.build();
43             fail("reactor.process should fail due to duplicate name in key");
44         } catch (ReactorException e) {
45             final Throwable cause = e.getCause();
46             assertTrue(cause instanceof SourceException);
47             assertTrue(cause.getMessage().startsWith("Key argument 'key1 key2 key2' contains duplicates"));
48         }
49     }
50 }