Bug 6859: Binding generator v1 refactoring
[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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.reset;
18
19 import java.io.File;
20 import java.net.URI;
21 import java.util.NoSuchElementException;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedTypeBuilderImpl;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
42 import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
43 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45
46 public class TypeProviderImplTest {
47
48     @Test (expected = YangValidationException.class)
49     public void testLeafRefRelativeSelfReference() throws Exception {
50         File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
51
52         final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(relative);
53         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
54         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
55
56         final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
57         final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
58         DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
59                 .getDataChildByName(leafNode);
60         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
61         TypeDefinition<?> leafType = leaf.getType();
62         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
63         assertNotNull(leafrefResolvedType);
64     }
65
66     @Test (expected = YangValidationException.class)
67     public void testLeafRefAbsoluteSelfReference() throws Exception {
68         File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
69
70         final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(relative);
71         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
72         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
73
74         final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
75         final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
76         DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
77                 .getDataChildByName(leafNode);
78         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
79         TypeDefinition<?> leafType = leaf.getType();
80         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
81         assertNotNull(leafrefResolvedType);
82     }
83
84     @Test
85     public void testLeafRefRelativeAndAbsoluteValidReference() throws Exception {
86         File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
87
88         final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(valid);
89         final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
90         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
91
92         final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
93         final QName leaf1Node = QName.create(moduleValid.getQNameModule(), "neighbor-id");
94         DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
95                 .getDataChildByName(leaf1Node);
96         LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
97         TypeDefinition<?> leafTypeRel = leafRel.getType();
98         Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
99         assertNotNull(leafrefRelResolvedType);
100
101         final QName leaf2Node = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
102         DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
103                 .getDataChildByName(leaf2Node);
104         LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
105         TypeDefinition<?> leafTypeAbs = leafAbs.getType();
106         Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
107         assertNotNull(leafrefAbsResolvedType);
108     }
109
110     @Test
111     public void testMethodsOfTypeProviderImpl() throws Exception {
112         final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang").toURI());
113
114         final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(abstractTopology);
115
116         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
117
118         final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
119         final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
120         typeProvider.putReferencedType(refTypePath, refType);
121         final StringTypeDefinition stringType = BaseTypes.stringType();
122
123         final LeafSchemaNode leafSchemaNode = mock(LeafSchemaNode.class);
124         doReturn(stringType).when(leafSchemaNode).getType();
125         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
126         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
127
128         // test constructor
129         assertNotNull(typeProvider);
130
131         // test getAdditionalTypes() method
132         assertFalse(typeProvider.getAdditionalTypes().isEmpty());
133
134         // test getConstructorPropertyName() method
135         assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
136         assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
137
138         // test getParamNameFromType() method
139         assertEquals("string", typeProvider.getParamNameFromType(stringType));
140
141         // test getTypeDefaultConstruction() method for string type
142         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
143
144         // binary type
145         final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
146
147         reset(leafSchemaNode);
148         doReturn(binaryType).when(leafSchemaNode).getType();
149         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
150         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
151
152         assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
153
154         // boolean type
155         final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
156
157         reset(leafSchemaNode);
158         doReturn(booleanType).when(leafSchemaNode).getType();
159         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
160         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
161
162         assertEquals("java.lang.Boolean.FALSE", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
163
164         // decimal type
165         final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
166
167         reset(leafSchemaNode);
168         doReturn(decimalType).when(leafSchemaNode).getType();
169         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
170         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
171
172         assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
173
174         // empty type
175         final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
176
177         reset(leafSchemaNode);
178         doReturn(emptyType).when(leafSchemaNode).getType();
179         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
180         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
181
182         assertEquals("java.lang.Boolean.valueOf(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
183
184         // enum type
185         final EnumTypeDefinition enumType =  BaseTypes.enumerationTypeBuilder(refTypePath).build();
186
187         reset(leafSchemaNode);
188         doReturn(enumType).when(leafSchemaNode).getType();
189         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
190         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
191
192         try {
193             assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
194             fail("Expected NoSuchElementException");
195         } catch (Exception e) {
196             assertTrue(e instanceof NoSuchElementException);
197         }
198
199         // identityref type
200         final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
201         final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
202         identityRefBuilder.addIdentity(identitySchemaNode);
203         final IdentityrefTypeDefinition identityRef =  identityRefBuilder.build();
204
205         reset(leafSchemaNode);
206         doReturn(identityRef).when(leafSchemaNode).getType();
207         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
208         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
209
210         try {
211             assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
212             fail("Expected UnsupportedOperationException");
213         } catch (Exception e) {
214             assertTrue(e instanceof UnsupportedOperationException);
215             assertEquals("Cannot get default construction for identityref type", e.getMessage());
216         }
217     }
218 }