2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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.InferenceException;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
25 public class ListKeysTest {
28 public void correctListKeysTest() throws ReactorException {
30 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
31 "/list-keys-test/correct-list-keys-test.yang", false);
33 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
34 addSources(reactor, yangFile);
36 final EffectiveSchemaContext result = reactor.buildEffective();
37 assertNotNull(result);
41 public void incorrectListKeysTest1() throws IOException, YangSyntaxErrorException, URISyntaxException {
43 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
44 "/list-keys-test/incorrect-list-keys-test.yang", false);
46 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
47 addSources(reactor, yangFile);
50 reactor.buildEffective();
51 fail("effective build should fail due to list instead of leaf referenced in list key");
52 } catch (Exception e) {
53 assertEquals(InferenceException.class, e.getClass());
54 assertTrue(e.getMessage().startsWith("Key 'test1_key1 test1_key2' misses node 'test1_key2'"));
59 public void incorrectListKeysTest2() throws IOException, YangSyntaxErrorException, URISyntaxException {
61 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
62 "/list-keys-test/incorrect-list-keys-test2.yang", false);
64 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
65 addSources(reactor, yangFile);
68 reactor.buildEffective();
69 fail("effective build should fail due to missing leaf referenced in list key");
70 } catch (Exception e) {
71 assertEquals(InferenceException.class, e.getClass());
72 assertTrue(e.getMessage().startsWith("Key 'test1_key1 test1_key2' misses node 'test1_key2'"));
77 public void incorrectListKeysTest3() throws IOException, YangSyntaxErrorException, URISyntaxException {
79 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
80 "/list-keys-test/incorrect-list-keys-test3.yang", false);
82 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
83 addSources(reactor, yangFile);
86 reactor.buildEffective();
87 fail("effective build should fail due to list instead of leaf in grouping referenced in list key");
88 } catch (Exception e) {
89 assertEquals(InferenceException.class, e.getClass());
90 assertTrue(e.getMessage().startsWith("Key 'grp_list' misses node 'grp_list'"));
95 public void incorrectListKeysTest4() throws IOException, YangSyntaxErrorException, URISyntaxException {
97 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
98 "/list-keys-test/incorrect-list-keys-test4.yang", false);
100 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
101 addSources(reactor, yangFile);
104 reactor.buildEffective();
105 fail("effective build should fail due to list instead of leaf in grouping augmented to list referenced " +
107 } catch (Exception e) {
108 assertEquals(InferenceException.class, e.getClass());
109 assertTrue(e.getMessage().startsWith("Key 'grp_leaf' misses node 'grp_leaf'"));
113 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
114 final YangStatementSourceImpl... sources) {
115 for (YangStatementSourceImpl source : sources) {
116 reactor.addSource(source);