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.odlext.parser;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertSame;
15 import java.util.List;
16 import java.util.stream.Collectors;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.odlext.model.api.ContextInstanceEffectiveStatement;
21 import org.opendaylight.yangtools.odlext.model.api.ContextReferenceEffectiveStatement;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
36 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
38 public class ContextReferenceTest {
39 private static final QNameModule FOO = QNameModule.create(XMLNamespace.of("foo"));
40 private static final QName LEAF_TYPE = QName.create(FOO, "leaf-type");
41 private static final QName LIST_TYPE = QName.create(FOO, "list-type");
43 private static CrossSourceStatementReactor reactor;
46 public static void createReactor() {
47 reactor = RFC7950Reactors.vanillaReactorBuilder()
48 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
49 new ContextInstanceStatementSupport(YangParserConfiguration.DEFAULT))
50 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
51 new ContextReferenceStatementSupport(YangParserConfiguration.DEFAULT))
56 public static void freeReactor() {
61 public void test() throws Exception {
62 final ModuleEffectiveStatement foo = reactor.newBuild()
63 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource("/yang-ext.yang")))
64 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource("/ctxref.yang")))
66 .getModuleStatements()
69 final DataTreeEffectiveStatement<?> list = foo.findDataTreeNode(QName.create(FOO, "list")).orElseThrow();
70 assertThat(list, instanceOf(ListEffectiveStatement.class));
72 final ContextInstanceEffectiveStatement listType = list
73 .findFirstEffectiveSubstatement(ContextInstanceEffectiveStatement.class).orElseThrow();
74 assertEquals(LIST_TYPE, listType.argument());
75 assertEquals(LIST_TYPE, listType.contextType().argument());
77 final ContextInstanceEffectiveStatement leafType = list
78 .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow()
79 .findFirstEffectiveSubstatement(ContextInstanceEffectiveStatement.class).orElseThrow();
80 assertEquals(LEAF_TYPE, leafType.argument());
81 assertEquals(LEAF_TYPE, leafType.contextType().argument());
83 final List<GroupingEffectiveStatement> groupings = foo
84 .streamEffectiveSubstatements(GroupingEffectiveStatement.class).collect(Collectors.toList());
85 assertEquals(2, groupings.size());
87 final ContextReferenceEffectiveStatement listRef = groupings.get(1)
88 .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow()
89 .findFirstEffectiveSubstatement(ContextReferenceEffectiveStatement.class).orElseThrow();
90 assertEquals(LIST_TYPE, listType.argument());
91 assertSame(listType.contextType(), listRef.contextType());
93 final ContextReferenceEffectiveStatement leafRef = groupings.get(0)
94 .findFirstEffectiveSubstatement(LeafListEffectiveStatement.class).orElseThrow()
95 .findFirstEffectiveSubstatement(ContextReferenceEffectiveStatement.class).orElseThrow();
96 assertEquals(LEAF_TYPE, leafType.argument());
97 assertSame(leafType.contextType(), leafRef.contextType());