2 * Copyright (c) 2017 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27 public class LeafrefStatementTest {
30 public void testRequireInstanceInLeafrefs() throws Exception {
31 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/leafref-stmt/foo.yang");
32 assertNotNull(schemaContext);
34 final Module foo = schemaContext.findModule("foo", QName.parseRevision("2016-12-20")).get();
35 final Set<TypeDefinition<?>> typeDefinitions = foo.getTypeDefinitions();
36 assertEquals(1, typeDefinitions.size());
38 final TypeDefinition<?> typeDefinition = typeDefinitions.iterator().next();
39 final LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) typeDefinition;
40 assertTrue(leafrefTypeDefinition.requireInstance());
42 final LeafSchemaNode leafrefA = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
44 assertNotNull(leafrefA);
45 assertRequireInstanceInLeafref(leafrefA, true);
47 final LeafSchemaNode leafrefB = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
49 assertNotNull(leafrefB);
50 assertRequireInstanceInLeafref(leafrefB, true);
52 final LeafSchemaNode leafrefC = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
54 assertNotNull(leafrefC);
55 assertRequireInstanceInLeafref(leafrefC, false);
58 private static void assertRequireInstanceInLeafref(final LeafSchemaNode leaf, final boolean requireInstance) {
59 final LeafrefTypeDefinition leafrefTypeDefnition = (LeafrefTypeDefinition) leaf.getType();
60 assertEquals(requireInstance, leafrefTypeDefnition.requireInstance());
64 public void testInvalidYang10() throws Exception {
66 StmtTestUtils.parseYangSource("/rfc7950/leafref-stmt/foo10.yang");
67 fail("Test should fail due to invalid Yang 1.0");
68 } catch (final ReactorException e) {
69 assertTrue(e.getCause().getMessage().startsWith("REQUIRE_INSTANCE is not valid for TYPE"));