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.mdsal.binding.yang.types;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import org.junit.Ignore;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
16 import org.opendaylight.mdsal.binding.model.api.Type;
17 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 public class TypeProviderImplTest {
33 @Test(expected = IllegalArgumentException.class)
34 public void testLeafRefRelativeSelfReference() {
35 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResource(
36 "/leafref/leafref-relative-invalid.yang");
37 final Module moduleRelative = schemaContext.findModules(XMLNamespace.of("urn:xml:ns:yang:lrr"))
39 final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
41 final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
42 final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
43 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
44 .getDataChildByName(leafNode);
45 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
46 TypeDefinition<?> leafType = leaf.getType();
47 typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
50 @Test(expected = IllegalArgumentException.class)
51 public void testLeafRefAbsoluteSelfReference() {
52 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResource(
53 "/leafref/leafref-absolute-invalid.yang");
54 final Module moduleRelative = schemaContext.findModules(XMLNamespace.of("urn:xml:ns:yang:lra"))
56 final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
58 final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
59 final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
60 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
61 .getDataChildByName(leafNode);
62 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
63 TypeDefinition<?> leafType = leaf.getType();
64 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
65 assertNotNull(leafrefResolvedType);
69 public void testLeafRefRelativeAndAbsoluteValidReference() {
70 final EffectiveModelContext schemaContext =
71 YangParserTestUtils.parseYangResource("/leafref/leafref-valid.yang");
72 final Module moduleValid = schemaContext.findModules(XMLNamespace.of("urn:xml:ns:yang:lrv")).iterator().next();
73 final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
75 final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
76 final QName leaf1Node = QName.create(moduleValid.getQNameModule(), "neighbor-id");
77 DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
78 .getDataChildByName(leaf1Node);
79 LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
80 TypeDefinition<?> leafTypeRel = leafRel.getType();
81 Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
82 assertNotNull(leafrefRelResolvedType);
84 final QName leaf2Node = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
85 DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
86 .getDataChildByName(leaf2Node);
87 LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
88 TypeDefinition<?> leafTypeAbs = leafAbs.getType();
89 Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
90 assertNotNull(leafrefAbsResolvedType);
94 public void testMethodsOfTypeProviderImpl() {
95 final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(
96 YangParserTestUtils.parseYangResource("/base-yang-types.yang"));
98 final SchemaPath refTypePath = SchemaPath.create(true, QName.create("", "cont1"), QName.create("", "list1"));
99 final CodegenGeneratedTypeBuilder refType = new CodegenGeneratedTypeBuilder(
100 JavaTypeName.create("org.opendaylight.yangtools.test", "TestType"));
101 typeProvider.putReferencedType(refTypePath, refType);
102 final StringTypeDefinition stringType = BaseTypes.stringType();
104 // test getAdditionalTypes() method
105 assertEquals(1, typeProvider.getAdditionalTypes().size());