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.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
37 public class ContextReferenceTest {
38 private static final QNameModule FOO = QNameModule.create(XMLNamespace.of("foo"));
39 private static final QName LEAF_TYPE = QName.create(FOO, "leaf-type");
40 private static final QName LIST_TYPE = QName.create(FOO, "list-type");
42 private static CrossSourceStatementReactor reactor;
45 public static void createReactor() {
46 reactor = RFC7950Reactors.vanillaReactorBuilder()
47 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, ContextInstanceStatementSupport.INSTANCE)
48 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, ContextReferenceStatementSupport.INSTANCE)
53 public static void freeReactor() {
58 public void test() throws Exception {
59 final ModuleEffectiveStatement foo = reactor.newBuild()
60 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource("/yang-ext.yang")))
61 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource("/ctxref.yang")))
63 .getModuleStatements()
66 final DataTreeEffectiveStatement<?> list = foo.findDataTreeNode(QName.create(FOO, "list")).orElseThrow();
67 assertThat(list, instanceOf(ListEffectiveStatement.class));
69 final ContextInstanceEffectiveStatement listType = list
70 .findFirstEffectiveSubstatement(ContextInstanceEffectiveStatement.class).orElseThrow();
71 assertEquals(LIST_TYPE, listType.argument());
72 assertEquals(LIST_TYPE, listType.contextType().argument());
74 final ContextInstanceEffectiveStatement leafType = list
75 .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow()
76 .findFirstEffectiveSubstatement(ContextInstanceEffectiveStatement.class).orElseThrow();
77 assertEquals(LEAF_TYPE, leafType.argument());
78 assertEquals(LEAF_TYPE, leafType.contextType().argument());
80 final List<GroupingEffectiveStatement> groupings = foo
81 .streamEffectiveSubstatements(GroupingEffectiveStatement.class).collect(Collectors.toList());
82 assertEquals(2, groupings.size());
84 final ContextReferenceEffectiveStatement listRef = groupings.get(1)
85 .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow()
86 .findFirstEffectiveSubstatement(ContextReferenceEffectiveStatement.class).orElseThrow();
87 assertEquals(LIST_TYPE, listType.argument());
88 assertSame(listType.contextType(), listRef.contextType());
90 final ContextReferenceEffectiveStatement leafRef = groupings.get(0)
91 .findFirstEffectiveSubstatement(LeafListEffectiveStatement.class).orElseThrow()
92 .findFirstEffectiveSubstatement(ContextReferenceEffectiveStatement.class).orElseThrow();
93 assertEquals(LEAF_TYPE, leafType.argument());
94 assertSame(leafType.contextType(), leafRef.contextType());