Bump to odlparent-9.0.0/yangtools-7.0.1-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / yang / types / TypeProviderImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.yang.types;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
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;
30
31 @Ignore
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"))
38             .iterator().next();
39         final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
40
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);
48     }
49
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"))
55             .iterator().next();
56         final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
57
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);
66     }
67
68     @Test
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);
74
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);
83
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);
91     }
92
93     @Test
94     public void testMethodsOfTypeProviderImpl() {
95         final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(
96             YangParserTestUtils.parseYangResource("/base-yang-types.yang"));
97
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();
103
104         // test getAdditionalTypes() method
105         assertEquals(1, typeProvider.getAdditionalTypes().size());
106     }
107 }